gpt4 book ai didi

c++ - 我怎样才能合并 Blob /轮廓

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:31:11 26 4
gpt4 key购买 nike

我使用 findContours 进行 Blob 检测。现在,我会将相近且相似的 blob 合并在一起。

以下是一些示例图片:

enter image description here enter image description here enter image description here

普通的 Opencv 可以吗?

最佳答案

您提供给我们的输入图像非常容易处理:

enter image description here enter image description here enter image description here

第一步是将黄色 Blob 与其他一切隔离开,简单的颜色分割技术可以完成这项任务。你可以看看Segmentation & Object Detection by colorTracking colored objects in OpenCV了解如何去做。

enter image description here enter image description here enter image description here

然后,是时候合并 blob 了。一种特别有用的技术是 bounding box , 将所有 blob 放在一个矩形内。请注意,在下图中, Blob 周围有一个绿色矩形:

enter image description here enter image description here enter image description here

之后,您需要做的就是用您选择的颜色填充矩形,从而连接所有 Blob 。我把最后这个作为作业留给你。

这是我能想到的最快、最简单的方法。以下代码演示了如何实现我刚才描述的内容:

#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;
}

关于c++ - 我怎样才能合并 Blob /轮廓,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10281775/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com