1 | initial version |
Using your first image(processed image) I just processed some more step like
made the result as shown below.
Code:- Here is the C++ code for the above steps,
Mat tmp,thr;
Mat src=imread("river.jpg",1); //Your processed image
cvtColor(src,tmp,CV_BGR2GRAY);
threshold(tmp,thr,10,255,THRESH_BINARY_INV);
//Find biggest contour to segment exact object
vector< vector <Point> > contours; // Vector for storing contour
vector< Vec4i > hierarchy;
int largest_contour_index=0;
int largest_area=0;
Mat dst(src.rows,src.cols,CV_8UC1,Scalar::all(0)); //create destination image
findContours( thr, 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( dst,contours, largest_contour_index, Scalar(255,255,255),CV_FILLED, 8, hierarchy ); // Draw the largest contour using previously stored index.
///Either directly draw the contour
drawContours( src,contours, largest_contour_index, Scalar(0,255,0),3, 8, hierarchy );
///Or use convex hull to draw the contour
vector<vector<Point> >hull(1);
convexHull(contours[largest_contour_index], hull[0],false,true );
drawContours( src, hull, 0, Scalar(0,0,255), 2, 8, vector<Vec4i>(), 0, Point() );
imshow("contour",dst);
imshow("src",src);
Result:- Red boundary is result of convexhull and Green boundary is the result of drawcontour using largest contour.
2 | No.2 Revision |
Using your first image(processed image) I just processed proceed some more step like
made the result as shown below.
Code:- Here is the C++ code for the above steps,
Mat tmp,thr;
Mat src=imread("river.jpg",1); //Your processed image
cvtColor(src,tmp,CV_BGR2GRAY);
threshold(tmp,thr,10,255,THRESH_BINARY_INV);
//Find biggest contour to segment exact object
vector< vector <Point> > contours; // Vector for storing contour
vector< Vec4i > hierarchy;
int largest_contour_index=0;
int largest_area=0;
Mat dst(src.rows,src.cols,CV_8UC1,Scalar::all(0)); //create destination image
findContours( thr, 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( dst,contours, largest_contour_index, Scalar(255,255,255),CV_FILLED, 8, hierarchy ); // Draw the largest contour using previously stored index.
///Either directly draw the contour
drawContours( src,contours, largest_contour_index, Scalar(0,255,0),3, 8, hierarchy );
///Or use convex hull to draw the contour
vector<vector<Point> >hull(1);
convexHull(contours[largest_contour_index], hull[0],false,true );
drawContours( src, hull, 0, Scalar(0,0,255), 2, 8, vector<Vec4i>(), 0, Point() );
imshow("contour",dst);
imshow("src",src);
Result:- Red boundary is result of convexhull and Green boundary is the result of drawcontour using largest contour.
3 | No.3 Revision |
Using your first image(processed image) I just proceed some more step like
made the result as shown below.
Code:- Here is the C++ code for the above steps,
Mat tmp,thr;
Mat src=imread("river.jpg",1); //Your processed image
cvtColor(src,tmp,CV_BGR2GRAY);
threshold(tmp,thr,10,255,THRESH_BINARY_INV);
//Find biggest contour to segment exact object
vector< vector <Point> > contours; // Vector for storing contour
vector< Vec4i > hierarchy;
int largest_contour_index=0;
int largest_area=0;
Mat dst(src.rows,src.cols,CV_8UC1,Scalar::all(0)); //create destination image
findContours( thr, 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( dst,contours, largest_contour_index, Scalar(255,255,255),CV_FILLED, 8, hierarchy ); // Draw the largest contour using previously stored index.
///Either directly draw the contour
drawContours( src,contours, largest_contour_index, Scalar(0,255,0),3, 8, hierarchy );
///Or use convex hull to draw the contour
vector<vector<Point> >hull(1);
convexHull(contours[largest_contour_index], hull[0],false,true );
drawContours( src, hull, 0, Scalar(0,0,255), 2, 8, vector<Vec4i>(), 0, Point() );
imshow("contour",dst);
imshow("src",src);
Result:- Red boundary is result of convexhull and Green boundary is the result of drawcontour using largest contour.contour.