我正在使用opencv 2.4.9。我想加载存储为Y16(16位)格式的12位灰度原始(.raw)图像。此格式仅包含单个16位Y平面用于单色图像。每个像素由16位小端格式表示。
我使用以下代码加载图片。
Mat Img_Source16Bit_Gray(m_ImgWidth,m_ImgHeight,CV_16UC1);
Mat Img_Destination8Bit_Gray;
FILE * f;
f=fopen(FileName_S.c_str(),"rb");
if ( !f )
{
MessageBox(L"File Not Found");
return;
}
uchar* Pixels_Char_16Bit;
Pixels_Char_16Bit = new uchar[m_ImgWidth * m_ImgHeight *2];
fread(Pixels_Char_16Bit,m_ImgWidth * m_ImgHeight*2,1,f);
fclose(f);
Img_Source16Bit_Gray.data= Pixels_Char_16Bit;
Img_Source16Bit_Gray.convertTo(Img_Destination8Bit_Gray,CV_8UC1,1);
imshow("Img_Source16Bit_Gray",Img_Source16Bit_Gray);
imshow("Img_Destination8Bit_Gray",Img_Destination8Bit_Gray);
实际图像显示在右侧&输出左手边我得到的不正确& Result8位图像充满了白色像素。任何人都可以请我提供加载16位灰度图像的步骤吗?
答案 0 :(得分:2)
感谢您帮助我找到答案!这是我更新的代码。
Mat Img_Source16Bit_Gray(m_ImgHeight,m_ImgWidth,CV_16UC1);
Mat Img_Destination8Bit_Gray(m_ImgHeight,m_ImgWidth,CV_8UC1);
FILE * f;
f=fopen(FileName_S.c_str(),"rb");
if ( !f )
{
MessageBox(L"File Not Found");
return;
}
char16_t* pY16Pixels;//w-2592 h- 1944
pY16Pixels = new char16_t[m_ImgWidth * m_ImgHeight];
fread(pY16Pixels,m_ImgWidth*m_ImgHeight*2,1,f);
Img_Source16Bit_Gray.data= reinterpret_cast<uchar*>(pY16Pixels);
double minVal, maxVal;
minMaxLoc(Img_Source16Bit_Gray, &minVal, &maxVal); //find minimum and maximum intensities
Img_Source16Bit_Gray.convertTo(Img_Destination8Bit_Gray, CV_8U, 255.0/(maxVal - minVal), -minVal * 255.0/(maxVal - minVal));
namedWindow("Img_Source16Bit_Gray",WINDOW_NORMAL);
namedWindow("Img_Destination8Bit_Gray",WINDOW_NORMAL);
imshow("Img_Source16Bit_Gray",Img_Source16Bit_Gray);
imshow("Img_Destination8Bit_Gray",Img_Destination8Bit_Gray);