Last active
April 20, 2022 11:52
-
-
Save AllanChen/d65b7c5d0c181fe63b0449aac1b1c739 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//OpenCV Convert YUV buffer to rgb mat | |
Mat y(actual_size, CV_8UC1); | |
Mat u(half_size, CV_8UC1); | |
Mat v(half_size, CV_8UC1); | |
// | |
memcpy(y.data, frame_buffer->DataY(), actual_size.width * actual_size.height); | |
memcpy(u.data, frame_buffer->DataU(), half_size.width * half_size.height); | |
memcpy(v.data, frame_buffer->DataV(), half_size.width * half_size.height); | |
// | |
cv::Mat u_resized, v_resized; | |
cv::resize(u, u_resized, actual_size, 0, 0, cv::INTER_NEAREST); //repeat u values 4 times | |
cv::resize(v, v_resized, actual_size, 0, 0, cv::INTER_NEAREST); //repeat v values 4 times | |
cv::Mat yuv; | |
std::vector<cv::Mat> yuv_channels = { y, u_resized, v_resized }; | |
cv::merge(yuv_channels, yuv); | |
cv::Mat bgr; | |
cv::cvtColor(yuv, bgr, cv::COLOR_YUV2BGR); | |
// YUV420/I420 Convert to nv12 | |
void yuv420p_to_nv12(uint8_t* src, uint8_t* dst, int width, int height) | |
{ | |
memcpy(dst, src, height * width); | |
int yBtyeSize = width * height; | |
int unBtySize = width * height / 2; | |
for (int i = 0; i < unBtySize / 2; i++) { | |
dst[yBtyeSize + 2 * i] = src[yBtyeSize + i]; | |
dst[yBtyeSize + 2 * i + 1] = src[yBtyeSize + unBtySize / 2 + i]; | |
} | |
} | |
// NV12 Convert to YUV420 | |
void nv12_to_yuv420(uint8_t* src, uint8_t* dst, int width, int height) | |
{ | |
memcpy(dst, src, height * width); | |
int yBtyeSize = width * height; | |
int unBtySize = width * height / 2; | |
for (int i = 0; i < unBtySize / 2; i++) { | |
src[yBtyeSize + i] = dst[yBtyeSize + 2 * i] ; | |
src[yBtyeSize + unBtySize / 2 + i] = dst[yBtyeSize + 2 * i + 1] ; | |
} | |
} | |
void splitYUV(uint8_t *yuvData, uint8_t* YData, uint8_t *UData, uint8_t *VData, int width, int height){ | |
memcpy(YData, yuvData, width * height); | |
memcpy(UData, yuvData + width * height, width *height / 4); | |
memcpy(VData, yuvData + width * height * 5 / 4, width * height / 4); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment