How to get image width and height in OpenCV?

PythonC++Opencv

Python Problem Overview


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?

Python Solutions


Solution 1 - Python

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;

Solution 2 - Python

Also for openCV in python you can do:

img = cv2.imread('myImage.jpg')
height, width, channels = img.shape 

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionsarmad mView Question on Stackoverflow
Solution 1 - PythonMikiView Answer on Stackoverflow
Solution 2 - PythonAnoroahView Answer on Stackoverflow