#include "opencv2/opencv.hpp"#include "highgui/highgui.hpp"#include "imgproc/imgproc.hpp"#include "iostream"#include "vector"using namespace std;using namespace cv;int main(){ Mat src,dst1,dst2,dst3; src=imread("/home/ma/QT/renwu33/renwusi.jpg"); imshow("src",src); //均衡化 vector<Mat> Split; cvtColor(src,dst1,COLOR_BGR2HSV); namedWindow("hsv",WINDOW_AUTOSIZE); imshow("hsv",dst1); split(dst1,Split); equalizeHist(Split[2],Split[2]); merge(Split,dst1); //irange一下 inRange(dst1,Scalar(104,39,0),Scalar(135,91,37),dst2); //來個開閉運算 Mat kernel = getStructuringElement(MORPH_RECT,Size(3,3)); morphologyEx(dst2,dst2,MORPH_OPEN,kernel); morphologyEx(dst2, dst2, MORPH_CLOSE,kernel); vector<vector<Point>>contours; vector<Vec4i>hierarchy; findContours(dst2,contours,hierarchy,RETR_EXTERNAL,CHAIN_APPROX_NONE,Point(0,0)); vector<Rect>boundRect(contours.size()); //imshow("dst2",dst2); for(int i=0;i<=contours.size();i++){ boundRect[i] = boundingRect(Mat(contours[i]));// Rect drawing=boundRect[i]; rectangle(dst2,boundRect[i].tl(),boundRect[i].br(),Scalar(0,0,255),1,1,0);}// putText(dst2,".(912,445)",Point(912,445),FONT_HERSHEY_COMPLEX,1,Scalar(0,0,255),3,LINE_AA); //cvtColor(dst2,dst2,COLOR_HSV2BGR); imshow("xiaoguo",dst2); waitKey(0); return 0;}