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

屎克螂 2019-08-13 07:43:58
如题,已有某个矩形坐标,无损旋转后,坐标不知道如何计算回来。
如下代码 希望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
...全文
270 1 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2019-08-14
  • 打赏
  • 举报
回复
获取变换矩阵 矩阵求逆
屎克螂 2019-08-14
  • 打赏
  • 举报
回复
加一行代码解决了 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 C#开发的强大测量工具源码,涵盖了圆卡尺、矩形卡尺、直线卡尺及距离测量工具,所有工具均经实际项目验证,运行稳定。文中详细介绍了仿Halcon的视觉控件源码,该控件支持平移、无损缩放、自定义图形显示等功能,极大提高了视觉项目的开发效率。此外,文章提供了距离测量工具的代码示例与分析,包括命名空间引用、类定义、测量方法等内容。针对圆卡尺和矩形卡尺,分别介绍了极坐标转换找边缘点和动态ROI旋转机制的核心逻辑。最后,探讨了控件部分的渲染逻辑,如双缓冲思路下的三层图层实现、鼠标拖动时的坐标换算等。; 适合人群:从事机器视觉项目的开发者,以及对OpenCV C#开发感兴趣的初学者或有一定经验的研发人员。; 使用场景及目标:①在机器视觉项目中实现高效、稳定的测量工具;②学习OpenCV C#开发技巧,掌握视觉控件的高级功能;③理解并应用测量工具的核心算法,如极坐标转换、ROI处理、双缓冲渲染等。; 其他说明:项目中还包含了测试图片,便于快速上手测试。同时,提供了一些实用技巧,如进程间大图传输、测量结果表格自动生成、SIMD指令加速等。阅读过程中可以结合提供的代码示例进行实践,加深理解。

24,860

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧