Ask Your Question

Conversion of C API to C++ API problem [closed]

asked 2018-06-02 02:11:32 -0500

krshrimali gravatar image

updated 2018-06-02 02:15:52 -0500

I have the following lines of code in C API :

  IplImage *imdist_scaled = cvCreateImage(cvSize(orig_bw->width/pow((double)2,itr_scale-1), orig_bw->height/pow((double)2,itr_scale-1)), IPL_DEPTH_64F, 1);
    cvResize(orig_bw, imdist_scaled,CV_INTER_CUBIC);

And I've converted it into C++ API as :

Mat imdist_scaled(Size(orig_bw.cols/cv::pow((double)2, itr_scale-1), orig_bw.rows/pow((double)2, itr_scale-1)), CV_64F, 1);

  resize(orig_bw, imdist_scaled, imdist_scaled.size(), 0, 0, INTER_CUBIC); // INTER_CUBIC
  imdist_scaled.convertTo(imdist_scaled, CV_64FC1); // if not done, then it shows error in later steps
  // depth == 5 || 6 check error in pow function

In first case, when I try to show the image to the screen (imdist_scaled) - it shows me resized image. But in the second case, when I do the same, it shows a blank white image.

When I check the output without using convertTo command, then it works but in the later steps it says depth error, and thus I've converted it to 64F depth, which unfortunately leads it to be a blank white image.

Also note that I've verified orig_bw.cols, orig_bw.rows etc. have correct outputs.

Here orig_bw is a sample image I'm taking.

Why would that be?

edit retag flag offensive reopen merge delete

Closed for the following reason duplicate question by krshrimali
close date 2018-06-02 02:25:40.397720


1 answer

Sort by ยป oldest newest most voted

answered 2018-06-02 02:30:33 -0500

berak gravatar image

resize() will reallocate your dst image, if the src depth is not the same, so using CV_64F is a bad idea. maybe the (much simpler) :

Size dst_siz(orig_bw.cols/cv::pow((double)2,itr_scale-1),orig_bw.rows/pow((double)2,itr_scale-1));
Mat imdist_scaled;
resize(orig_bw, imdist_scaled, dst_siz, 0, 0, INTER_CUBIC);

which unfortunately leads it to be a blank white image.

imshow() assumes floating point images to be in the [0..1] range, while CV_8U images are in [0..255], so any value larger than 1 will be white.

you can apply a scaling factor with convertTo():

imdist_scaled.convertTo(imdist_scaled, CV_64FC1, 1.0/255);

and then your image will look ok, with imshow(), but you have to carefully read the original code, and see, if scaling down the image to [0..1] is legit for the further calculation !!

again, maybe it's better, to only scale a local copy for viewing:

imshow("win", img/255);
edit flag offensive delete link more


Thanks @berak. Have one more doubt, in functions, there is IplImage* object being passed. I'm passing Mat object then. How should I edit to make sure that it make changes to the image object not just locally?

Like, how should I use Mat& with it.

krshrimali gravatar imagekrshrimali ( 2018-06-02 02:38:22 -0500 )edit

sorry, but that's impossible to say, without any context.

but sure, you should pass Mat & , IF you plan to modify it, and const Mat & if you don't.

berak gravatar imageberak ( 2018-06-02 02:55:30 -0500 )edit

Actually the source code is too long ( - Am trying to convert it to C++ API.

But I'll try and do that what you suggested.

krshrimali gravatar imagekrshrimali ( 2018-06-02 04:05:40 -0500 )edit

If I want to return the modified image, I can return that image and store in another function, right?

Mat img_new = function_(image, params);

Like this^ But it doesn't work. Passing & is affecting other functions actually. Do you think doing the above way is wrong somewhere?

krshrimali gravatar imagekrshrimali ( 2018-06-02 04:06:59 -0500 )edit

yea, that's terribly old. rather try to capture the idea behind it, than messing with code from 2011

opencv has easy to use PCA and SVM classes builtin, nowadays.

berak gravatar imageberak ( 2018-06-02 04:12:57 -0500 )edit

Well I have updated the code, but don't know why the outputs of both the codes are different. Mind if I share with you somewhere? If you can check.

krshrimali gravatar imagekrshrimali ( 2018-06-02 04:23:25 -0500 )edit

i'll try , but don't expect too much (i have a meal for a dozen persons to prepare, too....)

what are you actually interested in there ? finding matching dresses ? or brisque features ? (also here

berak gravatar imageberak ( 2018-06-02 04:27:02 -0500 )edit

Ahh, I get that :D That's really a lot of work.

Well, yes, brisque features you linked. I'll upload it on GitHub soon. No worries, if I have any specific problem, I'll ask it here. Thanks.

krshrimali gravatar imagekrshrimali ( 2018-06-02 04:56:14 -0500 )edit

I'll upload it on GitHub soon.


berak gravatar imageberak ( 2018-06-03 05:40:50 -0500 )edit


Please also note that looks like I've to retrain the model, as it has to be in .yml format for using with OpenCV :'( Just too much work. :D

krshrimali gravatar imagekrshrimali ( 2018-06-03 06:41:41 -0500 )edit

Question Tools

1 follower


Asked: 2018-06-02 02:11:32 -0500

Seen: 201 times

Last updated: Jun 02 '18