使用 python opencv 绕任意点旋转后将图像裁剪到新的四个角

问题描述 投票:0回答:1

如何围绕图像中心旋转,但如何围绕原始图像中的任意点旋转并在旋转后将新的旋转图像裁剪到新的四个角(原始图像的角而不是添加的黑色背景)有很多答案。

我根据这里某人的回答尝试了这个,但结果是图像裁剪错误。

import cv2
import imutils

#https://stackoverflow.com/a/32929315/8384006
def rotate(image, angle, center = None, scale = 1.0):
    (h, w) = image.shape[:2]

    if center is None:
        center = (w / 2, h / 2)

    # Perform the rotation
    M = cv2.getRotationMatrix2D(center, angle, scale)
    rotated = cv2.warpAffine(image, M, (w, h))

    return rotated

image = cv2.imread('DJI_0433.jpg', 1)
point = (10, 10)  # Example point to rotate around
rotated_image = rotate(image, 35, point) #35 degrees

cv2.imshow("Rotated Image", cv2.resize(rotated_image, (800, 600)))
cv2.waitKey(0) 
cv2.destroyAllWindows()

这就是所显示的内容,并非所有四个角都显示并剪裁到。

编辑 由于我的英文不太好,所以补充一些说明:

python opencv rotation crop
1个回答
0
投票

我尝试了一种方法,通过在边缘添加空白图像来使您选择的点作为中心点。旋转后,对旋转后的图像进行一些后处理以获得所需的结果。请参阅下面的代码,并在注释中找到解释。希望这有帮助。

import cv2
import imutils
import numpy as np

#https://stackoverflow.com/a/32929315/8384006
def rotate(image, angle, center = None, scale = 1.0):
    (h, w) = image.shape[:2]

    if center is None:
        center = (w / 2, h / 2)

    # Perform the rotation
    M = cv2.getRotationMatrix2D(center, angle, scale)
    rotated = cv2.warpAffine(image, M, (w, h))

    return rotated

image = cv2.imread('to_rotate.jpg', 1)
(h, w) = image.shape[:2]
point = (10, 10)  # Example point to rotate around

#MAKE THE POINT CENTER OF THE IMAGE
#######################################################################
image_to_rotate=None
if point[1]<w/2:
    blank_image = np.zeros((h,w-point[1],3), np.uint8)
    image_to_rotate = np.concatenate((blank_image, image), axis=1)
else:
    blank_image = np.zeros((h,w-(w - point[1]), 3), np.uint8)
    image_to_rotate = np.concatenate((image,blank_image), axis=1)

cv2.imwrite("center_img1.jpg",image_to_rotate)
(t_h, t_w) = image_to_rotate.shape[:2]
if point[0]<h/2:
    blank_image = np.zeros((h - point[0],t_w,3), np.uint8)
    image_to_rotate = np.concatenate((blank_image, image_to_rotate), axis=0)
else:
    blank_image = np.zeros((h - (h - point[0]),t_w, 3), np.uint8)
    image_to_rotate = np.concatenate((image_to_rotate,blank_image), axis=0)

#######################################################################

scale=0.6
##Rotate Image
rotated_image = rotate(image_to_rotate, 35, None, scale=scale) #Now point is the center and sclae the image to avoid edge cuts

##Postprocess Image
#Get back to original scale
rotated_image_org_scale = cv2.resize(rotated_image, (0,0), fx=(1/scale),fy=(1/scale))
#Find contour get the image outline
contours, hierarchy = cv2.findContours(cv2.cvtColor(rotated_image_org_scale, cv2.COLOR_BGR2GRAY), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
#Find bounding rest of outer contour
#Crop the image using bounding rect to get the full image.
if len(contours)>0:
    x_crop, y_crop, w_crop, h_crop = cv2.boundingRect(contours[0])
    crop_img = rotated_image_org_scale[y_crop:y_crop + h_crop, x_crop:x_crop + w_crop]
    cv2.imwrite("rotated.jpg",crop_img )
    cv2.imshow("cropped", crop_img)
    cv2.waitKey(0)
else:#Failed to find contour condition
    print("Failed to find contour")
    cv2.imshow("Rotated Image", cv2.resize(rotated_image_org_scale, (800, 600)))
    cv2.waitKey(0)
    cv2.destroyAllWindows()

输出图像示例

© www.soinside.com 2019 - 2024. All rights reserved.