想請問一個讀BMP圖檔的問題 |
答題得分者是:syntax
|
starhua
一般會員 發表:10 回覆:18 積分:5 註冊:2003-08-26 發送簡訊給我 |
我想寫一個開啟BMP圖檔的程式,希望把每一點的pixel值讀到一個陣列儲存起來,
我檢查了好久就是不知道哪裡出了問題,幫幫忙因為我寫程式的功力不太高>< int (*figR)[512]=new int[512][512];
int (*figG)[512]=new int[512][512];
int (*figB)[512]=new int[512][512];
int secR[512][512];
int secG[512][512];
int secB[512][512];
Byte *ptr;
//--------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
if(OpenPictureDialog1->Execute()==true)
{
int x,y; Image1 -> Picture -> LoadFromFile (OpenPictureDialog1 ->FileName); for (int y = 0; y < Image1->Height; y )
{
ptr = (Byte *)Image1->Picture->Bitmap->ScanLine[y];
for (int x = 0; x < Image1->Width*3; x=x 3)
{
figB[x][y] = ptr[x];
secB[x][y] = figB[x][y];
figG[x][y] = ptr[x 1];
secG[x][y] = figG[x][y];
figR[x][y] = ptr[x 2];;
secR[x][y] = figR[x][y];
}
}
}
}
|
syntax
尊榮會員 發表:26 回覆:1139 積分:1258 註冊:2002-04-23 發送簡訊給我 |
1.你的 BMP 不知是哪種格式,以 24bits 格式的 BMP 而言,RGB 為 000000 ~ FFFFFF 各佔一個 byte,你的陣列是 int ?,有必要嗎 ?
2.你用這樣的語法:
B = C
A = B
為何不直接用 A = C ?
3.若是你的儲存陣列的格式宣告跟該圖檔的格式一樣,那用 Scanline 後,應該很好指定才是,如 24 bits 512x512 的 RGB-based BMP 圖檔而言: Scanline 一次讀取的資料為 512 x (1 1 1) byte,在記憶體中是這樣的排列: R0G0B0R1G1B1R2G2B2R3G3B3......... R0G0B0 代表第一個點 R1G1B1 代表第二個點,以此類推 512 個點
所以你只要宣告 Byte RGB[3][512][512] (<-- 這裡可能記錯,也許是 [512][3][512],[512][512][3],要實做才會知道,看哪一個宣告後的格式會跟 scan line 讀進來後的一樣)
然後用 記憶體搬移的函式 memcpy(ptr,&RGB,(512 * 3))
這樣一次就可以指定一列點的 RGB 值到陣列中,輕鬆愉快,簡潔又快速
存取時 RGB[n][x][y] x,y 為座標, n = 0 為 R 值, n = 1 為 G 值,n = 2 為 B 值 至於詳細的方式與能運作的程式碼,就靠你自己去嘗試,有嘗試才有進步
|
JerryKuo
版主 發表:42 回覆:571 積分:322 註冊:2003-03-10 發送簡訊給我 |
你好: 本版有很多範例,搜尋"影像處理"會有一堆文章給你參考。請多多利用
http://delphi.ktop.com.tw/topic.php?topic_id=33305
int (*figR)[512]=new int[512][512]; int (*figG)[512]=new int[512][512]; int (*figB)[512]=new int[512][512]; int secR[512][512]; int secG[512][512]; int secB[512][512];可以解釋一下你宣告的陣列嗎?陣列好像怪怪的,改一下應該就能work 發表文章時,請先看一下版規 http://delphi.ktop.com.tw/topic.php?TOPIC_ID=36133 |
starhua
一般會員 發表:10 回覆:18 積分:5 註冊:2003-08-26 發送簡訊給我 |
請問如果我想讀一張未知大小(未預定長與寬)的BMP24bite
我該如何宣告陣列的長度呢
int secR[512][512]; int secG[512][512]; int secB[512][512]; BYTE *ptr; //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { if(OpenPictureDialog1->Execute()==true) { int x,y; Image1->Picture->LoadFromFile(OpenPictureDialog1->FileName); for (int y = 0; y < Image1->Picture->Bitmap->Height; y ) { ptr = (Byte *)Image1->Picture->Bitmap->ScanLine[y]; for (int x = 0; x < Image1->Picture->Bitmap->Width*3; x=x 3) { secB[x][y]=ptr[x]; secG[x][y]= ptr[x 1]; secR[x][y]= ptr[x 2]; } } } |
taishyang
站務副站長 發表:377 回覆:5490 積分:4563 註冊:2002-10-08 發送簡訊給我 |
|
starhua
一般會員 發表:10 回覆:18 積分:5 註冊:2003-08-26 發送簡訊給我 |
|
syntax
尊榮會員 發表:26 回覆:1139 積分:1258 註冊:2002-04-23 發送簡訊給我 |
未定大小時,要用動態的宣告方式 byte ***RGB; <-- 宣告 RGB = new byte*[3];
for (int i = 0; i < 3; j++)
RGB[i] = new byte*[X];
for (int i = 0; i < Y; j++)
RGB[i][X] = new byte[Y];
引言: 請問如果我想讀一張未知大小(未預定長與寬)的BMP24bite 我該如何宣告陣列的長度呢int secR[512][512]; int secG[512][512]; int secB[512][512]; BYTE *ptr; //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { if(OpenPictureDialog1->Execute()==true) { int x,y; Image1->Picture->LoadFromFile(OpenPictureDialog1->FileName); for (int y = 0; y < Image1->Picture->Bitmap->Height; y ) { ptr = (Byte *)Image1->Picture->Bitmap->ScanLine[y]; for (int x = 0; x < Image1->Picture->Bitmap->Width*3; x=x 3) { secB[x][y]=ptr[x]; secG[x][y]= ptr[x 1]; secR[x][y]= ptr[x 2]; } } } |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |