use*_*658 0 c++ opencv image-processing feature-extraction image-segmentation
目的: 我想检测叶片图像的某些区域。我发现了我的相关问题,该问题是在白板上使用图像处理(从白色背景中移除叶子)的分段对象(叶子),但我的问题超出了它,它的目的是提取/分割患病区域的叶子。
问题: 如何准确分割和提取图像中叶片的患病区域。
我的尝试:
1. inRange()OpenCV函数
-设置绿色阈值,这样我就不会为非绿色区域(棕色,灰色等)设置多个inRange值,并且希望删除绿色;我应用了高斯模糊,在分割之前将其从RGB转换为HSV
链接到 image1,image2输入和结果的文件:image1
:
结果:绿色被分割(认为不是很准确),但是我仍然不知道如何提取非绿色区域(下一步)
图片2:
结果:黑色小圆圈被包含/被认为是绿色,这应该不应该
我是OpenCV(也是C ++)的新手,我已经阅读了几种分割技术(例如,聚类方法Fuzzy-C和k-means等),但是我无法确定要对图像使用哪种分割技术。我还从阅读的文章中了解到,没有通用的分割技术可以应用于所有图像。
因此,我想知道哪种技术(聚类方法,基于区域的区域,直方图等)或过程最适合于我拥有的各种图像,以便准确地分割所述图像。
非常感谢你。
只需尝试以下步骤
创建蒙版图像:-首先,您需要为叶子创建一个蒙版图像,需要进行阈值处理,找到轮廓(最大),绘制轮廓(填充)等...此外,要去除边缘效果,您还需要腐蚀蒙版,这会带来更好的结果。
下面的代码片段将完成以上操作
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 );
Run Code Online (Sandbox Code Playgroud)
提取绿色区域:-在这里,您应该使用问题中提到的hsv颜色空间,范围等...。
Mat HSV,hsv_thr,dst;
cvtColor(src,HSV,CV_BGR2HSV);
inRange(HSV,Scalar(20,10,10),Scalar(90,255,255),hsv_thr);
Run Code Online (Sandbox Code Playgroud)
以上图像的bitwise_not:-在此应使用上面创建的蒙版。
bitwise_not(hsv_thr, dst, mask);
Run Code Online (Sandbox Code Playgroud)
绘制患病区域:-在这里再次需要找到轮廓,绘制轮廓等。
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 );
Run Code Online (Sandbox Code Playgroud)
您可以通过适当的过滤,使用适当的hsv范围进行阈值处理等来改善结果。此外,上述算法还考虑到背景始终为白色,而对于其他背景,则需要更改创建蒙版图像的步骤。
希望这些对您有帮助...