extract largest contour from set of contours open cv - c++

After reading some stack overflow posts on image processing, I made a program to detect and a paper in an image and remove the background. But currently the program only detects the contours. How can I crop out the biggest contour? Eventually, I will use this region and use tesseract for character recognition. I am stuck at the preprocessing part.
the image I used:
And this is what I got after running my script:
How can I approximate the big blue contour and crop it out for things like character detection?
Here is the code I used:
static void on_canny( int,void* ){
blur( dst, detected_edges, Size(3,3) );
Canny(detected_edges, detected_edges, cannyThreshold, cannyThreshold*r, kernel_size);
findContours(detected_edges, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );
Mat drawing = Mat::zeros( detected_edges.size(), CV_8UC3 );
for(int i=0;i<contours.size();i++)
{
Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );
drawContours( drawing, contours, i, color, 2, 8, hierarchy, 0, Point() );
}
imshow("Contour",drawing);
imshow("Canny", detected_edges);
}

Related

OpenCV - how many black objects are there in a photo?

Okay, so I'm trying openCV with c++, and I want to do a simple detection program for objects that are black colored. So I have this simple code:
int main()
{
Mat3b bgr = imread("C:/Users/sesoa/Desktop/photos/shapes.png");
Mat3b hsv;
cvtColor(bgr, hsv, COLOR_BGR2HSV);
Mat1b mask1, mask2;
inRange(hsv, Scalar(0, 0, 0, 0), Scalar(180, 255, 30, 0), mask1);
inRange(hsv, Scalar(0, 0, 0, 0), Scalar(180, 255, 40, 0), mask2);
Mat1b mask = mask1 | mask2;
imshow("Mask", mask);
waitKey();
return 0;
}
shapes.png is this:
all the shapes are rounded with color black. I would like for my program to tell me how many of connected black objects are there. Also the writing under the shapes is also black. So It shows me this as well, that's okay, cause this is a test photo anyway.
How can I modify my program to detect how many connected black objects are in the photo? (In this photo, the output should be "60" as there are 8 objects and 49 letters + 3 letters are 'i' so we have to count the dots).
EDIT:
I want the program to count black objects. I already get all black objects out like this:
If you want to count the number of objects just do the following:
std::vector<std::vector<cv::Point> > contours;
std::vector<cv::Vec4i> hierarchy;
findContours( canny_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) ); // canny_output is the binary image
This will give you all the contours in the binary image (contours.size()). If you want only specific contours you can filter with contour area.
Hope it helps!

crop triangle from blackbackground

I'm new to image processing and development.I have a triangle in black background. I want to save that triangle as a Mat object without black pixels[0]. In order to do I tried as below.
Set threshold
find contours
identify contour[0] as trangle // has 2 contours one is triangle other one is backpixels.
save the contour points
crop the image.
My code please find below.
Mat finalImage = imread("test.png, CV_LOAD_IMAGE_GRAYSCALE);
img.copyTo(finalImage, mask);
Mat canny_output;
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
int thresh = 100;
int max_thresh = 255;
RNG rng(12345);
/// Detect edges using canny
Canny(finalImage, canny_output, thresh, thresh * 2, 3);
/// Find contours
findContours(canny_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0)); //find this method
/// Draw contours
Mat drawing = Mat::zeros(canny_output.size(), CV_8UC1);
for (int i = 0; i< contours.size(); i++)
{
Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
drawContours(drawing, contours, i, color, 2, 8, hierarchy, 0, Point()); // find this method.
}
I have points of contour but by using points of contours i have no idea how to crop only the trangle in input image.
You can get the bounding Rect of the various contours at the same time you are re drawing the contours. So in Your for loop where you are iterating the contours, you may use cv::boundingRect() to get the bounding Rect of the respective contour as:
/// Draw contours
Mat drawing = Mat::zeros(canny_output.size(), CV_8UC1);
for (int i = 0; i< contours.size(); i++)
{
Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
drawContours(drawing, contours, i, color, 2, 8, hierarchy, 0, Point()); // find this method.
cv::Rect boundingRect = cv::boundingRect(contours[i]);
}

opencv crop a portion of image within contour

I've just started learning OpenCv. i wanted to crop a portion of an image which is a text surrounded by the red circle. can you guys help me to find the solution like what are all the methods i should follow to crop it. I've tried few things and got the red circle cropped and stored it in a mat.
while(1)
{
capture>>img0;
imshow("original", img0);
imwrite("original.jpg", img0);
cv::inRange(img0,cv::Scalar(0,0,100),cv::Scalar(76,85,255),img1);
imshow("threshold.jpg", img1);
imwrite("threshold.jpg", img1);
// find the contours
vector< vector<Point> > contours;
findContours(img1, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
Mat mask = Mat::zeros(img1.rows, img1.cols, CV_8UC1);
drawContours(mask, contours, -1, Scalar(255), CV_FILLED);
Mat crop(img0.rows, img0.cols, CV_8UC3);
crop.setTo(Scalar(255,255,255));
img0.copyTo(crop, mask);
normalize(mask.clone(), mask, 0.0, 255.0, CV_MINMAX, CV_8UC3);
imshow("mask", mask);
imshow("cropped", crop);
imwrite("mask.jpg", mask);
imwrite("cropped.jpg", crop);
if(waitKey(30)=='27')
{
break;
}
}
return 0;`[original image[cropped image][1]`
From this image i wanted to crop a text alone. do help me to find the solution by sharing me the methods or steps to follow.
Thanks in advance
If you wish to extract the text alone, you can try this:-
drawContours(mask, contours, -1, Scalar(255), CV_FILLED);
vector<Rect> boundRect( contours.size() );
for(int i=0;i<contours.size();i++)
{
boundRect[i] = boundingRect(contours[i]);//enclose in Rect
Mat ROI,ROI_txt;
if(boundRect[i].width>30 && boundRect[i].height>30)//ignore noise rects
{
ROI=img0(boundRect[i]);//extract Red circle on ROI
inRange(ROI,Scalar(0,0,0),cv::Scalar(50,50,50),ROI_txt);
//black colour threshold to extract black text
}
}

Can't execute approxPolyDp function in OpenCV

I am trying to convert contour to set of polygonal curves, but I am stuck when I try to use approxPolyDP function. First I tested if findContours works properly and try to draw contours in my image - It works for contourIdx = 0. Then I try to use approxPolyDp as shown in example: http://docs.opencv.org/doc/tutorials/imgproc/shapedescriptors/bounding_rects_circles/bounding_rects_circles.html
But during execution I have error "Acces violation" connected with vector class and funciton size(). Here is my code:
IplImage* image = cvLoadImage("F:\\triangle.png");
waitKey(5000);
//Mat img = imread("triangle.png");
Mat img(image,true);
if(!img.data)
{
cout <<"image file not found";
cv::waitKey(5000);
return -1;
}
//namedWindow( "window", 0 );
//imshow( "window", img );
cvNamedWindow("window");
cvShowImage("window",image);
Mat imgGray;
Mat imgEdges;
cvtColor(img,imgGray,CV_BGR2GRAY);
blur(imgGray,imgGray,Size(3,3));
threshold(imgGray,imgEdges,128,255,CV_THRESH_BINARY);
Mat canny_output;
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
/// Detect edges using canny
Canny( imgGray, canny_output,100, 100*2, 3 );
/// Find contours
findContours( canny_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );
/// Draw contours
RNG rng(12345);
Mat drawing = Mat::zeros( canny_output.size(), CV_8UC3 );
if (drawing.type() != CV_8UC3)
{
cout << "Error: image type different then CV_8UC3";
}
Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );
drawContours( drawing, contours, 0, color, 2, 8, hierarchy, 0, Point() );
IplImage img3 = drawing;
cvNamedWindow( "Contours", CV_WINDOW_AUTOSIZE );
cvShowImage( "Contours", &img3 );
vector<vector<Point>> contoursOUT/*(contours.size())*/;
approxPolyDP(Mat(contours[0]),contoursOUT,3,true );
waitKey(0);
return 0;
Has anyone any idea what's wrong here?
The OpenCV-doc says about approxPolyDP:
void approxPolyDP (InputArray curve,
OutputArray approxCurve,
double epsilon,
bool closed)
approxCurve - ... The type should match the type of the input curve. ...
So the last part of your implementation should be:
vector<Point> contoursOUT;
approxPolyDP( Mat(contours[0]), contoursOUT, 3, true );
I teste your code with this small change, and it compiles and outputs resonable results.
I would recommend you to first study the documentation of approxPolyDP in opencv properly.
Now talking about your code, you have done a wrong declaration.
vector<vector<Point>> contoursOUT;
Use this one which is correct,
vector<Point> contoursOUT;
Also, in case of multiple object use for loop.

Drawing Convexity Defects C++ OpenCV

From the code below, I am able to draw the biggest contour with the centroid marked as a small circle and the hull as a yellow line. How do I draw the convexity defects? Should I use the circle() function or the drawContours() function?
Mat bw;
Mat canny_output;
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
findContours( bw, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );
int s = getBiggestContour(contours);
Mat drawing = Mat::zeros( src.size(), CV_8UC3 ); //UC1
Point2f mc = getCentroidPoint(contours[s]);
drawContours( drawing, contours, s, Scalar(255,255,255), -1, 8, hierarchy, 0, Point() );
circle( drawing, mc, 4, Scalar(0,0,255), 1, 8, 0 );
vector<vector<Point> >hull( contours[s].size() );
convexHull( Mat(contours[s]), hull[s], false );
drawContours( drawing, hull, s, Scalar(0,255,255), 1, 8, vector<Vec4i>(), 0, Point() );
The code above works but there's only one contour to be used which is the biggest contour so I think using vector> for hull is too much. How do I simplify that?
The code below is from another stackoverflow question but it doesn't show how to use the defects variable in drawing the convexity defect onto the Mat image. How can this be achieved?
vector<vector<int> > hullsI(contours.size());
vector<vector<Point> > hullsP(contours.size());
vector<vector<Vec4i> > defects(contours.size());
for(int i = 0; i <contours.size(); ++i){
//find the hulls
convexHull(contours[i], hullsI[i], false, false);
convexHull(contours[i], hullsP[i], false, true);
//find the defects
if (contours[i].size() >3 ){
convexityDefects(contours[i], hullsI[i], defects[i]);
}
}
I do not want to use IplImage. I prefer Mat.
You can draw the result of a convex hull operation with 'cvDrawContours()', but you do need to set your parameters correct for that to be possible. I have an example but it uses 'cvConvexHull2()' and IplImages but it should work the same way for a Mat and the other convexHull operation:
IplImage* src; //the image where the contours are detected on
IplImage frm; //the image you want the results to be drawn on
CvMemStorage* storage = cvCreateMemStorage(0);
CvSeq* contours = NULL;
cvFindContours(src, storage, &contours, sizeof (CvContour), CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);
for (CvSeq* c = contours; c != NULL; c = c->h_next) {
CvSeq* dest = NULL;
CvMemStorage* hullStorage = cvCreateMemStorage(0);
dest = cvConvexHull2(c, hullStorage, CV_CLOCKWISE, 1);
cvDrawContours(frm, dest, cvScalarAll(255), cvScalarAll(255), 0, 2, 8);
cvReleaseMemStorage(&hullStorage);
}
cvReleaseMemStorage(&storage);