Ask Your Question

Opencv shows images totally white?

asked 2018-08-01 10:34:52 -0500

user8469759 gravatar image

updated 2018-08-01 11:11:36 -0500

Hi guys, can any of you tell me if there's a bug in the convertTo function?

With reference to this question, I'm running that code and I'm getting the pictures shown.

The code I'm running is this one:

int testImageAndGradients(int argc, char **argv) {
    if (argc != 2) {
        std::cout << "Need filename..." << std::endl;
        return 1;

    Mat im = imread(argv[1], CV_LOAD_IMAGE_GRAYSCALE);

    Mat imfloat, dx, dy;
    im.convertTo(imfloat, CV_32FC1);
    Mat u(imfloat.size(), imfloat.type());

    int & X = im.cols;
    int & Y = im.rows;

    Sobel(imfloat, dx, CV_32F, 1, 0, 3);
    Sobel(imfloat, dy, CV_32F, 0, 1, 3);

    Mat tmp, output(Size(3*X,Y),CV_8UC1);

    double minval, maxval;

    minMaxLoc(imfloat, &minval, &maxval);
    imfloat.convertTo(tmp, CV_8UC1, 255 / (maxval - minval), -minval);
    tmp.copyTo(output(Rect(0, 0, X, Y)));

    minMaxLoc(dx, &minval, &maxval);
    dx.convertTo(tmp, CV_8UC1, 255 / (maxval - minval), -minval);
    tmp.copyTo(output(Rect(X, 0, X, Y)));

    minMaxLoc(dy, &minval, &maxval);
    dy.convertTo(tmp, CV_8UC1, 255 / (maxval - minval), -minval);
    tmp.copyTo(output(Rect(2*X, 0, X, Y)));

    namedWindow("Img/Dx/Dy", WINDOW_AUTOSIZE);
    imshow("Img/Dx/Dy", output);

    return 1;

And my suspect is eventually a misuse of the method convertTo, but I can't figure out why.

Thank you.

edit retag flag offensive close merge delete


Why the downvote?

user8469759 gravatar imageuser8469759 ( 2018-08-01 10:51:45 -0500 )edit

May be you shoud read convertTo doc :

image description

LBerger gravatar imageLBerger ( 2018-08-01 10:53:55 -0500 )edit

I read the doc, and I don't get what am I missing.

user8469759 gravatar imageuser8469759 ( 2018-08-01 10:55:48 -0500 )edit

I think I got the problem... my beta argument in convertTo is wrong, I should scale it as well. I'll update the question with a better description, and I'll post an answer.

user8469759 gravatar imageuser8469759 ( 2018-08-01 11:02:00 -0500 )edit

Downvote Try to give a link on stackoverflow as a question...

beta = -255*minval / (maxval - minval)

LBerger gravatar imageLBerger ( 2018-08-01 11:02:37 -0500 )edit

I did figure at last...

user8469759 gravatar imageuser8469759 ( 2018-08-01 11:07:43 -0500 )edit

Did try to post an answer, but apparently I can't. But anyway yes, that was the issue... I should learn how to read a formula again.

user8469759 gravatar imageuser8469759 ( 2018-08-01 11:19:34 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2018-08-01 10:39:04 -0500

berak gravatar image

updated 2018-08-01 10:58:57 -0500

no, not a bug, more an expectation mismatch.

opencv expects floating point images in the [0..1] range for drawing, so if your image iss [0..255] it will show up all white.

depending on what you need in your calculations, you can either apply a scale factor with convertTo() or just scale(divide) it for drawing, like:

imshow("IM", image / 255);

(it's actually imshow(), which does some magic here (by pre-multiplying float imgs with 255, so you sometimes have to compensate that))

edit flag offensive delete link more


Hi, is this pointed out anywhere? But anyway if you run the code you can see that the convertTo doesn't actually do anything. Specifically it doesn't apply the normalization given by the last two arguments of my convertTo call.

user8469759 gravatar imageuser8469759 ( 2018-08-01 10:43:28 -0500 )edit

please show code to prove it.

berak gravatar imageberak ( 2018-08-01 10:56:34 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2018-08-01 10:34:52 -0500

Seen: 36 times

Last updated: Aug 01 '18