Deutsche westliche 2021-05-27 07:15 采纳率: 64.3%
浏览 74
已结题

Open CV 识别指针偏转角度

 在学习一段代码的时候 有段代码阅读的不是很懂 该段代码的目的是通过旋转虚拟指针找到和原指针重合最高的地方 并求出角度

t1 = img.copy(); t1[temp[:, :, 2] == 255] = 255; c = img[temp[:, :, 2] == 255]; points = c[c == 0]; freq_list.append((len(points), i))

这五行代码就十分疑惑不知所云 不知道各位朋友能不能详细解惑一番

def get_pointer_rad(img):
    shape = img.shape
    c_y, c_x, depth = int(shape[0] / 2), int(shape[1] / 2), shape[2]
    x1=c_x+c_x*0.8
src = img.copy()
    freq_list = []
    for i in range(361):
        x = (x1 - c_x) * cos(i * pi / 180) + c_x
        y = (x1 - c_x) * sin(i * pi / 180) + c_y
        temp = src.copy()
        cv2.line(temp, (c_x, c_y), (int(x), int(y)), (0, 0, 255), thickness=3)
        t1 = img.copy()
        t1[temp[:, :, 2] == 255] = 255
        c = img[temp[:, :, 2] == 255]
        points = c[c == 0]
        freq_list.append((len(points), i))
        cv2.imshow('d', temp)
        cv2.imshow('d1', t1)
        cv2.waitKey(1)
    print('当前角度:',max(freq_list, key=lambda x: x[0]),'度')
    cv2.destroyAllWindows()
return max(freq_list, key=lambda x: x[0])
  • 写回答

2条回答 默认 最新

  • 爱晚乏客游 2021-05-27 09:39
    关注

    你这个格式确定没问题吗?对齐了很好理解了。前面几行看图应该就能理解是干什么的,从11开始,就是在temp中画一条(c_x,c_y)到点(x,y)的红色的直线,13行是判断temp中,如果某个点的红色通道灰度值为255,则将t1中同样位置的该点的颜色置为255(白色),14行c是将temp中红色通道为白色(255)的位置置为True,其他为false,15行是将C中等于0的点位置坐标传给points(确定这里没错吗?应该是c==True才能符合预期效果吧),
    然后freq_list 保存points的长度和此时的角度i。最后几行的显示图像,20和22的max()是找到freq_list[0]中最大的点。

    综合起来说,就是用一个旋转的直线,来判断图中和直线重合的点最多的直线的旋转角度是多少,及最后的重合点的个数(15行要修改才行),像下面的那个图,最后的结果就是重合点最多的是3261个,旋转角是319度

    
    import cv2
    from math import cos,sin,pi
    import numpy as np
    
    def get_pointer_rad(img):
        shape = img.shape
        c_y, c_x, depth = int(shape[0] / 2), int(shape[1] / 2), shape[2]
        x1=c_x+c_x*0.8
        src = img.copy()
        freq_list = []
        for i in range(361):
            x = (x1 - c_x) * cos(i * pi / 180) + c_x
            y = (x1 - c_x) * sin(i * pi / 180) + c_y
            temp = src.copy()
            cv2.line(temp, (c_x, c_y), (int(x), int(y)), (0, 0, 255), thickness=3)
            t1 = img.copy()
            t1[temp[:, :, 2] == 255] = [0,0,255]
            c = img[temp[:, :, 2] == 255]
            points = c[c == True]
            freq_list.append((len(points), i))
            cv2.imshow('d', temp)
            cv2.imshow('d1', t1)
    
            cv2.waitKey(1)
        print('当前角度:',max(freq_list, key=lambda x: x[0]),'度')
        cv2.destroyAllWindows()
        return max(freq_list, key=lambda x: x[0])
    
    img=np.ones((500,500),np.uint8)
    img=cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)
    cv2.line(img,(250,250),(20,20),(0,0,255),3,8)
    get_pointer_rad(img)
    
    
    #output
    当前角度: (3261, 319) 度
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应