//
cv::Mat bgr_mat = cv::imread("D:\\LoadBmp\\1.jpg", cv::IMREAD_COLOR);
if (!bgr_mat.empty()) {
int height = bgr_mat.rows;
int width = bgr_mat.cols;
cv::Mat img_nv12;
cv::Mat yuv_mat;
cv::cvtColor(bgr_mat, yuv_mat, cv::COLOR_BGR2YUV_I420);
uint8_t* yuv = yuv_mat.ptr<uint8_t>();
img_nv12 = cv::Mat(height * 3 / 2, width, CV_8UC1);
uint8_t* ynv12 = img_nv12.ptr<uint8_t>();
int32_t uv_height = height / 2;
int32_t uv_width = width / 2;
// copy y data
int32_t y_size = height * width;
memcpy(ynv12, yuv, y_size);
// copy uv data
uint8_t* nv12 = ynv12 + y_size;
uint8_t* u_data = yuv + y_size;
uint8_t* v_data = u_data + uv_height * uv_width;
for (int32_t i = 0; i < uv_width * uv_height; i++) {
*nv12++ = *u_data++;
*nv12++ = *v_data++;
}
int32_t yuv_size = y_size + 2 * uv_height * uv_width;
FILE* yuvFd = fopen("1.yuv", "w+");
fwrite(img_nv12.ptr<uint8_t>(), 1, yuv_size, yuvFd);
fclose(yuvFd);
}
opencv jpg转NV12
最新推荐文章于 2024-07-28 23:13:09 发布