c++ - 使用 Opencv 加载 12 位原始灰度图像

标签 c++ opencv

我正在使用 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 位灰度图像的步骤吗?

enter image description here

最佳答案

谢谢你帮我找到答案!这是我更新的代码。

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/

相关文章:

c++ - GCC 4.9.1 在 RHEL 6.0 上编译失败,编译错误在 "libsanitizer"

c++ - IcmpSendEcho 返回 183

python - python在重叠和旋转的瓷砖中分割图像

c - OpenCV 上的逆向过滤——访问 DFT 值并乘以 DFT 矩阵

python - 如何在 Python 中正确使用 opencv.imread 的参数 `flags`?

c++ - 如何在 OpenCV 2.4.3 中编写视频文件

c++ - 在数组中搜索字符串

c++ - 为什么 QList::at() 不检查索引是否存在并且还返回一个只读值?

python - 在 C++ 程序中嵌入 python-opencv 的问题(单张图片正常,但在网络摄像头上失败)

c++ - 链接到 opencv 的奇怪链接器错误... "LNK1107: invalid or corrupt file: cannot read at 0x2E8"