Now to resize the image, we must keep in mind preserving the aspect ratio. Project Structureīefore we get started implementing our Python script for this tutorial, let’s first review our project directory structure:įigure 2: Displaying the original image to screen. And decreasing the size of the image leaves us with fewer pixels to process which saves time when working with image processing algorithms or deep learning models. That’s why you can see the image is been stretch either along the $ x $- axis or along the $ y $ – axis.Īpart from the aspect ratio, we also need to keep in mind, what interpolation method to use when resizing the image.Īs increasing the size of the pixels requires us to fill in the gaps of pixels we don’t even know exists. And on the right, we have both of the resized images without considering the aspect ratio of the image. And not having this in mind, can lead to having a squished or compressed image: Figure 1: Neglecting the aspect ratio of an image while resizing. Most of the times, a common mistake people make when resizing is neglecting the aspect ratio - which is the ratio of an image’s width to its height. Resizing, or also known as scaling refers to the process of either increasing or decreasing the size of an image in response to it’s width and height. The original image with dimensions has been resized to using resize() function.What is Resizing and Why should you consider the Aspect Ratio ? Output Original Dimensions : (149, 200, 4) Print('Resized Dimensions : ',resized.shape) Resized = cv2.resize(img, dim, interpolation = cv2.INTER_AREA) Height = int(img.shape * scale_percent / 100) Width = int(img.shape * scale_percent / 100) Scale_percent = 60 # percent of original size Print('Original Dimensions : ',img.shape) Img = cv2.imread('/home/img/python.png', cv2.IMREAD_UNCHANGED) We slot bonus new member 100 di awal will use this scale_percent value along with original image’s dimensions to calculate the width and height of output image. Providing a value <100 downscales the image provided. In the following example, scale_percent value holds the percentage by which image has to be scaled. Downscale – Resize and Preserve Aspect Ratio INTER_CUBIC – a bicubic interpolation over 4×4 pixel neighborhood INTER_LANCZOS4 – a Lanczos interpolation over 8×8 pixel neighborhoodġ. But when the image is zoomed, it is similar to the INTER_NEAREST method. It may be a preferred method for image decimation, as it gives moire’-free results. INTER_NEAREST – a nearest-neighbor interpolation INTER_LINEAR – a bilinear interpolation (used by default) INTER_AREA – resampling using pixel area relation. flag that takes one of the following methods. The syntax of resize function in OpenCV is cv2.resize(src, dsize]]]) To resize an image, OpenCV provides cv2.resize() function. Also, the aspect ratio of the original image could be preserved in the resized image. Resizing an image means changing the dimensions of it, be it width alone, height alone or changing both of them. We can use cv2.resize() function to upscale, downscale, or resize to a desired size (considering or not considering the aspect ratio). In this OpenCV tutorial, we learn the syntax of cv2.resize() and how to use this function to resize a given image.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |