我有一个图像,我要在其中提取一个圆的感兴趣区域
然后裁剪。
我已经做了一些预处理,我正在共享结果和代码。
请让我知道我如何实现这一目标。
import imutils
import cv2
import numpy as np
#edge detection
image = cv2.imread('newimage.jpg')
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
image = cv2.GaussianBlur(image, (5, 5), 0)
canny = cv2.Canny(image, 30, 150)
#circle detection
img = cv2.medianBlur(image,5)
cimg = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)
circles = cv2.HoughCircles(canny,cv2.HOUGH_GRADIENT,1,20,
param1=50,param2=30,minRadius=0,maxRadius=0)
circles = np.uint16(np.around(circles))
for i in circles[0,:]:
#outer circle
cv2.circle(cimg,(i[0],i[1]),i[2],(0,255,0),2)
#center of the circle
cv2.circle(cimg,(i[0],i[1]),2,(0,0,255),3)
plt.title('Circle Detected')
plt.xticks([])
plt.yticks([])
plt.imshow(cimg,cmap = 'gray')
plt.show()
答案 0 :(得分:2)
在您的示例中,您应该获得最大半径为i[2]
max_circle = max(circles[0,:], key=lambda x:x[2])
print(max_circle)
i = max_circle
#outer circle
cv2.circle(cimg,(i[0],i[1]),i[2],(0,255,0),2)
#center of the circle
cv2.circle(cimg,(i[0],i[1]),2,(0,0,255),3)
答案 1 :(得分:-1)
检查一下,主要提示:
img = cv2.imread('newimage.jpg', cv2.IMREAD_COLOR)
# Convert to grayscale and blur
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray_blurred = cv2.bilateralFilter(gray, 11, 30, 30)
# tune circles size
detected_circles = cv2.HoughCircles(gray_blurred,
cv2.HOUGH_GRADIENT, 1,
param1=50,
param2=30,
minDist=100,
minRadius=50,
maxRadius=70)
if detected_circles is not None:
# Convert the circle parameters a, b and r to integers.
detected_circles = np.uint16(np.around(detected_circles))
for pt in detected_circles[0, :]:
a, b, r = pt[0], pt[1], pt[2]
# Draw the circumference of the circle.
cv2.circle(img, (a, b), r, (0, 255, 0), 2)
# Draw a small circle (of radius 1) to show the center.
cv2.circle(img, (a, b), 1, (0, 0, 255), 3)
cv2.imshow("Detected circles", img)
cv2.waitKey(0)
小圆圈值:
minRadius=50
maxRadius=60
大圆圈值:
minRadius=100
maxRadius=200