Ask Your Question
0

strange result of transformation of frame of VideoCapture

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

Aiyyaa gravatar image

I have a code

VideoCapture cap("test.mp4"); 
 if(!cap.isOpened()){
    cout << "Error opening video stream or file" << endl;
    return -1;
  }
while(1){

        Mat frame; 

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

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

    // Press  ESC on keyboard to  exit
    char c = (char)waitKey(1);
    if( c == 27 ) 
      break;
  }
  cap.release();

and function test()

Mat test(Mat frame){
for(int i = 0; i < frame.rows; i++){
for(int j = 0; j < frame.cols; j++){
frame.at<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

Comments

2
sturkmen gravatar imagesturkmen ( 2018-09-02 15:43:17 -0600 )edit
1

main (noob) problem:

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

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

1 answer

Sort by ยป oldest newest most voted
3

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

berak gravatar image

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

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 img.at<Vec3b> , not img.at<uchar>

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

img.setTo(Scalar(255,255,255));

or even:

img = Scalar::all(255);
edit flag offensive delete link more

Question Tools

1 follower

Stats

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

Seen: 402 times

Last updated: Sep 03 '18