OpenCV version : 4.5.2-dev
ffmpeg version : 4.3.2
os version : osx 11.2.3 Big Sur
I’m trying to use OpenCV combined to ffmpeg/ffplay for streaming video.
So far, I tried to encode the frame using cv::imencode() but ffplay cannot read the output data
The C++ code:
Mat frame;
while (waitKey(1) < 0){
video.read(frame);
// Processing frame
// output to ffmpeg / ffplay
std::vector<uchar> buffer;
imencode(".jpg", frame, buffer);
for (auto i = buffer.begin(); i != buffer.end(); ++i){
std::cout << *i;
}
}
Ok it was due to the input resolution set to ffplay. Now it is good but I still have issue with the color.
I know that the frame’s output of opencv should be in BGR24. But I still have weird color issue
first of all, you have to understand the difference between sending raw bitmap data, and sending a compressed image as a jpeg or png or something.
when you understand that, you should be able explain why you see “noise” in that first album you gave.
next… you should check the actual size and number of channels of the data in frame (after video.read(frame);)
it is probably 640 x 480 and you fixed that by changing the VideoCapture resolution.
third… you are probably sending BGR color data. check that it looks correct using imshow.
ffmpeg is told to expect bgr24 so this should result in a proper picture. it is not, so either you aren’t sending BGR24 or ffmpeg isn’t interpreting it as BGR24.
you haven’t shown your source code. it is probably causing this. show your source code.
Yes, indeed, it was due to the resolution. After changing the resolution I got the third image I sent.
→ But if you look closely to the third picture, the first pixel column in the begin of the frame should be located in the last pixel column of the frame. I don’t know why I got this behavior…
Yes, opencv is sending BGR color data. The output for imshow is correct:
But I find out weird behavior:
My code tree looks like this
├── src
—├── main.cpp
—└── detection
-----└── detection.hpp
the output of the code below works well only if I put it in the main.cpp.
If the code is running inside a class, the output will be as the third picture I sent above.
Im trying to understand why but for now I didn’t find it…
Do you have the same behavior if you write the frame to ouput inside a class ?
In my main.cpp, I printed out some data and it affects obviously ffplay which takes that dummy datas into account in his stdin and it revert everything…