opencv connectedComponentsWithStates
来源:互联网 发布:人工智能的概念 编辑:程序博客网 时间:2024/06/14 23:40
这是一段从 opencv essential书里面来的代码,收藏起来以后可能有用。
#include <opencv2/core/utility.hpp>#include "opencv2/imgproc.hpp"#include "opencv2/highgui.hpp"#include <iostream>using namespace cv;using namespace std;Mat img;int threshval = 227;static void on_trackbar(int, void*){ Mat bw = threshval < 128 ? (img < threshval) : (img > threshval); Mat labelImage(img.size(), CV_32S); Mat stats, centroids; int nLabels = connectedComponentsWithStats(bw, labelImage, stats, centroids); // Show connected components with random colors std::vector<Vec3b> colors(nLabels); colors[0] = Vec3b(0, 0, 0);//background for(int label = 1; label < nLabels; ++label){ colors[label] = Vec3b( (rand()&200), (rand()&200), (rand()&200) ); } Mat dst(img.size(), CV_8UC3); for(int r = 0; r < dst.rows; ++r){ for(int c = 0; c < dst.cols; ++c){ int label = labelImage.at<int>(r, c); Vec3b &pixel = dst.at<Vec3b>(r, c); pixel = colors[label]; } } // Text labels with area of each cc (except background) for (int i=1; i< nLabels;i++) { float a=stats.at<int>(i,CC_STAT_AREA); Point org(centroids.at<double>(i,0), centroids.at<double>(i,1)); String txtarea; std::ostringstream buff; buff << a; txtarea=buff.str(); putText( dst, txtarea, org,FONT_HERSHEY_COMPLEX_SMALL, 1, Scalar(255,255,255), 1); } imshow( "Connected Components", dst );}int main( int argc, const char** argv ){ img = imread("stuff.jpg", 0); namedWindow( "Connected Components", 1 ); createTrackbar( "Threshold", "Connected Components", &threshval, 255, on_trackbar ); on_trackbar(threshval, 0); waitKey(0); return 0;}
0 1
- opencv connectedComponentsWithStates
- opencv
- openCV
- openCV
- Opencv
- opencv
- opencv
- opencv
- OpenCV
- OpenCV
- OpenCV
- OpenCV
- opencv
- Opencv。。
- OpenCV
- opencv
- opencv
- opencv
- utils部分--一些通用的工具类封装
- httpFS访问
- Linx监控分享--重点进程监控(mysqld)+邮件提醒
- 整数整除性质的一些通用证明方法
- tomcat 配置项描述(1)
- opencv connectedComponentsWithStates
- Android中的优化问题
- opencv学习系列(七)--- 细化算法
- Cordys 内部类调用SOA【WebService】方法
- android简易双屏支持
- pyqt demo
- Struts2中的OGNL详解
- jQuery Mobile 资料
- 题目1527:首尾相连数组的最大子数组和