【发布时间】:2014-03-06 09:55:42
【问题描述】:
所以我一直致力于用 Hough Circles 识别瑜伽球。现在,当转换为灰度时,它可以立即工作。不幸的是,我不得不采取更复杂的程序,因为这些彩色球有多个,我只想检测蓝色。
未过滤的球:
过滤球:
我的算法步骤:
- 从 BGR 转换为 HSV
- 模糊图像
- 过滤 HSV 仅选择值(在我的情况下,由于照明,深蓝色到浅蓝色)
- 反转图像
- 使用形态填充被点亮的部分
- 再次模糊
- 过滤模糊,使其成为实心形状,而不是无法识别的模糊灰度
- 用霍夫圆检测。 MAT 仍然是灰度,所以这不是问题。
代码:
#include <iostream>
#include <string>
#include <iomanip>
#include <sstream>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace std;
using namespace cv;
int main(int argc, char *argv[])
{
// Morphology stuff
Mat element5(30, 30, CV_8U, Scalar(1));
int morph_elem = 1; // 2
int morph_size = 33;// 30
int morph_operator = 2; // 2
Mat element = getStructuringElement(morph_elem, Size(2 * morph_size + 1, 2 * morph_size + 1), Point(morph_size, morph_size));
int const max_operator = 4;
int const max_elem = 2;
int const max_kernel_size = 21;
Mat kernel;
// Display Windows Name
namedWindow("Testing Purposes", CV_WINDOW_AUTOSIZE);
Mat src; // loaded image
Mat hsv; // changed src into HSV
Mat Filtered; // filtered w/ inRange for blue ball
Mat Gray; // gray filter for src
Mat dst; // destination for canny edge
Mat detected_edges; // matrix of edges w/ canny
// thresholds for canny
int edgeThresh = 45;
int lowThreshold;
int const max_lowThreshold = 100;
src = imread(argv[1]);
cvtColor(src, Gray, CV_BGR2GRAY);
cvtColor(src, hsv, CV_BGR2HSV);
/*
// CannyEdge Testing
blur(Gray, detected_edges, Size(3, 3)); // blur the grayimage
Canny(detected_edges, detected_edges, lowThreshold, lowThreshold * ratio, kernel_size);
dst = Scalar::all(0);
src.copyTo( dst, detected_edges);
imshow(window_name,dst);
*/
// hsv blur and then thresholds
blur(hsv,hsv,Size(4, 4), Point(-1, -1));
inRange(hsv, Scalar(100, 100, 0), Scalar(200, 200, 255), Filtered); //filtering after blur
vector<Vec3f> circles; //vector for holding info on circles
// houghcircles - attempts to detect circles in the Filtered image we passed it
// morphology defintion for Kernel
bitwise_not(Filtered, Filtered);
// imwrite("/home/bjacobs/Desktop/Testing.jpg", Filtered);
imwrite("/home/bjacobs/Desktop/Testingg.jpg", Filtered);
morphologyEx(Filtered, dst, MORPH_OPEN, element);
blur(dst, dst, Size(20, 20), Point(-1, -1));
Mat baw = dst > 128;
HoughCircles(baw ,circles, CV_HOUGH_GRADIENT, 1, baw.rows/8,200,100,0,0);
imwrite("/home/bjacobs/Desktop/Testing.jpg", baw);
// Draw the circles detected onto the SRC file
for(size_t i = 0; i < circles.size(); i++)
{
Point center(cvRound(circles[i][0]), cvRound(circles[i][3]));
int radius = cvRound(circles[i][2]);
// circle center
circle(src, center, 3, Scalar(0, 255, 0), -1, 8, 0);
// circle outline
circle(src, center, radius, Scalar(0, 0, 255), 3, 8, 0);
}
imwrite("/home/bjacobs/Desktop/Test.jpg", hsv);
imshow("Testing Purposes", src);
waitKey(0);
}
我已经在网上尽可能多地阅读了有关此问题的内容,但到目前为止我发现的任何内容都没有帮助。原谅草率的评论,并且使用 Canny Edge 检测包含一些失败的算法,所以不要太在意它们。有人知道这个检测问题的解决方案吗?
【问题讨论】:
-
您到底有什么问题?对我来说,你描述的算法很好。
-
所以 HoughCircles 在灰度上工作?为什么不先检测 HoughCircles,然后从所有检测到的球中创建一个蒙版,对彩色图像进行蒙版,从蒙版图像中计算 HSV,然后选择中位数最蓝的“集群”?如果你能提供一个很棒的示例图片 =)
-
jnovacho:问题是 Houghcircles 看不到黑色过滤图像。 Micka:似乎是一个可行的解决方案,我会努力尝试。
标签: c++ opencv geometry computer-vision vision