Resize an image without distortion OpenCV

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

    Does not quite align with what the original question is asking, but I landed here searching for an answer to a similar question.

    import cv2
    def resize_and_letter_box(image, rows, cols):
        """
        Letter box (black bars) a color image (think pan & scan movie shown 
        on widescreen) if not same aspect ratio as specified rows and cols. 
        :param image: numpy.ndarray((image_rows, image_cols, channels), dtype=numpy.uint8)
        :param rows: int rows of letter boxed image returned  
        :param cols: int cols of letter boxed image returned
        :return: numpy.ndarray((rows, cols, channels), dtype=numpy.uint8)
        """
        image_rows, image_cols = image.shape[:2]
        row_ratio = rows / float(image_rows)
        col_ratio = cols / float(image_cols)
        ratio = min(row_ratio, col_ratio)
        image_resized = cv2.resize(image, dsize=(0, 0), fx=ratio, fy=ratio)
        letter_box = np.zeros((int(rows), int(cols), 3))
        row_start = int((letter_box.shape[0] - image_resized.shape[0]) / 2)
        col_start = int((letter_box.shape[1] - image_resized.shape[1]) / 2)
        letter_box[row_start:row_start + image_resized.shape[0], col_start:col_start + image_resized.shape[1]] = image_resized
        return letter_box
    

提交回复
热议问题