Hi,
I am trying to convert white image in raw RGB . Using BGR2YUV and YUV2BGR to convert from BGR to YUV and vice-versa.
int width, height, bpc;
vector<unsigned char> data;
vector<unsigned short> data16;
void show(string str) {
Mat img(height, width, CV_8UC3, data.data());
uint32_t size = (uint32_t)pow(2, bpc)-1;
Mat displayImage;
img.convertTo(displayImage, CV_8UC3, 255/size);
imshow(str, displayImage);
waitKey(0);
}
void show16(string str) {
Mat img(height, width, CV_16UC3, data16.data());
uint32_t size = (uint32_t)pow(2, bpc)-1;
Mat displayImage;
img.convertTo(displayImage, CV_16UC3, 65535/size);
imshow(str, displayImage);
waitKey(0);
}
void convertToYCbCr444() {
Mat rgb;
if (bpc <= 8) {
rgb = cv::Mat(height, width, CV_8UC3, data.data());
} else {
rgb = cv::Mat(height, width, CV_16UC3, data16.data());
}
Mat ycbcr;
cvtColor(rgb, ycbcr, COLOR_BGR2YUV);
if (bpc<=8) {
memcpy(data.data(), ycbcr.data, data.size());
show("YUV8 bits");
} else {
memcpy(data16.data(), ycbcr.data, data16.size() * sizeof(unsigned short));
show16("YUV16 bits");
}
}
void convertToRawRGB() {
Mat ycbcr;
if (bpc <= 8) {
ycbcr = cv::Mat(height, width, CV_8UC3, data.data());
} else {
ycbcr = cv::Mat(height, width, CV_16UC3, data16.data());
}
Mat rgb;
cvtColor(ycbcr, rgb, COLOR_YUV2BGR);
if (bpc<=8) {
memcpy(data.data(), rgb.data, data.size());
show("RGB bits");
} else {
memcpy(data16.data(), rgb.data, data16.size() * sizeof(unsigned short));
show16("RGB16 bits");
}
}
But when I display image using above show()/show16(), rgb image is being displayed correctly. But YUV image is totally different not even near to RGB.
Example:
Input image: White, R=255, G=255, B=255 for 8 bits
I am able to display this white image in RGB format.
Once I convert RGB image into YUV, and display, it will not be white and in different color.
I am trying to convert RGB to YCbCr444/YUV444 and vice versa.
am I missing something here?