Difference in BGR values between C++ and Python

asked 2020-07-31 06:42:47 -0500

tkEAB gravatar image

updated 2020-07-31 07:55:22 -0500


I'm using OpenCV in C++ and Python and compare the results. I noticed that BGR values slightly differ (+/- 1, sometimes +/- 2) between those two languages after reading the same frame from the same video. OpenCV in Python should actually be using C++ codes and so I'm not sure why the values differ. How can I get identical BGR values?

Thanks in advance.

edit retag flag offensive close merge delete


hmm, how do you measure that ?

berak gravatar imageberak ( 2020-07-31 07:12:20 -0500 )edit

Thanks for the reply. I simply checked BGR values both in C++ and Python and compared the values at each pixel. ' Oh, I realized that I said "the same frame from a webcam." That was incorrect. I should have said "the same frame from the same video." I made that edit in my original question.

tkEAB gravatar imagetkEAB ( 2020-07-31 07:53:57 -0500 )edit

so the usual: opencv versions, how did you iinstall the python one ? which opencv backend is used to play the video ?

berak gravatar imageberak ( 2020-07-31 09:51:29 -0500 )edit

Thanks. Here is the information about OpenCV on my computer.

Python The OpenCV version is I installed it using Anaconda with Python 3.7.0 (i.e., "pip install opencv-python")

I play the video with the following codes:

video = cv2.VideoCapture(video_filepath) frame = video.read()

C++ The OpenCV version is 3.4.2. I built it using CMake.

I play the video with the following codes:

cv::VideoCapture video(video_filepath); cv::Mat frame; video >> frame;

I'm using Ubuntu 18.04 LTS.

tkEAB gravatar imagetkEAB ( 2020-08-01 01:25:23 -0500 )edit

3.4.2 <-> 4.2.0 -- that's quite a difference, no ? it'll probably use different FMPEG versions to read video

berak gravatar imageberak ( 2020-08-01 01:31:21 -0500 )edit

Following your suggestion, I uninstalled FFMPEG and OpenCV 3.4.2. Then I installed FFMPEG and, this time, OpenCV 4.2.0. More specifically, cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_FFMPEG=ON. But there was no change in the BGR values in C++.

I also checked the information about FFMPEG:

Python FFMPEG: YES avcodec: YES (58.65.103) avformat: YES (58.35.101) avutil: YES (56.38.100) swscale: YES (5.6.100) avresample: NO

C++ libavutil: 56.57.100 libavcodec: 58.98.100 libavformat: 58.49.100 libavdevice: 58.11.101 libavfilter: 7.87.100 libswscale: 5.8.100 libswresample: 3.8.100

tkEAB gravatar imagetkEAB ( 2020-08-01 07:48:39 -0500 )edit

ah, sorry for being misleading, on windows, opencv comes with it's own ffmpeg.

however i don't understand, why your ffmpeg information differs here

berak gravatar imageberak ( 2020-08-01 08:01:03 -0500 )edit

Actually, I'm using Ubuntu 18.04 LTS, not Windows. I downloaded FFMPEG with "git clone git://source.ffmpeg.org/ffmpeg.git ffmpeg" before cmake for for OpenCV whereas I I did not explicitly download FFMPEG for opencv-python -- maybe it came with opencv-python.

tkEAB gravatar imagetkEAB ( 2020-08-02 02:56:52 -0500 )edit