Ask Your Question

strange result of transformation of frame of VideoCapture

asked 2018-09-02 15:25:51 -0500

Aiyyaa gravatar image

I have a code

VideoCapture cap("test.mp4"); 
    cout << "Error opening video stream or file" << endl;
    return -1;

        Mat frame; 

    // Capture frame-by-frame 
    cap >> frame;

    // If the frame is empty, break immediately
    if (frame.empty())
    // Display the resulting frame    
    imshow( "Fr", test(frame ));

    // Press  ESC on keyboard to  exit
    char c = (char)waitKey(1);
    if( c == 27 ) 

and function test()

Mat test(Mat frame){
for(int i = 0; i < frame.rows; i++){
for(int j = 0; j < frame.cols; j++){<uchar>(i,j) = 250;
return frame;

I expected that this function will return a transformed frame, totally white. But instead, it returns me a frame where 1/3 of columns white: image description

if I transform code above

for(int j = 0; j < 2*frame.cols; j++){

instead of

for(int j = 0; j < frame.cols; j++){

I got this:

image description

and if I change code like this:

for(int j = 0; j < 3*frame.cols; j++){

I eventually got that I expected, white frame. image description

Why it works so weird? I have a loop on all rows and columns and yet I don't get totally white picture. If I use this function test() on usual picture, it returns white Mat. But with frame of VideoCapture result is different. Why?

edit retag flag offensive close merge delete


sturkmen gravatar imagesturkmen ( 2018-09-02 15:43:17 -0500 )edit

main (noob) problem:

you're using a "random access" operator to manipulate a whole image.

berak gravatar imageberak ( 2018-09-02 19:20:47 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2018-09-03 01:17:17 -0500

berak gravatar image

updated 2018-09-03 01:18:01 -0500

please do not abuse Mat::at to manipulate whole images, as it is slow and error-prone.

your image has 3 channels, thus you (would) have to use<Vec3b> , not<uchar>

but again, to set a whole image to white, rather use:


or even:

img = Scalar::all(255);
edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower


Asked: 2018-09-02 15:25:51 -0500

Seen: 33 times

Last updated: Sep 03