资源简介
基于opencv,利用hsv颜色空间识别出特定水果,并且根据水果面积及形状进行筛选,识别出水果,内含详细备注
代码片段和文件信息
#include
#include “opencv2/highgui/highgui.hpp“
#include “opencv2/imgproc/imgproc.hpp“
using namespace cv;
using namespace std;
int b;
Mat draw(Mat imagevector > contours)
{
Mat result(image.size()CV_8UScalar(255));
drawContours(resultcontours-1Scalar(0)1);
return result;
}
void getContours(Mat image)
{
Mat element=getStructuringElement(MORPH_RECTSize(22));
erode(imageimageelementPoint(-1-1)1); //腐蚀
//imshow(“腐蚀“image);
Mat element1=getStructuringElement(MORPH_RECTSize(22));
dilate(imageimageelement1Point(-1-1)10); //膨胀
//imshow(“膨胀“image);
Mat srs3;
image.copyTo(srs3);//把image复制给srs3
//imshow(“hh“srs3);
vector> contours;
findContours(imagecontoursRETR_LISTCHAIN_APPROX_NONE);
//imshow(“qqq“image);
Mat result(image.size()CV_8UScalar(255));//定义一个空白的图片
for(int i=0;i {
drawContours(resultcontours-1Scalar(0)2);
}
imshow(“ddd“result);
size_t maxSize=0;//存放最大轮廓
vector maxContour;
vector>::const_iterator itc=contours.begin();
/* while(itc!=contours.end())
{
if (itc->size() < 200|| itc->size() > 3000)
{
itc = contours.erase(itc); // 删除当前连通域轮廓 250为最小轮廓长 2000为最大轮廓长
}
else if(((double)boundingRect(Mat(*itc)).height)/((double)boundingRect(Mat(*itc)).width)>1||
((double)boundingRect(Mat(*itc)).height)/((double)boundingRect(Mat(*itc)).width)<0.5)
{itc=contours.erase(itc);}
else
{itc++;}
}*/
while(itc!=contours.end())
{
if(itc->size()>maxSize)
{
maxSize=itc->size();
maxContour=*itc;
}
++itc;
}
itc = contours.begin();
while(itc!= contours.end())
{ //Eliminate smaller contours
if(itc->size() < maxSize)
itc=contours.erase(itc);
else
++itc;
}
cout<< maxSize< imshow(“333“draw(imagecontours));//排除了没有用的信息
Mat image_dst=draw(imagecontours).clone();
Rect*r=new Rect[contours.
- 上一篇:c++成语接龙代码.rar
- 下一篇:C++实现通用链表
评论
共有 条评论