線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:1765
推到 Plurk!
推到 Facebook!

二值化執行程式碼有問題...

尚未結案
tracydai1025
一般會員


發表:13
回覆:4
積分:3
註冊:2005-05-08

發送簡訊給我
#1 引用回覆 回覆 發表時間:2005-07-26 23:12:14 IP:218.165.xxx.xxx 未訂閱
請教各位大大 我在圖片完成執行二值化時 滑鼠在image裡點一下 會恢復成原來的圖片 是不是我mousedown裡的設定有錯或是有問題 以下是我的程式碼 請各位指教一下 THX~        這是我mousedown裡的程式碼    void __fastcall TForm1::Image1MouseDown(TObject *Sender,       TMouseButton Button, TShiftState Shift, int X, int Y) {         Graphics::TBitmap *TheBitmap;             char buf[64];         sprintf(buf,"X座標=>%d, Y座標=>%d",X,Y);         ListBox1->Items->Add(buf);         x_buffer[number]=X;         y_buffer[number]=Y;         number++;            StartX=X;         StartY=Y;            if(Pasting_Enable)          {                 Pasting=true; Masking=false;                 Pasting_Enable=false;          }         else         {                 Masking=true; Pasting=false;                 TheBitmap=Image1->Picture->Bitmap;                 TheBitmap->Canvas->Draw(0,0,OrgBitmap);         }                Masking=true;            TheBitmap=Image1->Picture->Bitmap;         TheBitmap->Canvas->Draw(0,0,OrgBitmap); }    這是二值化的程式碼    void __fastcall TForm1::Button1Click(TObject *Sender) {    Graphics::TBitmap *TheBitmap;    Byte *ptr;    int r,g,b;    int gray;    int i,j;    TheBitmap = Image1->Picture->Bitmap;    for (j=0; jHeight; j ) { ptr = (Byte *)TheBitmap->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>125) gray = 255; else gray = 0; ptr[i*3 ] = (Byte)gray; ptr[i*3 1] = (Byte)gray; ptr[i*3 2] = (Byte)gray; } } Image1->Picture->Assign(TheBitmap); }
taishyang
站務副站長


發表:377
回覆:5490
積分:4563
註冊:2002-10-08

發送簡訊給我
#2 引用回覆 回覆 發表時間:2005-07-26 23:59:03 IP:218.168.xxx.xxx 未訂閱
您好:    PO程式碼的方式與版規說明請參考下面連結,煩請修改謝謝您的配合 >
richtop
資深會員


發表:122
回覆:646
積分:468
註冊:2003-06-10

發送簡訊給我
#3 引用回覆 回覆 發表時間:2005-07-27 00:06:10 IP:211.76.xxx.xxx 未訂閱
tracydai1025 您好    兩個建議: 1.您在MouseDown的程式碼似乎都會將OrgBitmap填回Image1->Picture->Bitmap,是否為問題癥結所在?您可再確認一下.
  TheBitmap=Image1->Picture->Bitmap;
  TheBitmap->Canvas->Draw(0,0,OrgBitmap);
2.您在二值化的程式部份建議加入下列紅色的程式碼:
  .....
  TheBitmap = Image1->Picture->Bitmap;
  TheBitmap->PixelFormat = pf24bit; // 建議加此行
  for (j=0; jPicture->Height; j  )
  .....
RichTop 敬上 =====***** 把數學當工具,可以解決問題;將數學變能力,能夠發現並解決問題! =====#####
tracydai1025
一般會員


發表:13
回覆:4
積分:3
註冊:2005-05-08

發送簡訊給我
#4 引用回覆 回覆 發表時間:2005-07-29 14:31:24 IP:218.169.xxx.xxx 未訂閱
引言: tracydai1025 您好 兩個建議: 1.您在MouseDown的程式碼似乎都會將OrgBitmap填回Image1->Picture->Bitmap,是否為問題癥結所在?您可再確認一下.
  TheBitmap=Image1->Picture->Bitmap;
  TheBitmap->Canvas->Draw(0,0,OrgBitmap);
2.您在二值化的程式部份建議加入下列紅色的程式碼:
  .....
  TheBitmap = Image1->Picture->Bitmap;
  TheBitmap->PixelFormat = pf24bit; // 建議加此行
  for (j=0; jPicture->Height; j  )
  .....
RichTop 敬上 =====***** 把數學當工具,可以解決問題;將數學變能力,能夠發現並解決問題! =====#####
RichTop您好: 我的問題是卡在在MouseDown的程式碼似乎都會將OrgBitmap填回Image1->Picture->Bitmap...我有測試如果把二值化去掉...就不會出現問題...把二值化放在一個只有開檔jpg的檔案裡...二值化後也不出現問題...可是當兩個合在一起就會出現二值化後滑鼠在image裡點一下會恢復成原來的圖片... 這是我全部的程式碼:
        //---------------------------------------------------------------------------    #include 
#pragma hdrstop
#include
#include "Unit1.h"
#include
int number=0;
int x_buffer[255];
int y_buffer[255];    //---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
                    OrgBitmap = new Graphics::TBitmap();
        Masking=false;
}
//---------------------------------------------------------------------------        void __fastcall TForm1::ExitwithSaving1Click(TObject *Sender)
{
        Close();        
}
//---------------------------------------------------------------------------    void __fastcall TForm1::N2Click(TObject *Sender)
{            if(OpenPictureDialog1->Execute())
        {
                Image1->AutoSize=true;
                Image1->Picture->LoadFromFile(OpenPictureDialog1->FileName);
                OrgBitmap->Assign(Image1->Picture->Bitmap);
        }
}
//---------------------------------------------------------------------------        void __fastcall TForm1::N10Click(TObject *Sender)
{
        if(SavePictureDialog1->Execute())
        {
                Image1->Picture->SaveToFile(SavePictureDialog1->FileName);            }
}
//---------------------------------------------------------------------------    void __fastcall TForm1::Image1MouseMove(TObject *Sender, TShiftState Shift,
      int X, int Y)
{               COLORREF color;
        color=Image1->Canvas->Pixels[X][Y];
        Label1->Caption=GetRValue(color);
        Label2->Caption=GetGValue(color);
        Label3->Caption=GetBValue(color);                Graphics::TBitmap *TheBitmap;
        TheBitmap=Image1->Picture->Bitmap;                        if(TheBitmap->ScanLine[0])
        {
                StatusBar1->Panels->Items[0]->Text="X座標=>" IntToStr(X);
                StatusBar1->Panels->Items[1]->Text="Y坐標=>" IntToStr(Y);
        }            if(Masking)
        {
                TheBitmap->Canvas->Pen->Style=psDash;
                TheBitmap->Canvas->Pen->Mode=pmBlack;
                TheBitmap->Canvas->Brush->Style=bsClear;                    TheBitmap->Canvas->Draw(0,0,OrgBitmap);
                TheBitmap->Canvas->Rectangle(StartX,StartY,X,Y);            }
      if(Pasting)
      {
        TheBitmap->Canvas->Draw(0,0,OrgBitmap);
        TheBitmap->Canvas->Draw(X-StartX,Y-StartY,PasteBitmap);
        TheBitmap->Canvas->Pen->Style=psDash;
        TheBitmap->Canvas->Pen->Mode=pmBlack;
        TheBitmap->Canvas->Brush->Style=bsClear;
        TheBitmap->Canvas->Rectangle(X-StartX,Y-StartY,X-StartX PasteBitmap->Width,
                           Y-StartY PasteBitmap->Height);          }    }
//---------------------------------------------------------------------------        void __fastcall TForm1::ComboBox1Change(TObject *Sender)
{
        float ScaleFactor;            if(ComboBox1->Text=="25%")
                ScaleFactor=4.0;
        else if(ComboBox1->Text=="50%")
                ScaleFactor=2.0;
        else if(ComboBox1->Text=="75%")
                ScaleFactor=1.25;
        else if(ComboBox1->Text=="100%")
                ScaleFactor=1.0;
        else if(ComboBox1->Text=="150%")
                ScaleFactor=0.75;
        else if(ComboBox1->Text=="200%")
                ScaleFactor=0.5;
        else
        {
                ScaleFactor=1.0;
                ComboBox1->Text="100%";
        }            Image1->AutoSize=false;
        Image1->Stretch=true;
        Image1->Width=Image1->Picture->Bitmap->Width/ScaleFactor;
        Image1->Height=Image1->Picture->Bitmap->Height/ScaleFactor;        }
//---------------------------------------------------------------------------        void __fastcall TForm1::N12Click(TObject *Sender)
{
        Graphics::TBitmap *TheBitmap, *TempBitmap;
        int Width,Height;
        Byte *ptr1,*ptr2;            TempBitmap= new Graphics::TBitmap();            TheBitmap= Image1->Picture->Bitmap;
        TheBitmap->PixelFormat=pf8bit;            TempBitmap->Assign(TheBitmap);            Width=TheBitmap->Width;
        Height=TheBitmap->Height;            for(int y=0;yScanLine[y];
                ptr2=(Byte*)TheBitmap->ScanLine[y];                    for(int x=0;xAssign(TempBitmap);
        delete TempBitmap;        
}
//---------------------------------------------------------------------------    void __fastcall TForm1::N13Click(TObject *Sender)
{
        Graphics::TBitmap *TheBitmap, *TempBitmap;
        int Width,Height;
        Byte *ptr1,*ptr2;            TempBitmap= new Graphics::TBitmap();            TheBitmap= Image1->Picture->Bitmap;            TempBitmap->Assign(TheBitmap);            Width=TheBitmap->Width;
        Height=TheBitmap->Height;            for(int y=0;yCanvas->Pixels[x][y]=
           TheBitmap->Canvas->Pixels[x][Height-1-y];            TheBitmap->Assign(TempBitmap);
        delete TempBitmap;
}
//---------------------------------------------------------------------------    void __fastcall TForm1::N15Click(TObject *Sender)
{
        Graphics::TBitmap *TheBitmap, *TempBitmap;
        int Width,Height;
        Byte *ptr1,*ptr2;            TempBitmap= new Graphics :: TBitmap();            TheBitmap=Image1->Picture->Bitmap;
        TheBitmap->PixelFormat=pf8bit;            Width=TheBitmap->Width;
        Height=TheBitmap->Height;            TempBitmap->Assign(TheBitmap);            TempBitmap->Width=Height;
        TempBitmap->Height=Width;            for(int y=0;yScanLine[y];
                for(int x=0;xScanLine[Width-1-x];
                        ptr1[y]=ptr2[x];
                }            }            TheBitmap->Assign(TempBitmap);
        delete TempBitmap;
}
//---------------------------------------------------------------------------    void __fastcall TForm1::N16Click(TObject *Sender)
{
          Graphics::TBitmap *TheBitmap, *TempBitmap;
        int Width,Height;
        Byte *ptr1,*ptr2;            TempBitmap= new Graphics :: TBitmap();            TheBitmap=Image1->Picture->Bitmap;
        TheBitmap->PixelFormat=pf8bit;            Width=TheBitmap->Width;
        Height=TheBitmap->Height;            TempBitmap->Assign(TheBitmap);            TempBitmap->Width=Height;
        TempBitmap->Height=Width;            for(int y=0;yScanLine[y];
                for(int x=0;xScanLine[x];
                        ptr1[Height-1-y]=ptr2[x];
                }            }            TheBitmap->Assign(TempBitmap);
        delete TempBitmap;    }
//---------------------------------------------------------------------------        void __fastcall TForm1::Image1MouseDown(TObject *Sender,
      TMouseButton Button, TShiftState Shift, int X, int Y)
{
        Graphics::TBitmap *TheBitmap;             char buf[64];
        sprintf(buf,"X座標=>%d, Y座標=>%d",X,Y);
        ListBox1->Items->Add(buf);
        x_buffer[number]=X;
        y_buffer[number]=Y;
        number  ;            StartX=X;
        StartY=Y;            if(Pasting_Enable)
         {
                Pasting=true; Masking=false;
                Pasting_Enable=false;
         }
        else
        {
                Masking=true; Pasting=false;
                TheBitmap=Image1->Picture->Bitmap;
                TheBitmap->Canvas->Draw(0,0,OrgBitmap);
        }                Masking=true;            TheBitmap=Image1->Picture->Bitmap;
        TheBitmap->Canvas->Draw(0,0,OrgBitmap);        
}
//---------------------------------------------------------------------------    void __fastcall TForm1::Image1MouseUp(TObject *Sender, TMouseButton Button,
      TShiftState Shift, int X, int Y)
{
        Graphics::TBitmap *TheBitmap;            if(Pasting)
        {
                TheBitmap=Image1->Picture->Bitmap;
                TheBitmap->Canvas->Draw(0,0,OrgBitmap);
                TheBitmap->Canvas->Draw(X-StartX,Y-StartY,PasteBitmap);
                OrgBitmap->Assign(TheBitmap);
                Pasting=false;
        }                Masking=false;            EndX=X;
        EndY=Y;
}
//---------------------------------------------------------------------------    void __fastcall TForm1::N6Click(TObject *Sender)
{
        Graphics::TBitmap *TheBitmap,*TempBitmap;
        int Width,Height;
        int TFX,TFY,BRX,BRY;
        TRect SrcRect,DestRect;            TheBitmap=Image1->Picture->Bitmap;            TheBitmap->Canvas->Draw(0,0,OrgBitmap);            TempBitmap= new Graphics::TBitmap();            Width=abs(EndX-StartX);
        Height=abs(EndY-StartY);            TFX= min(EndX,StartX); BRX= max(EndX,StartX);
        TFY= min(EndY,StartY); BRY= max(EndY,StartY);           TempBitmap->Width=Width;
        TempBitmap->Height=Height;
        TempBitmap->PixelFormat=pf24bit;
        TempBitmap->Canvas->CopyMode=cmSrcCopy;
        SrcRect=Rect(TFX,TFY,BRX,BRY);
        DestRect=Rect(0,0,Width,Height);
        TempBitmap->Canvas->CopyRect(DestRect,TheBitmap->Canvas,SrcRect);            Clipboard()->Assign(TempBitmap);            TheBitmap->Canvas->Pen->Style=psSolid;
        TheBitmap->Canvas->Pen->Mode=pmWhite;
        TheBitmap->Canvas->Brush->Style=bsSolid;
        TheBitmap->Canvas->Brush->Color=clWhite;
        TheBitmap->Canvas->Rectangle(TFX,TFY,BRX,BRY);            OrgBitmap->Assign(TheBitmap);        }
//---------------------------------------------------------------------------    void __fastcall TForm1::N7Click(TObject *Sender)
{
       Graphics::TBitmap *TheBitmap,*TempBitmap;
       int Width,Height;
       int TFX,TFY,BRX,BRY;
       TRect SrcRect,DestRect;           TheBitmap=Image1->Picture->Bitmap;           TheBitmap->Canvas->Draw(0,0,OrgBitmap);           TempBitmap=new Graphics::TBitmap();           Width=abs(EndX-StartX);
       Height=abs(EndY-StartY);           TFX=min(EndX,StartX); BRX=max(EndX,StartX);
       TFY=min(EndY,StartY); BRY=max(EndY,StartY);           TempBitmap->Width=Width;
       TempBitmap->Height=Height;
       TempBitmap->PixelFormat=pf24bit;
       TempBitmap->Canvas->CopyMode=cmSrcCopy;
       SrcRect=Rect(TFX,TFY,BRX,BRY);
       DestRect=Rect(0,0,Width,Height);           TempBitmap->Canvas->CopyRect(DestRect,TheBitmap->Canvas,SrcRect);           Clipboard()->Assign(TempBitmap);
}
//---------------------------------------------------------------------------    void __fastcall TForm1::N8Click(TObject *Sender)
{
        Graphics::TBitmap *TheBitmap;            TheBitmap=Image1->Picture->Bitmap;            if(Clipboard()->HasFormat(CF_BITMAP))
        {
                PasteBitmap->Assign(Clipboard());                    TheBitmap->Canvas->Draw(0,0,PasteBitmap);                    TheBitmap->Canvas->Pen->Width=2;
                TheBitmap->Canvas->Pen->Mode=pmBlack;
                TheBitmap->Canvas->Brush->Style=bsClear;
         TheBitmap->Canvas->Rectangle(0,0,PasteBitmap->Width,PasteBitmap->Height);
                Pasting_Enable=true;
        }
}
//---------------------------------------------------------------------------     void __fastcall TForm1::Button1Click(TObject *Sender)
{
   Graphics::TBitmap *TheBitmap;
   Byte *ptr;
   int r,g,b;
   int gray;
   int i,j;
   TheBitmap = Image1->Picture->Bitmap;
   for (j=0; jHeight; j  )
   {
      ptr = (Byte *)TheBitmap->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>125)
         gray = 255;
         else
            gray = 0;
         ptr[i*3  ] = (Byte)gray;
         ptr[i*3 1] = (Byte)gray;
         ptr[i*3 2] = (Byte)gray;
      }
   }
   Image1->Picture->Assign(TheBitmap);
}        //---------------------------------------------------------------------------        
richtop
資深會員


發表:122
回覆:646
積分:468
註冊:2003-06-10

發送簡訊給我
#5 引用回覆 回覆 發表時間:2005-08-01 17:20:36 IP:140.129.xxx.xxx 未訂閱
tracydai1025 您好: 因貼文的資料牽涉元件略多不方便重建該程式場景! 如果您不介意的話是否可將原始程式上傳,這樣比較容易找出問題所在! RichTop 敬上 =====***** 把數學當工具,可以解決問題;將數學變能力,能夠發現並解決問題! =====#####
tracydai1025
一般會員


發表:13
回覆:4
積分:3
註冊:2005-05-08

發送簡訊給我
#6 引用回覆 回覆 發表時間:2005-08-02 04:57:10 IP:218.165.xxx.xxx 未訂閱
RichTop大大您好 我已將檔案上傳 http://delphi.ktop.com.tw/topic.php?TOPIC_ID=76025 THX~
richtop
資深會員


發表:122
回覆:646
積分:468
註冊:2003-06-10

發送簡訊給我
#7 引用回覆 回覆 發表時間:2005-08-02 08:22:51 IP:140.129.xxx.xxx 未訂閱
tracydai1025 您好:    在Image1MouseDown(.)中,您總會將 OrgBitmap 填回 Image1,這應該就是問題所在! 相信這只要修改您的工作邏輯就可解決! 因要參加研討會,所以只能先簡單回答,請參考!    
void __fastcall TForm1::Image1MouseDown(TObject *Sender,
      TMouseButton Button, TShiftState Shift, int X, int Y)
{
        Graphics::TBitmap *TheBitmap;             char buf[64];
        sprintf(buf,"X座標=>%d, Y座標=>%d",X,Y);
        ListBox1->Items->Add(buf);
        x_buffer[number]=X;
        y_buffer[number]=Y;
        number  ;            StartX=X;
        StartY=Y;            if(Pasting_Enable)
         {
                Pasting=true; Masking=false;
                Pasting_Enable=false;
         }
        else
        {
                Masking=true; Pasting=false;
                TheBitmap=Image1->Picture->Bitmap;
                TheBitmap->Canvas->Draw(0,0,OrgBitmap);
                ShowMessage("Welcome!");
        }            Masking=true;            TheBitmap=Image1->Picture->Bitmap;
        TheBitmap->Canvas->Draw(0,0,OrgBitmap);
        ShowMessage("Welcome again!");
}
RichTop 敬上 =====***** 把數學當工具,可以解決問題;將數學變能力,能夠發現並解決問題! =====#####
tracydai1025
一般會員


發表:13
回覆:4
積分:3
註冊:2005-05-08

發送簡訊給我
#8 引用回覆 回覆 發表時間:2005-08-05 10:50:49 IP:220.134.xxx.xxx 未訂閱
謝謝RichTop大大的指教    問題已解決~
系統時間:2024-04-30 0:47:03
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!