Opencv python中的椭圆检测

您所在的位置:网站首页 50cmx50cm等于多少平方米 Opencv python中的椭圆检测

Opencv python中的椭圆检测

2023-08-07 16:25| 来源: 网络整理| 查看: 265

百度翻译此文   有道翻译此文 问题描述

My image is here:

my photo is here.

i'm looking for a better solution or algorithm to detect the ellipse part (dish) in this photo and mask it in an other photo in Opencv. could you please give me some advice or solution. and my code is :

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))) 推荐答案

There is an alternative for it in skimage made by Xie, Yonghong, and Qiang Ji and published as...

“A new efficient ellipse detection method.” Pattern Recognition, 2002. Proceedings. 16th International Conference on. Vol. 2. IEEE, 2002.

Their Ellipse detection code is relatively slow and the example takes about 70 seconds; compared to website claimed "28 seconds".

If you have conda or pip: "name" install scikit-image and give it a shot...

Their code can be found here or as copy/pasted below:

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'}) ax1.set_title('Original picture') ax1.imshow(image_rgb) ax2.set_title('Edge (white) and result (red)') ax2.imshow(edges) plt.show() 其他推荐答案

APPROACH 1:

As suggested by Miki, I was able to detect the ellipse in the given image using contour properties (in this I used the area property).

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)

This is what I obtained:

enter image description here

APPROACH 2:

You can also use the approach suggested by you in this case. Hough detection of ellipse/circle.

You have to pre-process the image. I performed adaptive threshold and obtained this:

enter image description here

Now you can perform Hough circle detection on this image.

Hope it is not a mouthful!! :D



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3