count the number of objects--物体计数
来源:互联网 发布:淘宝网游戏交易平台 编辑:程序博客网 时间:2024/05/18 03:36
count the number of objects--物体计数
//Program to count the number of objects using morphology operations and the watershed transform
#include <opencv2/opencv.hpp>#include <opencv2/imgproc/imgproc.hpp>#include <opencv2/highgui/highgui.hpp>#include <iostream>using namespace std;using namespace cv;class objectCounter{private:Mat image,gray,markers,output;int count;public:objectCounter(Mat); //constructorvoid get_markers();//function to get markers for watershed segmentationint count_objects();//function to implement watershed segmentation and count catchment basins};objectCounter::objectCounter(Mat _image){image=_image.clone();cvtColor(image,gray,CV_BGR2GRAY);imshow("image",image);}void objectCounter::get_markers(){//equalize histogram of image to improve contrastMat im_e;equalizeHist(gray,im_e);//imshow(im_e,im_e);//dilate to remove small black spotsMat strel=getStructuringElement(MORPH_ELLIPSE,Size(9,9));Mat im_d;dilate(im_e,im_d,strel);//imshow("im_d",im_d);//open and close to highlight objectsstrel=getStructuringElement(MORPH_ELLIPSE,Size(19,19));Mat im_oc;morphologyEx(im_d,im_oc,MORPH_OPEN,strel);morphologyEx(im_oc,im_oc,MORPH_CLOSE,strel);//imshow("im_oc",im_oc);//adaptive threshold to create binary imageMat th_a;adaptiveThreshold(im_oc,th_a,255,ADAPTIVE_THRESH_MEAN_C,THRESH_BINARY,105,0);//imshow("th_a",th_a);//erode binary image twice to separate regionsMat th_e;erode(th_a,th_e,strel,Point(-1,-1),2);//imshow("th_e",th_e);vector<vector<Point>> c,contours;vector<Vec4i> hierarchy;findContours(th_e,c,hierarchy,CV_RETR_CCOMP,CV_CHAIN_APPROX_NONE);//remove very small contoursfor(int idx=0;idx>=0;idx=hierarchy[idx][0])if(contourArea(c[idx])>20)contours.push_back(c[idx]);cout<<"Extracted:"<<contours.size()<<" contours"<<endl;count=contours.size();markers.create(image.rows,image.cols,CV_32SC1);for(int idx=0;idx<contours.size();idx++)drawContours(markers,contours,idx,Scalar::all(idx+1),-1,8);}int objectCounter::count_objects(){//colors generated randomly to make the output look prettyvector<Vec3b> colorTab;for (int i=0;i<count;i++){int b=theRNG().uniform(0,255);int g=theRNG().uniform(0,255);int r=theRNG().uniform(0,255);colorTab.push_back(Vec3b((uchar)b,(uchar)g,(uchar)r));}//watershed output imageMat wshed(markers.size(),CV_8UC3);//paint the watershed output imagefor (int i=0;i<markers.rows;i++){for (int j=0;j<markers.cols;j++){int index=markers.at<int>(i,j);if(index==-1)wshed.at<Vec3b>(i,j)=Vec3b(255,255,255);else if (index<=0 || index>count){wshed.at<Vec3b>(i,j)=Vec3b(0,0,0);}elsewshed.at<Vec3b>(i,j)=colorTab[index-1];}}//superimpose the watershed image with 50% transparence on the grayscale original imageMat imgGray;cvtColor(gray,imgGray,CV_GRAY2BGR);wshed=wshed*0.5+imgGray*0.5;imshow("Segmentation",wshed);return count;}int main(){Mat im=imread("E:\\图片\\YJ.jpg");objectCounter oc(im);oc.get_markers();int count=oc.count_objects();cout<<"Counted:"<<count<<" fruits"<<endl;while(char(waitKey(1))!='q') { }return 0;}
0 0
- count the number of objects--物体计数
- 物体计数--Learning To Count Objects in Images
- Count the number of 1(MIT hackmem)
- count the number of 2s
- leetcode:count the number of nodes
- hnustoj 1030: Count the Number of Cycles
- function to count the number of "1" of one byte
- count the number of "1" in the binary figure
- count the number of 1 in a decimal integer
- COJ 1068: Count the Number of Cycles(向量点积)
- 三角形的数目 Count the number of possible triangles
- 非零比特的个数 count the number of bits
- python int to binary and count the number of 1
- Q9.11 count the number of ways of parenthesizing the expression
- LintCode_284 Count of Smaller Number
- LintCode_248 Count of Smaller Number
- #248 Count of Smaller Number
- [Lintcode]Count of Smaller Number
- ES6 模块modules
- eclipse, Log4j配置
- Java4Android第11课:内部类和匿名内部类
- 关于矩阵范数求导数--区分2范数和F=2时范数
- 读什么,让我们在这混乱的时代中走出精神困境
- count the number of objects--物体计数
- 保持顺序的 Java Properties 类
- 生成二维码
- 10007---crontab 启动 、运行 和编辑 查看
- windows服务器登录记录查看
- iOS录音功能
- 系统分析员考试复习笔记-5:第五章 数据库系统
- PAT 1020完美数列(25)(贪心暴力)
- JS篇——javascript中的this