回答
输入图像我们是相当容易的工作:
的第一步是从一切隔离黄色斑点和简单的色彩分割技术可以做到这一点任务。你可以看看Segmentation & Object Detection by color或Tracking colored objects in OpenCV有一个想法如何做到这一点。
然后,是时候合并斑点。一种特别有用的技术是bounding box,以将所有斑点放在矩形内。在下面的图片请注意,有一个绿色的长方形周边的斑:
之后,所有你需要做的是填充矩形与您选择的颜色,从而连接所有的斑点。我将最后留给你做作业。
这是我能想到的最快最简单的方法。下面的代码演示了如何实现我刚才所描述的:
#include <cv.h>
#include <highgui.h>
#include <iostream>
#include <vector>
int main(int argc, char* argv[])
{
cv::Mat img = cv::imread(argv[1]);
if (!img.data)
{
std::cout "!!! Failed to open file: " << argv[1] << std::endl;
return 0;
}
// Convert RGB Mat into HSV color space
cv::Mat hsv;
cv::cvtColor(img, hsv, CV_BGR2HSV);
// Split HSV Mat into HSV components
std::vector<cv::Mat> v;
cv::split(hsv,v);
// Erase pixels with low saturation
int min_sat = 70;
cv::threshold(v[1], v[1], min_sat, 255, cv::THRESH_BINARY);
/* Work with the saturated image from now on */
// Erode could provide some enhancement, but I'm not sure.
// cv::Mat element = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(3, 3));
// cv::erode(v[1], v[1], element);
// Store the set of points in the image before assembling the bounding box
std::vector<cv::Point> points;
cv::Mat_<uchar>::iterator it = v[1].begin<uchar>();
cv::Mat_<uchar>::iterator end = v[1].end<uchar>();
for (; it != end; ++it)
{
if (*it) points.push_back(it.pos());
}
// Compute minimal bounding box
cv::RotatedRect box = cv::minAreaRect(cv::Mat(points));
// Display bounding box on the original image
cv::Point2f vertices[4];
box.points(vertices);
for (int i = 0; i < 4; ++i)
{
cv::line(img, vertices[i], vertices[(i + 1) % 4], cv::Scalar(0, 255, 0), 1, CV_AA);
}
cv::imshow("box", img);
//cv::imwrite(argv[2], img);
cvWaitKey(0);
return 0;
}
thx为您的答案......但你误解了我一点。黄色的斑点不是真的黄色。我只是给他们上色以表明你想要合并的巫婆斑点。所以我不能使用颜色分割隔离其他斑点。也像区域信息将无法正常工作,因为也许有一些其他更大的斑点女巫我不喜欢合并... – rouge 2012-04-25 10:07:24
Bah! = \稍后会考虑其他事情。 – karlphillip 2012-04-25 11:37:26
你对最大的斑点感兴趣吧? – karlphillip 2012-04-25 12:25:17
我想我做到了,感谢你的程序的详细信息,我发现这个解决方案:(评论欢迎)
vector<vector<Point> > contours;
vector<vector<Point> > tmp_contours;
findContours(detectedImg, tmp_contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
vector<vector<Point> >::iterator it1;
it1 = tmp_contours.begin();
Mat test;
test = Mat(FImage.size(), CV_32FC3);
while (it1 != tmp_contours.end()) {
vector<Point> approx1;
approxPolyDP(Mat(*it1), approx1, 3, true);
Rect box1 = boundingRect(approx1);
float area1 = contourArea(approx1);
if ((area1 > 50) && (area1 < 13000) && (box1.width < 100) && (box1.height < 120)) {
vector<vector<Point> >::iterator it2;
it2 = tmp_contours.begin();
while (it2 != tmp_contours.end()) {
vector<Point> approx2;
approxPolyDP(Mat(*it2), approx2, 3, true);
Moments m1 = moments(Mat(approx1), false);
Moments m2 = moments(Mat(approx2), false);
float x1 = m1.m10/m1.m00;
float y1 = m1.m01/m1.m00;
float x2 = m2.m10/m2.m00;
float y2 = m2.m01/m2.m00;
vector<Point> dist;
dist.push_back(Point(x1, y1));
dist.push_back(Point(x2, y2));
float d = arcLength(dist, false);
Rect box2 = boundingRect(approx2);
if (box1 != box2) {
if (d < 25) {
//Method to merge the vectors
approx1 = mergePoints(approx1, approx2);
}
}
++it2;
}
Rect b = boundingRect(approx1);
rectangle(test, b, CV_RGB(125, 255, 0), 2);
contours.push_back(approx1);
}
++it1;
}
- 1. 在轮廓上合并足够的点
- 2. 从C++中的斑点检测中提取OpenCV轮廓阵列
- 3. 使用轮廓分析斑点的混乱
- 4. 如何获得轮廓的像素点?
- 5. 轮廓轮廓着色器
- 6. OpenCV - 如何将像素跟踪轮廓转换为顶点跟踪轮廓
- 7. R轮廓和填充轮廓不能吻合
- 8. 2d点云的轮廓
- 9. 如何制作轮廓轮播
- 10. 如何生成轮廓图?
- 11. 如何使用JavaCV识别另一轮廓内的轮廓?
- 12. 从轮廓中提取最外轮廓
- 13. 轮廓匹配 - 找到轮廓位移
- 14. 绘制轮廓后找到轮廓
- 15. 轮廓发现
- 16. 轮廓形状
- 17. Opencv轮廓
- 18. 提取轮廓
- 19. OpenCV轮廓?
- 20. 轮廓提取
- 21. 轮廓匹配
- 22. True Type字体轮廓重叠检测和合并C#
- 23. 奇怪的轮廓,当点击contentEditable div
- 24. 算法需要:绘制轮廓圆点
- 25. 添加轮廓颜色,点属性
- 26. 根据轮廓颜色的颜色点
- 27. 插值轮廓点上的值
- 28. 三维轮廓点(凹壳)列表
- 29. Python Opencv - 查找轮廓内的黑点
- 30. 查找一组点的特定轮廓
你能发布样本图像? – karlphillip 2012-04-23 13:47:28
如果您添加图像,效果会更好。上载到imageshack.us并在此处给出链接。还请指定类似的含义。它的形状相似吗?或者有类似的区域? etc – 2012-04-23 15:04:21
好的,我想合并相邻的相似形状。这里有三个例子(标记为黄色)感谢您的帮助! [Image 1](http://img713.imageshack.us/img713/2152/image1xg.png) [Image 2](http://img32.imageshack.us/img32/2149/image2kl.png) [Image 3](http://img256.imageshack.us/img256/1000/image3jg.png) – rouge 2012-04-24 06:43:43