saving frames is returning gray scale image
AnsweredI am using below code to store individual video frame. It's not stored as color (RGB) image. it's stored as a Gray scale Image. convert color is also not working. How to fix this?
cv::Mat img_color;
cv::Mat img(
videoFrame->height(),
videoFrame->width(),
CV_8UC1, //< BGR color space (default for OpenCV)
(void*) videoFrame->data(0),
(size_t) videoFrame->lineSize(0));
cv::cvtColor(img, img_color, CV_GRAY2RGB);
cv::imwrite(file_path,img_color);
Any help would be appreciable.
-
Hello Mohamed,
Could you please explain in more detail what are doing? What is you objective? What is this piece of code from?
-
Hello Andrey,
The above code snippet I have added at pushUncompressedVideoFrame function in device agent CPP file.DeviceAgent::pushUncompressedVideoFrame(const IUncompressedVideoFrame* videoFrame)
I want to extract images from camera. The above code snippet export it as image. But it's in gray-scale. I want this as a color image.
-
Hello Mohamed,
videoFrame parameter of pushUncompressedVideoFrame refers the actual frame data the Server receives from a camera, i.e. if there is a gray-scale image in the frame data, it means the camera has sent such a frame to the Server and it passed the frame to the plugin.
Please, check your camera settings.
-
Muhamed,
The code you posted and cv::imwrite(file_path,img_color); seems to be OpenCV specific and has no relation to Nx Witness SDK.
I'm afraid I can't help you with OpenCV programming at the moment. Try to consult the opencv documentation or ask in the forum https://answers.opencv.org/questions/
-
Hello Andrey,
I have raised the question on stackoverflow. https://stackoverflow.com/questions/61221880/image-export-is-returning-grey-scale-image-in-opencv/61222004?noredirect=1#comment108311334_61222004.It's not working as I expected.
The above code snippet I took out from this post. https://support.networkoptix.com/hc/en-us/community/posts/360037395653-Convert-IUncompressedVideoFrame-to-OpenCV-MatI hope this is what NX team also using to save it as a file as mentioned by Roman Inflianskas.
i am really struggling a lot to convert this into colour image. I hope you understand the issue. please help me to solve the issue.
NOTE: The above post not answered my problem
-
Hello Mohamed,
This is a good post on your issue. It lead me to the root cause of your problem, I hope.
As my colleague has mentioned "The server will transcode video into a requested pixel format,"
That means that a plugin can request from the Server needed frame format. It is done in Engine manifest in capabilities section.
Let's have a look at sample plugin engine.cpp.
std::string Engine::manifestString() const
{
// Ask the Server to supply uncompressed video frames in YUV420 format (see
// https://en.wikipedia.org/wiki/YUV).
//
// Note that this format is used internally by the Server, therefore requires minimum
// resources for decoding, thus it is the recommended format.
return /*suppress newline*/ 1 + R"json(
{
"capabilities": "needUncompressedVideoFrames_yuv420|deviceDependent"
}
)json";
}There is needUncompressedVideoFrames_yuv420. The yuv420 specifies the frame type the Server will feed to the plugin.
The yuv420 frame has 3 (three) planes.
0 - black-n-white frame
1,2 - differential frames, using which you can restore RGB frame.
In your code your are referencing and using 0-plane
(void*) videoFrame->data(0),
That's is why you have a black-n-white picture.
Depending on your plugin objective you can either restore the RGB from yuv420 frame or request another frame time. In both cases it will increase the load, because either your plugin or the Server will transcode frames.
The list of possible frame type can be found in pixel_format.cpp of Metadat SDK.
INIT_PIXEL_FORMAT_DESCRIPTOR(yuv420, 3, 8, 2, 2);
INIT_PIXEL_FORMAT_DESCRIPTOR(argb, 1, 32, 1, 1);
INIT_PIXEL_FORMAT_DESCRIPTOR(abgr, 1, 32, 1, 1);
INIT_PIXEL_FORMAT_DESCRIPTOR(rgba, 1, 32, 1, 1);
INIT_PIXEL_FORMAT_DESCRIPTOR(bgra, 1, 32, 1, 1);
INIT_PIXEL_FORMAT_DESCRIPTOR(rgb, 1, 24, 1, 1);
INIT_PIXEL_FORMAT_DESCRIPTOR(bgr, 1, 24, 1, 1); -
Hello Andrey,
I tried instead of using
(void*) videoFrame->data(0),
I modifed,
(void*) videoFrame->data(1),
Still It's not working.
Could you tell me where exactly I have to specify frame type?Am I doing what you suggested in the previous post?
It's quite difficult understand what you have mentioned previously for me. -
Hello Mohamed,
(void*) videoFrame->data(1),
> Still It's not working.
It is expected. It would not give your color framed.
> Am I doing what you suggested in the previous post?
No, you are not. Please, read this article to get yourself familiar how YUV frames built. https://en.wikipedia.org/wiki/YUV
> It's quite difficult understand what you have mentioned previously for me.
Could you please specify what is not quite clear? I would give you more explanations.
What is your objective? What do you need color frame for? What are you going to do next with the frame?
-
Hello Andrey,
I tried the suggested solution, It didn't wok for `needUncompressedVideoFrames_rbg` but it gives color image for `
needUncompressedVideoFrames_rgb`. Eventhough frames are extracted as RGB image,
the actual colors of images are missing. i.e.,
Actual Image.CPP program produced (exported frame) image.
Any idea how to resolve the issue?
Please sign in to leave a comment.
Comments
14 comments