我想获取图像的宽度和高度,在OpenCV中该如何实现?
例如:
Mat src = imread("path_to_image");
cout << src.width;
这是正确的吗?
您可以使用rows
和cols
属性:
cout << "Width : " << src.cols << endl;
cout << "Height: " << src.rows << endl;
或者size()
:
cout << "Width : " << src.size().width << endl;
cout << "Height: " << src.size().height << endl;
或者size
cout << "Width : " << src.size[1] << endl;
cout << "Height: " << src.size[0] << endl;
同时,在Python中使用openCV,你可以这样做:
img = cv2.imread('myImage.jpg')
height, width, channels = img.shape
img
对象的类型为 <class 'numpy.ndarray'>
。如果图像路径不正确并且您尝试在 cv2.imread
中使用它,它会给出一个警告而不是错误。您的程序不会在那里停止! - Milan