Resize an image without distortion OpenCV

前端 未结 9 1949
日久生厌
日久生厌 2020-12-07 12:30

I am using python 3 and latest version of openCV. I am trying to resize an image using the resize function provided but after resizing the image is very distorted. Code :

9条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-07 13:12

    The code is given a window_height by which it calculates the window_width variable while maintaining the aspect ratio of the image. In order to prevent it from any distortion.

    import cv2
    
    def resize(self,image,window_height = 500):
        aspect_ratio = float(image.shape[1])/float(image.shape[0])
        window_width = window_height/aspect_ratio
        image = cv2.resize(image, (int(window_height),int(window_width)))
        return image
    
    img = cv2.imread(img_source)         #image location
    img_resized = resize(img,window_height = 800)
    cv2.imshow("Resized",img_resized)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    

提交回复
热议问题