1. #include "opencv2/opencv.hpp"
    2. #include "highgui/highgui.hpp"
    3. #include "imgproc/imgproc.hpp"
    4. #include "iostream"
    5. #include "vector"
    6. using namespace std;
    7. using namespace cv;
    8. int main(){
    9. Mat src,dst1,dst2,dst3;
    10. src=imread("/home/ma/QT/renwu33/renwusi.jpg");
    11. imshow("src",src);
    12. //均衡化
    13. vector<Mat> Split;
    14. cvtColor(src,dst1,COLOR_BGR2HSV);
    15. namedWindow("hsv",WINDOW_AUTOSIZE);
    16. imshow("hsv",dst1);
    17. split(dst1,Split);
    18. equalizeHist(Split[2],Split[2]);
    19. merge(Split,dst1);
    20. //irange一下
    21. inRange(dst1,Scalar(104,39,0),Scalar(135,91,37),dst2);
    22. //來個開閉運算
    23. Mat kernel = getStructuringElement(MORPH_RECT,Size(3,3));
    24. morphologyEx(dst2,dst2,MORPH_OPEN,kernel);
    25. morphologyEx(dst2, dst2, MORPH_CLOSE,kernel);
    26. vector<vector<Point>>contours;
    27. vector<Vec4i>hierarchy;
    28. findContours(dst2,contours,hierarchy,RETR_EXTERNAL,CHAIN_APPROX_NONE,Point(0,0));
    29. vector<Rect>boundRect(contours.size());
    30. //imshow("dst2",dst2);
    31. for(int i=0;i<=contours.size();i++){
    32. boundRect[i] = boundingRect(Mat(contours[i]));
    33. // Rect drawing=boundRect[i];
    34. rectangle(dst2,boundRect[i].tl(),boundRect[i].br(),Scalar(0,0,255),1,1,0);
    35. }
    36. // putText(dst2,".(912,445)",Point(912,445),FONT_HERSHEY_COMPLEX,1,Scalar(0,0,255),3,LINE_AA);
    37. //cvtColor(dst2,dst2,COLOR_HSV2BGR);
    38. imshow("xiaoguo",dst2);
    39. waitKey(0);
    40. return 0;
    41. }