kcire arraveug
kcire arraveug

Reputation: 213

Converting wxString to std::string for OpenCv

Anybody knows why my application crash everytime the code was parsing this line:

deskew(filename); //filename string

My declaration is like this:

void deskew(std::string fname);

And then I also used this code to convert my wxString to string:

string fname = string(path.mb_str());

I just read it from the tutorial but it isn't working. And by the way, I'm using wxWidgets for my c++.

here is the body of deskew:

void DImage::deskew(string filename, unsigned int angle)
{

if (filename == "")
    return;
Mat img = imread(filename, CV_LOAD_IMAGE_GRAYSCALE);

vector<Point> points;
Mat_<uchar>::iterator it = img.begin<uchar>();
Mat_<uchar>::iterator end = img.end<uchar>();
for (; it != end; ++it)
  if (*it)
    points.push_back(it.pos());

RotatedRect box = minAreaRect(Mat(points));

Mat rot_mat = getRotationMatrix2D(box.center, angle, 1);

Mat rotated;
warpAffine(img, rotated, rot_mat, img.size(), INTER_CUBIC);

Size box_size = box.size;
if (box.angle < -45.)
  swap(box_size.width, box_size.height);
Mat cropped;
  getRectSubPix(rotated, box_size, box.center, cropped);

//imshow("Original", img);
//imshow("Output", rotated);
//imshow("Cropped", cropped);

imwrite("icons/DESKEW.png", cropped);
waitKey(0);
}

Here it is, I've just paste it all so all of you can see.

Upvotes: 0

Views: 4970

Answers (3)

Salva Savall
Salva Savall

Reputation: 43

I think you can load an image from wxString with

Mat src_img = imread(mywxstring.mb_str().data());

If it doesn't work, check that the image exists and that imread need \\ or / in the paths.

Upvotes: 0

ravenspoint
ravenspoint

Reputation: 20496

I notice that you have:

deskew(filename); //filename string

but you have

void DImage::deskew(string filename, unsigned int angle)

So what happened to the 'angle' parameter?

Upvotes: 0

Matt Phillips
Matt Phillips

Reputation: 9691

Use wxString::ToStdString(), i.e.

string fname = path.ToStdString();

Edit: Also, you don't use std:: consistently; if you are using namespace std; then you don't need it at all; otherwise std:: needs to prefix string and other STL types/functions wherever they occur.

Upvotes: 4

Related Questions