首页 > 解决方案 > OpenCv获取到圆心的边缘距离

问题描述

有点偏离介绍,我需要做一个视觉辅助来将工作表与固定点对齐。我的设置有 3 个点,需要使用叉车将钣金板定位在这些点上。这是一项温和的任务,我们不能用蛮力来对齐板材,所以我想安装摄像头来帮助他们对齐钣金板。

到目前为止的代码:

import sys
import cv2 as cv
import numpy as np

cap = cv.VideoCapture(0)
val = 50

while(True):
    # Capture frame-by-frame
    ret, frame = cap.read()
       
    gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)    
    gray = cv.GaussianBlur(gray, (5,5), 0)

    rows = gray.shape[0]
    circles = cv.HoughCircles(gray, cv.HOUGH_GRADIENT, 1, rows / 8,
                           param1=100, param2=30,
                           minRadius=1, maxRadius=30)

    edges = cv.Canny(gray,val,val*3,apertureSize = 3)
    lines = cv.HoughLines(edges,1.2,np.pi/180,200)

    font = cv.FONT_HERSHEY_SIMPLEX
    color = (255, 255, 255)
    thickness = 2
    index = 1
    
    if len(circles[0]) > 2 :
    
        circles = np.uint16(np.floor(circles))           
        circles2=sorted(circles[0],key=lambda x:x[0],reverse=False)  
    
        print (circles2)
            
        for i in circles2:
            center = (i[0], i[1])
            cv.circle(frame, center, 1, (0, 255, 0), 3)
        
            text = str(index)  +' ' +  str(i[0]) +' ' +  str(i[1])
            cv.putText(frame, text, center, font, 1, color, thickness, cv.LINE_AA)
        
        index += 1
        
    cv.imshow("detected circles", frame)
    cv.imshow("detected edges", edges)

    if cv.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv.destroyAllWindows()

代码的输出

所以找到了点,不知何故,我需要在第 2 和第 3 点“上方”的边缘找到第一个 255 值,以及第一个点旁边的最后 255 个值,我正在努力切片?数组,找到值 255,返回它的索引,所以我可以计算点和板之间的距离。

关于如何获得之间距离的任何想法?

先感谢您

标签: opencvopencv-python

解决方案


我知道了。

编码:

import sys
import cv2 as cv
import numpy as np

cap = cv.VideoCapture(0)
val = 50
singleprint = 0
# Dots per millimeter
dpmm = 2 

def distance(circle):
    # Calculating the distance np.where(array[row, column])
    p = 0
    if axis == 1:
        p = np.where(edges[:,circle[0]] == 255)[0][0]
        return (circle[1] - p - circle[2])/dpmm
    else:
        p = np.where(edges[circle[1],:] == 255)[0][-1]
        return (p - circle[0] - circle[2])/dpmm

while(True):
    # Capture frame-by-frame
    ret, frame = cap.read()
           
    gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)    
    gray = cv.GaussianBlur(gray, (5,5), 0)

    rows = gray.shape[0]
    circles = cv.HoughCircles(gray, cv.HOUGH_GRADIENT, 1, rows / 8,
                           param1=100, param2=30,
                           minRadius=1, maxRadius=30)

    edges = cv.Canny(gray,val,val*3,apertureSize = 3)
    lines = cv.HoughLines(edges,1.2,np.pi/180,200)

    # Text property's
    font = cv.FONT_HERSHEY_SIMPLEX
    color = (255, 255, 255)
    thickness = 2  

    index = 1
    axis = 0

    if len(circles[0]) > 2 :
    
        circles = np.uint16(np.floor(circles))           
        circles2=sorted(circles[0],key=lambda x:x[0],reverse=False)  
                 
        for i in circles2:
            center = (i[0], i[1])
        
            cv.circle(frame, center, 1, (0, 255, 0), 3)
        
            text = str(distance(i))
            cv.putText(frame, text, center, font, 1, color, thickness, cv.LINE_AA)
        
            index += 1
            axis = 1
     
    cv.imshow("detected circles", frame)

    if cv.waitKey(1) & 0xFF == ord('q'):
        break

# When everything done, release the capture
cap.release()
cv.destroyAllWindows()

在此处输入图像描述 在此处输入图像描述

关键是学习在特定行或列中使用 Numpy

np.where(edges[:,circle[0]] == 255)[0][0]

资源:https ://youtu.be/GB9ByFAIAH4?t=1103

希望这对其他人有帮助。谢谢大家


推荐阅读