Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ifstream in text module not working #1

Closed
Neuroforge opened this issue Aug 17, 2016 · 6 comments
Closed

ifstream in text module not working #1

Neuroforge opened this issue Aug 17, 2016 · 6 comments

Comments

@Neuroforge
Copy link

Hello,

I am calling a function from OpenCV that uses ifstream in C++. ifstream doesn't seem to work when tried in isolation. This works from source, but your copy has tesseract bundled in for VS2015, any idea what could be the problem?

Ptr er_filter1 = createERFilterNM1(loadClassifierNM1("trained_classifierNM1.xml"), 8, 0.00015f, 0.13f, 0.2f, true, 0.1f);
But getting.....

OpenCV Error: Bad argument (Default classifier file not found!) in cv::text::ERClassifierNM1::ERClassifierNM1, file ~\opencv_contrib-3.1.0\modules\text\src\erfilter.cpp, line 1039

The offending line is.....

if (ifstream(filename.c_str()))
{
.. Stuff works ....
}
else
{
error!!
}
Everything worked in VS2013 but i have to switch to VS2015.

I cannot get ifstream to work at all, even though the file is located in the .exe folder itsself or project directory. Everywhere really.

It's like ifstream has stopped working during the switch to VS2015

https://github.com/opencv/opencv_cont...

@ghost
Copy link

ghost commented Aug 18, 2016

@Neuroforge I have this code:

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

#include  <vector>
#include  <iostream>

using namespace std;
using namespace cv;
using namespace cv::text;

vector<Mat> separateChannels(Mat& src) 
{
    vector<Mat> channels;
    //Grayscale images
    if (src.type() == CV_8U || src.type() == CV_8UC1) {
        channels.push_back(src);
        channels.push_back(255-src);
        return channels;
    }

    //Colored images
    if (src.type() == CV_8UC3) {
        computeNMChannels(src, channels);
        int size = static_cast<int>(channels.size())-1;
        for (int c = 0; c < size; c++)
            channels.push_back(255-channels[c]);
        return channels;
    }

    //Other types
    cout << "Invalid image format!" << endl;
    exit(-1);   
}

int main(int argc, const char * argv[])
{
    char* image = argc < 2 ? "easel.png" : argv[1];    
    auto input = imread(image);

    //Convert the input image to grayscale.
    //Just do Mat processed = input; to work with colors.
    Mat processed;
    cvtColor(input, processed, CV_RGB2GRAY);

    auto channels = separateChannels(processed);

    // Create ERFilter objects with the 1st and 2nd stage classifiers
    auto filter1 = createERFilterNM1(loadClassifierNM1("trained_classifierNM1.xml"),15,0.00015f,0.13f,0.2f,true,0.1f);
    auto filter2 = createERFilterNM2(loadClassifierNM2("trained_classifierNM2.xml"),0.5);

    //Extract text regions using Newmann & Matas algorithm
    cout << "Processing " << channels.size() << " channels..." << endl;
    vector<vector<ERStat> > regions(channels.size());
    for (int c=0; c < channels.size(); c++)
    {
        cout << "    Channel " << (c+1) << endl;
        filter1->run(channels[c], regions[c]);
        filter2->run(channels[c], regions[c]);      
    }    
    filter1.release();
    filter2.release();

    //Separate character groups from regions
    vector< vector<Vec2i> > groups;
    vector<Rect> groupRects;
    //erGrouping(input, channels, regions, groups, groupRects, ERGROUPING_ORIENTATION_HORIZ);
    erGrouping(input, channels, regions, groups, groupRects, ERGROUPING_ORIENTATION_ANY, "trained_classifier_erGrouping.xml", 0.5);

    // draw groups boxes    
    for (auto rect : groupRects)
        rectangle(input, rect, Scalar(0, 255, 0), 3);

    imshow("grouping",input);
    waitKey(0);
}

It's work for me.

cmd
window

@Neuroforge
Copy link
Author

Hello.

This is turning out to be a Visual Studio problem.

Still investigating, but i feel we can close this for now.

Anyone searching this problem.

  1. Stackoverflow suggests VS2015 has wrong working directory.
  2. Error may not exist in release mode.

@ghost
Copy link

ghost commented Aug 18, 2016

Hello, the problem is because you are using absolute paths.

Look this. I modified the example with absolute paths .
cmd2

This does not happen on relative paths.

@Neuroforge
Copy link
Author

Neuroforge commented Aug 18, 2016

I've tried both relative and full paths.

I have the files at each level of the project structure, and in the c drive root directory.

And i tried "C:\trained_classifierNM1.xml" then "trained_classifierNM1.xml"

Could this be a configuration in visual studio somewhere?

@ghost
Copy link

ghost commented Aug 18, 2016

this is my project:
https://github.com/cesardelgadof/text_detection

Before unrar opencvBinaries in C:/

@Neuroforge
Copy link
Author

Neuroforge commented Aug 18, 2016

Ok..... so that totally worked. This looks like some sort of folder permissions thing then.

I don't understand why it won't do that in my user directory projects.

Thank you. I will use your project as a template for now. Thank you.

Also, please maintain this project. OpenCV is a nightmare to build and get working with varying configs and builds. You should also perhaps think about releasing via NUGET.

Your hard work is appreciated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant