سؤال

Objective: I want to detect certain regions of the leaf images. I've found related problems of mine, close to which is this segment object(leaf) which is on the white paper using image processing (removing leaf from white background) but mine goes beyond it, it aims to extract/segment the diseased area of the leaf.

Problem: How to accurately segment and extract the diseased areas of the leaf in the image.

My attempt:
1. inRange() OpenCV function -Threshold green color so that I won't be making several inRange values for non-green areas(brown, gray,etc) and I will remove the green color hopefully; I applied Gaussian blur, converted from RGB to HSV before segmenting

Link to Files of image1, image2 input and results:
Image1: Result: Green was segmented(thought not quite accurately), but I still don't know how to extract the non-green areas(as the next step)

Image2: Result: The dark small circles were included/considered green which supposedly should not

I am new to OpenCV(and also C++) and I have read several techniques(like clustering methods fuzzy-c and k-means, etc) for segmentation but I can't decide which segmentation technique to use for my images. I've also learned from the articles I read that there's no such thing as universal segmentation technique that can be applied to all images.

Thus, I would like to know which technique(clustering method? region based? histogram?, etc) or process is best to apply for the kinds of images I have in order to accurately segment said images.

Thank you very much.

هل كانت مفيدة؟

المحلول

Just try below steps

Create Mask Image:- First you need to create a mask image for the leaf, you need to do thresholding, find contour(largest), draw contour(with filled) etc...also for removing edge effect you need to erode your mask, which will give better result.

enter image description here enter image description here

enter image description here enter image description here

The below code snippets will do the above

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 );

Extract green region:- Here you should use hsv color space, inrange etc... as mentioned in your question.

enter image description here enter image description here

Mat HSV,hsv_thr,dst;
cvtColor(src,HSV,CV_BGR2HSV);
inRange(HSV,Scalar(20,10,10),Scalar(90,255,255),hsv_thr);

bitwise_not for above image:- Here you should use the mask created above.

enter image description here enter image description here

  bitwise_not(hsv_thr, dst, mask);

Draw the diseased area:- Here again you need to do find contour draw contour etc...

enter image description here enter image description here

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 ); 

You can improve the result by proper filtering, thresholding use proper hsv range etc... Also the above algorithm consider that your background always white and for other background you need to change the steps for creating mask image.

Hope these helpful...

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top