I want to get image width and height, how can I do that in OpenCV?
For example:
Mat src = imread("path_to_image"); cout << src.width;
Is that right?
When working with OpenCV Python, images are stored in numpy ndarray. To get the image shape or size, use ndarray. shape to get the dimensions of the image. Then, you can use index on the dimensions variable to get width, height and number of channels for each pixel.
If you use cv2 , correct method is to use . copy() method in Numpy. It will create a copy of the array you need. Otherwise it will produce only a view of that object.
open() is used to open the image and then . width and . height property of Image are used to get the height and width of the image.
cv2. imread() method loads an image from the specified file. If the image cannot be read (because of missing file, improper permissions, unsupported or invalid format) then this method returns an empty matrix.
You can use rows
and cols
:
cout << "Width : " << src.cols << endl; cout << "Height: " << src.rows << endl;
or size()
:
cout << "Width : " << src.size().width << endl; cout << "Height: " << src.size().height << endl;
or size
cout << "Width : " << src.size[1] << endl; cout << "Height: " << src.size[0] << endl;
Also for openCV in python you can do:
img = cv2.imread('myImage.jpg') height, width, channels = img.shape
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With