Unhandled exception at imwrite(),findcontours() in visual studio 2008 with opencv 3.0.0 alpha version
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.....
include <cxcore.h>
include <highgui.h>
include <string>
include <iostream>
include <cv.h>
include "opencv2/objdetect/objdetect.hpp"
include "opencv2/highgui/highgui.hpp"
include "opencv2/imgproc/imgproc.hpp"
include "opencv2/core/core.hpp"
include <iostream>
include <windows.h>
ifdef _CH_
pragma package <opencv>
endif
ifndef _EiC
include "cv.h"
include "highgui.h"
include "ml.h"
include <stdio.h>
include <stdlib.h>
include <ctype.h>
endif
using namespace std; using namespace cv;
void main() { cv::Mat img = cv::imread("D:\mahi images\equation.jpg", 0); imshow("",img);waitKey(0);
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);
cv::Mat img2 = 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());
cv::RotatedRect box = cv::minAreaRect(cv::Mat(points));
double angle = box.angle;
if (angle < -45.)
angle += 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::Mat rot_mat = cv::getRotationMatrix2D(box.center, angle, 1);
cv::Mat rotated;
cv::warpAffine(img2, rotated, rot_mat, img.size(), cv::INTER_CUBIC);
cv::Size box_size = box.size;
if (box.angle < -45.)
std::swap(box_size.width, box_size.height);
cv::Mat cropped;
cv::getRectSubPix(rotated, box_size, box.center, cropped);
cv::imshow("Cropped", cropped);waitKey(0);
imwrite("example5.jpg",cropped);
Mat cropped2=cropped.clone();
cvtColor(cropped2,cropped2,CV_GRAY2RGB);
Mat cropped3 = cropped.clone();
cvtColor(cropped3,cropped3,CV_GRAY2RGB);
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
cv:: findContours( cropped, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_TC89_KCOS, Point(0, 0) );
vector<vector<Point> > contours_poly( contours.size() );
vector<Rect> boundRect( contours.size() );
vector<Point2f>center( contours.size() );
vector<float>radius( contours.size() );
for( int i = 0; i < contours.size(); i++ )
{
approxPolyDP( Mat(contours[i]), contours_poly[i], 3, true );
}
vector<vector<Point> > validContours;
for (int 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 ...
your code is missing.
without mycode, how could you telling like that.... there no errors in my code.. it showing exception.,... did you get my problem '
without your code, we can't tell anything, so please add it to the question.
yaah, i posted my code now.. would you do any help now
Could it be a namespace issue? Have you tried removing the using namespace std and using namespace cv?
hello Mr.boaz001.. here i copied only main code okay.... im my program i put all these.. okay i will again put my entire code
my last suggestions are; (1) clean up the code, you don't really seem to understand what you need, I see includes of highgui.h and highgui.hpp and other unneeded includes, bad indentation and many inconsistencies. Also remove the using namespace std and using namespace cv, sooner or later you will regret this, I consider it bad practice. (2) why do you use 3.0.0, it is not even stable and still under development (3) I've cleaned your code and applied the suggestions from (1) and tested it at linux with version 2.4.5 -> works like a charm, i see three gui windows and some processing going on and no exceptions or access violations.
boaz001@i will try again sir,Thank you
Hi friends, i got edited again my code, this time i went for opencv 2.4.9 instead of 3.0.0 so but now im getting unhandled exception again at (first function of opencv in my program) imread(). So please guys try to understand me..
Please give suggession thank you...
Try double backslashes.