You also learned how to use a scaling factor to preserve the aspect ratio while resizing the resulting image didn't look distorted.įinally, I showed you how to resize an image by preserving the aspect ratio without using a scaling factor. In this tutorial, you learned how to resize an image using a custom width and height but the resulting image looked distorted. Related article: How to Crop Images with OpenCV and Python Summary If we want to resize using a new width, we calculate the ratio using new_width/width and if we want to resize using a new height, we calculate the ratio using new_height/height. The ratio is equal to: ratio = new_width/width = new_height/height. We use this new width to calculate the ratio and we calculate the new height by multiplying the original height by the ratio. Let's say we want our new image to have a width of 400px. Ratio = new_width / width # (or new_height / height) # and compute the new height based on the aspect ratio # let's say we want the new width to be 400px So in order to do that, we need to calculate the aspect ratio of the original image and use it to resize the image. Usually, you don't want to set a scale factor, instead, you want to resize the image to a specific width or height, and you want the aspect ratio to be automatically maintained. Resizing with a Specific Width or Height (Preserve Aspect Ratio) Here, we wanted our new image to be 60% of the original one, so we multiplied the width and height by 0.6. # We want the new image to be 60% of the original imageĬv2.destroyAllWindows() Original shape: (400, 600, 3) This way, you are sure that the aspect ratio of the original image will be the same as that of the new image. For example, if you want your new image to be half of the original image, then the scaling factor should be 0.5. Basically, a scaling factor is a number by which you multiply the dimension of the image. We can resize an image by using a scaling factor. Resizing with a Scaling Factor (Preserve Aspect Ratio) New_image = cv2.resize(image, dimensions, interpolation=cv2.INTER_LINEAR)Īs you can see, the new image is a bit distorted because we didn't take into account the aspect ratio. We can downscale or upscale an image when resizing it. Let's downscale the original image to 300x300 pixels: import cv2 Resizing with a Specific Width and Height (Don't Preserve Aspect Ratio) Edge and Contour Detection with OpenCV and Python.Morphological Operations with OpenCV and Python.Image Thresholding with OpenCV and Python.Image Filtering and Blurring with OpenCV and Python.Bitwise Operations and Image Masking with OpenCV and Python.How to Annotate Images with OpenCV and Python (coming soon).How to Rotate Images with OpenCV and Python.How to Crop Images with OpenCV and Python.How to Resize Images with OpenCV and Python (this article).How to Read and Write Videos with OpenCV and Python.How to Read, Write, and Save Images with OpenCV and Python.This article is part 3 of the tutorial series on computer vision and image processing with OpenCV: See InterpolationFlags for the list of options available. interpolation: (optional) The algorithm used to reconstruct the new pixels.fy: (optional) The scale factor along the vertical axis.fx: (optional) The scale factor along the horizontal axis.dst: (optional) The output image with size dsize.dsize: (required) The size for the output image.src: (required) This is the input image.The cv2.resize(src, dsize, dst, fx, fy, interpolation) takes 2 required arguments and 4 optional arguments: The following image will be used as an example throughout this tutorial: I will also show you how to resize an image by preserving the aspect ratio so that the resized image doesn't appear distorted. In this tutorial, I will show you how to resize images using OpenCV's cv2.resize() function.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |