Ask Your Question
-1

Unhandled exception at imwrite(),findcontours() in visual studio 2008 with opencv 3.0.0 alpha version

asked 2014-09-25 01:40:30 -0600

govardhana padavala gravatar image

updated 2014-09-30 07:00:31 -0600

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 ...
(more)
edit retag flag offensive close merge delete

Comments

1

your code is missing.

berak gravatar imageberak ( 2014-09-25 01:41:29 -0600 )edit

without mycode, how could you telling like that.... there no errors in my code.. it showing exception.,... did you get my problem '

govardhana padavala gravatar imagegovardhana padavala ( 2014-09-25 05:21:13 -0600 )edit

without your code, we can't tell anything, so please add it to the question.

berak gravatar imageberak ( 2014-09-25 05:22:54 -0600 )edit

yaah, i posted my code now.. would you do any help now

govardhana padavala gravatar imagegovardhana padavala ( 2014-09-25 06:08:20 -0600 )edit
1

Could it be a namespace issue? Have you tried removing the using namespace std and using namespace cv?

boaz001 gravatar imageboaz001 ( 2014-09-25 06:55:19 -0600 )edit

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

govardhana padavala gravatar imagegovardhana padavala ( 2014-09-25 23:20:07 -0600 )edit
1

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 gravatar imageboaz001 ( 2014-09-26 11:09:10 -0600 )edit

boaz001@i will try again sir,Thank you

govardhana padavala gravatar imagegovardhana padavala ( 2014-09-29 23:10:56 -0600 )edit

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...

govardhana padavala gravatar imagegovardhana padavala ( 2014-09-30 00:13:15 -0600 )edit

Try double backslashes.

boaz001 gravatar imageboaz001 ( 2014-09-30 02:52:01 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2014-09-30 10:35:25 -0600

boaz001 gravatar image

Maybe you can use this:

#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"

void main()
{
  cv::Mat img = cv::imread("D:\\mahi images\\equation.jpg", 0);
  cv::imshow("", img);
  cv::waitKey(0);

  cv::Size size(3, 3);
  cv::GaussianBlur(img, img, size, 0);
  cv::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 (unsigned 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::Size2f 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);
  cv::waitKey(0);
  cv::imwrite("example5.jpg", cropped);

  cv::Mat cropped2 = cropped.clone();
  cv::cvtColor(cropped2, cropped2, CV_GRAY2RGB);

  cv::Mat cropped3 = cropped.clone();
  cv::cvtColor(cropped3, cropped3, CV_GRAY2RGB);

  std::vector<std::vector<cv::Point> > contours;
  std::vector<cv::Vec4i> hierarchy;

  cv::findContours(cropped, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_TC89_KCOS, cv::Point(0, 0));

  std::vector<std::vector<cv::Point> > contours_poly(contours.size());
  std::vector<cv::Rect> boundRect(contours.size());
  std::vector<cv::Point2f> center(contours.size());
  std::vector<float> radius(contours.size());

  for (unsigned int i = 0; i < contours.size(); ++i)
  { 
    cv::approxPolyDP(cv::Mat(contours.at(i)), contours_poly.at(i), 3, true);
  }

  std::vector<std::vector<cv::Point> > validContours;
  for (unsigned int i = 0; i < contours_poly.size(); ++i)
  {
    cv::Rect r = cv::boundingRect(cv::Mat(contours_poly.at(i)));
    if (r.area() < 100)
    {
      continue;
    }
    bool inside = false;
    for (unsigned int j = 0; j < contours_poly.size(); ++j)
    {
      if (j == i)
      {
        continue;
      }

      cv::Rect r2 = cv::boundingRect(cv::Mat(contours_poly.at(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 == true)
    {
      continue;
    }
    validContours.push_back(contours_poly.at(i));
  }

  // Get bounding rects
  for (unsigned int i = 0; i < validContours.size(); ++i)
  {
    boundRect.at(i) = cv::boundingRect(cv::Mat(validContours.at(i)));
  }

  // Display
  cv::Scalar color(0, 255, 0);
  for(unsigned int i = 0; i < validContours.size(); ++i)
  {
    if(boundRect.at(i).area() < 100)
    {
      continue;
    }
    cv::drawContours(cropped2, validContours, i, color, 1, 8, std::vector<cv::Vec4i>(), 0, cv::Point());
    cv::rectangle(cropped2, boundRect.at(i).tl(), boundRect.at(i).br(), color, 2, 8, 0);
  }

  //cv::imwrite("example6.jpg", cropped2);
  cv::imshow("Contours", cropped2);
  //extractContours(cropped3, validContours); I don't have the implementation of extractContours
  cv::waitKey(0);
}
edit flag offensive delete link more

Comments

thank you for your interest, but im getting same error like "CXX0030: Error: expression cannot be evaluated " , at imread("equation.jpg",0); staring function.

once again thank you, please help me

govardhana padavala gravatar imagegovardhana padavala ( 2014-09-30 23:25:59 -0600 )edit

i felt this is compatibility problem? what s ur openion mr.boaz001

govardhana padavala gravatar imagegovardhana padavala ( 2014-09-30 23:27:38 -0600 )edit

Is the path to the image in your IDE or debugger correctly set or do you run the executable from the same directory as the equation.jpg is at. It probably cannot locate the image. As a test you could put your image at C: root directory and load it with cv::imread("C:\\equation.jpg", 0) (note: use two backslashes after C:). And what exactly do you mean with 'compatibility problem', and what are your reasons to think so?

boaz001 gravatar imageboaz001 ( 2014-10-01 02:36:25 -0600 )edit

compatibility means, i have installed opencv lib files successfully (because while declaring opencv headerfiles i didnt get any error or warning messages) and i have declared the imread function like cv::Mat img = cv::imread("D:\images\equation.jpg",0); so problem in these two statements then i felt here system downgrade problem..

govardhana padavala gravatar imagegovardhana padavala ( 2014-10-01 05:46:38 -0600 )edit

Question Tools

Stats

Asked: 2014-09-25 01:40:30 -0600

Seen: 1,470 times

Last updated: Sep 30 '14