[opencv]图像和特征点旋转

本来说这是很简单的一个内容,图像旋转只需要使用opencv中自带的旋转函数即可完成,但是最近在做特征点旋转的时候发现使用内置rotate函数给图像旋转90度,再用getRotationMatrix2D得出的旋转矩阵对特征点旋转,画出来的特征点位置全部错误!

这是用gpt生成的代码编写的效果(AI还是不靠谱啊)

这里放出AI的代码:

python 复制代码
#这里我只放出核心代码
#旋转图像
rotated_img = cv2.rotate(img, cv2.ROTATE_90_CLOCKWISE)

#旋转特征点
M = cv2.getRotationMatrix2D((cols/2, rows/2), 90, 1)
rotated_keypoints = []
for kp in keypoints:
    x, y = kp.pt
    x -= cols / 2
    y -= rows / 2
    rotated_x = x * M[0, 0] + y * M[0, 1] + cols / 2
    rotated_y = x * M[1, 0] + y * M[1, 1] + rows / 2
    rotated_keypoints.append(cv2.KeyPoint(rotated_x, rotated_y, kp.size, kp.angle - 90, kp.response, kp.octave, kp.class_id))

首先我们先研究一下 rotate这个函数:

dst=cv.rotate(src, rotateCode[, dst])

src输入的图像

rotateCode输入需要旋转的flag

这是opencv4.2.0文档介绍cv.ROTATE_90_CLOCKWISE顺时针旋转90度。

这里 rotate函数是按照原图(0,0)点进行旋转的,AI生成的是按照图像的中心点旋转,肯定没法旋转到制定位置。我们改为按照(0,0)结果还是错误。

再来看一下getRotationMatrix2D函数:

正常来说2D平面的旋转是:

可以看出Opencv得出的旋转矩阵是这个矩阵的转置!因为图像的坐标Y轴是向下的,这和数学中的XY坐标相反。

官方参数介绍:

|--------|------------------------------------------------------------------------------------------------------------------------------------------|
| center | Center of the rotation in the source image. |
| angle | Rotation angle in degrees. Positive values mean counter-clockwise rotation (the coordinate origin is assumed to be the top-left corner). |
| scale | Isotropic scale factor. |

angle正则表示逆时针,负数表示顺时针。源码应该改为-90度。这样特征点得到矩阵才是正确的。但是这样旋转后的到坐标还是在原来图像坐标系下的点,我们需要还原到旋转后图像的点。

我们旋转后得到的坐标,转换到旋转图像后的坐标需要在X轴加上一个图像行数:

举例子:

原始坐标(1,2)顺时针旋转90度得到坐标(-2,1),这个是在原始坐标系下的坐标。

这个坐标放在新图,位置肯定错误,两个坐标在x轴上相差一个图像的行数(假设图像480*640)

在新图下的坐标为(-2+640,1)=(638,1)。这样我们得到的特征点旋转的坐标才正确。

本文只适应旋转90度这样的类型,如果需要特定角度,这里x,y偏移的坐标需要重新计算。

修改后的源码:

python 复制代码
#只需要修改旋转特征点部分
M = cv2.getRotationMatrix2D((0, 0), -90, 1)
rotated_keypoints = []
for kp in keypoints:
    x, y = kp.pt
    rotated_x = x * M[0, 0] + y * M[0, 1] + rows
    rotated_y = x * M[1, 0] + y * M[1, 1] 
    rotated_keypoints.append(cv2.KeyPoint(rotated_x, rotated_y, kp.size, kp.angle - 90, kp.response, kp.octave, kp.class_id))

修改源码后的效果:

方便大家获取贴出源码,求个关注收藏:

python 复制代码
import cv2
import numpy as np

# 读取图像
img = cv2.imread('image.png')

# 使用FAST算法提取特征点
fast = cv2.FastFeatureDetector_create()
keypoints = fast.detect(img, None)

# 旋转图像
rotated_img = cv2.rotate(img, cv2.ROTATE_90_CLOCKWISE)

# 旋转特征点并进行平移
rows, cols = img.shape[:2]
M = cv2.getRotationMatrix2D((0, 0), -90, 1)
rotated_keypoints = []
for kp in keypoints:
    x, y = kp.pt
    rotated_x = x * M[0, 0] + y * M[0, 1] + rows
    rotated_y = x * M[1, 0] + y * M[1, 1] 
    rotated_keypoints.append(cv2.KeyPoint(rotated_x, rotated_y, kp.size, kp.angle - 90, kp.response, kp.octave, kp.class_id))

# 绘制特征点
img_with_keypoints = cv2.drawKeypoints(img, keypoints, None)
rotated_img_with_keypoints = cv2.drawKeypoints(rotated_img, rotated_keypoints, None)

# 显示结果
cv2.imshow('Original Image with Keypoints', img_with_keypoints)
cv2.imshow('Rotated Image with Keypoints', rotated_img_with_keypoints)
cv2.waitKey(0)
cv2.destroyAllWindows()

参考文章:

OpenCV: OpenCV modules

二维旋转矩阵与向量旋转 - 知乎

相关推荐
yvestine14 分钟前
自然语言处理——文本表示
人工智能·python·算法·自然语言处理·文本表示
zzc92122 分钟前
MATLAB仿真生成无线通信网络拓扑推理数据集
开发语言·网络·数据库·人工智能·python·深度学习·matlab
点赋科技23 分钟前
沙市区举办资本市场赋能培训会 点赋科技分享智能消费新实践
大数据·人工智能
HeteroCat30 分钟前
一周年工作总结:做了一年的AI工作我都干了什么?
人工智能
YSGZJJ44 分钟前
股指期货技术分析与短线操作方法介绍
大数据·人工智能
Guheyunyi1 小时前
监测预警系统重塑隧道安全新范式
大数据·运维·人工智能·科技·安全
码码哈哈爱分享1 小时前
[特殊字符] Whisper 模型介绍(OpenAI 语音识别系统)
人工智能·whisper·语音识别
郄堃Deep Traffic1 小时前
机器学习+城市规划第十三期:XGBoost的地理加权改进,利用树模型实现更精准的地理加权回归
人工智能·机器学习·回归·城市规划
Lucky-Niu1 小时前
解决transformers.adapters import AdapterConfig 报错的问题
人工智能·深度学习