2014-09-04 37 views
1

我正在使用opencv 2.4.9。我想加載以Y16(16位)格式存儲的12位灰度原始(.raw)圖像。此格式僅包含一個用於單色圖像的16位Y平面。每個像素由16位小尾數格式表示。使用Opencv加載12位原始GreyScale圖像

我用下面的代碼來加載圖像。

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

+0

標題中的「12」是一個錯字?你有沒有嘗試使用anydepth參數的cv :: imread? – Micka 2014-09-04 10:37:38

+0

否12位不是拼寫錯誤。您知道12位數據存儲爲16位格式。是的,我嘗試IMREAD_ANYDEPTH帕拉姆imread和它拋出異常。 – 2014-09-04 11:06:47

+0

順便說一句,(不應該是這個問題,但會給你以後的訪問問題):在你的墊創建你有寬度和高度交換。它應該是'墊Img_Source16Bit_Gray(m_ImgHeight,m_ImgWidth,CV_16UC1)' – Micka 2014-09-04 11:25:32

回答

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);