CV2 将图片中某个点与中心点的角度变换成0-360度

众所周知,CV2中的坐标方向是这样的:

所以一般我们想计算图片中某个点P1(x1,y1)与中心点P0(x0,y0)的方向时,我们会先将y坐标翻上去,然后计算角度。即:

p1_x=int(x1) # 
p1_y=int(y1)

p0_x=int(x0)  #图像大小为512*512中心点坐标为255,255
p0_y=int(y0)

dx = p1_x-255
dy= -(p1_y-255) #因为cv2朝下,所以方向为-

angle=math.degrees(math.atan2(dy,dx)) #先转化成弧度再转化成角度



现在得到的角度是以中心点P0为原点,以x轴正方向为0度,取值范围为-180-+180度的角度。

我们想要将其转化成下图所示的这种360度的形式:

观察上上图后可以知道,在0-180度(-pi~pi的坐标系下)的时候,我们需要用180减去当前角度,在0到-180(-pi~pi的坐标系下)的时候,我们需要用180加上负的当前角度。

"""
 if angle>=0:
    angle=180-angle
 if angle<0:
    angle=180+(-angle)
"""
#总之,就是:
angle=180-angle

  • 6
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要使用Python对二维码进行霍夫变换(Hough Transform)进行矫正,需要先安装OpenCV库。以下是步骤: 1. 导入所需的库 ```python import cv2 import numpy as np from matplotlib import pyplot as plt ``` 2. 读取图像 ```python img = cv2.imread('qrcode.png') ``` 3. 转换为灰图像 ```python gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) ``` 4. 边缘检测 ```python edges = cv2.Canny(gray,50,150,apertureSize = 3) ``` 5. 进行霍夫变换 ```python lines = cv2.HoughLines(edges,1,np.pi/180,200) ``` 6. 计算旋转角度 ```python for rho,theta in lines[0]: a = np.cos(theta) b = np.sin(theta) x0 = a*rho y0 = b*rho x1 = int(x0 + 1000*(-b)) y1 = int(y0 + 1000*(a)) x2 = int(x0 - 1000*(-b)) y2 = int(y0 - 1000*(a)) cv2.line(img,(x1,y1),(x2,y2),(0,0,255),2) angle = np.arctan2(y2-y1, x2-x1) * 180.0 / np.pi ``` 7. 计算心坐标 ```python rows,cols = img.shape[:2] M = cv2.getRotationMatrix2D((cols/2,rows/2),angle,1) dst = cv2.warpAffine(img,M,(cols,rows)) gray = cv2.cvtColor(dst,cv2.COLOR_BGR2GRAY) ret,thresh = cv2.threshold(gray,127,255,cv2.THRESH_BINARY) M = cv2.moments(thresh) cx = int(M['m10']/M['m00']) cy = int(M['m01']/M['m00']) ``` 8. 转化为0-1矩阵 ```python binary = cv2.threshold(gray, 0, 1, cv2.THRESH_BINARY)[1] ``` 完整代码如下: ```python import cv2 import numpy as np from matplotlib import pyplot as plt img = cv2.imread('qrcode.png') gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) edges = cv2.Canny(gray,50,150,apertureSize = 3) lines = cv2.HoughLines(edges,1,np.pi/180,200) for rho,theta in lines[0]: a = np.cos(theta) b = np.sin(theta) x0 = a*rho y0 = b*rho x1 = int(x0 + 1000*(-b)) y1 = int(y0 + 1000*(a)) x2 = int(x0 - 1000*(-b)) y2 = int(y0 - 1000*(a)) cv2.line(img,(x1,y1),(x2,y2),(0,0,255),2) angle = np.arctan2(y2-y1, x2-x1) * 180.0 / np.pi rows,cols = img.shape[:2] M = cv2.getRotationMatrix2D((cols/2,rows/2),angle,1) dst = cv2.warpAffine(img,M,(cols,rows)) gray = cv2.cvtColor(dst,cv2.COLOR_BGR2GRAY) ret,thresh = cv2.threshold(gray,127,255,cv2.THRESH_BINARY) M = cv2.moments(thresh) cx = int(M['m10']/M['m00']) cy = int(M['m01']/M['m00']) binary = cv2.threshold(gray, 0, 1, cv2.THRESH_BINARY)[1] plt.imshow(binary,cmap = 'gray') plt.show() print("旋转角度为:", angle) print("心坐标为:", cx, cy) ``` 需要注意的是,该方法只适用于二维码是正方形的情况,如果是长方形的话需要进行额外的处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

擅长划水的小汪同学

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值