1 | initial version |
You need to do,
See below code,
Mat src=imread("a.png",1);
Mat dst;//(src.rows,src.cols,CV_8UC4);
Mat tmp,thr;
cvtColor(src,tmp,CV_BGR2GRAY);
threshold(tmp,thr,50,255,THRESH_BINARY);
vector< vector <Point> > contours; // Vector for storing contour
vector< Vec4i > hierarchy;
int largest_contour_index=0;
int largest_area=0;
Mat alpha(src.size(),CV_8UC1,Scalar(0));
findContours( tmp, 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( alpha,contours, largest_contour_index, Scalar(255),CV_FILLED, 8, hierarchy );
Mat rgb[3];
split(src,rgb);
Mat rgba[4]={rgb[0],rgb[1],rgb[2],alpha};
merge(rgba,4,dst);
imwrite("dst.png",dst);
Note:
To keep the edge smooth check out below links,
http://stackoverflow.com/questions/21795643/image-edge-smoothing-with-opencv
http://stackoverflow.com/questions/17161088/how-to-refine-or-blur-or-smooth-just-the-edges
2 | No.2 Revision |
You need to do,
See below code,
Mat src=imread("a.png",1);
Mat dst;//(src.rows,src.cols,CV_8UC4);
Mat tmp,thr;
cvtColor(src,tmp,CV_BGR2GRAY);
threshold(tmp,thr,50,255,THRESH_BINARY);
threshold(tmp,thr,100,255,THRESH_BINARY);
vector< vector <Point> > contours; // Vector for storing contour
vector< Vec4i > hierarchy;
int largest_contour_index=0;
int largest_area=0;
Mat alpha(src.size(),CV_8UC1,Scalar(0));
findContours( tmp, 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( alpha,contours, largest_contour_index, Scalar(255),CV_FILLED, 8, hierarchy );
Mat rgb[3];
split(src,rgb);
Mat rgba[4]={rgb[0],rgb[1],rgb[2],alpha};
merge(rgba,4,dst);
imwrite("dst.png",dst);
Note:
To keep the edge smooth check out below links,
http://stackoverflow.com/questions/21795643/image-edge-smoothing-with-opencv
http://stackoverflow.com/questions/17161088/how-to-refine-or-blur-or-smooth-just-the-edges