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'
balu
  • 1,023
  • 12
  • 18

1 Answers1

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

balu
  • 1,023
  • 12
  • 18