Ask Your Question
2

How does cv::randu works on images

asked 2018-08-31 09:17:14 -0500

infoclogged gravatar image

I was expecting a black image from the following code, but instead get a mix of black and white. Can someone explain what is hapenning?

int main(int argc, char *argv[]) {
    cv::Size image_size(1200,200);

    cv::Mat rectangle(image_size, CV_8UC3);

    cv::randu(rectangle, 0, 0);
    cv::imshow("rectangle", rectangle);
    cv::waitKey(0);
}
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
3

answered 2018-08-31 14:11:54 -0500

LBerger gravatar image

updated 2018-09-01 03:21:30 -0500

berak gravatar image

Bug ? Post an issue and you can give this code as example

        cv::Mat imgf(5, 5, CV_32FC3);
        cv::randu(imgf, 0, 0);
        cout << imgf << "\n";
        cout << "******************************\n";

        cv::Mat img(5, 5, CV_8UC3);
        cv::randu(img, 0, 0);
        cout << img << "\n";
        cout << "******************************\n";

        cv::randu(img, 0, 1);
        cout << img << "\n";
        cout << "******************************\n";

results :

[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0;
 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0;
 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0;
 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0;
 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
******************************
[255, 255, 255,  81, 255, 255, 255,  94,   0,   0,   0,   0, 255, 255, 255;
  82,   0,   0,   0,   0, 255, 255, 255,  62, 255, 255, 255,  96,   0,   0;
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0, 255;
 255, 255,  26, 255, 255, 255,  12, 255, 255, 255,   6,   0,   0,   0,   0;
 255, 255, 255,  71,   0,   0,   0,   0,   0,   0,   0,   0, 255,   0, 255]
******************************
[  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0;
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0;
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0;
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0;
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0]
******************************

I think error is here

int idiff = ip[j][0] = cvFloor(b) - ip[j][1] - 1;

cvFloor(b) - ip[j][1] - 1 should be checked and when value is negative set to 0 or may be check param1 arg must be < param2arg

(https://github.com/opencv/opencv/blob...)

edit flag offensive delete link more

Comments

2

thanks for your answer. Would be great if you could notify the developers, since I am new to opencv.

infoclogged gravatar imageinfoclogged ( 2018-09-01 06:20:58 -0500 )edit
1

I can do it myself but you just need to create a github account, create an issue https://github.com/opencv/opencv/issues and copy code in my answer in the issue: that's opensource !

New opencv user or developer are welcome

LBerger gravatar imageLBerger ( 2018-09-01 06:59:31 -0500 )edit
LBerger gravatar imageLBerger ( 2018-09-03 07:32:13 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2018-08-31 09:17:14 -0500

Seen: 100 times

Last updated: Sep 01 '18