图像处理---平滑处理(柔化效果)
轉自:圖像處理:柔化(平滑)處理
1.效果圖:
2.算法說明:
柔化(平滑)處理是將原圖像的每個像素的顏色值用與其相鄰的n*n個像素的平均值來代替,
可利用算術平均值或加權平均值來計算。
例如3*3的點陣,設源圖像某像素的值為f(i,j):
|
f(i-1,j-1) |
f(i-1,j) |
f(i-1,j+1) |
|
f(i,j-1) |
f(i,j) |
f(i,j+1) |
|
f(i+1,j-1) |
f(i+1,j) |
f(i+1,j+1) |
3.實現代碼:
1///<summary>
2///算術平均值實現柔化
3///</summary>
4///<paramname="img">原始圖像</param>
5///<returns></returns>
6publicstaticImageSmoothImage(Imageimg)
7{
8intwidth=img.Width;
9intheight=img.Height;
10
11BitmapoldImg=(Bitmap)img;
12BitmapnewImg=newBitmap(width,height);//實例一個與原圖像一樣大小的對象
13
14Colorcolor=newColor();
15intr,g,b;//存放圖像顏色點的R、G、B值
16intrAvg,gAvg,bAvg;//存放某一點與相鄰點平均值后的R、G、B值
17
18//循環得到圖像的每一個像素點
19for(inti=1;i<width-1;i++)
20{
21for(intj=1;j<height-1;j++)
22{
23intrSum=0,gSum=0,bSum=0;//存放3*3點陣R、G、B值的和
24
25//取得與某一點相鄰的3*3個像素點
26for(introw=-1;row<=1;row++)
27{
28for(intcol=-1;col<=1;col++)
29{
30color=oldImg.GetPixel(i+row,j+col);
31r=color.R;
32g=color.G;
33b=color.B;
34
35rSum+=r;
36gSum+=g;
37bSum+=b;
38}
39}
40//相鄰點的算術平均值
41rAvg=(int)(rSum/9);
42gAvg=(int)(gSum/9);
43bAvg=(int)(bSum/9);
44
45//處理顏色值得溢出
46rAvg=rAvg<0?0:rAvg;
47rAvg=rAvg>255?255:rAvg;
48gAvg=gAvg<0?0:gAvg;
49gAvg=gAvg>255?255:gAvg;
50bAvg=bAvg<0?0:bAvg;
51bAvg=bAvg>255?255:bAvg;
52
53//設置新對象的每一點的顏色值
54newImg.SetPixel(i,j,Color.FromArgb(rAvg,gAvg,bAvg));
55}
56}
57returnnewImg;
58}
1///<summary>
2///加權平均值實現柔化
3///</summary>
4///<paramname="img">原始圖像</param>
5///<returns></returns>
6publicstaticImageSmoothImageWeight(Imageimg)
7{
8intwidth=img.Width;
9intheight=img.Height;
10
11BitmapoldImg=(Bitmap)img;
12BitmapnewImg=newBitmap(width,height);//實例一個與原圖像一樣大小的對象
13
14Colorcolor=newColor();
15intr,g,b;//存放圖像顏色點的R、G、B值
16intrAvg,gAvg,bAvg;//存放某一點與相鄰點平均值后的R、G、B值
17int[]gauss={1,2,1,2,4,2,1,2,1};//高斯模版
18
19//循環得到圖像的每一個像素點
20for(inti=1;i<width-1;i++)
21{
22for(intj=1;j<height-1;j++)
23{
24intindex=0;//高斯模版數組的索引
25intrSum=0,gSum=0,bSum=0;//存放按高斯模版所得點陣R、G、B值的和
26for(introw=-1;row<=1;row++)
27{
28for(intcol=-1;col<=1;col++)
29{
30color=oldImg.GetPixel(i+row,j+col);
31r=color.R;
32g=color.G;
33b=color.B;
34
35rSum+=r*gauss[index];
36gSum+=g*gauss[index];
37bSum+=b*gauss[index];
38
39index++;
40}
41}
42//相鄰點的加權平均值,"16" 為3*3高斯模版權重的總和
43rAvg=rSum/16;
44gAvg=gSum/16;
45bAvg=bSum/16;
46
47//處理顏色值得溢出
48rAvg=rAvg<0?0:rAvg;
49rAvg=rAvg>255?255:rAvg;
50gAvg=gAvg<0?0:gAvg;
51gAvg=gAvg>255?255:gAvg;
52bAvg=bAvg<0?0:bAvg;
53bAvg=bAvg>255?255:bAvg;
54
55//設置新對象的每一點的顏色值
56newImg.SetPixel(i,j,Color.FromArgb(rAvg,gAvg,bAvg));
57}
58}
59returnnewImg;
60}
61}
4.知識點說明:
a.加權平均值與算術平均值:
**加權平均值:
將各數值乘以相應的單位數,然后加總求和得到總體值,再除以總的單位數;
平均數的大小不僅取決于總體中各單位的標志值(變量值)的大小,而且取決于各標志值出現的次數(頻數);
由于各標志值出現的次數對其在平均數中的影響起著權衡輕重的作用,因此叫做權數或權重。
**算術平均值:
(a1+a2+……an)/n為這幾個數的算術平均值
**舉例:
下面是一個同學的某一科的考試成績:平時測驗 80, 期中 90, 期末 95。
學校規定的科目成績的計算方式是:平時測驗占 20%;期中成績占 30%;期末成績占 50%;
加權平均值 = (80*20% + 90*30% + 95*50%)/(20%+30%+50%) = 90.5
算數平均值 = (80 + 90 + 95)/3 = 88.3
b.高斯模版:
3*3高斯模版:
1 2 1
2 4 2*(1/16)
1 2 1
詳細參見:http://www.cnblogs.com/hoodlum1980/articles/1088567.html
總結
以上是生活随笔為你收集整理的图像处理---平滑处理(柔化效果)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 批处理系统和分时系统各具有什么特点?为什
- 下一篇: mysql 获取自增主键