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

屎克螂 屎克螂 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
如题,已有某个矩形坐标,无损旋转后,坐标不知道如何计算回来。
如下代码 希望sub_image2函数能重新标回坐标, 实在不好意思我是做python的 只能放python代码


# -*- 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
...全文

相关推荐

评论 1
收藏 已收藏
分享
返回顶部