[英]Extract circles from one image after have apply the circular hough transform
I'm trying to extract the detected circles in one image using the circular hough transform.我正在尝试使用圆形霍夫变换在一张图像中提取检测到的圆圈。 My idea is get every circle or separate each one to then get his color histogram features and after send this features to one classifier as SVM, ANN, KNN etc.. This is my input image:
我的想法是获取每个圆圈或将每个圆圈分开,然后获取他的颜色直方图特征,然后将此特征发送给一个分类器,如 SVM、ANN、KNN 等。这是我的输入图像:
I'm getting the circles of this way:我得到这样的圈子:
import numpy as np
import cv2
import matplotlib.pyplot as plt
cv2.__version__
#read image
file = "lemon.png"
image = cv2.imread(file)
#BGR to RGB
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
#convert to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
circles = cv2.HoughCircles(gray,
cv2.HOUGH_GRADIENT,
15,
41,
param1=31,
param2=31,
minRadius=0,
maxRadius=33)
circles = np.uint16(np.around(circles))
for i in circles[0,:]:
# draw the outer circle
cv2.circle(image,(i[0],i[1]),i[2],(0,255,0),2)
# draw the center of the circle
cv2.circle(image,(i[0],i[1]),2,(0,0,255),3)
print("Number of circles: "+ str(len(circles[0,:])))
plt.imshow(image, cmap='gray', vmin=0, vmax=255)
plt.show()
The next step is try to extract those circles but I don't have idea how to do it.下一步是尝试提取这些圆圈,但我不知道该怎么做。
Well guys I would like to see your suggestions, any I idea I will apreciate it.好吧,伙计们,我想看看你的建议,任何我想我都会欣赏的。 Thanks so much.
非常感谢。
You can create a binary mask for every circle you detect.您可以为检测到的每个圆圈创建一个二进制掩码。 Use this mask to extract only the
ROIs
from the input image.使用此掩码仅从输入图像中提取
ROIs
。 Additionally, you can crop these ROIs
and store them in a list to pass them to your classifier.此外,您可以裁剪这些
ROIs
并将它们存储在一个列表中,以将它们传递给您的分类器。
Here's the code:这是代码:
import numpy as np
import cv2
# image path
path = "C://opencvImages//"
file = path + "LLfN7.png"
image = cv2.imread(file)
# Convert to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
circles = cv2.HoughCircles(gray,
cv2.HOUGH_GRADIENT,
15,
41,
param1=31,
param2=31,
minRadius=0,
maxRadius=33)
# Here are your circles:
circles = np.uint16(np.around(circles))
# Get input size:
dimensions = image.shape
# height, width
height = image.shape[0]
width = image.shape[1]
# Prepare a list to store each ROI:
lemonROIs = []
The idea is that you process one circle at a step.这个想法是你一步处理一个圆圈。 Get the current circle, create a mask, mask the original input, crop the
ROI
and store it inside the list:获取当前圆圈,创建蒙版,蒙版原始输入,裁剪
ROI
并将其存储在列表中:
for i in circles[0, :]:
# Prepare a black canvas:
canvas = np.zeros((height, width))
# Draw the outer circle:
color = (255, 255, 255)
thickness = -1
centerX = i[0]
centerY = i[1]
radius = i[2]
cv2.circle(canvas, (centerX, centerY), radius, color, thickness)
# Create a copy of the input and mask input:
imageCopy = image.copy()
imageCopy[canvas == 0] = (0, 0, 0)
# Crop the roi:
x = centerX - radius
y = centerY - radius
h = 2 * radius
w = 2 * radius
croppedImg = imageCopy[y:y + h, x:x + w]
# Store the ROI:
lemonROIs.append(croppedImg)
For each circle you get a cropped ROI:对于每个圆圈,您都会获得裁剪的 ROI:
You can pass that info to your classifier.您可以将该信息传递给您的分类器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.