2017-02-13 173 views
0

my photo is here.我正在尋找更好的解決方案或算法來檢測這張照片中的橢圓部分(盤),並掩蓋它在Opencv中的其他照片。 可否請給我一些建議或解決方案。 和我的代碼是:橢圓檢測opencv蟒蛇

circles = cv2.HoughCircles(img, cv2.HOUGH_GRADIENT, 1.2, 1, param1=128, minRadius=200, maxRadius=600) 
    # draw detected circles on image 
    circles = circles.tolist() 
    for cir in circles: 
     for x, y, r in cir: 
      x, y, r = int(x), int(y), int(r) 
      cv2.circle(img, (x, y), r, (0, 255, 0), 4) 

    # show the output image 
    cv2.imshow("output", cv2.resize(img, (500, 500))) 
+1

你這裏不需要橢圓檢測(順便說一句,OpenCV中不可用)。你可能對亮度值有一個簡單的閾值,並保持最大的連接組件。此外,請出示你已經嘗試什麼 – Miki

+0

一個簡單的顏色分割可以正常工作。 – ZdaR

+0

我添加的代碼的特殊組成部分,是連接到我的問題, 你會來看看它PLZ。 –

回答

1

沒有爲它在skimage由Xie, Yonghong, and Qiang Ji製成並公開爲一種替代...

「一個新的有效的橢圓檢測方法。」模式識別,2002. 論文集。第十六屆國際會議。卷。 2.IEEE,2002.

它們的橢圓檢測碼相對較慢,例子大約需要70秒;相比網站聲稱「28秒」。

如果你有暢達或PIP:「名字」安裝scikit形象,並給它一個鏡頭...

他們的代碼可以發現here或以下複製/粘貼:

import matplotlib.pyplot as plt 

from skimage import data, color, img_as_ubyte 
from skimage.feature import canny 
from skimage.transform import hough_ellipse 
from skimage.draw import ellipse_perimeter 

# Load picture, convert to grayscale and detect edges 
image_rgb = data.coffee()[0:220, 160:420] 
image_gray = color.rgb2gray(image_rgb) 
edges = canny(image_gray, sigma=2.0, 
       low_threshold=0.55, high_threshold=0.8) 

# Perform a Hough Transform 
# The accuracy corresponds to the bin size of a major axis. 
# The value is chosen in order to get a single high accumulator. 
# The threshold eliminates low accumulators 
result = hough_ellipse(edges, accuracy=20, threshold=250, 
         min_size=100, max_size=120) 
result.sort(order='accumulator') 

# Estimated parameters for the ellipse 
best = list(result[-1]) 
yc, xc, a, b = [int(round(x)) for x in best[1:5]] 
orientation = best[5] 

# Draw the ellipse on the original image 
cy, cx = ellipse_perimeter(yc, xc, a, b, orientation) 
image_rgb[cy, cx] = (0, 0, 255) 
# Draw the edge (white) and the resulting ellipse (red) 
edges = color.gray2rgb(img_as_ubyte(edges)) 
edges[cy, cx] = (250, 0, 0) 

fig2, (ax1, ax2) = plt.subplots(ncols=2, nrows=1, figsize=(8, 4), sharex=True, 
           sharey=True, 
           subplot_kw={'adjustable':'box-forced'}) 

ax1.set_title('Original picture') 
ax1.imshow(image_rgb) 

ax2.set_title('Edge (white) and result (red)') 
ax2.imshow(edges) 

plt.show() 
2

APPROACH 1:

如三木建議的,我能夠檢測使用輪廓特性的給定圖像中的橢圓(在此我使用的區域屬性) 。

CODE:

#--- First obtain the threshold using the greyscale image --- 
ret,th = cv2.threshold(gray,127,255, 0) 

#--- Find all the contours in the binary image --- 
_, contours,hierarchy = cv2.findContours(th,2,1) 
cnt = contours 
big_contour = [] 
max = 0 
for i in cnt: 
    area = cv2.contourArea(i) #--- find the contour having biggest area --- 
    if(area > max): 
     max = area 
     big_contour = i 

final = cv2.drawContours(img, big_contour, -1, (0,255,0), 3) 
cv2.imshow('final', final) 

這是我得到什麼:

enter image description here

方法2:

您還可以使用在這種情況下你所建議的方法。 Hough檢測橢圓/圓。

您必須預先處理圖像。我執行自適應閾值並獲得這樣的:

enter image description here

現在你可以在這個圖像上進行霍夫圓檢測。

希望它不是一口! :d

+0

感謝親愛的幫助我,但我的問題是,我不能使用的輪廓面積法 我試圖做到這一點與HoughCircles變換,但我的問題是我不能畫在我的菜橢圓 你會幫我我怎麼能用這個算法做到這一點? 在此先感謝 –

+0

您可以嘗試使用Hough變換檢測我張貼的第二張圖像上的圓。試一試 –

+1

有點提示:HoughCircles沒有檢測到橢圓......只是圈子;)@Moh – Miki