繁体   English   中英

OpenCV显示错误的图像宽度和高度

[英]Opencv showing wrong width and height of image

我有一张尺寸为612x408612x408 )的图像。

当我使用opencv cv2.imread(/path/to/img)打开它时,它显示了(408,612,3)

这不是问题

当我cv2.imshow()它像正常的水平矩形一样正确显示图像,其宽度大于高度

我添加了mouseCallback来获取像素位置,所以当我将鼠标更靠近图像的右侧时,会出现错误IndexError: index 560 is out of bounds for axis 0 with size 408尽管我单击了图像,但IndexError: index 560 is out of bounds for axis 0 with size 408

我搜索了SO,但找不到类似的问题

import cv2
def capture_pos(event,x,y,flags,param):
    if event == cv2.EVENT_LBUTTONDOWN:
        mouseX = x
        mouseY = y
        print('mouse clicked at x={},y={}'.format(mouseX,mouseY))
        h,s,v = img[mouseX,mouseY]
        print('h:{} s:{} v:{}'.format(h,s,v))
img = cv2.imread('./messi color.png')
img = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
cv2.namedWindow('get pixel color by clicking on image')
cv2.setMouseCallback('get pixel color by clicking on image',capture_pos)
cv2.imshow('get pixel color by clicking on image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

您得到的尺寸似乎正确。 您可以使用以下方法检查图像的尺寸

print(img.shape)

您将获得的图像为(height, width)但这可能有点令人困惑,因为我们通常根据width x height来指定图像。 这是因为图像在OpenCV中存储为Numpy数组。 因此,要索引图像,您可以简单地使用img[Y:X]因为height是形状中的第一项,而width是形状中的第二项

由于它是Numpy数组,我们得到的(rows,cols)等于(height,width)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM