当前位置:   article > 正文

【opencv初学者】9、RGB图像转YUV图像_opencv rgb转yuv

opencv rgb转yuv
#include <opencv2/opencv.hpp>
#include<opencv2/imgproc/imgproc.hpp>
#include<iostream>
using namespace std;
using namespace cv;
int main()
{


		Mat Image;
		Mat yuvImg;
		Mat test_yuv;
	
		Image = imread("C:/Users/xxx/Desktop/20220720-155604.jpeg");      //原图
		resize(Image, test_yuv, Size(976, 656));
		//imshow("xiugaihou", test_yuv);
		//cv::waitKey(0);
		//cv::imwrite("C:/Users/xxx/Desktop/656976.jpg", test_yuv);
		int w = test_yuv.cols;
		int h = test_yuv.rows;
		int buflen = w * h * 3 / 2;//
		int y_buflen = w * h;
		unsigned char* yuvbuf = new unsigned char[buflen];
		//unsigned char* y_yuvbuf = new unsigned char[y_buflen];
		//使用new关键字分配 buflen个unsigned char类型的内存,并用unsigned char类型的指针 yuvbuf 指向这块内存的首地址。

		FILE* pfile = fopen("C:/Users/xxx/Desktop/20220720-155604.yuv", "wb");//创建一个img.yuv的只写二进制文件,
		cv::cvtColor(test_yuv, yuvImg, COLOR_BGR2YUV_IYUV);
		Mat y_yuv = Mat(h, w, CV_8UC1);
		Mat yuv = cv::Mat(h * 3 / 2, w, CV_8UC1);

		memcpy(yuvbuf, yuvImg.data, buflen * sizeof(unsigned char));//从存储区yuv.Img复制buflen * sizeof(unsigned char)个字节到yuvbuf。
		memcpy(y_yuv.data, yuvImg.data, y_buflen * sizeof(unsigned char));
		cv::imshow("YUV", yuvImg);
	    cv::imshow("Y", y_yuv);
	    cv::waitKey(1);
		//cout << yuv << endl;
		//cout << y_yuv << endl;
		//保存yuv文件,包含所有通道
		fwrite(yuvbuf, buflen * sizeof(unsigned char), 1, pfile);//把yuvbuf指向的内容写入到一个字节大小为buflen。。。sizeof()的;1:元素个数;pfile
		fwrite(y_yuvbuf, y_buflen * sizeof(unsigned char), 1, y_pfile);
		fclose(pfile);//关闭流,刷新缓冲区
	}


  • 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
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/670267
推荐阅读
相关标签
  

闽ICP备14008679号