To resize images with OpenCV, use the cv2.resize() function. Resize an Image with cv2.resize() Function Now that you have read the image, let’s resize it. Plt.imshow(cv2.cvtColor(img_double, cv2.For example, if your script is on the same folder with “image.jpeg” you can read the image into your program by: import cv2 In : img_double = cv2.resize(img, None, fx = 2, fy = 2) Here, there is no need to calculate the pixel size of the new image. This is another way of scaling up the image to double the size by passing the value of x-axis and y-axis factor parameters as 2 to the cv2 resize function. Plt.imshow(cv2.cvtColor(img_double, cv2.COLOR_BGR2RGB))Įxample – 4: Scale Up the Image to Factor 2 (Double) Print('Image Height is',img_double.shape) Img_double = cv2.resize(img, (new_width, new_height)) It can be seen that the width and height of the new image are double of the original image. To double the image size, first, the pixel size of width and height for the new image are calculated and then they are passed to the cv2 resize() function. In : img_half = cv2.resize(img, None, fx = 0.5, fy = 0.5)Įxample – 3: Resize the Image to Double By Calculating Pixel Size In this case, there is no need to calculate the pixel size of the new image. This is another way of resizing the image to half by passing the value of x-axis and y-axis factor parameters as 0.5 to the cv2 resize function. Plt.imshow(cv2.cvtColor(img_half, cv2.COLOR_BGR2RGB))Įxample – 2: Scaling Down the Image to Factor 0.5 (Half) Img_half = cv2.resize(img, (new_width, new_height)) In : #Calculating the Pixel Size for Resizing It can be seen that the width and height of the new image are half of the original image. To resize the image to half, we first calculate the pixel size of width and height for the new image and then pass it to cv2 resize() function. Plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))Įxample – 1: Resize the Image to Half By Calculating Pixel Size We can see the original size of the image is 800×534 Next, we will read a sample image for our example and display it. Import matplotlib.pyplot as plt Read Sample Image and Display Let us start by importing the OpenCV library as shown below. Neighborhood, it produces the highest quality but is slow.Įxamples of cv2.resize() in Python OpenCV cv2.INTER_LANCZOS4 – It uses Lanczos interpolation over an 8 x 8 pixel.cv2.INTER_CUBIC – It uses bicubic interpolation over a 4 x 4 pixel.cv2.INTER_AREA – It uses pixel area interpolation which is better used for downscaling the image.cv2.INTER_NEAREST – It uses nearest-neighbor interpolation, which is fast but produces blocky images.This is the default option in cv2.resize() cv2.INTER_LINEAR – It uses bilinear interpolation, which is preferably used for scaling up the image to a larger size.And when the image is scaled down it needs to This is because when the image is scaled up it needs more pixels to fill up the spaces between actual pixels. To resize the image, the cv2 resize() function uses interpolation for resizing. Interpolation Parameters for cv2.resize()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |