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.
Related
I'm trying to detect and extract playing cards from an image. The plan is to detect the contours of the cards and then extract them using the area of the contours. (Is there a more efficient way of doing it?)
The problem is that I was having trouble with non-closed contours:
With this contours I'm not able to calculate the area of the rectangles. Hence, I performed morphological transformations to close the contours, producing this:
And after edge extracting:
Leaving me with these "rectangles" with twisted corners in the edges. How can I approximate these pseudo-rectangles to perfect geometric rectangles?
Is there a more efficient way of doing this?
Here is my code so far:
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <stdlib.h>
#include <stdio.h>
using namespace cv;
using namespace std;
#define BKG_THRESH 60 // preProcessImg
Mat src;
void preProcessImg(Mat* _img){
Mat aux_gray;
cvtColor(*_img, aux_gray, CV_BGR2GRAY );
GaussianBlur(aux_gray, *_img, Size(5,5), 0);
}
int main( int argc, char** argv ){
vector<vector<Point>> contours;
/// Load an image
src = imread("img.jpg");
preProcessImg(&src);
Canny(src, src, 30, 200);
//Mostrar imagem
namedWindow( "canny_output", CV_WINDOW_NORMAL); // Create a window
imshow( "canny_output", src);
waitKey(0);
Mat structuringElement = getStructuringElement(MORPH_ELLIPSE, Size(7, 7));
morphologyEx(src, src, MORPH_CLOSE, structuringElement);
//Mostrar imagem
namedWindow( "morph_transf", CV_WINDOW_NORMAL); // Create a window
imshow( "morph_transf", src);
waitKey(0);
findContours(src, contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
Mat drawing = Mat::zeros( src.size(), CV_8UC3 );
for( int i = 0; i< contours.size(); i++){
Scalar color( rand()&255, rand()&255, rand()&255 );
drawContours( drawing, contours, i, color );
}
//Mostrar imagem
namedWindow( "contours", CV_WINDOW_NORMAL); // Create a window
imshow( "contours", drawing);
waitKey(0);
return 0;
}
The more robust way is to find lines (Hough lines) afer Canny, intersect they and find rectangles. Contours are not robust for noise.
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);
}
I'm just now starting to learn how to use the openCV libraries. I've downloaded and installed openCV 2.4.10 , and have run an example " in tutorial of Finding contours in your image " using visual studio 2010. The code compiles, but every time I run it, it crashes,and I get the showing error in this image link which is as the following message:
Windows has triggered a breakpoint in EdgeDetection.exe.
This may be due to a corruption of the heap, which indicates a bug in >EdgeDetection.exe or any of the DLLs it has loaded.
This may also be due to the user pressing F12 while EdgeDetection.exe has focus.
The output window may have more diagnostic information.
I don't know, what is the reason.Here's the offending code:
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace cv;
using namespace std;
Mat src; Mat src_gray;
int thresh = 100;
int max_thresh = 255;
RNG rng(12345);
/// Function header
void thresh_callback(int, void* );
/** #function main */
int main( int argc, char** argv )
{
/// Load source image and convert it to gray
// src = imread( argv[1], 1 );
src=cvLoadImage("fish.bmp");
/// Convert image to gray and blur it
cvtColor( src, src_gray, CV_BGR2GRAY );
blur( src_gray, src_gray, Size(3,3) );
/// Create Window
char* source_window = "Source";
namedWindow( source_window, CV_WINDOW_AUTOSIZE );
imshow( source_window, src );
createTrackbar( " Canny thresh:", "Source", &thresh, max_thresh, thresh_callback );
thresh_callback( 0, 0 );
waitKey(0);
return(0);
}
/** #function thresh_callback */
void thresh_callback(int, void* )
{
Mat canny_output;
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
/// Detect edges using canny
Canny( src_gray, canny_output, thresh, thresh*2, 3 );
/// Find contours
findContours( canny_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );
/// Draw contours
Mat drawing = Mat::zeros( canny_output.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() );
}
/// Show in a window
namedWindow( "Contours", CV_WINDOW_AUTOSIZE );
imshow( "Contours", drawing );
}
the breaking appears at line :
findContours( canny_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );
So,I would like you to help me.
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
}
}
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);