Convert from RGB to YUYV in OpenCV - c++

Is there a way to convert from RGB to YUYV (YUY 4:2:2) format? I noted that OpenCV has reverse operation, but not RGB to YUYV for some reason. Maybe someone can point to code which does that (even outside of OpenCV library)?
UPDATE
I found libyuv library which may work for this purpose by doing BGR to ARGB conversion and then ARGB to YUY2 format (hopefully this is the same as YUYV 4:2:2). But it doesn't seem to work. Do you happen to know what yuyv buffer dimensions/type should look like? What its stride?
To clarify YUYV and YUY2 are the same formats if it helps.
UPDATE 2
Here is my code of using libyuv library:
Mat frame;
// Convert original image im from BGR to BGRA for further use in libyuv
cvtColor(im, frame, CVX_BGR2BGRA);
// Actually libyuv requires ARGB (i.e. reverse of BGRA), so I swap channels here
int from_to[] = { 0,3, 1,2, 2,1, 3,0 };
mixChannels(&frame, 1, &frame, 1, from_to, 4);
// This is the most confusing part. Not sure what argb_stride suppose to be - length of a row in bytes or size of single value in the array?
const uint8_t* argb_data = frame.data;
int argb_stride = 8;
// Also it is not clear what size of yuyv frame should be since we duplicate one Y
Mat yuyv(frame.rows, frame.cols, CVX_8UC2);
uint8_t* yuyv_data = yuyv.data;
int yuyv_stride = 16;
// Do actual conversion
libyuv::ARGBToYUY2(argb_data, argb_stride, yuyv_data, yuyv_stride,
frame.cols, frame.rows);
// Then I feed yuyv_data to video stream buffer and see green or purple image instead of video stream.
UPDATE 3
Mat frame;
cvtColor(im, frame, CVX_BGR2BGRA);
// ARGB
int from_to[] = { 0,3, 1,2, 2,1, 3,0 };
Mat rgba(frame.size(), frame.type());
mixChannels(&frame, 1, &rgba, 1, from_to, 4);
const uint8_t* argb_data = rgba.data;
int argb_stride = rgba.cols*4;
Mat yuyv(rgba.rows, rgba.cols, CVX_8UC2);
uint8_t* yuyv_data = yuyv.data;
int yuyv_stride = width * 2;
int res = libyuv::ARGBToYUY2(argb_data, argb_stride, yuyv_data, yuyv_stride, rgba.cols, rgba.rows);

It appears that although method is called ARGBToYUY2 it requires BGRA order of channels (not reverse).

Related

Loading an array of pixel values in OpenCV

I have a 32-bit integer array containing pixel values of a 3450x3450 image I want to create a Mat image with. Tried the following:
int *image_array;
image_array = (int *)malloc( 3450*3450*sizeof(int) );
memset( (char *)image_array, 0, sizeof(int)*3450*3450 );
image_array[0] = intensity_of_first_pixel;
...
image_array[11902499] = intensity_of_last_pixel;
Mat M(3450, 3450, CV_32FC1, image_array);
and upon displaying the image I get a black screen. I should also note the array contains a 16-bit grayscale image.
I guess you should try to convert the input image, which I assume is in RGB[A] format using:
cv::Mat m(3450, 3450, CV_8UC1, image_array) // For GRAY image
cv::Mat m(3450, 3450, CV_8UC3, image_array) // For RGB image
cv::Mat m(3450, 3450, CV_8UC4, image_array) // For RGBA image

convert image from BGR to ARGB using opencv

I want to convert BGR image to ABGR/ARGB.There are conversion BGR2RGBA in opencv but not BGR2ABGR or BGR2ARGB.
It is possible with opencv or using any other method?
The required operation can be accomplished by swapping the image channels using cv::mixChannels as follows:
cv::Mat bgr, bgra;
//bgr initialization code here...
//.
//.
//.
cv::cvtColor(bgr, bgra, cv::COLOR_BGR2BGRA);
cv::Mat abgr(bgra.size(), bgra.type());
int from_to[] = { 0,3, 1,1, 2,2, 3,0 };
cv::mixChannels(&bgra,1,&abgr,1,from_to,4);
from_to array is the mapping function which specifies which channels from source will be copied to which channels of the destination image. The pairs indicate that channel number 0 of the input will be copied to channel number 3 of the output, 1 to 1, 2 to 2, and channel number 3 will be copied to channel number 0 of the output.
Alternatively, we can split the image channels, swap the required channels and merge again. It can be done as follows:
cv::cvtColor(bgr, bgra, cv::COLOR_BGR2BGRA);
std::vector<cv::Mat> channels_bgra;
cv::split(bgra, channels_bgra);
std::vector<cv::Mat> channels_abgr = { channels_bgra[3], channels_bgra[1], channels_bgra[2], channels_bgra[0] };
cv::merge(channels_abgr, abgr);
OpenCV doesn't support ARGB or ABGR formats, so you will not be able to display it or use some of the functions on it... However, it is possible to create them with split and merge functions of OpenCV. Here is some code to explain what I mean.
cv::Mat src, final_image;
// fill src as you prefer
std::vector<cv::Mat> channels;
cv::split(src, channels); // this will put each channel in a mat in the vector
// swap or add channels in the vector
cv::Mat alpha(src.rows, src.cols, CV_8U, cv::Scalar(255));
channels.push_back(alpha);
std::reverse(channels.begin(), channels.end()); //needs <algorithm>
// merge the channels in one new image
cv::merge(channels, final_image);
This can be done faster (maybe it will be just shorter) with the function mixChannels, but I will say that this one is a little bit more confusing.

OpenCV cv::cvtColor drops alpha channel, How to keep alpha data?

I have realized several things:
that OpenCV does not handle alpha channels in cvtColor(RGBAMat, HSVXMat, RGB2HSV, 4) even though it silently accepts and ignores the 4 channel parameter and the 4 channel output matrix.
that OpenCV does not convert datatypes in cvtColor though it happily accepts an output matix of different datatype from the input without warning.
that OpenCV's CV_AUTOSTEP as the last parameter to Mat(int h, int w, enum val DATATYPE, char* data, size_t step) does not work when creating Mats from SDL_Surfaces. It causes segfaults that are remedied by using surface->w * sizeof(whatever datatype the surface is).
Because of these realizations, I have accepted #ypnos answer since it is clearly on the right track. Be warned that none of the code on this page works as is.
void surface_change_sat(SDL_Surface* src_surf, SDL_Surface* dst_surf, float sat_diff) {
using namespace cv;
SDL_LockSurface(src_surf);
Mat src_mat = Mat(src_surf->h, src_surf->w, CV_8UC4); // Make mat from surf
memcpy(src_mat.data, src_surf->pixels, src_surf->h * src_surf->w * sizeof(Uint32));
SDL_UnlockSurface(src_surf);
Mat src_rgbI = Mat(src_mat.rows, src_mat.cols, CV_8UC3); // split rgba into rgb and a
Mat aI_mat = Mat(src_mat.rows, src_mat.cols, CV_8UC1); // since hsv has no a
Mat to_ar[] {src_rgbI, aI_mat};
int from_to[] = { 0,0, 1,1, 2,2, 3,3 }; // r=0, ... a=3
mixChannels(&src_mat, 1, to_ar, 2, from_to, 4);
Mat rgbF_mat = Mat(src_mat.rows, src_mat.cols, CV_32FC3); // Make ints into floats
src_rgbI.convertTo(rgbF_mat, CV_32F);
typedef Vec<float, 3> flt_vec_t; // The type of pixel in hsv
Mat hsv_mat = Mat(src_mat.rows, src_mat.cols, CV_32FC3);
cvtColor(rgbF_mat, hsv_mat, CV_RGB2HSV, 3); // convert to HSV
flt_vec_t pix_vec;
for (MatIterator_<flt_vec_t> mat_it = hsv_mat.begin<flt_vec_t>();
mat_it != hsv_mat.end<flt_vec_t>();
mat_it++) {
pix_vec = *mat_it;
Matx<float, 3, 1> pix_matx = (Matx<float, 3, 1>)pix_vec;
CV_Assert(pix_matx.val[1] <= 1.0f && pix_matx.val[1] >= 0.0f);
pix_matx.val[1] += sat_diff;
if (pix_matx.val[1] > 1.0f) { pix_matx.val[1] = 1.0f; }
if (pix_matx.val[1] < 0.0f) { pix_matx.val[1] = 0.0f; }
}
cvtColor(hsv_mat, rgbF_mat, CV_HSV2RGB, 3); // convert back to RGB
Mat dst_mat = Mat(dst_surf->h, dst_surf->w, CV_8UC4);
Mat dst_rgbI = Mat(dst_mat.rows, dst_mat.cols, CV_8UC3);
rgbF_mat.convertTo(dst_rgbI, CV_8U); // float back to int
Mat from_ar[] {dst_rgbI, aI_mat};
int to_from[] = { 0,0, 1,1, 2,2, 0,3 }; // r=0, ... a=3
mixChannels(from_ar, 2, &dst_mat, 1, to_from, 4); // add alpha for RGBA
SDL_LockSurface(dst_surf);
memcpy(dst_surf->pixels, (void*)dst_mat.data, dst_surf->h * dst_surf->w * sizeof(Uint32));
SDL_UnlockSurface(dst_surf);
}
//-------------------Old Question -------------------------
I was getting the most mysterious memory errors until I added the CV_Asserts to the following function and realized that OpenCV was silently destroying the alpha channel in cv::Mat hsv_mat. I'm confused as to how to resolve this problem:One note, several other answers to related questions suggested thing like using shell scripts or cli tools like imagemagick. These are not helpful in this particular case. I need a standalone function with dependencies limited to opencv and the standard library.
Note: Following a suggestion in the comments, I explicitly split the alpha channel and converted from Uint8 to Float32. Now the assertions function, however, there is still a problem with invalid memory access causing a segfault.
Well, basically cvtColor only works with a fixed set of matrix types, including the format and number of channels. So unmixing and remixing the channels is unavoidable.
The next thing I would do is use the templated data types. They give you a better understanding of what is held within a matrix, however sometimes OpenCV manages to violate that, too (at least in older versions).
This is how I would do it:
SDL_LockSurface(surf);
cv::Mat4b rgba(surf->h, surf->w, surf->pixels);
std::vector<cv::Mat1b> channels_rgba;
cv::split(rgba, channels_rgba);
// create matrix with first three channels only
std::vector<cv::Mat1b> channels_rgb(channels_rgba.begin(),
channels_rgba.begin()+3);
cv::Mat3b rgb;
cv::merge(channels_rgb, rgb);
// create floating point representation
cv::Mat3f rgbhsv;
rgbhsv = rgb; // implicit conversion
// convert to HSV
cv::cvtColor(rgbhsv, rgbhsv, CV_RGB2HSV);
for (cv::Mat3f::iterator mat_it = rgbhsv.begin();
mat_it != rgbhsv.end(); mat_it++) {
cv::Vec3f &pix_vec = *mat_it;
pix_vec[0] += hue_diff;
if (pix_vec[0] >= 360.0f || pix_vec[0] < 0.0f) {
pix_vec[0] = (180.0f / M_PI) * (std::asin(std::sin((pix_vec[0] * M_PI / 180.0f))));
}
}
// convert back to RGB
cv::cvtColor(rgbhsv, rgbhsv, CV_HSV2RGB);
// back to unsigned char channels
rgb = rgbhsv; // implicit conversion
cv::split(rgb, channels_rgb);
// replace first three channels only
for (size_t i = 0; i < channels_rgb.size(); ++i)
channels_rgba[i] = channels_rgb[i];
cv::merge(channels_rgba, rgba);
SDL_UnlockSurface(surf);
Please note:
I did not test this code!
It might be necessary to adjust data ranges (going back to convertTo() instead of assignment operator!
My last operation may not overwrite the surface data (you can go back to mixChannels which is known to not allocate any data ever)

How to Convert to ARGB format

I am trying to convert a RGB image to ARGB format. Is there any way to this in openCV? As far as I know, it is possible to convert the image to RGBA, but am not sure about ARGB.
This is what I am doing currently:
cv::Mat argb(myImage.rows, myImage.cols, CV_8UC4);
cv::Mat alpha(myImage.rows, myImage.cols, CV_8UC1);
cv::Mat in[] = { myImage, alpha };
int from_to[] = { 0,1, 1,2, 2,3, 3,0 };
cv::mixChannels( in, 2, &argb, 1, from_to, 4 );
myImage is in 8uc3 format, which is my input image. But I need to change the format to ARGB for another application.
As far as i know cvtColor function doesn't provide such convertion, so you need to write it on your own - look at the source code of cvtColor function, your convertion will be quite similar to CV_BGR2RGB - you just need to switch first and last channel.

OpenCV cv::Mat 'ones' for multi-channel matrix?

When working with 1-channel (e.g. CV_8UC1) Mat objects in OpenCV, this creates a Mat of all ones: cv::Mat img = cv::Mat::ones(x,y,CV_8UC1).
However, when I use 3-channel images (e.g. CV_8UC3), things get a little more complicated. Doing cv::Mat img = cv::Mat::ones(x,y,CV_8UC3) puts ones into channel 0, but channels 1 and 2 contain zeros. So, how do I use cv::Mat::ones() for multi-channel images?
Here's some code that might help you to see what I mean:
void testOnes() {
int x=2; int y=2; //arbitrary
// 1 channel
cv::Mat img_C1 = cv::Mat::ones(x,y,CV_8UC1);
uchar px1 = img_C1.at<uchar>(0,0); //not sure of correct data type for px in 1-channel img
printf("px of 1-channel img: %d \n", (int)px1); //prints 1
// 3 channels
cv::Mat img_C3 = cv::Mat::ones(x,y,CV_8UC3); //note 8UC3 instead of 8UC1
cv::Vec3b px3 = img_C3.at<cv::Vec3b>(0,0);
printf("px of 3-channel img: %d %d %d \n", (int)px3[0], (int)px3[1], (int)px3[2]); //prints 1 0 0
}
So, I would have expected to see this printout: px of 3-channel img: 1 1 1, but instead I see this: px of 3-channel img: 1 0 0.
P.S. I did a lot of searching before posting this. I wasn't able to resolve this by searching SO for "[opencv] Mat::ones" or "[opencv] +mat +ones".
I don't use OpenCV, but I believe I know what's going on here. You define a data-type, but you are requesting the value '1' for that. The Mat class appears not to pay attention to the fact that you have a multi-channel datatype, so it simply casts '1' as a 3-byte unsigned char.
So instead of using the ones function, just use the scalar constructor:
cv::Mat img_C3( x, y, CV_8UC3, CV_RGB(1,1,1) );
You can also initialize like this:
Mat img;
/// Lots of stuff here ...
// Need to initialize again for some reason:
img = Mat::Mat(Size(width, height), CV_8UC3, CV_RGB(255,255,255));