我目前正在尝试检测巢中的卵。为此,我使用的是OpenCV。
[首先,我拍摄了两个鸡蛋的图像。然后我将其转换为HSV。并定义了阈值范围。
但是您可以看到,当我尝试显示阈值窗口时,并未删除所有内容。所以这是我的问题,我怎么只能检测到鸡蛋。
谢谢,
如果要从那一点继续,您可以简单地将其应用于阈值输出minAreaRect()函数,该函数将帮助您为每个轮廓绘制一个合适的矩形。之后,您可以比较这些矩形的长度,也可以检查这些矩形的颜色密度以达到结果。
作为替代,我尝试了HoughCircle(),它也能够找到具有适当参数的卵。
这里是带有houghcircle的结果和代码:
输入图像:
输出图像:
代码:
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
using namespace cv;
using namespace std;
int main()
{
// Loads an image
Mat src_hough_circle = imread("/ur/image/directory/eggs.png", IMREAD_COLOR );
Mat gray;
cvtColor(src_hough_circle, gray, COLOR_BGR2GRAY);
Mat gray_segmentation = gray.clone();
//medianBlur(gray, gray, 5);
GaussianBlur(gray,gray,Size(5,5),0);
vector<Vec3f> circles;
HoughCircles(gray, circles, HOUGH_GRADIENT, 1,
gray.rows/8, // change this value to detect circles with different distances to each other
100, 30, 80, 170 // change the last two parameters
// (min_radius & max_radius) to detect larger circles
);
for( size_t i = 0; i < circles.size(); i++ )
{
Vec3i c = circles[i];
Point center = Point(c[0], c[1]);
// circle center
circle( src_hough_circle, center, 1, Scalar(0,100,100), 3, LINE_AA);
// circle outline
int radius = c[2];
circle( src_hough_circle, center, radius, Scalar(255,0,255), 3, LINE_AA);
}
imshow("detected circles", src_hough_circle);
waitKey(0);
return 0;
}