0

I want to find the maximum value of the image but I got an error


    import cv2
    image = cv2.imread('photo.jpg')
    mask = image[:,:,2]
    (min_val, max_val, min_loc, max_loc) = cv2.minMaxLoc(image) 
    print(max_val)

ERROR

cv2.error: OpenCV(4.7.0) D:\a\opencv-python\opencv-python\opencv\modules\core\src\minmax.cpp:1504: error: (-215:Assertion failed) (cn == 1 && (_mask.empty() || _mask.type() == CV_8U)) || (cn > 1 && _mask.empty() && !minIdx && !maxIdx) in function 'cv::minMaxIdx'
3
  • See stackoverflow.com/questions/20195721/… Commented Jan 3, 2023 at 6:35
  • try it without the mask parameter first, just to rule out the potential issues. Also there is a possibility that its an error due to version. current opencv is at 4.7.x.x while yours is at 4.6.x.x Commented Jan 3, 2023 at 6:37
  • This is an RGB image, right? How do you define "maximum value"? Commented Jan 3, 2023 at 7:20

1 Answer 1

1

min max locs are defined across each channel

for i in range(3):
  (min_val, max_val, min_loc, max_loc) = cv2.minMaxLoc(image[:,:,i])
  print(max_val)

Doc clearly says

The function do not work with multi-channel arrays. If you need to find minimum or maximum elements across all the channels, use Mat::reshape first to reinterpret the array as single-channel. Or you may extract the particular channel using either extractImageCOI , or mixChannels , or split .

https://docs.opencv.org/3.4/d2/de8/group__core__array.html#gab473bf2eb6d14ff97e89b355dac20707

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.