我正在使用 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 位灰度图像的步骤吗?
最佳答案
谢谢你帮我找到答案!这是我更新的代码。
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);
关于c++ - 使用 Opencv 加载 12 位原始灰度图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25662617/