![]() ![]() 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. ![]() It will resize the image according to the specified height and width.Ĭode: # Set rows and columns # Downsize the image using new width and height down_width = 800 down_height = 500 down_points = (down_width, down_height) resize_down = cv2.resize(image, down_points, interpolation= cv2.INTER_LINEAR) # Downsize the image using new width and height up_width = 1500 up_height = 2500 up_points = (up_width, up_height) resize_up = cv2.resize(image, up_points, interpolation = cv2.INTER_LINEAR) # Display images print("Resized Down by defining height and width\n") cv2_imshow(resize_down) cv2.waitKey() print("\nResized Up by defining height and width\n") cv2_imshow(resize_up) cv2.waitKey() cv2.destroyAllWindows()įor complete code please visit the following Colab notebook.In this tutorial, I will show you how to resize images using OpenCV's cv2.resize() function. As a result, when you upscale or downscale an image, it does not appear distorted.Ĭode: # Importing libraries import cv2 from import cv2_imshow # Reading the image image = cv2.imread("/content/neon.jpg") print("Shape:", image.shape) cv2_imshow(image) # Scaling Up the image 1.8 and 1.4 times at x and y axis respectively by specifying both scaling factors up_x = 1.8 up_y = 1.4 # Scaling Down the image 0.2 times specifying a single scale factor down = 0.2 scaled_down = cv2.resize(image, None, fx=down, fy=down, interpolation=cv2.INTER_LINEAR) scaled_up = cv2.resize(image, None, fx= up_x, fy= up_y, interpolation=cv2.INTER_LINEAR) # Display images and press any key to check next image print("Scaled Down\n") cv2_imshow(scaled_down) cv2.waitKey() print("\nScaled Up\n") cv2_imshow(scaled_up) cv2.waitKey() It aids in maintaining the aspect ratio and the display quality. Syntax: resize(src, dsize]]])ĭescription of variables Resizing MethodsĪ scaling factor, also known as a scale factor, is a number that scales or multiplies a quantity, in this case, the image’s width and height.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |