opencv 无损旋转后 坐标不知如何重新计算回来
如题,已有某个矩形坐标,无损旋转后,坐标不知道如何计算回来。
如下代码 希望sub_image2函数能重新标回坐标, 实在不好意思我是做python的 只能放python代码
4662975.jpg

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

...全文
相关推荐
Python+OpenCV 实现图片无损旋转90°且无黑边
今天小编就为大家分享一篇Python+OpenCV 实现图片无损旋转90°且无黑边,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
opencv图像 旋转 坐标
使用opencv对图像进行旋转,分为图像尺寸不变和尺寸变大两种,对尺寸变大时计算旋转后对应的坐标值。
双目已知图像坐标求空间坐标opencv
基于opencv的已知左右图像坐标求空间中三维坐标,以及已知空间坐标求其在左右图像中的二维坐标。
opencv 图像旋转 任一点坐标转换 裁剪图像
根据txt中记录的角度旋转图像并用黑色像素补齐,通过txt中记录的斜矩形框四个点的坐标求出新坐标,对图像进行裁剪
opencv双目视觉棋盘格标定,特征匹配点,三维坐标计算
根据官方例程进行改变。进行三角测量的两种方法进行三维坐标计算,非全图三维重建,是特征点,这里使用的是棋盘格角点作为匹配点


工具平台和程序库
