任务描述:将图片上的各个饮料瓶识别出来
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <math.h>
using namespace std;
using namespace cv;
void fun(){
Mat srcImage,dstImage1,dstImage2,dstImage3,dstImage4,dstImage5,dstImage6,dstImage7,dstImage8;
Mat harriscorner;
vector<vector<Point>>contours;
vector<Vec4i>hierarchy;
vector<vector<Point>>contours1;
vector<Vec4i>hierarchy1;
vector<vector<Point>>contours2;
vector<Vec4i>hierarchy2;
vector<vector<Point>>contours3;
vector<Vec4i>hierarchy3;
vector<vector<Point>>contours4;
vector<Vec4i>hierarchy4;
vector<vector<Point>>contours5;
vector<Vec4i>hierarchy5;
vector<vector<Point>>contours6;
vector<Vec4i>hierarchy6;
srcImage=imread(/home/ma/下载/renwu4.jpg);
blur(srcImage,dstImage1,Size(3,3));
cvtColor(dstImage1,dstImage1,COLOR_BGR2HSV);
inRange(dstImage1,Scalar(25,103,48),Scalar(76,255,222),dstImage2);
inRange(dstImage1,Scalar(133,55,59),Scalar(180,255,255),dstImage3);//hong
inRange(dstImage1,Scalar(11,131,29),Scalar(15,255,255),dstImage4);
inRange(dstImage1,Scalar(0,0,31),Scalar(180,111,88),dstImage5);
inRange(dstImage1,Scalar(15,90,141),Scalar(25,224,208),dstImage6);
inRange(dstImage1,Scalar(175,109,130),Scalar(180,255,255),dstImage7);
Mat ksize=getStructuringElement(0,Size(13,13));
dilate(dstImage3,dstImage3,ksize);
findContours(dstImage2,contours,hierarchy,RETR_EXTERNAL,CHAIN_APPROX_NONE,Point());
findContours(dstImage3,contours1,hierarchy1,RETR_EXTERNAL,CHAIN_APPROX_NONE,Point());//hong
findContours(dstImage4,contours2,hierarchy2,RETR_EXTERNAL,CHAIN_APPROX_NONE,Point());
findContours(dstImage3,contours3,hierarchy3,RETR_EXTERNAL,CHAIN_APPROX_NONE,Point());
findContours(dstImage5,contours4,hierarchy4,RETR_EXTERNAL,CHAIN_APPROX_NONE,Point());
findContours(dstImage6,contours5,hierarchy5,RETR_EXTERNAL,CHAIN_APPROX_NONE,Point());
findContours(dstImage7,contours6,hierarchy6,RETR_EXTERNAL,CHAIN_APPROX_NONE,Point());
Point pt1;
Point pt2;
for(auto i=0;i<contours.size();i++)
{
Rect rect=boundingRect(contours[i]);
int maxarea=rect.width*rect.height;
if(maxarea>8000){
pt1=rect.br();
}
}
for(auto i=0;i<contours.size();i++)
{
Rect rect=boundingRect(contours[i]);
int maxarea=rect.width*rect.height;
if(maxarea>3500&&maxarea<4000){
pt2.x=rect.tl().x-30;
pt2.y=rect.tl().y;
}
}
rectangle(srcImage,pt2,pt1,Scalar(255,0,0),2);
cout<<雪碧(<<(pt1.x-pt2.x)/2+pt2.x<<,<<(pt1.y-pt2.y)/2+pt2.y<<)<<endl;
for(auto j=0;j<contours1.size();j++){
int areax=11000;
int areay=5000;
Rect rect1=boundingRect(contours1[j]);
Point pt2;
pt2.x=rect1.x;
pt2.y=rect1.y-30;
int maxarea1=rect1.width*rect1.height;
if(maxarea1<areax&&maxarea1>areay){
rectangle(srcImage,pt2,rect1.br(),Scalar(255,0,0),2);
cout<<小可樂(<<(rect1.br().x-pt2.x)/2+pt2.x<<,<<(rect1.br().y-pt2.y)/2+pt2.y<<)<<endl;
}
}
for(auto j=0;j<contours2.size();j++){
int area1=5000;
Rect rect1=boundingRect(contours2[j]);
Point pt3;
pt3.x=rect1.br().x-35;
pt3.y=rect1.br().y+50;
int maxarea1=rect1.width*rect1.height;
cornerHarris(dstImage4,harriscorner,2,3,0.1);
if(area1<maxarea1){
rectangle(srcImage,rect1.tl(),pt3,Scalar(255,0,0),2);
cout<<牛奶箱(<<(pt3.x-rect1.tl().x)/2+rect1.tl().x<<,<<(pt3.y-rect1.tl().y)/2+rect1.tl().y<<)<<endl;
}
}
Point pt;
Point pr;
for(auto j=0;j<contours6.size();j++){
Rect rect1=boundingRect(contours6[j]);
int area=rect1.width*rect1.height;
if(area>2000&&area<3000){
pt.y=rect1.tl().y;
}
}
for(auto j=0;j<contours3.size();j++){
Rect rect1=boundingRect(contours3[j]);
int area=rect1.width*rect1.height;
if(area>20000&&area<25000){
pr.x=rect1.br().x;
}
}
for(auto j=0;j<contours4.size();j++){
Rect rect1=boundingRect(contours4[j]);
int area=rect1.width*rect1.height;
if(area>20000&&area<23000){
pr.y=rect1.br().y;
pt.x=rect1.tl().x;
}
}
rectangle(srcImage,pt,pr,Scalar(255,0,0),2);
cout<<大可樂<<(<<(pr.x-pt.x)/2+pt.x<<,<<(pr.y-pt.y)/2+pt.y<<)<<endl;
Point pa;
Point pb;
for(auto j=0;j<contours3.size();j++){
Rect rect1=boundingRect(contours3[j]);
int area=rect1.width*rect1.height;
if(area>10000&&area<18000){
pa=rect1.tl();
pb.x=rect1.br().x;
}
}
for(auto j=0;j<contours5.size();j++){
Rect rect1=boundingRect(contours5[j]);
int area=rect1.width*rect1.height;
if(area>5000&&area<8000){
pb.y=rect1.br().y;
}
}
rectangle(srcImage,pa,pb,Scalar(255,0,0),2);
cout<<蜜雪冰城<<(<<(pb.x-pa.x)/2+pa.x<<,<<(pb.y-pa.y)/2+pa.y<<)<<endl;
imshow(dst,srcImage);
waitKey(0);
}
int main()
{
fun();
return 0;
}
使用了颜色识别,vector类去寻找以及储存各个区域的起始点。