How to resize images using OpenCV2.0 and Python2.6

I want to use OpenCV2.0 and Python2.6 to display modified images. I used and adopted an example at http://opencv.willowgarage.com/documentation/python/cookbook.html , but unfortunately this code is for OpenCV2.1 and does not seem to work on 2.0. Here is my code:

import os, glob import cv ulpath = "exampleshq/" for infile in glob.glob( os.path.join(ulpath, "*.jpg") ): im = cv.LoadImage(infile) thumbnail = cv.CreateMat(im.rows/10, im.cols/10, cv.CV_8UC3) cv.Resize(im, thumbnail) cv.NamedWindow(infile) cv.ShowImage(infile, thumbnail) cv.WaitKey(0) cv.DestroyWindow(name) 

Since I can not use

 cv.LoadImageM 

I used

 cv.LoadImage 

which was not a problem in other applications. However, cv.iplimage does not have attribute strings, cols or size. Can someone give me a hint how to solve this problem? Thank.

+144
python image image-processing resize opencv
Nov 16 2018-10-16
source share
5 answers

If you want to use CV2, you need to use the resize function.

For example, this will resize both axes by half:

 small = cv2.resize(image, (0,0), fx=0.5, fy=0.5) 

and the image size will change to 100 columns (width) and 50 rows (height):

 resized_image = cv2.resize(image, (100, 50)) 

Another option is to use the scipy module using:

 small = scipy.misc.imresize(image, 0.5) 

Obviously, there are more options that you can read in the documentation for these functions ( cv2.resize , scipy.misc.imresize ).




Update:
According to SciPy documentation :

imresize is deprecated in SciPy 1.0.0, and will be removed in 1.2.0.
Use skimage.transform.resize instead.

Note that if you want to resize by a factor , you may need skimage.transform.rescale .

+311
Sep 12 '13 at 14:51
source share

Image Doubling Example

There are two ways to resize an image. You can specify a new size:

  • Manually

    height, width = src.shape[:2]

    dst = cv2.resize(src, (2*width, 2*height), interpolation = cv2.INTER_CUBIC)

  • Scaling factor.

    dst = cv2.resize(src, None, fx = 2, fy = 2, interpolation = cv2.INTER_CUBIC) where fx is the scale factor along the horizontal axis and fy along the vertical axis.

To reduce the image, it will look best with INTER_AREA interpolation, while to enlarge the image, it will look best with INTER_CUBIC (slow) or INTER_LINEAR (faster, but still looks normal).

Example of image reduction for maximum height / width (maintaining proportions)

 import cv2 img = cv2.imread('YOUR_PATH_TO_IMG') height, width = img.shape[:2] max_height = 300 max_width = 300 # only shrink if img is bigger than required if max_height < height or max_width < width: # get scaling factor scaling_factor = max_height / float(height) if max_width/float(width) < scaling_factor: scaling_factor = max_width / float(width) # resize image img = cv2.resize(img, None, fx=scaling_factor, fy=scaling_factor, interpolation=cv2.INTER_AREA) cv2.imshow("Shrinked image", img) key = cv2.waitKey() 

Using your code with cv2

 import cv2 as cv im = cv.imread(path) height, width = im.shape[:2] thumbnail = cv.resize(im, (width/10, height/10), interpolation = cv.INTER_AREA) cv.imshow('exampleshq', thumbnail) cv.waitKey(0) cv.destroyAllWindows() 
+54
Feb 10 '17 at 17:36
source share

You can use the GetSize function to get this information, cv.GetSize (them) will return a tuple with the image width and height. You can also use im.depth and img.nChan to get more information.

And to resize the image, I would use a slightly different process with a different image instead of a matrix. It’s better to try to work with the same data type:

 size = cv.GetSize(im) thumbnail = cv.CreateImage( ( size[0] / 10, size[1] / 10), im.depth, im.nChannels) cv.Resize(im, thumbnail) 

Hope this helps;)

Julien

+7
Nov 20 '11 at 10:23
source share
 def rescale_by_height(image, target_height, method=cv2.INTER_LANCZOS4): """Rescale 'image' to 'target_height' (preserving aspect ratio).""" w = int(round(target_height * image.shape[1] / image.shape[0])) return cv2.resize(image, (w, target_height), interpolation=method) def rescale_by_width(image, target_width, method=cv2.INTER_LANCZOS4): """Rescale 'image' to 'target_width' (preserving aspect ratio).""" h = int(round(target_width * image.shape[0] / image.shape[1])) return cv2.resize(image, (target_width, h), interpolation=method) 
+4
Jul 20 '18 at 22:49
source share

Here is the function of enlarging or reducing the image by the desired width or height while maintaining the aspect ratio

 # Resizes a image and maintains aspect ratio def maintain_aspect_ratio_resize(image, width=None, height=None, inter=cv2.INTER_AREA): # Grab the image size and initialize dimensions dim = None (h, w) = image.shape[:2] # Return original image if no need to resize if width is None and height is None: return image # We are resizing height if width is none if width is None: # Calculate the ratio of the height and construct the dimensions r = height / float(h) dim = (int(w * r), height) # We are resizing width if height is none else: # Calculate the ratio of the width and construct the dimensions r = width / float(w) dim = (width, int(h * r)) # Return the resized image return cv2.resize(image, dim, interpolation=inter) 

Usage

Usage
 import cv2 image = cv2.imread('1.png') cv2.imshow('width_100', maintain_aspect_ratio_resize(image, width=100)) cv2.imshow('width_300', maintain_aspect_ratio_resize(image, width=300)) cv2.waitKey() 



Using this example image

enter image description here

Just zoom out to width=100 (left) or zoom in to width=300 (right)

enter image description here enter image description here

0
Sep 04 '19 at 3:19
source share



All Articles