2012-07-12 45 views
0

我想能夠比較2個圖像(相同格式)並對這些圖像執行比特級比較。 1)爲標題創建結構.2)打開文件並從SOI標記的圖像數據偏移處開始讀取內容.3)將相應值存儲在3d數組或向量數組中.4)進行元素明智的比較,返回結果。我已經成功地使用fread()爲bmp做了這個工作,並且使用了一個3d數組作爲容器,並使用可以分配和釋放內存的方法(但是bmp是未壓縮的圖像)。不知何故,這個過程對jpeg和tiff來說似乎要困難得多。即使理解了這兩種格式的標題格式,我的代碼卻說它不能讀取元素[45] [24]的顏色。我看過其他幾個選項,比如libjpeg和CImg,但我希望在跳入新圖書館之前獲得觀點。jpeg和tiff像素值提取

我對BMP代碼如下: ...喀嚓...

unsigned char*** create3dArray(FILE **fptr1,int height,int width,int depth) 
{ 
     unsigned char*** databuff = new unsigned char **[height]; 

    // Allocate an array for each element of the first array 
    for(int x = 0; x < height; ++x) 
    { 
     databuff[x] = new unsigned char *[width]; 

     // Allocate an array of integers for each element of this array 
     for(int y = 0; y < width; ++y) 
     { 
      databuff[x][y] = new unsigned char[depth]; 

      // Specify an initial value (if desired) 
      for(int z = 0; z < depth; ++z) 
      { 
        databuff[x][y][z] = -1; 

      } 
     } 
    } 

    if ((sizeof(fheader) != 14) || (sizeof(iheader) != 40)) 
    { 
     printf("Header structs are not properly packed\n"); 
    return 0; 
    } 

    if (fread(&fheader, sizeof(fheader), 1, *fptr1) != 1) 
    { 
    printf("Couldn't read fheader.\n"); 
    return 0; 
    } 

    if (fread(&iheader, sizeof(iheader), 1, *fptr1) != 1) 
    { 
    printf("Couldn't read iheader.\n"); 
    return 0; 
} 
// uncomment to get an idea of what the headers look like. 
if ((iheader.height != height) || (iheader.width != width) || (iheader.bits != 24)) 
    { 
    printf("This only works for 512x512 24-color bitmaps\n"); 
    return 0; 
    } 



    if (fheader.offset != 54) { 
    printf("This only works if the offset is equal to 54\n"); 
    return 0; 
    } 
    for (int i = 0; i < iheader.height; i++) { 
    for (int j = 0; j < iheader.width; j++) { 
     if (fread(&databuff[i][j][0], 3, 1, *fptr1) != 1){ 
      printf("Couldn't read colors for element [%d][%d]\n", i, j); 
         return 0; 
        } 
      } 
      } 

return databuff; 
} 

template <typename Tx> 
void destroy3dArray(Tx*** myArray) 
{ 
delete[] **myArray; 
delete[] *myArray; 
delete[] myArray; 
} 

int main() 
{ 
FILE *fptr1,*fptr2;  // two file pointers one for each file. 
int count=0; 
float total_bits=0; 
float ber=0;    //variable for bit error rate 
int width,height,depth; 
cout<<"Please enter height of the image "<<endl; 
cin>>height; 
cout<<"Please enter width of the image "<<endl; 
cin>>width; 
cout<<"Please enter depth of the image. The max depth can be 3 for RGB values"<<endl; 
cin>>depth; 
char *filename = "lena512.bmp"; 
char *filename2 = "lena512_2.bmp"; 
//std::string trueBinaryDataInString[512][512][3]; 

if ((fptr1 = fopen(filename, "r")) == NULL) { 
      printf("Coulsn't open file %s for reading.\n", filename); 
      return 1; 
     } 
unsigned char*** trueArray = create3dArray(&fptr1,height,width,depth); 

for(int i=0;i<height;i++) 
{ 
    //std::cout << "Row " << i << std::endl; 
    for(int j=0;j<width;j++) 
    { 
     for(int k=0;k<depth;k++) 
     { 

      total_bits += ToBinary(trueArray[i][j][k]).length(); 

     } 
     //std::cout<<endl; 
    } 
//std::cout<<endl; 
} 
std::cout << total_bits<<endl; 
//createAnddestroy3dArray<unsigned char> MyArray; 
if ((fptr2 = fopen(filename2, "r")) == NULL) { 
      printf("Coulsn't open file %s for reading.\n", filename2); 
      return 1; 
     } 
unsigned char*** trueArray2 = create3dArray(&fptr2,height,width,depth); 
/*for(int i=0;i<512;i++) 
{ 
    std::cout << "Row " << i << std::endl; 
    for(int j=0;j<512;j++) 
    { 
     for(int k=0;k<3;k++) 
     { 
      std::cout<<" "<<ToBinary(trueArray2[i][j][k]); 
     } 
     std::cout<<endl; 
    } 
std::cout<<endl; 
} 
*/ 
/******** BIT Error Rate Calculation ******/ 
for(int i=0;i<height;i++) 
{ 
for(int j=0;j<width;j++) 
    { 
    for(int k=0;k<depth;k++) 
     { 
      if(ToBinary(trueArray[i][j][k])!= ToBinary(trueArray2[i][j][k])) 
      { 
std::cout<<ToBinary(trueArray[i][j][k])<< " " <<ToBinary(trueArray2[i] [j][k])<<endl; 
      count++; 
      } 
      else 
      continue; 
     } 
    } 
} 
ber = (count/total_bits)*100; 
std::cout<<"Bit Error Rate (BER) = "<<ber<<endl; 
destroy3dArray<unsigned char>(trueArray); //Deallocating memory for array 1 
destroy3dArray<unsigned char>(trueArray2); //Deallocating memory for array 2 
return 0; 
} 

回答

1

JPEG和TIFF壓縮與也許更大的自由度格式編碼,你或許會想到的圖像。

因此,您正從錯誤的角度接近問題。爲了支持成像格式的選擇,您需要庫來讀取文件並將其解壓縮爲位圖,例如24位RGB或其他。可能需要進行顏色空間轉換,因爲其中一個比較圖像可能會被解壓縮到4:2:2 YUV空間,另一個則是4:2:0等。

利用您選擇的某個圖像庫也有操作系統的限制),你將能夠加載和解壓縮文件爲您感興趣的二維像素格式。完成這些之後,您會將其填充到您的C++數字處理代碼中,並從那裏進行比較。

+0

所以我必須首先將這些文件格式轉換成bmp的。如先載入jpg,然後將其保存爲bmp,然後使用這些文件。如果是,那麼狀態的改變是否會影響任何標題信息? – user1227372 2012-07-12 21:23:27

+0

不一定要重新保存到.BMP中,這也是可能的。答案中提到的庫允許您在內存中加載和解壓縮,而不保存到中間文件中。這些庫還允許您訪問原始文件的元數據,以備需要時使用。 – 2012-07-12 22:21:34

0

成功解析並處理JPEG和TIFF文件中的可能變化是。有一個令人驚訝的數量的細節:顏色深度,漸進式編碼,EXIF數據,縮略圖, - 列表繼續。利用這些庫,不要重新發明輪子。使用libjpeg和libtiff加載合適的(RGB?)緩衝區進行比較。

http://www.libtiff.org/

http://www.ijg.org/

FWIW,libpng的是相當不錯的,太 - 如果你想擴展您的圖像比較,以這種格式,以及。 http://www.libpng.org/pub/png/libpng.html

相關問題