图像处理(2)--简单的效果处理


本文主要说说几种简单图像效果处理的原理和实现

重要源码都贴出来了,如果需要所有源码的留下Email吧

原图效果:
%E5%8E%9F%E5%9B%BE.JPG

一、反色:
         图像反色实际上就是取没一个像素点的相对颜色值
 比如图像某点像素RGB(128,52,38),则它的反色值为RGB(127,203,217)
 %E5%8F%8D%E8%89%B2.JPG

ExpandedBlockStart.gif ContractedBlock.gif          /**/ /// <summary>
InBlock.gif        
/// 以反色方式显示图像    
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="SImage">源图像</param>
ExpandedBlockEnd.gif        
/// <returns>反色处理后的图像</returns>

None.gif          public  Bitmap fanSe(Image SImage)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {            
InBlock.gif            
int Height = SImage.Height;
InBlock.gif            
int Width = SImage.Width;
InBlock.gif            Bitmap bitmap
=new Bitmap(Width,Height);
InBlock.gif            Bitmap MyBitmap
=(Bitmap)SImage;
InBlock.gif            Color pixel;
InBlock.gif            
for(int x=1;x<Width;x++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
for(int y=1;y<Height;y++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
int r,g,b;
InBlock.gif                    pixel
=MyBitmap.GetPixel(x,y);                    
InBlock.gif                    r
=255-pixel.R;
InBlock.gif                    g
=255-pixel.G;
InBlock.gif                    b
=255-pixel.B;
InBlock.gif                    bitmap.SetPixel(x,y,Color.FromArgb(r,g,b));            
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
return bitmap;            
ExpandedBlockEnd.gif        }



二、浮雕:
        通过对图像相邻像素点的像素值分别与相邻像素点的像素值相减之后加上一个常量128,然后作为新的像素点值(为防止颜色值溢出,需处理值小于0和大于255的情况颜色值),可以使图像产生浮雕效果
%E6%B5%AE%E9%9B%95.JPG

 

ExpandedBlockStart.gif ContractedBlock.gif          /**/ /// <summary> 
InBlock.gif        
///以浮雕方式显示图像        
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="SImage">源图像</param>
ExpandedBlockEnd.gif        
/// <returns>浮雕效果处理后的图像</returns>

None.gif          public  Bitmap fuDiao(Image SImage)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {    
InBlock.gif            
int Height = SImage.Height;
InBlock.gif            
int Width = SImage.Width;
InBlock.gif            Bitmap bitmap
=new Bitmap(Width,Height);
InBlock.gif            Bitmap MyBitmap
=(Bitmap)SImage;
InBlock.gif            Color pixel1,pixel2;
InBlock.gif            
for(int x=0;x<Width-1;x++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
for(int y=0;y<Height-1;y++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
int r=0,g=0,b=0;
InBlock.gif                    pixel1
=MyBitmap.GetPixel(x,y);                        
InBlock.gif                    pixel2
=MyBitmap.GetPixel(x+1,y+1);
InBlock.gif                    r 
= pixel1.R-pixel2.R+128;
InBlock.gif                    g 
= pixel1.G-pixel2.G+128;
InBlock.gif                    b 
= pixel1.B-pixel2.B+128;
InBlock.gif                    
if(r>255)
InBlock.gif                        r
=255;
InBlock.gif                    
if(r<0)
InBlock.gif                        r
=0;
InBlock.gif                    
if(g>255)
InBlock.gif                        g
=255;
InBlock.gif                    
if(g<0)
InBlock.gif                        g
=0;
InBlock.gif                    
if(b>255)
InBlock.gif                        b
=255;
InBlock.gif                    
if(b<0)
InBlock.gif                        b
=0;
InBlock.gif                    bitmap.SetPixel(x,y,Color.FromArgb(r,g,b));            
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
return bitmap;        
ExpandedBlockEnd.gif        }



三、黑白化处理:
        彩色图像黑白化处理通常有三种方法:最大值法、平均值法、加权平均值法
三种方法的原理
        最大值法:最大值法是每个像素点的RGB值等于原像素点的RGB值中最大的一个,
                            即R=G=B=MAX( R,G,B ); 效果,最大值发产生亮度很高的黑白图像
        平均值法:平均值法使每个像素点的RGB值等于原像素点的RGB值的平均值,即R=G=B=(R+G+B)/3 
        加权平均法:加权平均法根据需要指定每个像素点RGB的权数,并取其加权平均值,
                                即R=G=B=(Wr*R+Wg*G+Wb*B )/3 
                                Wr、Wg、Wb表示RGB的权数,均大于零,通过取不同的权数可实现不同的效果
%E9%BB%91%E7%99%BD.JPG

ExpandedBlockStart.gif ContractedBlock.gif          /**/ /// <summary>
InBlock.gif        
/// 以黑白方式显示图像
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="SImage">源图像</param>
InBlock.gif        
/// <param name="iType">黑白处理的方法参数,0-平均值法;1-最大值法;2-加权平均值法</param>
ExpandedBlockEnd.gif        
/// <returns></returns>

None.gif          public  Bitmap heiBai(Image SImage, int  iType)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {        
InBlock.gif            
int Height = SImage.Height;
InBlock.gif            
int Width = SImage.Width;
InBlock.gif            Bitmap bitmap
=new Bitmap(Width,Height);
InBlock.gif            Bitmap MyBitmap
=(Bitmap)SImage;
InBlock.gif            Color pixel; 
InBlock.gif            
for (int x=0; x<Width; x++
InBlock.gif                
for (int y=0; y<Height; y++
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    pixel
=MyBitmap.GetPixel(x,y);
InBlock.gif                    
int r,g,b,Result=0;
InBlock.gif                    r 
= pixel.R;                            
InBlock.gif                    g 
= pixel.G;                
InBlock.gif                    b 
= pixel.B;    
InBlock.gif                    
InBlock.gif                    
switch(iType)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
case 0://平均值法
InBlock.gif
                            Result=((r+g+b)/3);
InBlock.gif                            
break;
InBlock.gif                        
case 1://最大值法
InBlock.gif
                            Result=r>g?r:g;
InBlock.gif                            Result
=Result>b?Result:b;
InBlock.gif                            
break;
InBlock.gif                        
case 2://加权平均值法
InBlock.gif
                            Result=((int)(0.7*r)+(int)(0.2*g)+(int)(0.1*b));
InBlock.gif                            
break;
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    bitmap.SetPixel(x,y,Color.FromArgb(Result,Result,Result));
ExpandedSubBlockEnd.gif                }
     
InBlock.gif            
return bitmap;        
ExpandedBlockEnd.gif        }

 

四、柔化:
        柔化显示图像和锐化显示图像的操作刚好相反,但在算法上不是它的逆过程。它的主要思想是减少图像边缘值之间的剧烈变化。
        当将当前像素点的颜色值设为以该像素为中心的像素块中所有像素的平均值时,如果当前像素点的颜色值和周围相邻像素点的颜色值差别不大时,则取平均值不会产生显著影响;如果差别较大时,取平均值后,就会使当前像素点的颜色趋于一致,这样就达到了柔化图像的目的。也称这种方法为高斯模板
%E6%9F%94%E5%8C%96.JPG

ExpandedBlockStart.gif ContractedBlock.gif          /**/ /// <summary>
InBlock.gif        
/// 以柔化方式显示图像    
InBlock.gif        
/// </summary>
InBlock.gif        
/// <Note>高斯模板法</Note>
InBlock.gif        
/// <param name="SImage">源图像</param>
ExpandedBlockEnd.gif        
/// <returns>柔化处理后的图像</returns>

None.gif          public  Bitmap rouHua(Image SImage)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
int Height = SImage.Height;
InBlock.gif            
int Width = SImage.Width;
InBlock.gif            Bitmap bitmap
=new Bitmap(Width,Height);
InBlock.gif            Bitmap MyBitmap
=(Bitmap)SImage;
InBlock.gif            Color pixel;
InBlock.gif            
//高斯模板
ExpandedSubBlockStart.gifContractedSubBlock.gif
            int []Gauss=dot.gif{1,2,1,2,4,2,1,2,1};                                
InBlock.gif            
for(int x=1;x<Width-1;x++)
InBlock.gif                
for(int y=1;y<Height-1;y++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
int r=0,g=0,b=0;
InBlock.gif                    
int Index=0;
InBlock.gif                    
//int a=0;
InBlock.gif
                    for(int col=-1;col<=1;col++)
InBlock.gif                        
for(int row=-1;row<=1;row++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{                            
InBlock.gif                            pixel
=MyBitmap.GetPixel(x+row,y+col);                        
InBlock.gif                            r
+=pixel.R*Gauss[Index];
InBlock.gif                            g
+=pixel.G*Gauss[Index];
InBlock.gif                            b
+=pixel.B*Gauss[Index];
InBlock.gif                            Index
++;
ExpandedSubBlockEnd.gif                        }

InBlock.gif                    r
/=16;
InBlock.gif                    g
/=16;
InBlock.gif                    b
/=16;
InBlock.gif                    
//处理颜色值溢出
InBlock.gif
                    r=r>255?255:r;
InBlock.gif                    r
=r<0?0:r;
InBlock.gif                    g
=g>255?255:g;
InBlock.gif                    g
=g<0?0:g;
InBlock.gif                    b
=b>255?255:b;
InBlock.gif                    b
=b<0?0:b;
InBlock.gif                    bitmap.SetPixel(x
-1,y-1,Color.FromArgb(r,g,b));            
ExpandedSubBlockEnd.gif                }

InBlock.gif            
return bitmap;        
ExpandedBlockEnd.gif        }




五、锐化:
        图像的锐化就是要显示图像中有关形体的边缘。所谓形体的边缘就是图像像素点的颜色值发生显著变化的地方,在图像的平淡区,这种颜色值的变化比较平缓,而在图像的边缘区域这种变化相当明显。
也就是说在平缓区,相邻两像素的颜色值的差值较小,而在边缘区域,相邻两像素的颜色值变化抖得多,因而在边缘区域处理这个数值可以使突出效果更加突出,而在非边缘区域而使图像变得较暗,即图像的锐化。拉普拉斯模块法
%E9%94%90%E5%8C%96.JPG

ExpandedBlockStart.gif ContractedBlock.gif          /**/ /// <summary>
InBlock.gif        
/// 以锐化方式显示图像        
InBlock.gif        
/// </summary>
InBlock.gif        
/// <Note>拉普拉斯模板法</Note>
InBlock.gif        
/// <param name="SImage">源图像</param>
ExpandedBlockEnd.gif        
/// <returns>锐化处理后的图像</returns>

None.gif          public  Bitmap ruiHua(Image SImage)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
int Height=SImage.Height;
InBlock.gif            
int Width=SImage.Width;
InBlock.gif            Bitmap bitmap
=new Bitmap(Width,Height);
InBlock.gif            Bitmap MyBitmap
=(Bitmap)SImage;
InBlock.gif            Color pixel;
InBlock.gif            
//拉普拉斯模板
ExpandedSubBlockStart.gifContractedSubBlock.gif
            int []Laplacian=dot.gif{-1,-1,-1,-1,9,-1,-1,-1,-1};               
InBlock.gif            
for(int x=1;x<Width-1;x++)
InBlock.gif                
for(int y=1;y<Height-1;y++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
int r=0,g=0,b=0;
InBlock.gif                    
int Index=0;
InBlock.gif                    
int a=0;
InBlock.gif                    
for(int col=-1;col<=1;col++)
InBlock.gif                        
for(int row=-1;row<=1;row++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{                            
InBlock.gif                            pixel
=MyBitmap.GetPixel(x+row,y+col);                        
InBlock.gif                            r
+=pixel.R*Laplacian[Index];
InBlock.gif                            g
+=pixel.G*Laplacian[Index];
InBlock.gif                            b
+=pixel.B*Laplacian[Index];
InBlock.gif                            Index
++;
ExpandedSubBlockEnd.gif                        }

InBlock.gif                    
//处理颜色值溢出
InBlock.gif
                    r=r>255?255:r;
InBlock.gif                    r
=r<0?0:r;
InBlock.gif                    g
=g>255?255:g;
InBlock.gif                    g
=g<0?0:g;
InBlock.gif                    b
=b>255?255:b;
InBlock.gif                    b
=b<0?0:b;
InBlock.gif                    bitmap.SetPixel(x
-1,y-1,Color.FromArgb(r,g,b));            
ExpandedSubBlockEnd.gif                }

InBlock.gif            
return bitmap;        
ExpandedBlockEnd.gif        }

None.gif



六、雾化:
        图像雾化处理不是基于图像中像素点的运算,而是在图像中引入一定的随机性,使图像带有毛玻璃带水雾般的效果。
        影响图像雾化效果的一个重要因素是图像中像素块的确定,所选区的像素块越大,产生的效果越明显。
 %E9%9B%BE%E5%8C%96.JPG

ExpandedBlockStart.gif ContractedBlock.gif          /**/ /// <summary>
InBlock.gif        
/// 以雾化方式显示图像
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="SImage"></param>
ExpandedBlockEnd.gif        
/// <returns></returns>

None.gif          public  Bitmap wuHua(Image SImage)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {        
InBlock.gif            
int Height=SImage.Height;
InBlock.gif            
int Width=SImage.Width;
InBlock.gif            Bitmap bitmap
=new Bitmap(Width,Height);
InBlock.gif            Bitmap MyBitmap
=(Bitmap)SImage;
InBlock.gif            Color pixel;                                            
InBlock.gif            
for(int x=1;x<Width-1;x++)
InBlock.gif                
for(int y=1;y<Height-1;y++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    System.Random   MyRandom
=new Random();                    
InBlock.gif                    
int k=MyRandom.Next(123456);    
InBlock.gif                    
//像素块大小
InBlock.gif
                    int dx=x+k%19;
InBlock.gif                    
int dy=y+k%19;
InBlock.gif                    
if(dx>=Width)
InBlock.gif                        dx
=Width-1;
InBlock.gif                    
if(dy>=Height)
InBlock.gif                        dy
=Height-1;
InBlock.gif                    pixel
=MyBitmap.GetPixel(dx,dy);
InBlock.gif                    bitmap.SetPixel(x,y,pixel);            
ExpandedSubBlockEnd.gif                }

InBlock.gif            
return bitmap;        
ExpandedBlockEnd.gif        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值