对象提取,简而言之就是获取指定图像,去除其他图像.
解决思路
二值处理 + 形态学处理(开运算+闭运算,轮廓发现) + 横纵比过滤计算 .
代码实现
#include<opencv2/opencv.hpp>
#include<iostream>
#include<math.h>
using namespace cv;
using namespace std;
Mat src, binary, close_dst,open_dst;
int main(int argc, char** argv)
{
src = imread("D:/test/object.png", 0);
if (src.empty())
{
cout << "图片未找到!!!" << endl;
return -1;
}
imshow("input image", src);
//二值化操作
threshold(src, binary, 0, 255, THRESH_BINARY | THRESH_OTSU);
bitwise_not(binary, binary, Mat());
imshow("binary image",binary);
/*---------------------形态学操作-------------------------*/
Mat kernel = getStructuringElement(MORPH_RECT, Size(3, 3), Point(-1, -1));
//闭操作,把中间的细小点联通起来
morphologyEx(binary, close_dst, MORPH_CLOSE, kernel, Point(-1, -1));
imshow("close image", close_dst);
//开操作,去除多余的点
kernel = getStructuringElement(MORPH_RECT, Size(7, 7), Point(-1, -1));
morphologyEx(binary, open_dst, MORPH_OPEN, kernel, Point(-1, -1));
imshow("open image", open_dst);
/*---------------------获取对象轮廓-------------------------*/
Mat dst = Mat::zeros(src.size(), CV_8UC3);
vector <vector<Point>> contours;
vector<Vec4i> hierachy;
findContours(open_dst, contours, hierachy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0));
Point cc;
Mat result_img = src.clone();
cvtColor(result_img, result_img, CV_GRAY2BGR);
//RNG rng((int)time(0));
for (size_t t = 0; t < contours.size(); t++)
{
double area = contourArea(contours[t]);//获取每个轮廓围成的面积
if (area < 100) //如果轮廓围成的面积小于100,则过滤
continue;
//横纵比过滤
Rect rect = boundingRect(contours[t]);//用最小的外接矩形把对象给包起来
//因为检测提取的对象是圆,所以横纵比可以设定为在0.9-1.2之间
float h_w = (float)rect.height / rect.width;
if (h_w > 0.9&&h_w < 1.2)
{
drawContours(dst, contours, static_cast<int>(t), Scalar(0, 0, 255), 2, 8, Mat(), 0, Point(0, 0));
double length = arcLength(contours[t], true);//周长
printf("面积:%f\n",area);
printf("周长:%f\n", length);
/*----------------获取圆点坐标----------------------*/
int x = rect.x + rect.width / 2;//rect.x是矩形左上角的横坐标
int y = rect.y + rect.height / 2;//rect.y是矩形左上角的纵坐标
cc = Point(x, y);
//Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
circle(result_img, cc, 2, Scalar(0,0,255), 2, 8, 0);
}
}
imshow("contours image", dst);
imshow("result image", result_img);
waitKey(0);
return 0;
}
原图
二值处理
闭运算
小黑点被去除 .
开运算
小白块/点 被去除 .
轮廓发现
版权声明:本文为CSDN博主「DDsoup」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/m0_61897853/article/details/122888107
暂无评论