raw格式文件为裸数据,包含了相机原图像文件在图像传感器产生后计入相机图像处理器之前的一切图像信息。

从硬盘中读取raw格式文件存入opencv的图像容器中

	CString strPath= dlg.GetPathName();

	CFile file;

	file.Open(strPath,CFile::modeRead|CFile::typeBinary);

	file.SeekToBegin();
	
	BYTE * pfilebuf=new BYTE[m_width*m_hight*2] ;//相机采图分辨率

	if (m_width*m_hight*2!= file.Read(pfilebuf,m_width*m_hight*2)  )
	{  
		//提示文件读取错误  
		file.Close();  
		return;
	}  

	file.Close();  
	CvMat* mat_a = cvCreateMat(1, m_width*m_hight, CV_16U);

	int i=0;  

	do {  
		unsigned  short int a=0xffff;
		BYTE b = pfilebuf[i*2];
		BYTE c = pfilebuf[i*2+1];

		a= (c<<8)|b;

		CV_MAT_ELEM(*mat_a,unsigned short int, 0, i) =a; 

		i++;  
	}while(i<m_width*m_hight);  

	delete[] pfilebuf; 

	cvReshape(mat_a, mat_a, 0,m_hight);
	
	IplImage* img_a = cvCreateImage(cvSize(m_width,m_hight), IPL_DEPTH_16U, 1);

	cvCvtColor(mat_a, img_a, CV_BayerBG2GRAY); 
	
	/*cvNamedWindow("raw");
	cvShowImage("raw",img_a);*/
	image=img_a;
	
	//cvReleaseImage(&img_a);
	cvReleaseMat(&mat_a);
	}
将处理后的16位图像数据保存文raw格式文件

    errno_t err1;  
    FILE *pfile;
   err1=fopen_s(&pfile,"D:\\ceshiraw.raw","wb");
			
   unsigned short int* pBes=NULL ;//指向类型的指针	
   pBes = new unsigned short int[img16.rows*img16.cols];   //处理图像的指针
   memcpy(pBes,img16.data,img16.rows*img16.cols*2);
   if (pfile)
   {
	fwrite(pBes, sizeof(unsigned short)*img16.rows*img16.cols, 1, pfile);
			
   }
  fclose(pfile);

  delete [] pBes;
  pBes=NULL;



Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐