saving grayscale images as video in C++ - c++

In my code, there are created cv::Mat images whose format is CV_16UC1. I have a cv::VideoWriter object and insert created grayscale images into it. However, I have completely black video as a result. If I change the cv::Mat format to CV_8UC1 everything works as expected. Is there anything that I am missing?

Related

OpenCV create YUV420 or grayscale Mat object from YUYV pixelformat video frame

I am trying to process video frames from a Qt application. The input from the QML Camera is of format YUYV and I could not set it to something else like YUV420 by default. I need to capture the frame and create a Mat object of YUV420 or grayscale format.
I have tried cvtColor with the following codes and they all crashed during runtime -
COLOR_YUV2GRAY_YVY
COLOR_YUV2RGB_YUYV
COLOR_YUV2GRAY_YUYV
Any idea how I can do this?
So after playing around with the color conversion codes a bit this is the solution I found to be working for me.
cv::Mat img = cv::Mat(m_videoHeight, m_videoWidth, CV_8UC2, input->bits());
cv::Mat gray;
cv::cvtColor(img, gray, cv::COLOR_YUV2GRAY_YVYU);
This converts the given YUYV Mat object - img to a grayscale Mat object - gray. input->bits() refers to the first bit of the video frame buffer.

using opencv in iOS

I want to firstly choose a picture from my the local library, it shows on the UIImageView
and then I can use the opencv fuction UIImageToMat() to convert the UIImage to Mat and then I convert Mat to IPlImage to implement image processing algorithms(why I have to convert Mat to IplImage? Because most of my opencv image processing codes are based on IplImage).
So the choosen image shows on the UIImageView, if I want to do some image processing things, I'll first convert the UIImage to IplImage, process it and then convert IplImage to UIImage again. That's the way I use opencv to do image processing things.
But the problem is when I want to process the chosen picture from the local library, something strange happens:
the original picture is:
and then I want to use opencv to let some columns of this picture to be blue and some rows to be red, but the result is:
the implementation code is:
I don't know why this happens, the picture obviously lost many important features during the opencv image processing process.(I just want some columns to be blue and some rows to be red.).Why this happened?

How to create an image in opencv from a buffer that holds image in RGBA format

I have an image in a buffer that hold an image with format of RGBA. I want to create an openCV image from it (cv::Mat), but it seems that I can not do this.
The code that I have is this:
cv::Mat image=cv::Mat(cvSize(Width,Height), CV_8UC4,Buffer, cv::Mat::AUTO_STEP).clone();
The main problem is that since OpenCV is saving its images in BGRA format, and the buffer has image in RGBA format, the colours of generated image is not correct.
I know that I can convert the image to BGRA format, but is there any way that I can create an image in OpenCV that holds images in RGBA format?
You can use cvtColor to convert the Mat from RGBA to BGRA. And if you will do it right after creation of the "original" Mat from the buffer, you need not to use clone(), as anyway the new buffer will be allocated for the converted image.
cv::Mat image=cv::Mat(cvSize(Width,Height), CV_8UC4, Buffer, cv::Mat::AUTO_STEP);
cv::Mat converted;
cvtColor(image, converted, CV_RGBA2BGRA);
you create an image in RGBA format with your code, that's no problem.
The problem is that OpenCV assumes BGR(A) images when saving the image or using openCV methods.
Maybe you can use special libraries (png lib, jpeg lib etc) to save images directly in RGB(A) format, instead. OpenCV really is about BGR for displaying or saving the images, since no color model is assigned to the images (it's just a x bit 3/4 channel image, whatever color model is used must be maintained by the user).

Bitmap to Iplimage conversion produce wrong color

i am trying to convert a bitmap to opencv image i am using pinvoke by sending the Bitmap.Scan0 and in the unmanaged side i am creating iplimage by using the byte array the solution is seems to work but not always ! only if i created the image on my pc using my grahpic device but it fails on another the image colors seems to be not right and the image is shifted i am suspecting that i should transfer the bitmap to DIB how can this done ?
IplImage* image= cvCreateImage(cvSize(width,height),depth,3)
memcpy(image->imageData,(uchar*)(bitmap),width*height*3);
IplImage stores pixels in the BGR order while HBITMAP expects them to be RGB.
Luckely, for you, the whole IplImage can be converted from BGR to RGB with:
cvCvtColor(image, image, CV_BGR2RGB);

OpenCV - VideoWriter produces a video with a "repeated" image

I'm trying to process each frame in a pair of video files in OpenCV and then write the resulting frames to an output avi file. Everything works, except that the output video file looks strange: instead of one solid image, the image is repeated three times and horizontally compressed so all three copies fit into the window. I suspect there is something going wrong with the number of channels the writer is expecting, but I'm giving it 8-bit single channel images to write. Below are the setting with which I'm initializing my videowriter:
//Initialize the video writer
CvVideoWriter *writer = cvCreateVideoWriter("out.avi",CV_FOURCC('D','I','V','X'), 30, frame_sizeL, 0);
Has anyone encountered this strange output from the openCV videowriter before? I've been checking the resulting frames with cvSaveImage just to see if somehow my processing step is creating the "tripled" image, but it's not. It's only when I write to the output avi with cvWriteFrame that the image gets "tripled" and compressed.
Edit: So I've discovered that this only happens when I attempt to write single channel images using write frame. If I write 3 channel 8-bit RGB images, the output video turns out fine. Why is it doing this? I am correctly passing "0" for the color argument when initializing the CvVideoWriter, so it should be expecting single channel images.
In the C++ version you have to tell cv::VideoWriter that you are sending a single channel image by setting the last paramter "false", are you sure you are doing this?
edit: alternatively you can convert a greyscale image to color using cvtColor() and CV_GRAY2RGB