C++Builder Programming Forum
C++Builder  |  Delphi  |  FireMonkey  |  C/C++  |  Free Pascal  |  Firebird
볼랜드포럼 BorlandForum
 경고! 게시물 작성자의 사전 허락없는 메일주소 추출행위 절대 금지
C++빌더 포럼
Q & A
FAQ
팁&트릭
강좌/문서
자료실
컴포넌트/라이브러리
메신저 프로젝트
볼랜드포럼 홈
헤드라인 뉴스
IT 뉴스
공지사항
자유게시판
해피 브레이크
공동 프로젝트
구인/구직
회원 장터
건의사항
운영진 게시판
회원 메뉴
북마크
볼랜드포럼 광고 모집

C++빌더 팁&트릭
C++Builder Programming Tip&Tricks
[687] [ImageProcessing] 이미지(TBitmap) 대칭 변환
장성호 [nasilso] 9363 읽음    2007-07-24 14:37
이미지 변환에 관한 자료는 너무 많아서 딱히 어디서 펌해왔다고 하기는  멋하구...
그래도 CBuilder로 된 자료는 잘 안보이는것 같아 올려봅니다.

TImage 와 Graphics::TBitmap을 이용합니다.
//---------------------<>--------------------------------------------
void __fastcall iSwap(int *a,int *b) //데이타 바꿔치키
{
    int tmp;
    tmp=*a;
    *a=*b;
    *b=tmp;
}

//-------------------<<좌우대칭>>------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    Graphics::TBitmap *bmp=new Graphics::TBitmap;
    bmp->Width=Image1->Width;
    bmp->Height=Image1->Height;
    bmp->Assign(Image1->Picture->Bitmap);
    bmp->PixelFormat=pf32bit;

    int *pt1;
    int *pt2;
    for(int h=0;hHeight;h++)
    {
        pt1=(int *)bmp->ScanLine[h];
        pt2=pt1+bmp->Width-1;
        while(pt1Width=bmp->Width;
    Image2->Height=bmp->Height;
    Image2->Picture->Bitmap->Assign(bmp);
    delete bmp;

}

//---------------------<<상하대칭>>-------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
    Graphics::TBitmap *bmp=new Graphics::TBitmap;
    bmp->Width=Image1->Width;
    bmp->Height=Image1->Height;
    bmp->Assign(Image1->Picture->Bitmap);
    bmp->PixelFormat=pf32bit;

    int *pt1;
    int *pt2;
    for(int h=0;hHeight/2;h++)
    {
        pt1=(int *)bmp->ScanLine[h];
        pt2=(int *)bmp->ScanLine[bmp->Height-h-1];
        for(int w=0;wWidth;w++)
        {
            iSwap(pt1,pt2);
            pt1++;
            pt2++;
        }
    }
    Image2->Width=bmp->Width;
    Image2->Height=bmp->Height;
    Image2->Picture->Bitmap->Assign(bmp);
    delete bmp;
}

//---------------------<<대각대칭>>-------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
    Graphics::TBitmap *bmp=new Graphics::TBitmap;
    bmp->Width=Image1->Width;
    bmp->Height=Image1->Height;
    bmp->Assign(Image1->Picture->Bitmap);
    bmp->PixelFormat=pf32bit;

    int *pt1;
    int *pt2;
    for(int h=0;hHeight/2;h++)
    {
        pt1=(int *)bmp->ScanLine[h];
        pt2=(int *)bmp->ScanLine[bmp->Height-h-1];
        pt2=pt2+bmp->Width-1;
        for(int w=0;wWidth;w++)
        {
            iSwap(pt1,pt2);
            pt1++;
            pt2--;
        }
    }
    Image2->Width=bmp->Width;
    Image2->Height=bmp->Height;
    Image2->Picture->Bitmap->Assign(bmp);
    delete bmp;
}

//---------------------<>-----------------------------------------
void __fastcall TForm1::Button4Click(TObject *Sender)
{
    Graphics::TBitmap *bmp=new Graphics::TBitmap;
    bmp->Width=Image1->Width;
    bmp->Height=Image1->Height;
    bmp->Assign(Image1->Picture->Bitmap);
    bmp->PixelFormat=pf32bit;

    unsigned char *cpt;
    unsigned char  tmp;

    for(int h=0;hHeight;h++)
    {
        cpt=(unsigned char *)bmp->ScanLine[h];
        for(int w=0;wWidth;w++)
        {
            //방법1
            //tmp=(cpt[0]+cpt[1]+cpt[2])/3;      
            //방법2    ==> 이방법이 쬐금더 빠름
            tmp=(cpt[0]+cpt[1]*4+cpt[2]*3)>>3;   
            cpt[0]=tmp;
            cpt[1]=tmp;
            cpt[2]=tmp;
            cpt+=4;
        }
    }
    
    Image2->Width=bmp->Width;
    Image2->Height=bmp->Height;
    Image2->Picture->Bitmap->Assign(bmp);
    delete bmp;
}
//---------------------------------------------------------------------------


누구게 도움이 될진 모르겠지만..
만약 제가 CBuilder를 처음시작하때 이런 자료를 볼수있었다면 제게는 도움이 되었을것 같아서..
허정주 [tinydew4]   2007-07-24 22:24 X
방법1과 방법2 가 같은겁니까?
>>3 는 3비트 옮겨서 /8의 효과가 나지 않나요? 같은 값은 아닌거 같은데...상관없는 것인지요
장성호 [nasilso]   2007-07-25 00:45 X
확실이 그래이스케일결과가 다르네요
두번째가 쪼금 부드러운듯

델마당  게임제작 게시판의 
조무영님의[비dx강좌] 3.채널 분리, 그레이스케일, RGB 조절하기 의 내용을 참조했습니다
http://www.delmadang.com/cwb-bin/CrazyWWWBoard.exe?db=dmdgame3&mode=read&num=120&page=141
외랑 [jaehuns]   2007-07-25 13:58 X
먼저 좋은 팁에 감사드립니다. 읽는 즐거움...^^
상하 / 좌우 대칭은 이렇게 하시는게 휠씬 빠르겠죠.. 이것도 참고하시길~~
Picture에서 영상 표시하고 있다고 생각했을 때입니다.
사용하던 소스 중 따낸거라 완전하진 않습니다.
그냥 방법만 눈으로 보시면 아실거라 생각합니다.

int iw = imgPreview->Picture->Width;
int ih = imgPreview->Picture->Height;

if(좌우 대칭이면)
{
   bFlipLR = !bFlipLR;  // 상태 기억
   imgPreview->Canvas->StretchDraw(Rect( iw , 0, 0 , ih), imgPreview->Picture->Graphic);
}
else  // 상하 대칭이면
{
   bFlipTB = !bFlipTB;  // 역시 상태 기억
   imgPreview->Canvas->StretchDraw(Rect( 0 , ih, iw , 0), imgPreview->Picture->Graphic);
}

장성호 [nasilso]   2007-07-25 14:37 X

ㅎㅎ 훨씬 쉬운 방법이 있었네요
대각선 대칭도 같은 방법으로 가능하네요


Image2->Canvas->StretchDraw(Rect(Image1->Picture->Width , Image1->Picture->Height,0  , 0), Image1->Picture->Graphic);


해보지도 않고
Rect(x1,y1,x2,y2) 에서
x1 <x2  이고 y1 < y2 여야 된다는 선입관이 있었네요
장성호 [nasilso]   2007-07-26 16:28 X
torry.net에 보니  grayscale 하는 다른 방법도 있네요..

http://www.swissdelphicenter.ch/torry/showcode.php?id=437
김태선 [cppbuilder]   2007-07-26 21:14 X
몇 년전에 그래픽 프로젝트가 있어서 그때 해봤던 기억이 소록 소록 나는 군요.
장성호 [nasilso]   2009-09-28 20:50 X
최근에 영상처리관련 작업을 하게되어 다시 들어와 보게되었습니다.

gray-scale 방법에 여러가지가 있던데..

방법1   Result = (red+green+blue) / 3
방법2   Result =  red * 0.3 + green * 0.59 + blue*0.1 ;
방법3   Result =  sqrt( red^2 + green^2 + blue^2 ) /sqrt(3);
방법4   Result =  red * 0.21 + green * 0.71 + blue*0.07
방법5   Result =  RGB중 하나만 사용


등이 있던데요

OpenCV에서는 위 방법중 2번째 방법을 사용한다고 합니다.

그리고 위 소스에 방법은?

   Result = red * 3.75  + green * 5 + blue * 1.25 와 같네요

그럼..

+ -

관련 글 리스트
687 [ImageProcessing] 이미지(TBitmap) 대칭 변환 장성호 9363 2007/07/24
Google
Copyright © 1999-2015, borlandforum.com. All right reserved.