Hi,
I am using opencv 3.0.0 alpha advanced version in windows xp with visual studio 2008.
In my project, while im working with imwrite() function and findcontours(), programme giving exception like " unhandled exception at location and access violation ".
So please give me solution to this.
Im getting iritation with this exception.
this is my code.....
int main()
{
cv::Mat img = cv::imread("equation.jpg", 0);
imshow("",img);waitKey(0); imshow("",img);waitKey(0);
//Apply blur to smooth edges and use adapative thresholding
cv::Size size(3,3);
cv::GaussianBlur(img,img,size,0);
adaptiveThreshold(img, img,255,CV_ADAPTIVE_THRESH_MEAN_C, CV_THRESH_BINARY,75,10);
cv::bitwise_not(img,
img); img);
cv::Mat img2 =
img.clone(); std::vector<cv::point>
img.clone();
std::vector<cv::Point> points;
cv::Mat_<uchar>::iterator it = img.begin<uchar>();
cv::Mat_<uchar>::iterator end = img.end<uchar>();
for (; it != end; ++it)
if (*it)
points.push_back(it.pos()); points.push_back(it.pos());
cv::RotatedRect box =
cv::minAreaRect(cv::Mat(points)); cv::minAreaRect(cv::Mat(points));
double angle = box.angle;
if (angle < -45.)
angle +=
90.; 90.;
cv::Point2f vertices[4];
box.points(vertices);
for(int i = 0; i < 4; ++i)
cv::line(img, vertices[i], vertices[(i + 1) % 4], cv::Scalar(255, 0, 0), 1,
CV_AA); CV_AA);
cv::Mat rot_mat = cv::getRotationMatrix2D(box.center, angle,
1); 1);
cv::Mat rotated;
cv::warpAffine(img2, rotated, rot_mat, img.size(),
cv::INTER_CUBIC); cv::INTER_CUBIC);
cv::Size box_size = box.size;
if (box.angle < -45.)
std::swap(box_size.width, box_size.height);
cv::Mat
cropped; cropped;
cv::getRectSubPix(rotated, box_size, box.center, cropped);
cv::imshow("Cropped", cropped);waitKey(0);
imwrite("example5.jpg",cropped);////this is place my
exception
exception
Mat cropped2=cropped.clone();
cvtColor(cropped2,cropped2,CV_GRAY2RGB);
cvtColor(cropped2,cropped2,CV_GRAY2RGB);
Mat cropped3 = cropped.clone();
cvtColor(cropped3,cropped3,CV_GRAY2RGB); vector<vector<point>
cvtColor(cropped3,cropped3,CV_GRAY2RGB);
vector<vector<Point> > contours;
vector<vec4i> hierarchy; vector<Vec4i> hierarchy;
// Find contours
cv:: findContours( cropped, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_TC89_KCOS, Point(0, 0) );//this is place my
exception exception
// Approximate contours to polygons + get bounding rects and circles
vector<vector<point> vector<vector<Point> > contours_poly( contours.size() );
vector<rect> vector<Rect> boundRect( contours.size() );
vector<point2f>center( vector<Point2f>center( contours.size() );
vector<float>radius( contours.size()
); );
// Get poly contours
for( int i = 0; i < contours.size(); i++ )
{
approxPolyDP( Mat(contours[i]), contours_poly[i], 3, true );
} }
//Get only important contours, merge contours that are within another
vector<vector<point> vector<vector<Point> > validContours;
for (int
i=0;i<contours_poly.size();i++){< p="">
i=0;i<contours_poly.size();i++){
Rect r = boundingRect(Mat(contours_poly[i]));
if(r.area()<100)continue;
bool inside = false;
for(int j=0;j<contours_poly.size();j++){
if(j==i)continue;
Rect r2 = boundingRect(Mat(contours_poly[j]));
if(r2.area()<100||r2.area()<r.area())continue;
if(r.x>r2.x&&r.x+r.width<r2.x+r2.width&&
r.y>r2.y&&r.y+r.height<r2.y+r2.height){
inside = true;
}
}
if(inside)continue;
validContours.push_back(contours_poly[i]);
}
//Get bounding rects
for(int i=0;i<validContours.size();i++){
boundRect[i] = boundingRect( Mat(validContours[i]) );
}
//Display
Scalar color = Scalar(0,255,0);
for( int i = 0; i< validContours.size(); i++ )
{
if(boundRect[i].area()<100)continue;
drawContours( cropped2, validContours, i, color, 1, 8,
vector<vec4i>(), vector<Vec4i>(), 0, Point() );
rectangle( cropped2, boundRect[i].tl(), boundRect[i].br(),color, 2, 8, 0 );
} }
//imwrite("example6.jpg",cropped2);
imshow("Contours",cropped2); extractContours(cropped3,validContours);
imshow("Contours",cropped2);
extractContours(cropped3,validContours);
cv::waitKey(0);
return(0); }
return(0);
}
Thank you.