伊莉討論區

標題: 為啥抓BMP的像素點會錯? [打印本頁]

作者: snater12    時間: 2011-12-31 10:45 PM     標題: 為啥抓BMP的像素點會錯?

本帖最後由 snater12 於 2011-12-31 10:53 PM 編輯

作用:抓1024*768 BMP圖(灰階:R=G=B)每點色素
         並統計總共整張圖共有幾點,因為是灰階的緣故,所以只統計色素B

問題:附檔案
         圖一為網路上有人先把色素24位元轉成256色,我在抓下來用小畫家轉副檔名為24位元的BMP
         圖二為原本是彩色的24位元BMP,我用WORD轉灰階再複製到小畫家轉副檔名為24位元的BMP
         結果圖一抓到的總點數為63萬多
               圖二為36萬多
         為啥會這樣,因為1024*768應該是70多萬呀

程式

  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <fstream>
  4. #include <vector>
  5. #include <cmath>
  6. using namespace std;
  7. typedef unsigned short WORD;
  8. typedef unsigned char BYTE;
  9. typedef unsigned long DWORD;
  10. typedef long LONG;
  11. char chR,chG,chB;
  12. int i,chB1;
  13. double A[256],B[256];
  14. int main(int argc,unsigned char*argv[])
  15. {
  16. /*
  17. 變數定義
  18. */
  19. struct Color {
  20.    unsigned char R;
  21.    unsigned char G;
  22.    unsigned char B;
  23. };
  24. fstream file;
  25. char fileName[64],fileNameout[64];
  26. WORD bmpId;
  27. DWORD fileSize;
  28. WORD bmpReserved1;
  29. WORD bmpReserved2;
  30. DWORD bmpOffset;
  31. DWORD bmpInfoHeaderSize;
  32. LONG bmpWidth;
  33. LONG bmpHeight;
  34. WORD bmpPlanes;
  35. WORD bmpbitCount;
  36. DWORD bmpCompression;
  37. DWORD bmpDataSize;
  38. LONG bmpXPixelPerMeter;
  39. LONG bmpYPixelPerMeter;
  40. DWORD bmpColorUsed;
  41. DWORD bmpColorImportant;
  42. //預設 先歸零
  43. for(i=0;i!=256;i++)
  44. {
  45. A[i]=0.0;
  46. }
  47. /////////////////////////////////////////////
  48. /*
  49. 讀取
  50. */
  51. cout << "File name: ";
  52. cin >> fileName;
  53. cout << "Reading" << endl;
  54. file.open(fileName, ios::in|ios::binary);
  55. file.read((char*)&bmpId, sizeof(WORD));
  56. file.read((char*)&fileSize, sizeof(DWORD));
  57. file.read((char*)&bmpReserved1, sizeof(WORD));
  58. file.read((char*)&bmpReserved2, sizeof(WORD));
  59. file.read((char*)&bmpOffset, sizeof(DWORD));
  60. file.read((char*)&bmpInfoHeaderSize, sizeof(DWORD));
  61. file.read((char*)&bmpWidth, sizeof(LONG));
  62. file.read((char*)&bmpHeight, sizeof(LONG));
  63. file.read((char*)&bmpPlanes, sizeof(WORD));
  64. file.read((char*)&bmpbitCount, sizeof(WORD));
  65. file.read((char*)&bmpCompression, sizeof(DWORD));
  66. file.read((char*)&bmpDataSize, sizeof(DWORD));
  67. file.read((char*)&bmpXPixelPerMeter, sizeof(LONG));
  68. file.read((char*)&bmpYPixelPerMeter, sizeof(LONG));
  69. file.read((char*)&bmpColorUsed, sizeof(DWORD));
  70. file.read((char*)&bmpColorImportant, sizeof(DWORD));
  71. const size_t sizey = bmpHeight;
  72. const size_t sizex = bmpWidth;
  73. vector<vector<Color> > imageVec(sizey, vector<Color>(sizex));
  74. for(size_t y = 0; y!= imageVec.size(); ++y) {
  75.   for(size_t x = 0; x!= imageVec[0].size(); ++x) {
  76.       file.get(chB).get(chG).get(chR);
  77.       imageVec[y][x].B = (unsigned char)chB;
  78.       imageVec[y][x].G = (unsigned char)chG;
  79.       imageVec[y][x].R = (unsigned char)chR;
  80.    //統計
  81.    chB1 = (int)chB;
  82.    A[chB1]=A[chB1]+1.0;
  83.   }
  84. }
  85. file.close();

  86. for(i=0;i< 256;i++)
  87. {
  88. cout << "RGB" << " " << i << ":";
  89. cout << A[i] << endl;
  90. }
  91. system("pause");

  92. for(i=0;i<256;i++)
  93. {
  94. if(i!=0)
  95. {
  96.   B[i] = B[i-1] + A[i];
  97.      cout << "RGB" << " " << i << ":";
  98.      cout << B[i] << endl;
  99. }
  100. else
  101. {
  102.   B[i] = A[i];
  103.   cout << "RGB" << " " << i << ":";
  104.   cout << B[i] << endl;
  105. }
  106. }
  107. system("pause");
  108. return 0;
  109. }
複製代碼
A矩陣:為0~255各有的點數
B矩陣:用A矩陣累積統計,所以B[255]為全圖總點數


[attach]68104483[/attach]
作者: snater12    時間: 2012-1-1 09:52 PM

自頂一下
各位大大支援一下呀
作者: kaworucloud    時間: 2012-1-3 10:21 AM

  1. chB1 = chB;
  2. A[chB1]=A[chB1]+1.0;
複製代碼
chB1 型態是 int,chB 型態是 char,如果 chB 值大於 127 會變成負值,assign 給 chB1 時自然也是負值
執行 A[chB1] -> A[-1] 沒當是你好運(這情況應該算不幸),但會出現什麼結果已無法預期




歡迎光臨 伊莉討論區 (http://a401.file-static.com/) Powered by Discuz!