tracydai1025
一般會員
發表:13 回覆:4 積分:3 註冊:2005-05-08
發送簡訊給我
|
請教各位大大
我在圖片完成執行二值化時
滑鼠在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);
}
|
tracydai1025
一般會員
發表:13 回覆:4 積分:3 註冊:2005-05-08
發送簡訊給我
|
引言:
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);
} //---------------------------------------------------------------------------
|