伊莉討論區
標題:
為啥抓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多萬呀
程式
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <vector>
#include <cmath>
using namespace std;
typedef unsigned short WORD;
typedef unsigned char BYTE;
typedef unsigned long DWORD;
typedef long LONG;
char chR,chG,chB;
int i,chB1;
double A[256],B[256];
int main(int argc,unsigned char*argv[])
{
/*
變數定義
*/
struct Color {
unsigned char R;
unsigned char G;
unsigned char B;
};
fstream file;
char fileName[64],fileNameout[64];
WORD bmpId;
DWORD fileSize;
WORD bmpReserved1;
WORD bmpReserved2;
DWORD bmpOffset;
DWORD bmpInfoHeaderSize;
LONG bmpWidth;
LONG bmpHeight;
WORD bmpPlanes;
WORD bmpbitCount;
DWORD bmpCompression;
DWORD bmpDataSize;
LONG bmpXPixelPerMeter;
LONG bmpYPixelPerMeter;
DWORD bmpColorUsed;
DWORD bmpColorImportant;
//預設 先歸零
for(i=0;i!=256;i++)
{
A[i]=0.0;
}
/////////////////////////////////////////////
/*
讀取
*/
cout << "File name: ";
cin >> fileName;
cout << "Reading" << endl;
file.open(fileName, ios::in|ios::binary);
file.read((char*)&bmpId, sizeof(WORD));
file.read((char*)&fileSize, sizeof(DWORD));
file.read((char*)&bmpReserved1, sizeof(WORD));
file.read((char*)&bmpReserved2, sizeof(WORD));
file.read((char*)&bmpOffset, sizeof(DWORD));
file.read((char*)&bmpInfoHeaderSize, sizeof(DWORD));
file.read((char*)&bmpWidth, sizeof(LONG));
file.read((char*)&bmpHeight, sizeof(LONG));
file.read((char*)&bmpPlanes, sizeof(WORD));
file.read((char*)&bmpbitCount, sizeof(WORD));
file.read((char*)&bmpCompression, sizeof(DWORD));
file.read((char*)&bmpDataSize, sizeof(DWORD));
file.read((char*)&bmpXPixelPerMeter, sizeof(LONG));
file.read((char*)&bmpYPixelPerMeter, sizeof(LONG));
file.read((char*)&bmpColorUsed, sizeof(DWORD));
file.read((char*)&bmpColorImportant, sizeof(DWORD));
const size_t sizey = bmpHeight;
const size_t sizex = bmpWidth;
vector<vector<Color> > imageVec(sizey, vector<Color>(sizex));
for(size_t y = 0; y!= imageVec.size(); ++y) {
for(size_t x = 0; x!= imageVec[0].size(); ++x) {
file.get(chB).get(chG).get(chR);
imageVec[y][x].B = (unsigned char)chB;
imageVec[y][x].G = (unsigned char)chG;
imageVec[y][x].R = (unsigned char)chR;
//統計
chB1 = (int)chB;
A[chB1]=A[chB1]+1.0;
}
}
file.close();
for(i=0;i< 256;i++)
{
cout << "RGB" << " " << i << ":";
cout << A[i] << endl;
}
system("pause");
for(i=0;i<256;i++)
{
if(i!=0)
{
B[i] = B[i-1] + A[i];
cout << "RGB" << " " << i << ":";
cout << B[i] << endl;
}
else
{
B[i] = A[i];
cout << "RGB" << " " << i << ":";
cout << B[i] << endl;
}
}
system("pause");
return 0;
}
複製代碼
A矩陣:為0~255各有的點數
B矩陣:用A矩陣累積統計,所以B[255]為全圖總點數
圖
[attach]68104483[/attach]
作者:
snater12
時間:
2012-1-1 09:52 PM
自頂一下
各位大大支援一下呀
作者:
kaworucloud
時間:
2012-1-3 10:21 AM
chB1 = chB;
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!