将BGR转换成为YUV420SP图像格式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | //BGR2YUV(YUV420SP_NV21) void enCodeYUV420SP(unsigned char * yuv420sp, unsigned char * rgb, int width, int height) { if (yuv420sp == NULL || rgb == NULL) return ; int frameSize = width*height; int yIndex = 0; int uvIndex = frameSize; int R, G, B, Y, U, V; for ( int i = 0; i < height; i++) { for ( int j = 0; j < width; j++) { B = rgb[(i * width + j) * 3 + 0]; G = rgb[(i * width + j) * 3 + 1]; R = rgb[(i * width + j) * 3 + 2]; //RGB to YUV Y = ((66 * R + 129 * G + 25 * B + 128) >> 8) + 16; U = ((-38 * R - 74 * G + 112 * B + 128) >> 8) + 128; V = ((112 * R - 94 * G - 18 * B + 128) >> 8) + 128; yuv420sp[yIndex++] = (unsigned char )((Y < 0) ? 0 : ((Y > 255) ? 255 : Y)); if (i % 2 == 0 && j % 2 == 0) { yuv420sp[uvIndex++] = (unsigned char )((V < 0) ? 0 : ((V > 255) ? 255 : V)); //UV交替排列 yuv420sp[uvIndex++] = (unsigned char )((U < 0) ? 0 : ((U > 255) ? 255 : U)); } } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | cv::Mat img = cv::imread(imgPath); int height = img.rows; int width = img.cols; unsigned char * img_bgr_data = (unsigned char *) malloc (height*width * 3 * sizeof (unsigned char )); for ( int i = 0; i < height; i++) { unsigned char * current_row = img.ptr<uchar>(i); for ( int j = 0; j < width; j++) { img_bgr_data[(i * width + j) * 3 + 0] = current_row[j * 3 + 0]; //B img_bgr_data[(i * width + j) * 3 + 1] = current_row[j * 3 + 1]; //G img_bgr_data[(i * width + j) * 3 + 2] = current_row[j * 3 + 2]; //R } } //RGB->NV21(YUV420SP) unsigned char * img_nv21_data = (unsigned char *) malloc (height*width * 3 / 2 * sizeof (unsigned char )); enCodeYUV420SP(img_nv21_data, img_bgr_data, width, height); //Mat img_BGR(height, width, CV_8UC3); //cvtColor(img_nv21, img_BGR, CV_YUV2BGR_NV21);//YUV420sp->BGR //cv::cvtColor(img_nv21, img_BGR, cv::COLOR_YUV420sp2BGR); //cv::imwrite("./img_BGR.jpg", img_BGR); |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
2019-04-02 关于 tf.image.crop_and_resize的使用
2019-04-02 Mask R-CNN
2019-04-02 常用的tensorflow函数