C#圖片處理之:最簡單的柔化演算法

iDotNetSpace發表於2008-09-24

原文http://blog.csdn.net/ki1381/archive/2007/05/10/1603602.aspx
與銳化相反,柔化是使圖片看起來更柔滑(其實也是模糊的委婉的說法)。柔化的一個最簡單的實現演算法就是取圖片上的每一點(圖片邊緣點忽略),計算它周圍八個點的平均值作為新畫素值。想想也是,之所以看起來模糊,確實是因為周圍的點長的有點像,區別不太明顯。

        // ============================柔化==============================
        /// 
        
/// 柔化
        
/// 原始圖
        
/// 輸出圖

        public static Bitmap KiBlur(Bitmap b)
        
{

            
if (b == null)
            
{
                
return null;
            }


            
int w = b.Width;
            
int h = b.Height;

            
try
            
{

                Bitmap bmpRtn 
= new Bitmap(w, h, PixelFormat.Format24bppRgb);

                BitmapData srcData 
= b.LockBits(new Rectangle(00, w, h), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
                BitmapData dstData 
= bmpRtn.LockBits(new Rectangle(00, w, h), ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);

                
unsafe
                
{
                    
byte* pIn = (byte*)srcData.Scan0.ToPointer();
                    
byte* pOut = (byte*)dstData.Scan0.ToPointer();
                    
int stride = srcData.Stride;
                    
byte* p;

                    
for (int y = 0; y < h; y++)
                    
{
                        
for (int x = 0; x < w; x++)
                        
{
                            
//取周圍9點的值
                            if (x == 0 || x == w - 1 || y == 0 || y == h - 1)
                            
{
                                
//不做
                                pOut[0= pIn[0];
                                pOut[
1= pIn[1];
                                pOut[
2= pIn[2];
                            }

                            
else
                            
{
                                
int r1, r2, r3, r4, r5, r6, r7, r8, r9;
                                
int g1, g2, g3, g4, g5, g6, g7, g8, g9;
                                
int b1, b2, b3, b4, b5, b6, b7, b8, b9;

                                
float vR, vG, vB;

                                
//左上
                                p = pIn - stride - 3;
                                r1 
= p[2];
                                g1 
= p[1];
                                b1 
= p[0];

                                
//正上
                                p = pIn - stride;
                                r2 
= p[2];
                                g2 
= p[1];
                                b2 
= p[0];

                                
//右上
                                p = pIn - stride + 3;
                                r3 
= p[2];
                                g3 
= p[1];
                                b3 
= p[0];

                                
//左側
                                p = pIn - 3;
                                r4 
= p[2];
                                g4 
= p[1];
                                b4 
= p[0];

                                
//右側
                                p = pIn + 3;
                                r5 
= p[2];
                                g5 
= p[1];
                                b5 
= p[0];

                                
//右下
                                p = pIn + stride - 3;
                                r6 
= p[2];
                                g6 
= p[1];
                                b6 
= p[0];

                                
//正下
                                p = pIn + stride;
                                r7 
= p[2];
                                g7 
= p[1];
                                b7 
= p[0];

                                
//右下
                                p = pIn + stride + 3;
                                r8 
= p[2];
                                g8 
= p[1];
                                b8 
= p[0];

                                
//自己
                                p = pIn;
                                r9 
= p[2];
                                g9 
= p[1];
                                b9 
= p[0];

                                vR 
= (float)(r1 + r2 + r3 + r4 + r5 + r6 + r7 + r8 + r9);
                                vG 
= (float)(g1 + g2 + g3 + g4 + g5 + g6 + g7 + g8 + g9);
                                vB 
= (float)(b1 + b2 + b3 + b4 + b5 + b6 + b7 + b8 + b9);

                                vR 
/= 9;
                                vG 
/= 9;
                                vB 
/= 9;

                                pOut[
0= (byte)vB;
                                pOut[
1= (byte)vG;
                                pOut[
2= (byte)vR;

                            }


                            pIn 
+= 3;
                            pOut 
+= 3;
                        }
// end of x

                        pIn 
+= srcData.Stride - w * 3;
                        pOut 
+= srcData.Stride - w * 3;
                    }
 // end of y
                }


                b.UnlockBits(srcData);
                bmpRtn.UnlockBits(dstData);

                
return bmpRtn;
            }

            
catch
            
{
                
return null;
            }


        }
 // end of KiBlur

當然這是最簡單的一種,不支援柔化半徑選擇。實際中通常需要自定義模糊的程度,這個下回再談。

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-462848/,如需轉載,請註明出處,否則將追究法律責任。

相關文章