简体   繁体   中英

OpenCV has different RGB values from Matlab?

I am trying to transfer one matlab project to C++ code. However, when I trying to read an mp4 video by frame, the RGB values for each pixels is very different from Matlab. Does it mean OpenCV used a different RGB value representation? If so, how can I change the OpenCV value to Matlab? Otherwise I cannot verify my implementation are right by checking values.

For example: I am trying to check the point(0,0) values in OpenCV and Matlab. OpenCV gives the following result: blue=106 green=105 red=102 However, in Matlab, the result is: blue=85 green=86 red=83 I tried to get the RGB value in point(0,0) for every 200 frames which is point(1,1) in Matlab.

The C++ code to get RGB value in OpenCV are:

Mat img;
number = 0;
VideoCapture cap(filename_input_video);
if(!cap.isOpened()) {
    printf("No video to Read!\n");
    return -1;
}

for( ; ; ) {
    cap >> img;
    if(img.empty())
        break;
    number++;

    for(int i=0; i<img.rows; i++) {
        for(int j=0; j<img.cols; j++) {
            int blue = img.at<Vec3b>(i, j)[0];
            int green = img.at<Vec3b>(i, j)[1];
            int red = img.at<Vec3b>(i, j)[2];

            if(number == 200 && i==0 && j==0) {
                printf("blue=%d green=%d red=%d", blue, green, red);
            }
        }
    }

    if(number == 200) {
        number = 0;
    }
}

The Matlab code is:

OBJ = VideoReader(filename_source);
fBlock = 200;
nFrame = get(OBJ, 'NumberOfFrames');
nBlock = ceil(nFrame / fBlock);

for iBlock = 1:nBlock
    display(['Processing video 1 block #' num2str(iBlock) '...']);
    start_index = (iBlock-1)*fBlock+1;
    end_index = min(iBlock*fBlock, nFrame);

    vSource = read(OBJ,[start_index end_index]);
    display(['red ' num2str(vSource(1,1,1,200))]);
    display(['green ' num2str(vSource(1,1,2,200))]);
    display(['blue ' num2str(vSource(1,1,3,200))]);

How should i fix this problem?

To verify the difference, you should compare RGB value of single image read from disc. Reading identical values here shows your code is probably fine and there is a difference in decoding.

What is probably happening : If you read frame/image captured from video, there can be difference as the video decoder can be different for OpenCV(default is ffmpeg) and MATLAB. Different decoder can handle some events/errors differently and there is no guarantee of identical decoding.

Suggested Solution :

1) Same decoder - If you need both tools to be identical in result, use same decoder for both. Either change decoder for OpenCV or for MATLAB. If you google, you will find few article on how to do this. This and this can be helpful.

2) Same video - Use any decoder(I prefer ffmpeg) to convert video to raw format first. Now you can use it on both tool without fear of diff ;). Here is a command to get raw from compresed:

`c:/> ffmpeg -i compressed_or_original_video.avi -vcodec rawvideo raw_converted_video.avi`

No! it does not! you see different result because C++ array indexing starts from zero while matlab/octave indexing starts from 1.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM