全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:1850
推到 Plurk!
推到 Facebook!

二值化的問題

尚未結案
frankh
一般會員


發表:25
回覆:36
積分:12
註冊:2005-05-04

發送簡訊給我
#1 引用回覆 回覆 發表時間:2005-10-13 17:21:11 IP:220.134.xxx.xxx 未訂閱
我用ScrollBar來做二值化處理,而二值化的原圖示已經經過我正規劃的圖..但是卻不能做二值化...當我用ScrollBar顯示卻還是經過正規劃的原圖..請各位大大幫幫忙 二值化的部份
void __fastcall TForm1::ScrollBar1Change(TObject *Sender)
{
Graphics::TBitmap *Bmp = new Graphics::TBitmap();
 Byte *ptr;
 int r,g,b;
 int gray;
 int i,j;
 int threshold;
 threshold=ScrollBar1->Position;
 Edit1->Text=ScrollBar1->Position;     Bmp->Assign(Image2->Picture->Bitmap);     Image3->Height=Image2->Height;
 Image3->Width=Image2->Width;     for(j=0;jHeight;j  )
 {
  ptr = (Byte *)Bmp->ScanLine[j];
  for(i=0;iWidth;i  )
  {
   b=ptr[i*3];
   g=ptr[i*3 1];
   r=ptr[i*3 2];
   gray=0.299*r  0.587*g 0.114*b;
   if(gray>threshold)
    gray=255;
   else
    gray=0;
   ptr[i*3]=(Byte)gray;
   ptr[i*3 1]=(Byte)gray;
   ptr[i*3 2]=(Byte)gray;
  }
 }
 Image3->Picture->Assign(Bmp);
 delete Bmp;
 }    //要做二值化的正規化原圖    void __fastcall TForm1::Open1Click(TObject *Sender)
{
Graphics::TBitmap *Bmp = new Graphics::TBitmap();    OpenPictureDialog1->FileName;
if(OpenPictureDialog1->Execute())
       {
       Image1->Picture->LoadFromFile(OpenPictureDialog1->FileName);
       Image1->Width=Image1->Picture->Width;
       Image1->Height=Image1->Picture->Height;
       Bmp->Assign(Image1->Picture->Bitmap);
       Image2->Picture->Assign(Bmp);
       }    Graphics::TBitmap *NBmp = new Graphics::TBitmap();      int            w,h,newW,newH;
  double         dX,dY,x,y;
  int            i,j;
  int            ix,iy;                    // integer value of (x,y)
  double         fx,fy;                    // float value of (x,y)
  unsigned char  clr,
                 clrR,clrG,clrB,
                 nclr1,nclr2,nclr3,nclr4;  // color of neighbor 4 pixel      w = Image1->Width;
  h = Image1->Height;
  newW = 240;
  newH = 320;
  if((newW <=0) ||(newH <=0))
  {
    ShowMessage("Invalided parameter");
    return;
  }
  NBmp->Width  = newW;
  NBmp->Height = newH;      /* Caculate the step size of movement */
  dX = (double)w/(double)newW;
  dY = (double)h/(double)newH;      for(j = 0 ; j < newH ; j  )
     for(i =0 ; i < newW ; i  )
     {
       x = dX*(double)i;
       y = dY*(double)j;           ix = (int)x;         iy = (int)y;
       fx = x - (float)ix;  fy = y - (float)iy;           nclr1 = GetRValue(Bmp->Canvas->Pixels[ix][iy]);
       nclr2 = GetRValue(Bmp->Canvas->Pixels[ix 1][iy]);
       nclr3 = GetRValue(Bmp->Canvas->Pixels[ix][iy 1]);
       nclr4 = GetRValue(Bmp->Canvas->Pixels[ix 1][iy 1]);
       // Bilinear
       clrR=(byte)((1.0-fx)*(1.0-fy)*(double)nclr1 (fx)*(1.0-fy)*(double)nclr2  (1.0-fx)*    (fy)*(double)nclr3 (fx)*    (fy)*(double)nclr4);           nclr1 = GetGValue(Bmp->Canvas->Pixels[ix][iy]);
       nclr2 = GetGValue(Bmp->Canvas->Pixels[ix 1][iy]);
       nclr3 = GetGValue(Bmp->Canvas->Pixels[ix][iy 1]);
       nclr4 = GetGValue(Bmp->Canvas->Pixels[ix 1][iy 1]);
       // Bilinear
       clrG=(byte)((1.0-fx)*(1.0-fy)*(double)nclr1 (fx)*(1.0-fy)*(double)nclr2  (1.0-fx)*    (fy)*(double)nclr3 (fx)*    (fy)*(double)nclr4);           nclr1 = GetBValue(Bmp->Canvas->Pixels[ix][iy]);
       nclr2 = GetBValue(Bmp->Canvas->Pixels[ix 1][iy]);
       nclr3 = GetBValue(Bmp->Canvas->Pixels[ix][iy 1]);
       nclr4 = GetBValue(Bmp->Canvas->Pixels[ix 1][iy 1]);
       // Bilinear
       clrB=(byte)((1.0-fx)*(1.0-fy)*(double)nclr1 (fx)*(1.0-fy)*(double)nclr2  (1.0-fx)*  (fy)*(double)nclr3 (fx)*    (fy)*(double)nclr4);           NBmp->Canvas->Pixels[i][j] = (TColor)RGB(clrR,clrG,clrB);
     }
  Image2->Width = newW;
  Image2->Height = newH;
  Image2->Picture->Bitmap = NBmp;
  Image4->Picture->Assign(NBmp);
   delete NBmp;
delete Bmp;    
RedSnow
版主


發表:79
回覆:1322
積分:845
註冊:2003-12-15

發送簡訊給我
#2 引用回覆 回覆 發表時間:2005-10-14 19:54:31 IP:59.115.xxx.xxx 未訂閱
frankh 您好:    針對您的第一個處理程序,我按照我的寫作習慣改寫如下,看看是否能解決您的問題?
void __fastcall TForm1::ScrollBar1Change(TObject *Sender)
{
    Graphics::TBitmap *Bmp = new Graphics::TBitmap();
    TRGBTriple* ptr;  // 使用 TRGBTriple 以提高程式碼的易讀性
    BYTE gray;
    int i, j;
    int threshold = ScrollBar1->Position;        Image3->Height = Image2->Height;
    Image3->Width  = Image2->Width;
    Bmp->Height = Image2->Height;
    Bmp->Width  = Image2->Width;        // 用 CopyRect 來代替 Assign 的動作
    Bmp->Canvas->CopyRect(Rect(0, 0, Bmp->Width, Bmp->Height), Image2->Picture->Bitmap->Canvas, Rect(0, 0, Image2->Width, Image2->Height));
    Bmp->PixelFormat = pf24bit;  // 確保圖形格式為 24bit        for (j=0; jHeight; j  ) {
        ptr = (TRGBTriple *)Bmp->ScanLine[j];
        for (i=0; iWidth; i  ) {
            gray = BYTE( (ptr[i].rgbtRed*0.299)   (ptr[i].rgbtGreen*0.587)   (ptr[i].rgbtBlue*0.114) );
            if (gray > threshold) {
                gray = 255;
            } else {
                gray = 0;
            }
            ptr[i].rgbtRed   = gray;
            ptr[i].rgbtGreen = gray;
            ptr[i].rgbtBlue  = gray;
        }
    }        Image3->Picture->Bitmap->Assign(Bmp);
    Image3->Picture->Bitmap->PixelFormat = pf24bit;        delete Bmp;
    Bmp = NULL;
}
7 天天敲鍵盤 v 時時按滑鼠 8
frankh
一般會員


發表:25
回覆:36
積分:12
註冊:2005-05-04

發送簡訊給我
#3 引用回覆 回覆 發表時間:2005-10-14 23:02:49 IP:220.134.xxx.xxx 未訂閱
非常謝謝大大...已經可以把正規劃的圖片二值化..但是我有一個地方不懂...之前的問題是否出在圖檔檔頭的問題?
RedSnow
版主


發表:79
回覆:1322
積分:845
註冊:2003-12-15

發送簡訊給我
#4 引用回覆 回覆 發表時間:2005-10-15 22:32:03 IP:61.230.xxx.xxx 未訂閱
frankh 您好:    呵呵~其實我也有點不太瞭解那個問題是怎麼來的?我當初碰到和您類似的狀況時,也是百思不得其解....,後來是做了多種交叉測試後,才發現竟然是使用到 >? <> <>> 天天敲鍵盤 <>> 時時按滑鼠 <>>
系統時間:2024-04-28 6:11:48
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!