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

本文探讨了如何在使用OpenCV进行无损旋转图像后,重新计算旋转前的矩形坐标。通过应用旋转矩阵并调整尺寸,可以找到旋转后的对应坐标。示例代码展示了如何实现这一转换。
摘要由CSDN通过智能技术生成

如题,已有某个矩形坐标,无损旋转后,坐标不知道如何计算回来。

如下代码 希望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

96f01f73c1e4eb626d4ea97f2c5d9b79.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值