iT邦幫忙

2018 iT 邦幫忙鐵人賽
DAY 27
1
Software Development

提神?看程式比喝咖啡更有效。系列 第 27

[影像處理系列] 當模糊遇上細線化[2]結果分析

上一次是利用smooth把影像模糊化,來重現不同程度雜訊
再用laplacian把模糊的影像細線化,並觀察結果。

而從結果中得知,影像作簡單的模糊處理後,
對於後續的細線化,有更好的結果。

但因為模糊處理,較難於控制,而且很容易過度模糊,而不利後續處理

所以,這一次我們則用排序統計濾波器來對影像作初步處理
再利用laplacian輸出細線化後的影像。

先附上排序統計濾波器的程式碼
因為程式碼已有解說的文章,所以不再多講解了

int median(int *sort)
{
	int i,j,n;
	int max;

	for( i=0; i<5; i++ ){
		for( j=1, n=0, max=sort[0]; j<9; j++ ){
			if( max<sort[j] ){
				max = sort[j];
				n = j;
			    }   }
		sort[n] = -1;
     	}
	return max;
    }

=======================分格線=======================

接下來,就是laplacian的程式

void laplacian(unsigned char image_in[480][640], unsigned char image_out[480][640], double amp, int type)
{
	int	i, j;
	int d;
	int c[3][9] = { 0, -1,  0, -1,  4, -1,  0, -1,  0,
				   -1, -1, -1, -1,  8, -1, -1, -1, -1,
				    1, -2,  1, -2,  4, -2,  1, -2,  1};
	type = type - 1;
	if (type < 0) type = 0;
	if (type > 2) type = 2;
	for (i = 1; i < 480-1; i++) {
		for (j = 1; j < 640-1; j++) {
			d = c[type][0] * image_in[i-1][j-1]
			  + c[type][1] * image_in[i-1][j  ]
			  + c[type][2] * image_in[i-1][j+1]
			  + c[type][3] * image_in[i  ][j-1]
			  + c[type][4] * image_in[i  ][j  ]
			  + c[type][5] * image_in[i  ][j+1]
			  + c[type][6] * image_in[i+1][j-1]
			  + c[type][7] * image_in[i+1][j  ]
			  + c[type][8] * image_in[i+1][j+1];
			d = (int)(d * amp) ;
			if (d <   0) d = 0;
			if (d > 255) d = 255;
			image_out[i][j] = (unsigned char)d;
	    	}   }   }

=======================分格線=======================

https://ithelp.ithome.com.tw/upload/images/20180112/20107818qIarxhWJ9Y.png
(圖1:原始影像轉換成bmp之影像)(影像取自網絡)

https://ithelp.ithome.com.tw/upload/images/20180112/20107818onhm9rRorK.png
(圖2:直接對影像作1階laplacian處理、放大1倍後之影像)

https://ithelp.ithome.com.tw/upload/images/20180112/20107818TukW8yrLvm.png
(圖3:直接對影像作2階laplacian處理、放大1倍後之影像)

=======================分格線=======================

從圖2及圖3得知,直接作laplacian處理,雖然影像的邊緣有被標示
但連不重要的影像也同樣地被標示

https://ithelp.ithome.com.tw/upload/images/20180112/201078184kLtMoaP3c.png
(圖4:利用排序統計濾波器作3*3遮罩、取第5位置,處理後之影像)

https://ithelp.ithome.com.tw/upload/images/20180112/201078188ZulEZIlHw.png
(圖5:對圖4影像作1階laplacian處理、放大1倍後之影像)

https://ithelp.ithome.com.tw/upload/images/20180112/20107818g1DuQFpBQI.png
(圖6:對圖4影像作1階laplacian處理、放大4倍後之影像)

https://ithelp.ithome.com.tw/upload/images/20180112/20107818UoY7WQjdAF.png
(圖7:對圖4影像作2階laplacian處理、放大1倍後之影像)

https://ithelp.ithome.com.tw/upload/images/20180112/20107818ggIkUePwff.png
(圖8:對圖4影像作2階laplacian處理、放大2倍後之影像)

=======================分格線=======================

我們可以看到 經過33遮罩排序統計濾波器作初步的處理後
影像會有較好的輸出(能濾掉不重要的資訊
同時,我們也知道排序統計濾波器,也可以達到輕微的粗線化的效果

https://ithelp.ithome.com.tw/upload/images/20180112/20107818avyTMkrerO.png
(圖9:利用排序統計濾波器作3*3遮罩、取第2位置,處理後之影像)

https://ithelp.ithome.com.tw/upload/images/20180112/20107818F9rnEBRa9k.png
(圖10:對圖9影像作1階laplacian處理、放大1倍後之影像)

https://ithelp.ithome.com.tw/upload/images/20180112/2010781886I29GLT6f.png
(圖11:對圖9影像作1階laplacian處理、放大4倍後之影像)

https://ithelp.ithome.com.tw/upload/images/20180112/201078189UBBRYRde0.png
(圖12:對圖9影像作2階laplacian處理、放大1倍後之影像)

https://ithelp.ithome.com.tw/upload/images/20180112/20107818OdtQ3yKkJ0.png
(圖13:對圖9影像作2階laplacian處理、放大2倍後之影像)

=======================分格線=======================

利用33遮罩排序統計濾波器,取第2位置,比取第5位置,有更好的效果
在圖11、圖12中,我們可以清楚的看到 「ROUTE」 及「66」這兩個資料

接下來,我們去看排序統計濾波器 55遮罩的處理結果

https://ithelp.ithome.com.tw/upload/images/20180112/20107818yRaq7OBE61.png
(圖14:利用排序統計濾波器作5*5遮罩、取第13位置,處理後之影像)

因為取中位數(第13位置)後,輸出之影像過於模糊化,難以作後續的處理
因此更改遮罩所取的位置

https://ithelp.ithome.com.tw/upload/images/20180112/201078186XJDWwj83C.png
(圖15:利用排序統計濾波器作55遮罩、取第8位置,處理後之影像)

https://ithelp.ithome.com.tw/upload/images/20180112/20107818I9NIqf7mlL.png
(圖16:對圖15影像作1階laplacian處理、放大1倍後之影像)

https://ithelp.ithome.com.tw/upload/images/20180112/201078185DTb2O9c1i.png
(圖17:對圖15影像作1階laplacian處理、放大6倍後之影像)

https://ithelp.ithome.com.tw/upload/images/20180112/20107818euN8Ad0y5z.png
(圖18:對圖15影像作2階laplacian處理、放大1倍後之影像)

https://ithelp.ithome.com.tw/upload/images/20180112/20107818Qm321m21ho.png
(圖19:對圖15影像作2階laplacian處理、放大2倍後之影像)

=======================分格線=======================

即使有作簡單的微調,但55遮罩仍然難以把所有重要的資訊擷取下來
55遮罩相信已經難以做後續處理,但我們抱著好奇的心情去了解77遮罩所處理的結果

https://ithelp.ithome.com.tw/upload/images/20180112/201078182xwGeqzZKD.png
(圖20:利用排序統計濾波器作7*7遮罩、取第25位置,處理後之影像)

同樣地,因為取中位數(第25位置)後,輸出之影像過於模糊化,難以作後續的處理
因此需要修改遮罩所取的位置

https://ithelp.ithome.com.tw/upload/images/20180112/20107818NUSv1RXBZB.png
(圖21:利用排序統計濾波器作77遮罩、取第12位置,處理後之影像)

https://ithelp.ithome.com.tw/upload/images/20180112/201078186HL7DSumpI.png
(圖22:對圖21影像作1階laplacian處理、放大1倍後之影像)

https://ithelp.ithome.com.tw/upload/images/20180112/20107818sdk2Wn7Vfh.png
(圖23:對圖21影像作1階laplacian處理、放大4.4倍後之影像)

https://ithelp.ithome.com.tw/upload/images/20180112/20107818tw4Zm9Bv5i.png
(圖24:對圖21影像作2階laplacian處理、放大1倍後之影像)

https://ithelp.ithome.com.tw/upload/images/20180112/20107818sZs5jPf7xT.png
(圖25:對圖21影像作2階laplacian處理、放大3倍後之影像)

=======================結論=======================

排序統計濾波器對於濾高頻雜訊非常有效,且也有輕微粗線化的效果
但使用排序統計濾波器後,緊接使用laplacian細線化處理,卻不太適合。
根據以上結果,我們可以推論出:
第一步,使用排序統計濾波器,濾掉高頻雜訊
第二步,使用smooth,把影像模糊化,濾掉不重要的影像
第三步,使用laplacian細線化,來取得重要的影像的邊緣,方便給電腦讀取。
這三步方式,是讀取影像後優先處理的三步曲。
而以上三步是否正確,希望可以在下一次給大家一個結論。


上一篇
碰撞檢測 [2]方形檢測
下一篇
程式中的彩蛋,是工程師的驕傲
系列文
提神?看程式比喝咖啡更有效。30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言