在 OpenCV 中读取像素的 BGR 值

Reading BGR values of pixel in OpenCV

enter image description here我想检测图像中形状的颜色,所以当我 运行 代码 “bgr_list = img[cX,cY]” 我收到此错误:

bgr_list = img[cX,cY]                 
IndexError: index 648 is out of bounds for axis 0 with size 598
        
        
but when I run "bgr_list = img[300,300]" I do not get any error  
->where [300,300] is white portion in image  
-> where, cX = x-coordinate of centroid and cY = y-coordinate of centroid  
-> BGR_list is bgr value of pixel at centroid  
-> the size of image is 898x598 pixel

看起来您从某种预处理中获得了 X 和 Y 笛卡尔坐标,但是 numpy opencv 使用的矩阵,使用矩阵索引 (row, column, channel)。您的 X 坐标对应于列索引,Y 坐标对应于您的行索引。

翻转索引应该可以完成工作 bgr_list = img[cY,cX]