opencv 无损旋转后 坐标不知如何重新计算回来

如题,已有某个矩形坐标,无损旋转后,坐标不知道如何计算回来。
如下代码 希望sub_image2函数能重新标回坐标

# -*- coding: utf-8 -*-

import cv2, numpy as np

def sub_image(image, rect):

shape = image.shape[1::-1]

center = rect[0]

width, height = rect[1]

angle = rect[2]

if width < height:

width, height = rect[1][::-1]

angle = 90 + angle

matrix = cv2.getRotationMatrix2D(center=center, angle=angle, scale=1)

image = cv2.warpAffine(src=image, M=matrix, dsize=shape)

x = center[0] - width / 2

y = center[1] - height / 2

cv2.rectangle(image, (int(x), int(y)), (int(x + width), int(y + height)), (0, 0, 255), 2)

cv2.imshow('有损的', image)

cv2.waitKey()

cv2.destroyAllWindows()

def sub_image2(image, rect):

center = rect[0]

width, height = rect[1]

angle = rect[2]

w, h = image.shape[1::-1]

cX, cY = w // 2, h // 2

if width < height:

width, height = rect[1][::-1]

angle = 90 + angle

matrix = cv2.getRotationMatrix2D(center=(cX, cY), angle=angle, scale=1)

cos = np.abs(matrix[0, 0])

sin = np.abs(matrix[0, 1])

# compute the new bounding dimensions of the image

nW = int((h * sin) + (w * cos))

nH = int((h * cos) + (w * sin))

# adjust the rotation matrix to take into account translation

matrix[0, 2] += (nW / 2) - cX

matrix[1, 2] += (nH / 2) - cY

image = cv2.warpAffine(src=image, M=matrix, dsize=(nW, nH))

x = center[0] - width / 2 #值要是270 才能匹配上

y = center[1] - height / 2 #值要是604 才能匹配上

cv2.rectangle(image, (int(x), int(y)), (int(x + width), int(y + height)), (0, 0, 255), 2)

cv2.imshow('无损的', image)

cv2.waitKey()

cv2.destroyAllWindows()

path = "C:\\Users\\sa\\Desktop\\car_img\\1\\4662975.jpg" #记得不要有中文路径

sky = cv2.imread(path)

sub_image(sky, ((255.9073944091797, 512.5665893554688), (52.486114501953125, 129.72068786621094), -80.71644592285156))

sub_image2(sky, ((255.9073944091797, 512.5665893554688), (52.486114501953125, 129.72068786621094), -80.71644592285156))

4662975.jpg
opencv 无损旋转后 坐标不知如何重新计算回来


回答:

加一行代码就解决了

center = np.dot(matrix, list(center) + [1]) #旋转后重新计算矩形中心坐标

def sub_image2(image, rect):

center = rect[0]

width, height = rect[1]

angle = rect[2]

w, h = image.shape[1::-1]

cX, cY = w // 2, h // 2

if width < height:

width, height = rect[1][::-1]

angle = 90 + angle

matrix = cv2.getRotationMatrix2D(center=(cX, cY), angle=angle, scale=1)

cos = np.abs(matrix[0, 0])

sin = np.abs(matrix[0, 1])

# compute the new bounding dimensions of the image

nW = int((h * sin) + (w * cos))

nH = int((h * cos) + (w * sin))

# adjust the rotation matrix to take into account translation

matrix[0, 2] += (nW / 2) - cX

matrix[1, 2] += (nH / 2) - cY

image = cv2.warpAffine(src=image, M=matrix, dsize=(nW, nH))

center = np.dot(matrix, list(center) + [1]) #旋转后重新计算矩形中心坐标

x = center[0] - width / 2 #值要是270 才能匹配上

y = center[1] - height / 2 #值要是604 才能匹配上

cv2.rectangle(image, (int(x), int(y)), (int(x + width), int(y + height)), (0, 0, 255), 2)

cv2.imshow('无损的', image)

cv2.waitKey()

cv2.destroyAllWindows()

以上是 opencv 无损旋转后 坐标不知如何重新计算回来 的全部内容, 来源链接: utcz.com/a/164062.html

回到顶部