如何在OpenCV中获取图像宽度和高度?

sar*_*d m 38 c++ python opencv

我想得到图像的宽度和高度,我怎么能在OpenCV中做到这一点?

例如:

Mat src = imread("path_to_image");
cout << src.width;
Run Code Online (Sandbox Code Playgroud)

是对的吗?

Mik*_*iki 73

你可以使用rowscols:

cout << "Width : " << src.cols << endl;
cout << "Height: " << src.rows << endl;
Run Code Online (Sandbox Code Playgroud)

或者size():

cout << "Width : " << src.size().width << endl;
cout << "Height: " << src.size().height << endl;
Run Code Online (Sandbox Code Playgroud)


Ano*_*oah 45

另外对于python中的openCV,你可以这样做:

img = cv2.imread('myImage.jpg')
height, width, channels = img.shape 
Run Code Online (Sandbox Code Playgroud)

  • 是的,这是真的,但谷歌搜索python的人会带你到这里. (31认同)
  • AttributeError: 'NoneType' 对象没有属性 'shape' (3认同)
  • 很好,但是OP要求使用C ++ (2认同)