Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get image width and height in OpenCV? [duplicate]

Tags:

c++

python

opencv

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?

like image 525
sarmad m Avatar asked Oct 06 '15 13:10

sarmad m


People also ask

How do I get the height and width of an image in OpenCV?

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.

How do I duplicate an image in OpenCV?

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.

How do you find the height and width of an image in Python?

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.

What is cv2 Imread ()?

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.


2 Answers

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; 
like image 141
Miki Avatar answered Oct 10 '22 00:10

Miki


Also for openCV in python you can do:

img = cv2.imread('myImage.jpg') height, width, channels = img.shape  
like image 31
Anoroah Avatar answered Oct 10 '22 01:10

Anoroah