任务描述:将图片上的各个饮料瓶识别出来
    微信图片_20210313083207.jpg

    1. #include <iostream>
    2. #include <opencv2/opencv.hpp>
    3. #include <opencv2/imgproc/imgproc.hpp>
    4. #include <opencv2/highgui/highgui.hpp>
    5. #include <math.h>
    6. using namespace std;
    7. using namespace cv;
    8. void fun(){
    9. Mat srcImage,dstImage1,dstImage2,dstImage3,dstImage4,dstImage5,dstImage6,dstImage7,dstImage8;
    10. Mat harriscorner;
    11. vector<vector<Point>>contours;
    12. vector<Vec4i>hierarchy;
    13. vector<vector<Point>>contours1;
    14. vector<Vec4i>hierarchy1;
    15. vector<vector<Point>>contours2;
    16. vector<Vec4i>hierarchy2;
    17. vector<vector<Point>>contours3;
    18. vector<Vec4i>hierarchy3;
    19. vector<vector<Point>>contours4;
    20. vector<Vec4i>hierarchy4;
    21. vector<vector<Point>>contours5;
    22. vector<Vec4i>hierarchy5;
    23. vector<vector<Point>>contours6;
    24. vector<Vec4i>hierarchy6;
    25. srcImage=imread(/home/ma/下载/renwu4.jpg);
    26. blur(srcImage,dstImage1,Size(3,3));
    27. cvtColor(dstImage1,dstImage1,COLOR_BGR2HSV);
    28. inRange(dstImage1,Scalar(25,103,48),Scalar(76,255,222),dstImage2);
    29. inRange(dstImage1,Scalar(133,55,59),Scalar(180,255,255),dstImage3);//hong
    30. inRange(dstImage1,Scalar(11,131,29),Scalar(15,255,255),dstImage4);
    31. inRange(dstImage1,Scalar(0,0,31),Scalar(180,111,88),dstImage5);
    32. inRange(dstImage1,Scalar(15,90,141),Scalar(25,224,208),dstImage6);
    33. inRange(dstImage1,Scalar(175,109,130),Scalar(180,255,255),dstImage7);
    34. Mat ksize=getStructuringElement(0,Size(13,13));
    35. dilate(dstImage3,dstImage3,ksize);
    36. findContours(dstImage2,contours,hierarchy,RETR_EXTERNAL,CHAIN_APPROX_NONE,Point());
    37. findContours(dstImage3,contours1,hierarchy1,RETR_EXTERNAL,CHAIN_APPROX_NONE,Point());//hong
    38. findContours(dstImage4,contours2,hierarchy2,RETR_EXTERNAL,CHAIN_APPROX_NONE,Point());
    39. findContours(dstImage3,contours3,hierarchy3,RETR_EXTERNAL,CHAIN_APPROX_NONE,Point());
    40. findContours(dstImage5,contours4,hierarchy4,RETR_EXTERNAL,CHAIN_APPROX_NONE,Point());
    41. findContours(dstImage6,contours5,hierarchy5,RETR_EXTERNAL,CHAIN_APPROX_NONE,Point());
    42. findContours(dstImage7,contours6,hierarchy6,RETR_EXTERNAL,CHAIN_APPROX_NONE,Point());
    43. Point pt1;
    44. Point pt2;
    45. for(auto i=0;i<contours.size();i++)
    46. {
    47. Rect rect=boundingRect(contours[i]);
    48. int maxarea=rect.width*rect.height;
    49. if(maxarea>8000){
    50. pt1=rect.br();
    51. }
    52. }
    53. for(auto i=0;i<contours.size();i++)
    54. {
    55. Rect rect=boundingRect(contours[i]);
    56. int maxarea=rect.width*rect.height;
    57. if(maxarea>3500&&maxarea<4000){
    58. pt2.x=rect.tl().x-30;
    59. pt2.y=rect.tl().y;
    60. }
    61. }
    62. rectangle(srcImage,pt2,pt1,Scalar(255,0,0),2);
    63. cout<<雪碧(<<(pt1.x-pt2.x)/2+pt2.x<<,<<(pt1.y-pt2.y)/2+pt2.y<<)<<endl;
    64. for(auto j=0;j<contours1.size();j++){
    65. int areax=11000;
    66. int areay=5000;
    67. Rect rect1=boundingRect(contours1[j]);
    68. Point pt2;
    69. pt2.x=rect1.x;
    70. pt2.y=rect1.y-30;
    71. int maxarea1=rect1.width*rect1.height;
    72. if(maxarea1<areax&&maxarea1>areay){
    73. rectangle(srcImage,pt2,rect1.br(),Scalar(255,0,0),2);
    74. cout<<小可樂(<<(rect1.br().x-pt2.x)/2+pt2.x<<,<<(rect1.br().y-pt2.y)/2+pt2.y<<)<<endl;
    75. }
    76. }
    77. for(auto j=0;j<contours2.size();j++){
    78. int area1=5000;
    79. Rect rect1=boundingRect(contours2[j]);
    80. Point pt3;
    81. pt3.x=rect1.br().x-35;
    82. pt3.y=rect1.br().y+50;
    83. int maxarea1=rect1.width*rect1.height;
    84. cornerHarris(dstImage4,harriscorner,2,3,0.1);
    85. if(area1<maxarea1){
    86. rectangle(srcImage,rect1.tl(),pt3,Scalar(255,0,0),2);
    87. cout<<牛奶箱(<<(pt3.x-rect1.tl().x)/2+rect1.tl().x<<,<<(pt3.y-rect1.tl().y)/2+rect1.tl().y<<)<<endl;
    88. }
    89. }
    90. Point pt;
    91. Point pr;
    92. for(auto j=0;j<contours6.size();j++){
    93. Rect rect1=boundingRect(contours6[j]);
    94. int area=rect1.width*rect1.height;
    95. if(area>2000&&area<3000){
    96. pt.y=rect1.tl().y;
    97. }
    98. }
    99. for(auto j=0;j<contours3.size();j++){
    100. Rect rect1=boundingRect(contours3[j]);
    101. int area=rect1.width*rect1.height;
    102. if(area>20000&&area<25000){
    103. pr.x=rect1.br().x;
    104. }
    105. }
    106. for(auto j=0;j<contours4.size();j++){
    107. Rect rect1=boundingRect(contours4[j]);
    108. int area=rect1.width*rect1.height;
    109. if(area>20000&&area<23000){
    110. pr.y=rect1.br().y;
    111. pt.x=rect1.tl().x;
    112. }
    113. }
    114. rectangle(srcImage,pt,pr,Scalar(255,0,0),2);
    115. cout<<大可樂<<(<<(pr.x-pt.x)/2+pt.x<<,<<(pr.y-pt.y)/2+pt.y<<)<<endl;
    116. Point pa;
    117. Point pb;
    118. for(auto j=0;j<contours3.size();j++){
    119. Rect rect1=boundingRect(contours3[j]);
    120. int area=rect1.width*rect1.height;
    121. if(area>10000&&area<18000){
    122. pa=rect1.tl();
    123. pb.x=rect1.br().x;
    124. }
    125. }
    126. for(auto j=0;j<contours5.size();j++){
    127. Rect rect1=boundingRect(contours5[j]);
    128. int area=rect1.width*rect1.height;
    129. if(area>5000&&area<8000){
    130. pb.y=rect1.br().y;
    131. }
    132. }
    133. rectangle(srcImage,pa,pb,Scalar(255,0,0),2);
    134. cout<<蜜雪冰城<<(<<(pb.x-pa.x)/2+pa.x<<,<<(pb.y-pa.y)/2+pa.y<<)<<endl;
    135. imshow(dst,srcImage);
    136. waitKey(0);
    137. }
    138. int main()
    139. {
    140. fun();
    141. return 0;
    142. }

    使用了颜色识别,vector类去寻找以及储存各个区域的起始点。