刚刚尝试下面的步骤
创建模板图片: -首先,你需要为叶创建蒙版图像,你需要做的阈值,找到轮廓(最大),画出轮廓(用实心)等...为了消除边缘效应,你需要侵蚀你的面具,这会产生更好的效果。
![enter image description here](https://i.stack.imgur.com/oQqKq.png)
![enter image description here](https://i.stack.imgur.com/Hz9q6.png)
![enter image description here](https://i.stack.imgur.com/N9o4J.jpg)
![enter image description here](https://i.stack.imgur.com/AiVfD.jpg)
下面的代码片段会做上述
Mat thr;
Mat src=imread("image2.png",1); //Your processed image
cvtColor(src,thr,CV_BGR2GRAY);
threshold(thr,thr,180,255,THRESH_BINARY_INV);
vector< vector <Point> > contours; // Vector for storing contour
vector<Vec4i> hierarchy;
int largest_contour_index=0;
int largest_area=0;
Mat mask(src.rows,src.cols,CV_8UC1,Scalar::all(0)); //create destination image
findContours(thr.clone(), contours, hierarchy,CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE); // Find the contours in the image
for(int i = 0; i< contours.size(); i++) // iterate through each contour.
{
double a=contourArea(contours[i],false); // Find the area of contour
if(a>largest_area){
largest_area=a;
largest_contour_index=i; //Store the index of largest contour
}
}
drawContours(mask,contours, largest_contour_index, Scalar(255,255,255),CV_FILLED, 8, hierarchy); // Draw the largest contour using previously stored index.
int dilation_size = 2;
Mat element = getStructuringElement(MORPH_RECT,
Size(2*dilation_size + 1, 2*dilation_size+1),
Point(dilation_size, dilation_size));
erode(mask, mask, element);
提取绿色区域: -这里你应该用HSV色彩空间,INRANGE等。正如你的问题中提到的那样。
![enter image description here](https://i.stack.imgur.com/Qx2AW.jpg)
![enter image description here](https://i.stack.imgur.com/6qQTD.jpg)
Mat HSV,hsv_thr,dst;
cvtColor(src,HSV,CV_BGR2HSV);
inRange(HSV,Scalar(20,10,10),Scalar(90,255,255),hsv_thr);
以上图片bitwise_not: -这里你应该用上面创建的面具。
![enter image description here](https://i.stack.imgur.com/eohFB.jpg)
![enter image description here](https://i.stack.imgur.com/LyS1U.jpg)
bitwise_not(hsv_thr, dst, mask);
绘制发病面积: - 再次在这里,您需要做的找到轮廓画出轮廓等...
![enter image description here](https://i.stack.imgur.com/o3ihP.jpg)
![enter image description here](https://i.stack.imgur.com/LKl1x.jpg)
findContours(dst.clone(), contours, hierarchy,CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE); // Find the contours in the image
for(int i = 0; i< contours.size(); i++) // iterate through each contour.
drawContours(src,contours, i, Scalar(0,0,255),1, 8, hierarchy);
你可以通过适当的滤波,阈值提高结果使用适当的hsv范围等等......另外上面的算法认为你的背景总是白色的,对于其他背景你需要改变创建遮罩图像的步骤。
希望这些有帮助...
这是我真正需要的。这确实是非常有用的。 我只想问一下,有没有其他可用于感兴趣区域(非绿色)区域的格式?我想要一个roi(不是二进制)的灰度或彩色图像版本进行进一步处理。 – user3339658
在这里使用这个答案[link](http://stackoverflow.com/questions/10176184/with-opencv-try-to-extract-a-region-of-a-picture-described-by-arrayofarrays#),I试图让轮廓内的区域(其中只有roi是可见的,其他人是黑色的),但我只能在图像的第一轮廓内获得该区域,而不是所有轮廓。你能给我一些关于如何获得所有轮廓内容的指针吗?非常感谢你提前。 :) – user3339658
只需使用[Mat :: copyTo()](http://docs.opencv.org/modules/core/doc/basic_structures.html#mat-copyto)和轮廓作为蒙版。 – Haris