Resize an image without distortion OpenCV

前端 未结 9 1964
日久生厌
日久生厌 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:23

    I have a dataset of hand drawings and i needed to create small square images from asymmetric drawings.

    Thanks to @vijay jha i created square images while maintaining the aspect ratio of the original image. One issue though was that the more you downscaled the more information was lost.

    512x256 to 64x64 would look like this:

    I modified a bit the original code to smoothly downscale the image.

    from skimage.transform import resize, pyramid_reduce
    
    
    def get_square(image, square_size):
    
        height, width = image.shape    
        if(height > width):
          differ = height
        else:
          differ = width
        differ += 4
    
        # square filler
        mask = np.zeros((differ, differ), dtype = "uint8")
    
        x_pos = int((differ - width) / 2)
        y_pos = int((differ - height) / 2)
    
        # center image inside the square
        mask[y_pos: y_pos + height, x_pos: x_pos + width] = image[0: height, 0: width]
    
        # downscale if needed
        if differ / square_size > 1:
          mask = pyramid_reduce(mask, differ / square_size)
        else:
          mask = cv2.resize(mask, (square_size, square_size), interpolation = cv2.INTER_AREA)
        return mask
    

    512x256 -> 64x64

    512x256 -> 28x28

提交回复
热议问题