[英]Extract circles from one image after have apply the circular hough transform
我正在嘗試使用圓形霍夫變換在一張圖像中提取檢測到的圓圈。 我的想法是獲取每個圓圈或將每個圓圈分開,然后獲取他的顏色直方圖特征,然后將此特征發送給一個分類器,如 SVM、ANN、KNN 等。這是我的輸入圖像:
我得到這樣的圈子:
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()
下一步是嘗試提取這些圓圈,但我不知道該怎么做。
好吧,伙計們,我想看看你的建議,任何我想我都會欣賞的。 非常感謝。
您可以為檢測到的每個圓圈創建一個二進制掩碼。 使用此掩碼僅從輸入圖像中提取ROIs
。 此外,您可以裁剪這些ROIs
並將它們存儲在一個列表中,以將它們傳遞給您的分類器。
這是代碼:
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 = []
這個想法是你一步處理一個圓圈。 獲取當前圓圈,創建蒙版,蒙版原始輸入,裁剪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)
對於每個圓圈,您都會獲得裁剪的 ROI:
您可以將該信息傳遞給您的分類器。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.