Python 2.7 如何识别此圆边?

Python 2.7 如何识别此圆边?,python-2.7,opencv,image-processing,opencv3.0,Python 2.7,Opencv,Image Processing,Opencv3.0,我正在尝试使用python中的OpenCV识别这张图片中的一个圆: 我用网络摄像机拍到了这张照片。 这个灰色圆圈的内部也有一个徽标。该徽标与圆圈外部(黑色)的颜色相同 我想把这个灰色的圆圈和黑色的背景分开,在圆圈周围画一条锐利的线 我如何继续?一个建议: 将图像转换为灰度图像,并使用Hough圆变换检测圆。 opencv3.0.0中显示了一个示例。建议: 将图像转换为灰度图像,并使用Hough圆变换检测圆。 opencv3.0.0中显示了一个示例。以下是实现此目标的方法: 首先是阈值图像,然后

我正在尝试使用python中的OpenCV识别这张图片中的一个圆:

我用网络摄像机拍到了这张照片。 这个灰色圆圈的内部也有一个徽标。该徽标与圆圈外部(黑色)的颜色相同

我想把这个灰色的圆圈和黑色的背景分开,在圆圈周围画一条锐利的线

我如何继续?

一个建议: 将图像转换为灰度图像,并使用Hough圆变换检测圆。 opencv3.0.0中显示了一个示例。建议: 将图像转换为灰度图像,并使用Hough圆变换检测圆。
opencv3.0.0中显示了一个示例。

以下是实现此目标的方法: 首先是阈值图像,然后使用查找坐标函数,返回列表的最大值将是您的圆。找到山峰,画一个圆圈穿过它们

import cv2 
import numpy as np
import random
from random import randint
import math

image=cv2.imread('C:/Users/srlatch/Desktop/XtfWR.jpg')
img = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)


def f(list):
    max=[]
    for i in list:
        if len(i)>len(max):
            max=i
    return max 

def draw_circle(img,ctr):
    for i in ctr:
        img[i[0][1]][i[0][0]]=255


ret,tresh = cv2.threshold(img,40,255,cv2.THRESH_BINARY)
kernel = np.ones((5,5),np.uint8)
cv2.erode(img,kernel,iterations=2)
tresh,c,hr=cv2.findContours(tresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
circle=f(c)


max_y=0

min_y=circle[0][0][1]

max_x=0

min_x=circle[0][0][0]


pointY=tuple()
pointy=tuple()
pointX=tuple()
pointx=tuple()


for i in circle:
    if i[0][1]>max_y: 
      max_y=i[0][1]
      pointY=(i[0][1], i[0][0])

for i in circle:
    if i[0][1]<=min_y: 
      min_y=i[0][1]
      pointy=(i[0][1], i[0][0])

for i in circle:
    if i[0][0]>max_x: 
      max_x=i[0][0]
      pointX=(i[0][1], i[0][0])

for i in circle:
    if i[0][0]<=min_x: 
      min_x=i[0][0]
      pointx=(i[0][1], i[0][0])

cv2.ellipse(image, (pointY[1],pointY[0]),(2,2),0,0,360,(243,0,214),-1)  #min y
cv2.ellipse(image, (pointy[1],pointy[0]),(2,2),0,0,360,(243,0,214),-1)  #max y
cv2.ellipse(image, (pointX[1],pointX[0]),(2,2),0,0,360,(243,0,214),-1)  #min y
cv2.ellipse(image, (pointx[1],pointx[0]),(2,2),0,0,360,(243,0,214),-1)  #min y

center_x=(pointX[0]+pointx[0])/2
center_y=( (pointY[1]+pointy[1])/2 )-pointy[0]

cv2.circle(image, (int(center_x),int(center_y)), int((center_y+pointy[0])-60),(243,0,214), thickness=1, lineType=8, shift=0)

cv2.imshow('wnd', image)

cv2.waitKey(0)
导入cv2
将numpy作为np导入
随机输入
从随机导入randint
输入数学
image=cv2.imread('C:/Users/srlack/Desktop/XtfWR.jpg'))
img=cv2.cvt颜色(图像,cv2.COLOR\u bgr2灰色)
def f(列表):
最大值=[]
对于列表中的i:
如果len(i)>len(max):
max=i
返回最大值
def牵引圈(img、ctr):
对于ctr中的i:
img[i[0][1]][i[0][0]]=255
ret,tresh=cv2.threshold(img,40255,cv2.THRESH_二进制)
内核=np.ones((5,5),np.uint8)
侵蚀(img,内核,迭代次数=2)
tresh,c,hr=cv2.查找对象(tresh,cv2.检索树,cv2.链近似简单)
圆=f(c)
max_y=0
最小y=圆[0][0][1]
max_x=0
min_x=圆[0][0][0]
pointY=tuple()
pointy=tuple()
pointX=元组()
pointx=元组()
对于圈中的i:
如果i[0][1]>最大值:
max_y=i[0][1]
pointY=(i[0][1],i[0][0])
对于圈中的i:
如果i[0][1]max_x:
max_x=i[0][0]
pointX=(i[0][1],i[0][0])
对于圈中的i:

如果i[0][0]以下是实现此目标的方法: 首先是阈值图像,然后使用查找坐标函数,返回列表的最大值将是您的圆。找到山峰,画一个圆圈穿过它们

import cv2 
import numpy as np
import random
from random import randint
import math

image=cv2.imread('C:/Users/srlatch/Desktop/XtfWR.jpg')
img = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)


def f(list):
    max=[]
    for i in list:
        if len(i)>len(max):
            max=i
    return max 

def draw_circle(img,ctr):
    for i in ctr:
        img[i[0][1]][i[0][0]]=255


ret,tresh = cv2.threshold(img,40,255,cv2.THRESH_BINARY)
kernel = np.ones((5,5),np.uint8)
cv2.erode(img,kernel,iterations=2)
tresh,c,hr=cv2.findContours(tresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
circle=f(c)


max_y=0

min_y=circle[0][0][1]

max_x=0

min_x=circle[0][0][0]


pointY=tuple()
pointy=tuple()
pointX=tuple()
pointx=tuple()


for i in circle:
    if i[0][1]>max_y: 
      max_y=i[0][1]
      pointY=(i[0][1], i[0][0])

for i in circle:
    if i[0][1]<=min_y: 
      min_y=i[0][1]
      pointy=(i[0][1], i[0][0])

for i in circle:
    if i[0][0]>max_x: 
      max_x=i[0][0]
      pointX=(i[0][1], i[0][0])

for i in circle:
    if i[0][0]<=min_x: 
      min_x=i[0][0]
      pointx=(i[0][1], i[0][0])

cv2.ellipse(image, (pointY[1],pointY[0]),(2,2),0,0,360,(243,0,214),-1)  #min y
cv2.ellipse(image, (pointy[1],pointy[0]),(2,2),0,0,360,(243,0,214),-1)  #max y
cv2.ellipse(image, (pointX[1],pointX[0]),(2,2),0,0,360,(243,0,214),-1)  #min y
cv2.ellipse(image, (pointx[1],pointx[0]),(2,2),0,0,360,(243,0,214),-1)  #min y

center_x=(pointX[0]+pointx[0])/2
center_y=( (pointY[1]+pointy[1])/2 )-pointy[0]

cv2.circle(image, (int(center_x),int(center_y)), int((center_y+pointy[0])-60),(243,0,214), thickness=1, lineType=8, shift=0)

cv2.imshow('wnd', image)

cv2.waitKey(0)
导入cv2
将numpy作为np导入
随机输入
从随机导入randint
输入数学
image=cv2.imread('C:/Users/srlack/Desktop/XtfWR.jpg'))
img=cv2.cvt颜色(图像,cv2.COLOR\u bgr2灰色)
def f(列表):
最大值=[]
对于列表中的i:
如果len(i)>len(max):
max=i
返回最大值
def牵引圈(img、ctr):
对于ctr中的i:
img[i[0][1]][i[0][0]]=255
ret,tresh=cv2.threshold(img,40255,cv2.THRESH_二进制)
内核=np.ones((5,5),np.uint8)
侵蚀(img,内核,迭代次数=2)
tresh,c,hr=cv2.查找对象(tresh,cv2.检索树,cv2.链近似简单)
圆=f(c)
max_y=0
最小y=圆[0][0][1]
max_x=0
min_x=圆[0][0][0]
pointY=tuple()
pointy=tuple()
pointX=元组()
pointx=元组()
对于圈中的i:
如果i[0][1]>最大值:
max_y=i[0][1]
pointY=(i[0][1],i[0][0])
对于圈中的i:
如果i[0][1]max_x:
max_x=i[0][0]
pointX=(i[0][1],i[0][0])
对于圈中的i:

如果i[0][0]另一种方法是使用中值滤波器过滤图像。它减少了噪音并保留了边缘。在这之后,您可以使用固定的
阈值进行二值化(您有一个很好的双峰直方图),然后执行
findContours
。然后您可以计算
mineConclosingCircle
或使用
fitEllipse
或任何您想要实现的功能

# Blur the image to supress artefacts
# in the binary image while preserving edges
median = cv2.medianBlur(imgray,15)
# Do a global thresholding
ret, thresh = cv2.threshold(median,0,255,cv2.THRESH_BINARY + cv2.THRESH_OTSU)

#Copy image to show binary image (findContours maipulates source)
copy =thresh.copy()

#Detect just external contours
im2, contours, hierarchy = cv2.findContours(copy, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
height, width, channels = src.shape

#Image for displaying contour
drawing = np.zeros((height, width, 3), dtype='uint8')

#No loop needed, just one external contour
#Draw contour, minimal enclosing circle and ellispe
cv2.drawContours(drawing,contours,0,(255,0,0))
(x,y), radius = cv2.minEnclosingCircle(contours[0])
center = (int(x),int(y))
cv2.circle(drawing,center,int(radius),(0,0,255))
ellipse = cv2.fitEllipse(contours[0])
cv2.ellipse(drawing,ellipse,(0,255,0),1)

cv2.imshow("Binary", thresh)
cv2.imshow("Median", median)
cv2.imshow("Contour", drawing)

以下是结果,轮廓显示为蓝色,MineConclosingCircle显示为红色,椭圆显示为绿色


另一种方法是使用中值滤波器过滤图像。它减少了噪音并保留了边缘。在这之后,您可以使用固定的
阈值进行二值化(您有一个很好的双峰直方图),然后执行
findContours
。然后您可以计算
mineConclosingCircle
或使用
fitEllipse
或任何您想要实现的功能

# Blur the image to supress artefacts
# in the binary image while preserving edges
median = cv2.medianBlur(imgray,15)
# Do a global thresholding
ret, thresh = cv2.threshold(median,0,255,cv2.THRESH_BINARY + cv2.THRESH_OTSU)

#Copy image to show binary image (findContours maipulates source)
copy =thresh.copy()

#Detect just external contours
im2, contours, hierarchy = cv2.findContours(copy, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
height, width, channels = src.shape

#Image for displaying contour
drawing = np.zeros((height, width, 3), dtype='uint8')

#No loop needed, just one external contour
#Draw contour, minimal enclosing circle and ellispe
cv2.drawContours(drawing,contours,0,(255,0,0))
(x,y), radius = cv2.minEnclosingCircle(contours[0])
center = (int(x),int(y))
cv2.circle(drawing,center,int(radius),(0,0,255))
ellipse = cv2.fitEllipse(contours[0])
cv2.ellipse(drawing,ellipse,(0,255,0),1)

cv2.imshow("Binary", thresh)
cv2.imshow("Median", median)
cv2.imshow("Contour", drawing)

以下是结果,轮廓显示为蓝色,MineConclosingCircle显示为红色,椭圆显示为绿色


谢谢你的示例代码。什么是max_y_f()函数?图片下的max-min函数,我只是删除了函数定义谢谢…我自己做的..我在重新排列代码时得到了一些提示…所以,我学到了一些东西。。但是,它没有给出确切的中心点。。谢谢你的示例代码。什么是max_y_f()函数?图片下的max-min函数,我只是删除了函数定义hanks…我自己做的..我在重新排列代码时得到了一些提示…所以,我学到了一些东西。。但是,它没有给出确切的中心点。。好主意。也许我可以得到你的信息来改进我的代码。谢谢。你先只添加了idea。然后你编辑了答案…所以,我的意思是关于idea的…不管怎样,我使用的是Python…所以,我需要转换你的代码。谢谢谢谢…我用这个系统可以得到精确的中心点。但是,这个系统可以得到给定形状的任何中心点。你知道只检测圆心吗?你能解释一下你的意思吗?我不明白!你想要形状的确切中心吗?对不起,如果你进一步解释的话,我可能会明白。我想,如果我给一个圆的形状,它可以识别圆心。没问题。但是,如果我给一个矩形或任何其他形状,它也可以识别圆心。实际上,这个代码每次都会自动得到一个形状。怎么才能只找到一个圆心呢?好主意。也许我可以得到你的信息来改进我的代码。谢谢。你先只添加了idea。然后你编辑了答案…所以,我的意思是关于idea的…不管怎样,我使用的是Python…所以,我需要转换你的代码。谢谢谢谢…我用这个系统可以得到精确的中心点。但是,这个系统可以得到给定形状的任何中心点。你知道只检测圆心吗?你能解释一下你的意思吗?我不明白!你想要形状的确切中心吗?对不起,如果你再解释一下,也许我会明白的