24bit圖像轉成半色調圖像 |
|
hotlittlesun
一般會員 發表:1 回覆:2 積分:0 註冊:2006-10-06 發送簡訊給我 |
各位前輩大家好!在這裡想請問各位前輩問題,希望能為小弟解答
小弟要將圖片轉呈半色調圖像作應用,在轉成半色調卻成為我的大問題,轉出來的圖一直都不OK,會有雜雜的東西 自己花了數天找問題卻深陷其中,所以想請教各位前輩。 以下是我寫的程式當載入圖像之後執行就先跑一次灰階處理,之後再抓出灰階值作黑白的半色調圖像。 但轉出圖像式錯誤的,錯誤圖像見附檔。 void __fastcall TForm1::N5Click(TObject *Sender) { Byte *ptr, *tptr, *uptr, *dptr; int r,g,b; int gray; TheBitmap=Image1->Picture->Bitmap; //TheBitmap->PixelFormat=pf8bit; //------------產生一臨時圖像---------------------------------------- Graphics::TBitmap *TempBitmap= new Graphics::TBitmap(); TempBitmap->Assign(Image1->Picture->Bitmap); //-------------------執行圖像灰階----------------------------------- for (int y=0; y < TempBitmap->Height; y ) { tptr = (Byte*)TempBitmap->ScanLine[y]; for (int x=0; x < TempBitmap->Width; x ) { b=tptr[x*3]; g=tptr[x*3 1]; r=tptr[x*3 2]; gray=0.299*r 0.587*g 0.114*b; tptr[x*3]=(Byte)gray; tptr[x*3 1]=(Byte)gray; tptr[x*3 2]=(Byte)gray; } } Image2->Picture->Bitmap->Assign(TempBitmap); //-------------------執行擴散誤差----------------------------------- for (int y=0; y < TempBitmap->Height; y ) { int down=y 1; if (down==TempBitmap->Height) down=0; ptr = (Byte*)TheBitmap->ScanLine[y]; //新圖 tptr = (Byte*)TempBitmap->ScanLine[y]; //舊圖 dptr = (Byte*)TempBitmap->ScanLine[down]; for (int x=0; x < TempBitmap->Width; x ) { int left=x-1; int right=x 1; if (left<0) left=TempBitmap->Width -1; if (right==TempBitmap->Width) right=0; b=tptr[x*3]; g=tptr[x*3 1]; r=tptr[x*3 2]; gray=0.299*r 0.587*g 0.114*b; if ((Byte)gray>128) { ptr[x*3 ]=(Byte)(255); ptr[x*3 1]=(Byte)(255); ptr[x*3 2]=(Byte)(255); BW1_e=(Byte)gray-255; } else { ptr[x*3 ]=(Byte)0; ptr[x*3 1]=(Byte)0; ptr[x*3 2]=(Byte)0; BW1_e=(Byte)gray; } tptr[right*3 ] =(B1*BW1_e); tptr[right*3 1] =(B1*BW1_e); tptr[right*3 2] =(B1*BW1_e); dptr[left*3 ] =(B2*BW1_e); dptr[left*3 1] =(B2*BW1_e); dptr[left*3 2] =(B2*BW1_e); dptr[x*3 ] =(B3*BW1_e); dptr[x*3 1] =(B3*BW1_e); dptr[x*3 2] =(B3*BW1_e); dptr[right*3 ] =(B4*BW1_e); dptr[right*3 1] =(B4*BW1_e); dptr[right*3 2] =(B4*BW1_e); } // end x }// end y fclose(fn1); delete TempBitmap; //-------------------顯示圖像--------------------------------------- Repaint(); Image1->Picture->Bitmap->Assign(TheBitmap); } |
hotlittlesun
一般會員 發表:1 回覆:2 積分:0 註冊:2006-10-06 發送簡訊給我 |
各位前輩!小弟一直在努力的找Bug,最後得到一個最可能的結論。
那就是這現象是這演算法的敗點,除此之外我已經想不到其他原因了。 而以上方法我是參照www.czvc.com/tech/sztxclrm/chp4.doc 所改寫的 因此我先換換其他演算法好了,如果有錯誤在請各位前輩指正。感謝 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |