网站首页 > 技术文章 正文
今天练习了简单的图像分割与轮廓的绘制,以及在轮廓内的分量分析.
画出所有的轮廓出来
Mat src_img = imread(path); Mat gray_img; Mat bin_img; cvtColor(src_img, gray_img, COLOR_BGR2GRAY);
灰度均值
Scalar gray_mean = mean(gray_img); cout << gray_mean(0); threshold(gray_img, bin_img, 90, 255, THRESH_BINARY); 寻找全部轮廓 vector<vector<Point>>contours; vector<Vec4i>hierarchy; findContours(bin_img, contours, hierarchy, RETR_CCOMP, CHAIN_APPROX_SIMPLE); for (int i = 0; i < hierarchy.size(); i++) { Scalar color = Scalar(rand() % 255, rand() % 255, rand() % 255); drawContours(src_img, contours, i, color,5, 8, hierarchy); } 从所有的轮廓中找到寻找最大轮廓 double maxarea = 0; int maxAreaIdx = 0; for (int index = contours.size() - 1; index >= 0; index--) { double tmparea = fabs(contourArea(contours[index])); if (tmparea > maxarea) { maxarea = tmparea; maxAreaIdx = index;//记录最大轮廓的索引号 } } //Scalar color = Scalar(rand() % 255, rand() % 255, rand() % 255); //drawContours(src_img, contours, maxAreaIdx, color, 5, 8, hierarchy);
求出最大轮廓的内切圆,这样更加精确的bgr分量进行分析。
Rect r = boundingRect(contours[maxAreaIdx]); int nL = r.x, nR = r.br().x; //轮廓左右边界 int nT = r.y, nB = r.br().y; //轮廓上下边界 int dist = 0; int maxdist = 0; Point center1; for (int i = nL; i < nR; i++) //列 { for (int j = nT; j < nB; j++) //行 { //计算轮廓内部各点到最近轮廓点的距离 dist = pointPolygonTest(contours[maxAreaIdx], Point(i, j), true); if (dist > maxdist) { //求最大距离,只有轮廓最中心的点才距离最大 maxdist = dist; center1 = Point(i, j); } } } int radius = maxdist; //圆半径 Point center = center1; circle(src_img, center1 , radius, Scalar(0, 0, 0), 3, 8);
//
int point_idx_b_sum = 0; int point_idx_g_sum = 0; int point_idx_r_sum = 0; vector<int> b_vec; vector<int> g_vec; vector<int> r_vec; for (int i = nL; i < nR; i++) //列 { for (int j = nT; j < nB; j++) //行 { //计算轮廓内部各点到最近轮廓点的距离 dist = pointPolygonTest(contours[maxAreaIdx], Point(i, j), true); b_vec.push_back(src_img.at<Vec3b>(Point(i, j))[0]); g_vec.push_back(src_img.at<Vec3b>(Point(i, j))[1]); r_vec.push_back(src_img.at<Vec3b>(Point(i, j))[2]); //printf("b=%d\t", src_img.at<Vec3b>(Point(i, j))[0]); } } //cout << b_vec.size(); for (vector<int>::iterator iter = b_vec.begin(); iter != b_vec.end(); iter++) { point_idx_b_sum = point_idx_b_sum + *iter; } for (vector<int>::iterator iter = g_vec.begin(); iter != g_vec.end(); iter++) { point_idx_g_sum = point_idx_g_sum + *iter; } for (vector<int>::iterator iter = r_vec.begin(); iter != r_vec.end(); iter++) { point_idx_r_sum = point_idx_r_sum + *iter; } cout << path << " " << "b=" << point_idx_b_sum / b_vec.size() << endl; cout << path << " " << "g=" << point_idx_g_sum / b_vec.size() << endl; cout << path << " " << "r=" << point_idx_r_sum / b_vec.size(); }
输出结果
猜你喜欢
- 2024-10-12 智能监测:皮带输送系统堵料问题的解决方案
- 2024-10-12 Python-OpenCV 10. 图像边缘算法 opencv边缘识别
- 2024-10-12 Net AI学习笔记系列第五章 net教程
- 2024-10-12 深度学习和神经网络——图像读取和显示
- 2024-10-12 十三句Python搞定找茬游戏 找茬游戏规则
- 2024-10-12 「深度学习」手把手教你用PythonOpenCV物体识别-识别水果
- 2024-10-12 python代码实现OpenCV 轮廓近似原理
- 2024-10-12 OpenCV找出图片中的圆并标注圆心 opencv检测圆代码
- 2024-10-12 分享3个干货满满的Python实战项目,点赞收藏
- 2024-10-12 OpenCV(28)——凸包 opencv轮廓凹凸
你 发表评论:
欢迎- 最近发表
- 标签列表
-
- oraclesql优化 (66)
- 类的加载机制 (75)
- feignclient (62)
- 一致性hash算法 (71)
- dockfile (66)
- 锁机制 (57)
- javaresponse (60)
- 查看hive版本 (59)
- phpworkerman (57)
- spark算子 (58)
- vue双向绑定的原理 (68)
- springbootget请求 (58)
- docker网络三种模式 (67)
- spring控制反转 (71)
- data:image/jpeg (69)
- base64 (69)
- java分页 (64)
- kibanadocker (60)
- qabstracttablemodel (62)
- java生成pdf文件 (69)
- deletelater (62)
- com.aspose.words (58)
- android.mk (62)
- qopengl (73)
- epoch_millis (61)
本文暂时没有评论,来添加一个吧(●'◡'●)