2016年12月27日 星期二

03360723 林政儒 HW2

原圖



直方圖均化





處理後的直方圖

程式碼:

for (i = 0 ; i < 3; i++)
 for ( j = 0 ; j < 256 ; j++ )
  {
  iTotal [i][j] = 0 ; //存取累計
  iHistogram[i][j]  = 0 ;//存取
  }

for( j = 0  ; j < iImageHeight ; j++ )
 {
 bPtrImageRow0 = (Byte *)Image2->Picture->Bitmap->ScanLine[j];
 for ( i = 0; i < iImageWidth  ; i++ )
  for ( k = 0 ; k < 3 ; k++ )
   {
   iHistogram[k][bPtrImageRow0[ i * 3  + k ]]++ ; //
   if (iHistogram[k][bPtrImageRow0[ i * 3  + k ]]>iMax[k])
    iMax[k] = iHistogram[k][bPtrImageRow0[ i * 3  + k ]] ;//計算顏色
   }
 }
for (i = 0 ; i < 3; i++)
 for ( j = 0 ; j < 256 ; j++ )
  iMaxT[i] +=  iHistogram[i][j] ;//計算顏色總累計

for (i = 0 ; i < 3; i++)
 {
 iSum = 0 ;
 for ( j = 0 ; j < 256 ; j++ )
  {
  iSum += iHistogram[i][j] ;
  iHistogram[i][j]  = int( double ( iHistogram[i][j]*200/iMax[i]) );
  iTotal [i][j] = int( double ( iSum*200/iMaxT[i] ));
  }
 }
Image6->Canvas->Brush->Color = clBlack ;
Image6->Canvas->FillRect(Rect(0,0,256,200));
Image7->Canvas->Brush->Color = clBlack ;
Image7->Canvas->FillRect(Rect(0,0,256,200));
Image8->Canvas->Brush->Color = clBlack ;
Image8->Canvas->FillRect(Rect(0,0,256,200));
Image9->Canvas->Brush->Color = clBlack ;
Image9->Canvas->FillRect(Rect(0,0,256,200));
Image10->Canvas->Brush->Color = clBlack ;
Image10->Canvas->FillRect(Rect(0,0,256,200));
Image11->Canvas->Brush->Color = clBlack ;
Image11->Canvas->FillRect(Rect(0,0,256,200));

//線
for (i = 0 ; i < 256; i++)
 {
 Image6->Canvas->Pen->Color = clBlue ;
 Image6->Canvas->MoveTo(i  , 200) ;
 Image6->Canvas->LineTo(i  , 200-iHistogram[0][i]) ;
 Image7->Canvas->Pen->Color = clGreen ;
 Image7->Canvas->MoveTo(i  , 200) ;
 Image7->Canvas->LineTo(i  , 200-iHistogram[1][i]) ;
 Image8->Canvas->Pen->Color = clRed ;
 Image8->Canvas->MoveTo(i  , 200) ;
 Image8->Canvas->LineTo(i  , 200-iHistogram[2][i]) ;
 Image9->Canvas->Pen->Color = clBlue ;
 Image9->Canvas->MoveTo(i  , 200) ;
 Image9->Canvas->LineTo(i  , 200-iTotal[0][i]) ;
 Image10->Canvas->Pen->Color = clGreen ;
 Image10->Canvas->MoveTo(i  , 200) ;
 Image10->Canvas->LineTo(i  , 200-iTotal[1][i]) ;
 Image11->Canvas->Pen->Color = clRed ;
 Image11->Canvas->MoveTo(i  , 200) ;
 Image11->Canvas->LineTo(i  , 200-iTotal[2][i]) ;
 }



其實程式並不難
了解每一行做的事情
多花點心就可以做出來
但是在碰到很多卡住的地方
經過同學的指導後 才慢慢地去寫出來
雖然在之前的"互動媒體"已經有了解過
但當自己去寫出來的時候 那股成就感真的不是蓋的
同時通過這次功課也對圖片有更深的了解
在三原色的部份以及怎麼調整均化(圖片過暗)等問題
都有更進一步的認識了

沒有留言:

張貼留言