[英]Detecting A Half Circle Using Opencv Python
I am trying to detect a semi cricle in the following image using python opencv:我正在尝试使用 python opencv 检测下图中的半圆形:
Following is what i am trying to achieve: (Edited Image )以下是我想要实现的目标:(编辑图像) I want to exactly detect the semicircle a我想准确检测半圆 a
I have used contour detection but it also hasn't work that well here is the code for that:我已经使用了轮廓检测,但它也没有很好地工作这里是代码:
import cv2
import numpy as np
import matplotlib.pyplot as plt
img = cv2.imread('images/m_1.jpg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = cv2.blur(img,(5,5))
lower = np.array([60,60,60])
higher = np.array([80,80,80])
mask = cv2.inRange(img,lower,higher)
cont,_ = cv2.findContours(mask,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
cont_img = cv2.drawContours(img,cont,-1,255,3)
plt.imshow(cont_img)
#plt.imshow(img)
plt.show()
Following is the result of the image:以下是图像的结果:
How can i accurately detect a the semi circle (Accurately ) and also optional is how can i find the centre of the semicircle and draw a vertical line我怎样才能准确地检测到一个半圆(准确地)并且可选的是我怎样才能找到半圆的中心并画一条垂直线
Here is one way to do that in Python/OpenCV/Skimage.这是在 Python/OpenCV/Skimage 中执行此操作的一种方法。
The idea is to use some Gaussian blur.这个想法是使用一些高斯模糊。 Then use kmeans processing to get 3 colors.然后用kmeans处理得到3个colors。 Then threshold to get the middle color and clean it up with some morphology close and open.然后阈值得到中间颜色并用一些形态关闭和打开来清理它。 Then get the contour and draw it on the input.然后获取轮廓并将其绘制在输入上。
Input:输入:
import cv2
import numpy as np
from sklearn import cluster
# read input
image = cv2.imread('semi_circle.png')
h, w = image.shape[:2]
# convert to gray in range 0 to 1
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY).astype(np.float32)/255
# Gaussian blur gray image
gray_blur = cv2.GaussianBlur(gray, (0,0), sigmaX=3, sigmaY=3)
# reshape to 1D array
image_1d = gray_blur.reshape(h*w,1)
# set number of colors
numcolors = 3
# do kmeans processing
kmeans_cluster = cluster.KMeans(n_clusters=int(numcolors))
kmeans_cluster.fit(image_1d)
cluster_centers = kmeans_cluster.cluster_centers_
cluster_labels = kmeans_cluster.labels_
# need to scale result back to range 0-255
newimage = cluster_centers[cluster_labels].reshape(h, w)*255.0
newimage = newimage.astype('uint8')
# threshold to keep only mid gray values
lower = (75)
upper = (150)
thresh = cv2.inRange(newimage, lower, upper)
# use morphology to clean up
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (13,13))
thresh = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel)
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (19,19))
thresh = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel)
# get largest contour
contours = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours = contours[0] if len(contours) == 2 else contours[1]
big_contour = max(contours, key=cv2.contourArea)
# draw contour on input
result = image.copy()
cv2.drawContours(result, [big_contour], 0, (0,0,255), 1)
# display result
cv2.imshow('newimage', newimage)
cv2.imshow('thresh', thresh)
cv2.imshow('result', result)
cv2.waitKey(0)
cv2.destroyAllWindows()
# save results
cv2.imwrite('semi_circle_kmeans.png', newimage)
cv2.imwrite('semi_circle_thresh.png', thresh)
cv2.imwrite('semi_circle_result.png', result)
Gaussian Blur and Kmeans Image:高斯模糊和 Kmeans 图像:
Thresholded and Morphology Cleaned Image:阈值和形态清洁图像:
Contour on Input:输入轮廓:
import cv2
import numpy as np
def empty(a):
pass
path = 'media/USrSO.png'
cv2.namedWindow("TrackBars")
cv2.resizeWindow("TrackBars",640,240)
cv2.createTrackbar("Hue Min","TrackBars",0,179,empty)
cv2.createTrackbar("Hue Max","TrackBars",116,179,empty)
cv2.createTrackbar("Sat Min","TrackBars",0,255,empty)
cv2.createTrackbar("Sat Max","TrackBars",201,255,empty)
cv2.createTrackbar("Val Min","TrackBars",157,255,empty)
cv2.createTrackbar("Val Max","TrackBars",255,255,empty)
while True:
img = cv2.imread(path)
imgHSV = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
h_min = cv2.getTrackbarPos("Hue Min","TrackBars")
h_max = cv2.getTrackbarPos("Hue Max", "TrackBars")
s_min = cv2.getTrackbarPos("Sat Min", "TrackBars")
s_max = cv2.getTrackbarPos("Sat Max", "TrackBars")
v_min = cv2.getTrackbarPos("Val Min", "TrackBars")
v_max = cv2.getTrackbarPos("Val Max", "TrackBars")
print(h_min,h_max,s_min,s_max,v_min,v_max)
lower = np.array([h_min,s_min,v_min])
upper = np.array([h_max,s_max,v_max])
mask = cv2.inRange(imgHSV,lower,upper)
imgResult = cv2.bitwise_and(img,img,mask=mask)
cv2.imshow("Track Images", imgResult)
cv2.imshow("Original Images", img)
cv2.waitKey(1)
Just run the above code a tracker box like this只需运行上面的代码这样的跟踪器框 Then you can a just use a image detection capability Original image然后你可以只使用一个图像检测能力原始图像 Color adjusted颜色调整
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.