2015年1月14日 星期三

HW2 資工三乙 01360681 許智綱

一開始點選自己要的圖片


                                                           然後畫出機率密度函數圖(PDF)




                                                            接著畫出累積分佈函數圖(CDF)
                                                                        PDF的圖被覆蓋住


                                                                  清除PDF後畫CDF




                                                                     用另一張圖來試


以下是C#做出來的效果



這裡做的是把圖的寬跟高用iImageWidth及iImageHeight來表示

void __fastcall TForm1::Image1Click(TObject *Sender){
  if(OpenPictureDialog1->Execute())
Image2->Picture->LoadFromFile(OpenPictureDialog1->FileName);
iImageWidth=Image2->Picture->Width;
iImageHeight=Image2->Picture->Height;
}



//---------------------------------------------------------------------------
void __fastcall TForm1::HistogramClick(TObject *Sender)
{

for(i=0; i<=255; i++)
{
arr_Blue[i] = 0;
arr_Green[i] = 0;
arr_Red[i] = 0;
}
          把陣列裡的數值歸零




for(j=0; j<iImageHeight; j++)
{
bPtrImageRow = (Byte *)Image2->Picture->Bitmap->ScanLine[j];
for(int i=0; i<iImageWidth; i++)
{
arr_Red[bPtrImageRow[3*i+2]]++;
arr_Green[bPtrImageRow[3*i+1]]++;
arr_Blue[bPtrImageRow[3*i+0]]++;
}
}
        計算出三個色彩中數值0-255所分別出現的次數


for(i=0; i<=255; i++)
{
Image6->Canvas->Pen->Color = clRed;
Image6->Canvas->MoveTo(i,150);
Image6->Canvas->LineTo(i,150 - arr_Red[i]/80);

Image5->Canvas->Pen->Color = clGreen;
Image5->Canvas->MoveTo(i,150);
Image5->Canvas->LineTo(i,150 - arr_Green[i]/80);

Image4->Canvas->Pen->Color = clBlue;
Image4->Canvas->MoveTo(i,150);
Image4->Canvas->LineTo(i,150 - arr_Blue[i]/80);
}
}
畫出PDF圖再介面右半邊
//---------------------------------------------------------------------------
void __fastcall TForm1::CDFClick(TObject *Sender)
{
BSum=0;
RSum=0;
GSum=0;
for(i=0; i<=255; i++)
{
arr_Blue[i] = 0;
arr_Green[i] = 0;
arr_Red[i] = 0;
}
一樣先把陣列裡數值歸零

for(j=0; j<iImageHeight; j++)
{
bPtrImageRow = (Byte *)Image2->Picture->Bitmap->ScanLine[j];
for(i=0; i<iImageWidth; i++)
{
arr_Blue[bPtrImageRow[3*i+0]]++;
arr_Green[bPtrImageRow[3*i+1]]++;
arr_Red[bPtrImageRow[3*i+2]]++;
}
}

跟畫PDF時一樣計算出色彩值0-255每個值出現的次數


for(i=0; i<=255; i++)
{
BSum+=arr_Blue[i];
RSum+=arr_Red[i];
GSum+=arr_Green[i];
Image4->Canvas->Pen->Color = clBlue;
Image4->Canvas->MoveTo(i,150);
Image4->Canvas->LineTo(i,150 - BSum/3000);

Image5->Canvas->Pen->Color = clGreen;
Image5->Canvas->MoveTo(i,150);
Image5->Canvas->LineTo(i,150 - GSum/3000);

Image6->Canvas->Pen->Color = clRed;
Image6->Canvas->MoveTo(i,150);
Image6->Canvas->LineTo(i,150 - RSum/3000);
}
}
畫出CDF圖
而那個除3000是為了讓極值不會衝出畫面
//---------------------------------------------------------------------------
void __fastcall TForm1::ClearClick(TObject *Sender)
{
iImageWidth4=Image4->Picture->Width;
iImageHeight4=Image4->Picture->Height;
for(j=0;j<=iImageWidth4;j++)
{
Image4->Canvas->Pen->Color = clWhite;
Image4->Canvas->MoveTo(j,0);
Image4->Canvas->LineTo(j,iImageHeight4);

Image5->Canvas->Pen->Color = clWhite;
Image5->Canvas->MoveTo(j,0);
Image5->Canvas->LineTo(j,iImageHeight4);

Image6->Canvas->Pen->Color = clWhite;
Image6->Canvas->MoveTo(j,0);
Image6->Canvas->LineTo(j,iImageHeight4);
}
將圖用白色覆蓋過去


這是第一次用C++Builder寫程式,所以當然只能多
參考多問別人,語法也跟之前用的程式差異滿大的,
除了語法外的問題大概就時畫圖時即直飛出畫面
外了。

沒有留言:

張貼留言