바로 아래에 시도
이 마스크 이미지 만들기 단계 : - (채워진와) 윤곽을 그립니다 (최대) 윤곽을 찾을 먼저 당신이 잎을위한 마스크 이미지를 만들 필요가, 당신이 임계 값을 할 필요가 등 ... 또한 가장자리 효과를 제거하기 위해 마스크를 부식시켜야합니다. 그러면 더 나은 결과를 얻을 수 있습니다.
아래의 코드 조각은 위의
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 등을 사용한다 .. 귀하의 질문에 언급 된 바와 같이.위의 이미지에 대한
는
Mat HSV,hsv_thr,dst;
cvtColor(src,HSV,CV_BGR2HSV);
inRange(HSV,Scalar(20,10,10),Scalar(90,255,255),hsv_thr);
bitwise_not : -가 여기에 위에서 만든 마스크를 사용해야합니다.
bitwise_not(hsv_thr, dst, mask);
이 병에 걸린 영역 그리기 : - 당신이 윤곽 등 그릴 윤곽을 발견 할 필요가 다시 여기에을 ...
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#), 나는 윤곽선 내에서 영역을 얻으려고했는데 (단지 로이가 보이고 나머지는 검은 색), 이미지의 첫 번째 윤곽선 내에서만 영역을 얻을 수 있었지만 모든 윤곽선을 얻을 수는 없었습니다. 모든 윤곽의 내용을 얻는 방법에 대한 조언을 해 줄 수 있습니까? 대단히 감사드립니다. :) – user3339658
등고선을 마스크로 [Mat :: copyTo()] (http://docs.opencv.org/modules/core/doc/basic_structures.html#mat-copyto)를 사용하기 만하면됩니다. – Haris