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

對彩色轉灰階後的圖片濾波,卻對彩色圖片濾波,該如何解決。

尚未結案
circa
一般會員


發表:6
回覆:2
積分:1
註冊:2005-03-13

發送簡訊給我
#1 引用回覆 回覆 發表時間:2005-05-20 19:21:22 IP:220.143.xxx.xxx 未訂閱
下面是我的程式碼,我想要將一張彩色影像轉換成256色灰階之後,在使用平均、中值等等濾波器對灰階影像去濾波。但是,轉灰階之後,濾波卻是對彩色的圖片做濾波,請各位大大可以教我怎麼修改這個程式的程式碼。謝謝囉!!     
 #include 
#include <math.h>
#pragma hdrstop    #include "HW4.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------    void __fastcall TForm1::Open1Click(TObject *Sender)
{
if(this->OpenDialog1->Execute())
  {
    open = true;
    this->Image1->Picture->LoadFromFile(this->OpenDialog1->FileName);
    this->Image1->Picture->Bitmap->PixelFormat = pf32bit;
    ImageW = this->Image1->Picture->Bitmap->Width;
    ImageH = this->Image1->Picture->Bitmap->Height;        Data = (RGBA**)new RGBA*[ImageH];
    tempData = (RGBA**)new RGBA*[ImageH];
    for(int x=0;xImage1->Picture->Bitmap->ScanLine[x],sizeof(RGBA)*ImageW);
      memcpy(tempData[x],this->Image1->Picture->Bitmap->ScanLine[x],sizeof(RGBA)*ImageW);
    }        this->Image2->Picture->Bitmap->Width =  ImageW;
    this->Image2->Picture->Bitmap->Height = ImageH;
    
    this->Image2->Left = this->Image1->Left   this->Image1->Width   20;
    this->Image2->Visible = true;
    for(int x=0;xImage2->Canvas->Pixels[y][x] = this->Image1->Canvas->Pixels[y][x];
      }
  }        
}
//---------------------------------------------------------------------------    void __fastcall TForm1::Save1Click(TObject *Sender)
{
if(this->SaveDialog1->Execute())
    this->Image2->Picture->SaveToFile(this->SaveDialog1->FileName ".bmp");
}
//---------------------------------------------------------------------------    void __fastcall TForm1::Close1Click(TObject *Sender)
{
 Close();        
}
//---------------------------------------------------------------------------    void __fastcall TForm1::Averagefilter1Click(TObject *Sender)
{
 this->Image2->Visible = true;
  //配置AverageData
  RGBA** AverageData=(RGBA**)new RGBA*[ImageH];
  for (int y=0; yCanvas->Pixels[j][i]=(TColor)RGB(AverageData[i][j].R,AverageData[i][j].G,AverageData[i][j].B);
 //釋放meandata記憶體
  for (int y=0; y0; a--)
      {
        for(int b=0; btempr[b 1])
          {
            int temp=tempr[b 1];
            tempr[b 1]=tempr[b];
            tempr[b]=temp;
          }
          if(tempg[b]>tempg[b 1])
          {
            int temp=tempg[b 1];
            tempg[b 1]=tempg[b];
            tempg[b]=temp;
          }
          if(tempb[b]>tempb[b 1])
          {
            int temp=tempb[b 1];
            tempb[b 1]=tempb[b];
            tempb[b]=temp;
          }
        }
      }
      Middledata[i][j].R=tempr[4];
      Middledata[i][j].G=tempg[4];
      Middledata[i][j].B=tempb[4];
    }
  }
  //show圖
  for (int i=0; iCanvas->Pixels[j][i]=(TColor)RGB(Middledata[i][j].R,Middledata[i][j].G,Middledata[i][j].B);
 //釋放middledata記憶體
  for (int y=0; yPicture->Bitmap;
 if(!pic->Empty){
  unsigned char **Buffer = new unsigned char*[pic->Height];
  for (i=0; i< pic->Height; i  )
     Buffer[i] = new unsigned char[pic->Width];
  pic->PixelFormat = pf32bit;
  RGBQUAD *Pixel;      for (Row=0; Row < pic->Height ; Row  ){
    Pixel = (RGBQUAD *) pic->ScanLine[Row];
    for (Col=0; Col < pic->Width; Col  , Pixel  )
      Buffer[Row][Col] = (BYTE) ((float)Pixel->rgbRed*0.299
                                 (float)Pixel->rgbGreen*0.587
                                 (float)Pixel->rgbBlue*0.114);}      for (Row=0; Row < pic->Height ; Row  ){
    Pixel = (RGBQUAD *) pic->ScanLine[Row];
    for (Col=0; Col < pic->Width; Col  , Pixel  ){
       Pixel->rgbRed = (BYTE) Buffer[Row][Col];
       Pixel->rgbGreen = (BYTE) Buffer[Row][Col];
       Pixel->rgbBlue = (BYTE) Buffer[Row][Col];
       }
    }
  Image1->Invalidate();      for (i=0; iHeight; i  )
    delete[] Buffer[i];
    delete [] Buffer;
    }
}
//---------------------------------------------------------------------------    void __fastcall TForm1::Maxfilter1Click(TObject *Sender)
{
  //配置Maxdata
  RGBA** Maxdata=(RGBA**)new RGBA*[ImageH];
  for (int y=0; y0; a--)
      {
        for(int b=0; btempr[b 1])
          {
            int temp=tempr[b 1];
            tempr[b 1]=tempr[b];
            tempr[b]=temp;
          }
          if(tempg[b]>tempg[b 1])
          {
            int temp=tempg[b 1];
            tempg[b 1]=tempg[b];
            tempg[b]=temp;
          }
          if(tempb[b]>tempb[b 1])
          {
            int temp=tempb[b 1];
            tempb[b 1]=tempb[b];
            tempb[b]=temp;
          }
        }
      }
      Maxdata[i][j].R=tempr[8];
      Maxdata[i][j].G=tempg[8];
      Maxdata[i][j].B=tempb[8];
    }
  }
  //show圖
  for (int i=0; iCanvas->Pixels[j][i]=(TColor)RGB(Maxdata[i][j].R,Maxdata[i][j].G,Maxdata[i][j].B);
 //釋放Maxdata記憶體
  for (int y=0; y0; a--)
      {
        for(int b=0; btempr[b 1])
          {
            int temp=tempr[b 1];
            tempr[b 1]=tempr[b];
            tempr[b]=temp;
          }
          if(tempg[b]>tempg[b 1])
          {
            int temp=tempg[b 1];
            tempg[b 1]=tempg[b];
            tempg[b]=temp;
          }
          if(tempb[b]>tempb[b 1])
          {
            int temp=tempb[b 1];
            tempb[b 1]=tempb[b];
            tempb[b]=temp;
          }
        }
      }
      Mindata[i][j].R=tempr[0];
      Mindata[i][j].G=tempg[0];
      Mindata[i][j].B=tempb[0];
    }
  }
  //show圖
  for (int i=0; iCanvas->Pixels[j][i]=(TColor)RGB(Mindata[i][j].R,Mindata[i][j].G,Mindata[i][j].B);
 //釋放Maxdata記憶體
  for (int y=0; y    愛情換來的是幸福,是永恆,還是心碎的眼淚?
        
------
愛情換來的是幸福,是永恆,還是心碎的眼淚?
Demo99
一般會員


發表:2
回覆:8
積分:2
註冊:2003-06-27

發送簡訊給我
#2 引用回覆 回覆 發表時間:2005-05-25 12:33:11 IP:61.219.xxx.xxx 未訂閱
你讀檔後的影像資料放在tempData,各遮罩處理時也使用tempData的內容 而灰階化後的影像是存到Image2,卻沒有改變tempData的內容 所以只會對剛開始讀入的彩色影像做處理 在灰階化過程中更新tempData應該就能解決了
circa
一般會員


發表:6
回覆:2
積分:1
註冊:2005-03-13

發送簡訊給我
#3 引用回覆 回覆 發表時間:2005-05-25 21:55:04 IP:220.143.xxx.xxx 未訂閱
引言:你讀檔後的影像資料放在tempData,各遮罩處理時也使用tempData的內容 而灰階化後的影像是存到Image2,卻沒有改變tempData的內容 所以只會對剛開始讀入的彩色影像做處理 在灰階化過程中更新tempData應該就能解決了
謝謝這位大大的指點,可是我改了很多次都還是不行。 不知道是不是我改的方式錯了,希望大大能夠在指點我一下。感謝!! 愛情換來的是幸福,是永恆,還是心碎的眼淚?
------
愛情換來的是幸福,是永恆,還是心碎的眼淚?
Demo99
一般會員


發表:2
回覆:8
積分:2
註冊:2003-06-27

發送簡訊給我
#4 引用回覆 回覆 發表時間:2005-06-01 01:22:11 IP:59.112.xxx.xxx 未訂閱
Gray那個函式裡面,將tempData也改資料就可以了...如下(多了中間那幾個tempData..) 剛剛用你的程式碼測試了Average遮罩部分沒問題..其他應該也沒問題才對.. [code] for (Row=0; Row < pic->Height ; Row ){ Pixel = (RGBQUAD *) pic->ScanLine[Row]; for (Col=0; Col < pic->Width; Col , Pixel ){ Pixel->rgbRed = tempData[Row][Col].R = (BYTE) Buffer[Row][Col]; Pixel->rgbGreen = tempData[Row][Col].G = (BYTE) Buffer[Row][Col]; Pixel->rgbBlue = tempData[Row][Col].B = (BYTE) Buffer[Row][Col]; } } [code]
系統時間:2024-05-03 9:57:20
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!