Ask Your Question
0

MSER Sample in OpenCV 2.4.2 on Visual Studio 2012

asked 2012-08-11 12:35:27 -0600

I'm running OpenCV 2.4.2, on Visual Studio 2012. I want to use MSER to detect text in images. I started off with the sample MSER sample program provided in the OpenCV directory,

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>

using namespace cv;
using namespace std;

static const Vec3b bcolors[] =
{
    Vec3b(0,0,255),
    Vec3b(0,128,255),
    Vec3b(0,255,255),
    Vec3b(0,255,0),
    Vec3b(255,128,0),
    Vec3b(255,255,0),
    Vec3b(255,0,0),
    Vec3b(255,0,255),
    Vec3b(255,255,255)
};

int main( int argc, char** argv )
{
    string path;
    Mat img0, img, yuv, gray, ellipses;

    img0 = imread( argc != 2 ? "puzzle.png" : argv[1], 1 );
    if( img0.empty() )
    {
        if( argc != 2 )
            cout << "\nUsage: mser_sample <path_to_image>\n";
        else
            cout << "Unable to load image " << argv[1] << endl;
        return 0;
    }

    cvtColor(img0, yuv, COLOR_BGR2YCrCb);
    cvtColor(img0, gray, COLOR_BGR2GRAY);
    cvtColor(gray, img, COLOR_GRAY2BGR);
    img.copyTo(ellipses);

    vector<vector<Point> > contours;
    double t = (double)getTickCount();
    MSER()(yuv, contours);
    t = (double)getTickCount() - t;
    printf( "MSER extracted %d contours in %g ms.\n", (int)contours.size(),
           t*1000./getTickFrequency() );

    // draw mser's with different colors
    for( int i = (int)contours.size()-1; i >= 0; i-- )
    {
        const vector<Point>& r = contours[i];
        for ( int j = 0; j < (int)r.size(); j++ )
        {
            Point pt = r[j];
            img.at<Vec3b>(pt) = bcolors[i%9];
        }

        // find ellipse (it seems cvfitellipse2 have error or sth?)
        RotatedRect box = fitEllipse( r );

        box.angle=(float)CV_PI/2-box.angle;
        ellipse( ellipses, box, Scalar(196,255,255), 2 );
    }

    imshow( "original", img0 );
    imshow( "response", img );
    imshow( "ellipses", ellipses );

    waitKey(0);
}

When I build and run this code, I get the following output after which the application stops responding:

 MSER extracted 1584 contours in 1748.41 ms.
 OpenCV Error: Assertion failed (points.checkVector(2) >= 0 && (points.depth() == 
 CV_32F || points.depth() == CV_32S)) in unknown function, file 
 ..\..\..\src\opencv\modules\imgproc\src\contours.cpp, line 2019

Any idea what exactly the problem is? I am totally lost!

I've tried commenting out everything inside the outer for loop, and it seems that the problem lies with the RotatedRect and fitEllipse portions. I'm not completely sure though.Any help would be much appreciated!

edit retag flag offensive close merge delete

3 answers

Sort by ยป oldest newest most voted
1

answered 2013-02-03 08:58:54 -0600

Hongbo Miao gravatar image

updated 2013-02-03 09:16:27 -0600

Hi, I tried your code before rebuilding OpenCV by CMake. I got the same problem.

image description

This is because OpenCV does not work well with Visual Studio 2012, you have to use CMake to rebuild OpenCV. After that the codes work well.

Please follow this tutorial, good luck.

http://answers.opencv.org/question/6495/visual-studio-2012-and-rtlfreeheap-error/#6603

edit flag offensive delete link more
0

answered 2013-02-01 13:37:27 -0600

Kay Sarraute gravatar image

updated 2013-02-01 13:40:36 -0600

  1. Change your platform toolset to Visual Studio 2010. (Configuration Properties -> General -> Platform Toolset)

  2. Make sure you're using the binaries in opencv\build\x{86|64}\vc10

The binaries in vc10 were built with VS 2010 and are incompatible with VS 2012. OpenVC doesn't ship with a native VS 2012 build yet.

edit flag offensive delete link more
0

answered 2012-08-13 03:25:55 -0600

Daniil Osokin gravatar image

updated 2012-08-13 03:26:37 -0600

Hi! As I see, you can use this code for your problem:

vector<vector<Point> >::const_iterator contour_it = contours.begin();
for( ; contour_it != contours.end(); ++contour_it )
{
   RotatedRect box = fitEllipse(Mat(*contour_it));
   ...
} 

edit flag offensive delete link more

Question Tools

Stats

Asked: 2012-08-11 12:35:27 -0600

Seen: 2,355 times

Last updated: Feb 03 '13