Вы находитесь на странице: 1из 18

HO CHI MINH CITY UNIVERSITY OF TECHNOLOGY

Faculty of Electrical and Electronics Engineering


Department of Control Engineering and Automation
—————o0o—————

EXERCISE REPORT

IMAGE FILTER

Lecturer: Dr. Pham Viet Cuong

GROUP: 6

TRAN DINH HUY 1611332


TRAN THE CONG 1610335
HUYNH PHUOC LOI 1611917
NGUYEN DINH TRUONG 1613838
HO VIET HAN 1610940

September 13, 2019


Camera Calibration Lecturer: Dr. Pham Viet Cuong

1 Problem

Using OpenCV and Matlab to describe Point Processing and Histogram Equaliza-
tion.

2 imfillter()

1 img = imread ( ’ Image / lena . png ’)


2 h = [ -1 0 1];
3 A = imfilter ( img , h ) ;
4 figure
5 subplot (1 ,2 ,1) ; imshow ( img ) ; title ( ’ input ’)
6 subplot (1 ,2 ,2) ; imshow ( A ) ; title ( ’ imfillter ’)
Listing 1: imfillter() - matlab

Figure 1: imfilter() - Matlab

1
Camera Calibration Lecturer: Dr. Pham Viet Cuong

3 Roifilt2()

1 img = imread ( ’ lenna . png ’)


2 I = rgb2gray ( img )
3 figure
4 imshow ( I )
5 title ( ’ input ’)
6 c = [222 272 300 270 221 194]
7 r = [21 21 75 121 121 75]
8 BW = roipoly (I ,c , r )
9 H = fspecial ( ’ unsharp ’)
10 J = roifilt2 (H ,I , BW )
11 figure
12 imshow ( J )
13 title ( ’ output ’)
Listing 2: Roifilt2() - matlab

Figure 2: Roifilt2() - Matlab

2
Camera Calibration Lecturer: Dr. Pham Viet Cuong

4 Nlfilter() & Colfilt()

1 img = imread ( ’ lenna . png ’)


2 I = rgb2gray ( img )
3 figure
4 imshow ( I )
5 title ( ’ input ’)
6 fun = @ ( x ) median ( x (:) )
7 B = nlfilter (I ,[3 3] , fun )
8 figure
9 imshow ( B )
10 title ( ’ output ’)
Listing 3: Roifilt2() - matlab

Figure 3: Nlfilter() & Colfilt() - Matlab

3
Camera Calibration Lecturer: Dr. Pham Viet Cuong

5 Imgaussfilt()

1 I = imread ( ’ mina . jpg ’) ;


2 Iblur = imgaussfilt (I , 2) ;
3 subplot (1 ,2 ,1)
4 imshow ( I )
5 title ( ’ Original Image ’) ;
6 subplot (1 ,2 ,2)
7 imshow ( Iblur )
8 title ( ’ Gaussian filtered image , \ sigma = 2 ’)
Listing 4: Imgaussfilt() - matlab

1 let src = cv . imread ( ’ mina . png ’) ;


2 let dst = new cv . Mat () ;
3 let ksize = new cv . Size (3 , 3) ; // co the chon thong so khac
4 cv . GaussianBlur ( src , dst , ksize , 0 , 0 , cv . BORDER_DEFAULT ) ;
5 cv . imshow ( ’ canvasOutput ’ , dst ) ;
6 src . delete () ;
7 dst . delete () ;
Listing 5: Imgaussfilt() - OpenCV

Figure 4: Imgaussfilt() - Matlab

4
Camera Calibration Lecturer: Dr. Pham Viet Cuong

6 imgaussfilt3()

1 vol = load ( ’ anh ’) ;


2 figure
3 montage ( vol . D )
4 title ( ’ Original image volume ’)
5 siz = vol . siz ;
6 vol = squeeze ( vol . D ) ;
7 sigma = 2;
8
9 volSmooth = imgaussfilt3 ( vol , sigma ) ;
10
11 figure
12 montage ( reshape ( volSmooth , siz (1) , siz (2) ,1 , siz (3) ) )
13 title ( ’ Gaussian filtered image volume ’)
Listing 6: imgaussfilt3() - matlab

Figure 5: imgaussfilt3() - Matlab

7 wiener2()

1 RGB = imread ( ’ saturn . png ’) ;


2 I = rgb2gray ( RGB ) ;
3 J = imnoise (I , ’ gaussian ’ ,0 ,0.025) ;
4 imshow ( J (600:1000 ,1:600) ) ;
5 title ( ’ Portion of the Image with Added Gaussian Noise ’) ;
6 K = wiener2 (J ,[5 5]) ;
7 figure
8 imshow ( K (600:1000 ,1:600) ) ;

5
Camera Calibration Lecturer: Dr. Pham Viet Cuong

9 title ( ’ Portion of the Image with Noise Removed by Wiener Filter ’) ;


Listing 7: wiener2() - matlab

1 import numpy as np
2 import cv2 as cv
3 from matplotlib import pyplot as plt
4 cap = cv . VideoCapture ( ’ vtest . avi ’)
5 img = [ cap . read () [1] for i in xrange (5) ]
6 gray = [ cv . cvtColor (i , cv . COLOR_BGR2GRAY ) for i in img ]
7 gray = [ np . float64 ( i ) for i in gray ]
8 noise = np . random . randn (* gray [1]. shape ) *10
9 noisy = [ i + noise for i in gray ]
10 noisy = [ np . uint8 ( np . clip (i ,0 ,255) ) for i in noisy ]
11 dst = cv . f a s t N l M e a n s D e n o i s in g M u l t i ( noisy , 2 , 5 , None , 4 , 7 , 35)
12 plt . subplot (131) , plt . imshow ( gray [2] , ’ gray ’)
13 plt . subplot (132) , plt . imshow ( noisy [2] , ’ gray ’)
14 plt . subplot (133) , plt . imshow ( dst , ’ gray ’)
15 plt . show ()
Listing 8: wiener2() - OpenCV

Figure 6: wiener2()

6
Camera Calibration Lecturer: Dr. Pham Viet Cuong

8 wiener2()

1 I = imread ( ’ opencv . jpg ’) ;


2 figure , imshow ( I )
3 J = imnoise (I , ’ salt & pepper ’ ,0.02) ;
4 K = medfilt2 ( J ) ;
5 imshowpair (J ,K , ’ montage ’)
6 I = gpuArray ( imread ( ’ opencv . jpg ’) ) ;
7 J = imnoise (I , ’ salt & pepper ’ ,0.02) ;
8 K = medfilt2 ( J ) ;
9 figure , montage ({ J , K })
Listing 9: medfilt2() - matlab

1 median = cv2 . medianBlur ( img ,5)


Listing 10: medfilt2() - OpenCV

Figure 7: medfilt2()

7
Camera Calibration Lecturer: Dr. Pham Viet Cuong

9 Standard Deviation filtering:

1 I = imread ( ’ city . png ’) ;


2 J = stdfilt ( I ) ;
3 figure
4 imshow (J ,[])
5 title ( ’ Result of Standard Deviation Filtering ’)
Listing 11: stdfilt() - matlab

Figure 8: stdfilt()

8
Camera Calibration Lecturer: Dr. Pham Viet Cuong

10 rangefilt()

1 I = imread ( ’ city . png ’) ;


2 J = rangefilt ( I ) ;
3 imshowpair (I ,J , ’ montage ’)
Listing 12: rangefilt() - matlab

Figure 9: rangefilt()

9
Camera Calibration Lecturer: Dr. Pham Viet Cuong

11 Entropy filtering

1 I = imread ( ’ Lenna . png ’) ;


2 I_gray = rgb2gray ( I ) ;
3
4 I_fil = entropyfilt ( I_gray ) ;
5
6 figure ;
7 subplot (1 ,2 ,1) ; imshow ( I_gray ) ; title ( ’ Original ’) ;
8 subplot (1 ,2 ,2) ; imshow ( I_fil ,[]) ; title ( ’ After Filtering ’)
Listing 13: entropyfilt() - matlab

Figure 10: entropyfilt()

10
Camera Calibration Lecturer: Dr. Pham Viet Cuong

1 import matplotlib . pyplot as plt


2 import numpy as np
3 import cv2
4
5 from skimage import data
6 from skimage . util import img_as_ubyte
7 from skimage . filters . rank import entropy
8 from skimage . morphology import disk
9
10 image = cv2 . imread ( ’ Lenna . png ’)
11 image = cv2 . cvtColor ( image , cv2 . COLOR_BGR2GRAY )
12
13 fig , ( ax0 , ax1 ) = plt . subplots ( ncols =2 , figsize =(12 , 4) ,
14 sharex = True , sharey = True )
15
16 img0 = ax0 . imshow ( image , cmap = plt . cm . gray )
17 ax0 . set_title ( " Image " )
18
19 img1 = ax1 . imshow ( entropy ( image , disk (5) ) , cmap = ’ gray ’)
20 ax1 . set_title ( " Entropy " )
21
22 fig . tight_layout ()
23
24 plt . show ()
Listing 14: entropyfilt() - matlab

Figure 11: entropyfilt()

11
Camera Calibration Lecturer: Dr. Pham Viet Cuong

12 Box filtering

1 I = imread ( ’ Lenna . png ’) ;


2 I_gray = rgb2gray ( I ) ;
3
4 I_fil = imboxfilt ( I_gray ,5) ;
5
6 figure ;
7 subplot (1 ,2 ,1) ; imshow ( I_gray ) ; title ( ’ Original ’) ;
8 subplot (1 ,2 ,2) ; imshow ( I_fil ,[]) ; title ( ’ After Filtering ’)
Listing 15: imboxfilt() - matlab

Figure 12: Box Filtering - matlab

12
Camera Calibration Lecturer: Dr. Pham Viet Cuong

1 import matplotlib . pyplot as plt


2 import numpy as np
3 import cv2
4
5 I = cv2 . imread ( ’ Lenna . png ’)
6 I_gray = cv2 . cvtColor (I , cv2 . COLOR_BGR2GRAY )
7
8 I_blur = cv2 . blur ( I_gray ,(3 ,3) )
9
10 plt . subplot (1 ,2 ,1) , plt . imshow ( I_gray , cmap = plt . cm . gray ) , plt . title ( ’ Image ’)
11 plt . subplot (1 ,2 ,2) , plt . imshow ( I_blur , cmap = plt . cm . gray ) , plt . title ( ’ Filter ’)
12 plt . show ()
Listing 16: imboxfilt() - OpenCV

Figure 13: Box Filtering - OpenCV

13
Camera Calibration Lecturer: Dr. Pham Viet Cuong

13 Non Linear Filtering

1 I = imread ( ’ Lenna . png ’) ;


2 I_gray = rgb2gray ( I ) ;
3 I_gray = im2double ( I_gray ) ;
4
5 ham = @ ( x ) median ( x (:) ) ;
6
7 I_fil = nlfilter ( I_gray ,[3 3] , ham ) ;
8
9 figure ;
10 subplot (1 ,2 ,1) ; imshow ( I_gray ) ; title ( ’ Original ’) ;
11 subplot (1 ,2 ,2) ; imshow ( I_fil ,[]) ; title ( ’ After Filtering ’)
Listing 17: imboxfilt() - matlab

Figure 14: Non Linear - matlab

14
Camera Calibration Lecturer: Dr. Pham Viet Cuong

1 import matplotlib . pyplot as plt


2 import numpy as np
3 import cv2
4
5 I = cv2 . imread ( ’ Lenna . png ’)
6 I_gray = cv2 . cvtColor (I , cv2 . COLOR_BGR2GRAY )
7
8 I_blur = cv2 . medianBlur ( I_gray ,3)
9
10 plt . subplot (1 ,2 ,1) , plt . imshow ( I_gray , cmap = plt . cm . gray ) , plt . title ( ’ Image ’)
11 plt . subplot (1 ,2 ,2) , plt . imshow ( I_blur , cmap = plt . cm . gray ) , plt . title ( ’ Filter ’)
12 plt . show ()
Listing 18: imboxfilt() - OpenCV

Figure 15: Non Linear- OpenCV

15
Camera Calibration Lecturer: Dr. Pham Viet Cuong

14 Box Filter 3D

1 I = imread ( ’ Lenna . png ’) ;


2
3 I_fil = imboxfilt3 (I ,[5 5 3]) ;
4
5 figure ;
6 subplot (1 ,2 ,1) ; imshow ( I ) ; title ( ’ Original ’) ;
7 subplot (1 ,2 ,2) ; imshow ( I_fil ,[]) ; title ( ’ After Filtering ’)
Listing 19: Box Filter 3D - matlab

Figure 16: Non Linear - matlab

16
Camera Calibration Lecturer: Dr. Pham Viet Cuong

1 import matplotlib . pyplot as plt


2 import numpy as np
3 import cv2
4
5 I = cv2 . imread ( ’ Lenna . png ’)
6 I_blur = cv2 . imread ( ’ Lenna . png ’)
7
8 cv2 . boxFilter ( I_blur ,0 ,(5 ,5) , I_blur ,( -1 , -1) , True , cv2 . BORDER_DEFAULT )
9
10 plt . subplot (1 ,2 ,1) , plt . imshow ( cv2 . cvtColor (I , cv2 . COLOR_BGR2RGB ) ) , plt . title ( ’
Image ’)
11 plt . subplot (1 ,2 ,2) , plt . imshow ( cv2 . cvtColor ( I_blur , cv2 . COLOR_BGR2RGB ) ) , plt .
title ( ’ Filter ’)
12 plt . show ()
Listing 20: Box Filter 3D - OpenCV

Figure 17: Box Filter 3D - OpenCV

17

Вам также может понравиться