assignment report digital image processing image fundamental week 1 2 3 4

26 4 0
Tài liệu đã được kiểm tra trùng lặp
assignment report digital image processing image fundamental week 1 2 3 4

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Chapter 2Histogram2.1Histogram by My FunctionWe have the result.Figure 2.1: Gray image Figure 2.2: Histogram1 # Read image in grayscale2 image = cv2.. IMREAD_GRAYSCALE 3 cv2_imshow imag

Trang 1

HANOI UNIVERSITY OF SCIENCE AND TECHNOLOGYSCHOOL OF ELECTRICAL AND ELECTRONIC ENGINEERING

ASSIGNMENT REPORT

DIGITAL IMAGE PROCESSINGIMAGE FUNDAMENTAL (Week: 1-2-3-4)Instructor: Prof Tran Thi Thanh Hai

Student ID: 20213584

Hanoi, 2023

Trang 2

3.1.1 Linear Transfrom using Numpy 8

3.1.2 Linear Transfrom using OpenCV 11

3.2 Gamma correction 13

3.2.1 Gamma Correction by My Function 13

3.2.2 Gamma Correction using OpenCV 14

3.3 Histogram Equalization 15

3.3.1 Histogram Equalization by My Fuction 15

3.3.2 Histogram Equalization using OpenCV 17

3.4 Transformation Conclusion 17

4 Filters 184.1 Median Filter 18

4.1.1 Median Filter by My Function 18

4.1.2 Median Filter using OpenCV 19

4.2 Smoothing Filter 21

4.2.1 Smoothing Filter by My Function 21

4.2.2 Smoothing Filter using OpenCV 23

4.3 Sharpening Filter 24

4.3.1 Sharpening Filter by My Function 24

4.3.2 Sharpening Filter using OpenCV 25

Trang 4

Save the image using open CV library with thecv2.imwrite() function.

Figure 1.2: Save image

Trang 5

Chapter 2Histogram

2.1Histogram by My Function

We have the result.

Figure 2.1: Gray image Figure 2.2: Histogram

1 # Read image in grayscale

2 image = cv2 imre ad ( ’/ co ntent / drive / MyDrive / DIP / Lena png ’, cv2 IMREAD_GRAYSCALE )

3 cv2_imshow ( image )

5 # Function to compute and display the histogram of an image5

Trang 6

6 def draw_his ( img , bins ):

15 plt bar (x , hist , color = ’gray ’)

16 plt title ( ’ Histogram by My Function ’)

17 plt xlab el ( ’ Pixel Value ’)

18 plt ylab el ( ’ Frequency ’)

19 plt show ()

20 draw _hi s ( image , 256)

2.2Histogram using OpenCV

We have the result.

Figure 2.3: Gray Image Figure 2.4: Histogram

Trang 7

1 # Read image in grayscale

2 image = cv2 imread ( ’/ con tent / drive / MyDr ive / DIP / Lena png ’, cv2 IMREAD_GRAYSCALE )

3 cv2_imshow ( image )

5 # Calculate histogram and show by OpenCV

6 hist_openCV = cv2 calcHist ([ image ], [0] , None , [256] , [0 ,256])

7 hist_openCV = hist_openCV ravel ()

8 plt figu re ( figsi ze =(8 ,6) )

9 x = np ar an ge (2 56)

10 plt bar (x , hist_openCV , color = ’gray ’)

11 plt title ( ’ Histogram by OpenCV ’)

12 plt xlab el ( ’ Pixel Value ’)

Trang 9

Figure 3.1: Original Image

Figure 3.2: Original Histogram

Figure 3.3: Image After

Apply-ing Linear Transform Figure 3.4: Histogram After Applying LinearTransform

2 # Conv ert Co ntra st gif to Cont rast jpg

3 Image open ( ’/ content / drive / MyDrive / DIP / Contrast gif ’) convert(’ RGB ’) save ( ’ Contrast jpg ’)

4 # Read image in grayscale

5 image = cv2 imread ( ’/ con tent / C ontr ast jpg ’, cv2 9

Trang 10

21 plt bar (x , hist_openCV , color = ’gray ’)

22 plt title ( ’ Original Histogram ’)

23 plt xlab el ( ’ Pixel Value ’)

24 plt ylab el ( ’ Frequency ’)

25 plt show ()

27 # Histogram after linear transform

28 hist_openCV = cv2 calcHist ([ linear_Numpy ], [0] , None , [256] ,[0 , 256])

29 hist_openCV = hist_openCV ravel ()

30 plt figu re ( figsi ze =(8 ,6) )

31 x = np ar an ge (2 56)

32 plt bar (x , hist_openCV , color = ’gray ’)

33 plt title ( ’ Histogram After Applying Linear Transform ’)

34 plt xlab el ( ’ Pixel Value ’)

35 plt ylab el ( ’ Frequency ’)

36 plt show ()

Trang 11

3.1.2Linear Transfrom using OpenCVWe have the result.

Figure 3.5: Original Image

Figure 3.6: Original Histogram

Figure 3.7: Image After

Apply-ing Linear Transform Figure 3.8: Histogram After Applying LinearTransform

11

Trang 12

2 # Conv ert Co ntra st gif to Cont rast jpg

3 Image open ( ’/ content / drive / MyDrive / DIP / Contrast gif ’) convert(’ RGB ’) save ( ’ Contrast jpg ’)

4 # Read image in grayscale

5 image = cv2 imread ( ’/ con tent / C ontr ast jpg ’, cv2 IMREAD_GRAYSCALE )

6 cv2_imshow ( image )

8 # Change by linear transform ( openCV )

9 linear_openCV = cv2 convertScaleAbs ( image , alpha =1, beta =50)

17 plt bar (x , hist_openCV , color = ’gray ’)

18 plt title ( ’ Original Histogram ’)

19 plt xlab el ( ’ Pixel Value ’)

20 plt ylab el ( ’ Frequency ’)

21 plt show ()

23 # Histogram after linear transform

24 his t_o pe nCV = cv2 calcH ist ([ li near_ op en CV ], [0] , None , [256] ,[0 , 256])

25 hist_openCV = hist_openCV ravel ()

26 plt figu re ( figsi ze =(8 ,6) )

27 x = np ar an ge (2 56)

28 plt bar (x , hist_openCV , color = ’gray ’)

29 plt title ( ’ Histogram After Applying Linear Transform ’)

30 plt xlab el ( ’ Pixel Value ’)

31 plt ylab el ( ’ Frequency ’)

32 plt show ()

Trang 13

2 # Conv ert Co ntra st gif to Cont rast jpg

3 Image open ( ’/ content / drive / MyDrive / DIP / Contrast gif ’) convert(’ RGB ’) save ( ’ Contrast jpg ’)

4 # Read image in grayscale

5 image = cv2 imread ( ’/ con tent / C ontr ast jpg ’, cv2 IMREAD_GRAYSCALE )

6 cv2_imshow ( image )

8 # Gamma correction by my function

9 def ga mm a_ cor re cti on ( image , g amma ) :

10 width , height = image size

11 ga mm a_ cor re cte d_ ima ge = Image new (’L ’ , (width , height ))

13 for x in range ( width ) :

14 for y in range ( height ):13

Trang 14

15 pi xe l_ val ue = image g et pi xe l (( x , y) )

16 corrected_value = int (( pixel_value / 255.0) **gamma * 255.0)

17 ga mm a_ co rr ec te d_ im ag e p ut pix el (( x , y ) ,corrected_value )

19 return gamma_corrected_image

21 input_image = Image open ( ’/ content / Save_Contrast jpg ’)

22 gamma_img = gamma_correction ( input_image , 1.5)

23 ga mm a_i mg _cv 2 = cv2 cvt Co lo r ( np array ( g am ma _i mg ) , cv2 COLOR_RGB2BGR )

Trang 15

2 # Conv ert Co ntra st gif to Cont rast jpg

3 Image open ( ’/ content / drive / MyDrive / DIP / Contrast gif ’) convert(’ RGB ’) save ( ’ Contrast jpg ’)

4 # Read image in grayscale

5 image = cv2 imread ( ’/ con tent / C ontr ast jpg ’, cv2 IMREAD_GRAYSCALE )

6 cv2_imshow ( image )

8 def To_Gamma ( img , gamma ):

9 result = cv2 pow ( img /255.0 , gamma ) * 255.0

10 result = result a st ype ( np u int8 )

Trang 16

2 # Conv ert Co ntra st gif to Cont rast jpg

3 Image open ( ’/ content / drive / MyDrive / DIP / Contrast gif ’) convert(’ RGB ’) save ( ’ Contrast jpg ’)

4 # Read image in grayscale

5 image = cv2 imread ( ’/ con tent / C ontr ast jpg ’, cv2 IMREAD_GRAYSCALE )

6 cv2_imshow ( image )

8 his to gr am = np z eros (256 , dtype = np i nt32 )

9 for i in range ( image shape [0]) :

10 for j in range ( image shape [1]) :

11 pi xe l_ val ue = image [i , j ]

12 histogram [ pixel_value ] += 1

14 # Comp ute the c umulative di st ribut ion fun ctio n ( CDF )

15 cdf = np ze ros (256 , d type = np int32 )

24 # Map pixel intensities

25 equalized_image = cdf_normalized [ image ]

26 cv2_imshow ( equalized_image )

Trang 17

3.3.2Histogram Equalization using OpenCVWe have the result.

Figure 3.15: Original Image Figure 3.16: Image Applying HistogramEqualization

1 # Conv ert Co ntra st gif to Cont rast jpg

2 Image open ( ’/ content / drive / MyDrive / DIP / Contrast gif ’) convert(’ RGB ’) save ( ’ Contrast jpg ’)

3 # Read image in grayscale

4 image = cv2 imread ( ’/ con tent / C ontr ast jpg ’, cv2 IMREAD_GRAYSCALE )

5 cv2_imshow ( image )

7 # Equalize image by openCV

8 equalized_image = cv2 equalizeHist ( image )

9 cv2_imshow ( equalized_image )

3.4Transformation Conclusion

From the results in the Linear Transform, Gamma Correction, and Histogram ization sections, we observed that the transformations using both methods, which aremanual function usage and OpenCV, yield similar results.

Equal-17

Trang 18

Chapter 4Filters

Trang 19

1 # Read image in grayscale

2 image = cv2 imread ( ’/ con tent / drive / MyDr ive / DIP / Media n jpg ’,cv2 IMREAD_GRAYSCALE )

3 cv2_imshow ( image )

5 # Function to compute median filter

6 def med ia n_ filte r ( img ) :

20 temp = sorted ( temp )

21 img_new [i , j] = temp [4] # The fourth position is themedian

Trang 20

Figure 4.3: Original Image Figure 4.4: Image Applying Median ter

1 # Read image in grayscale

2 image = cv2 imread ( ’/ con tent / drive / MyDri ve / DIP / Media n jpg ’,cv2 IMREAD_GRAYSCALE )

Trang 21

1 # Read image in grayscale

2 image = cv2 imread ( ’/ con tent / drive / MyDri ve / DIP / Lena png ’)

3 cv2_imshow ( image )

5 # Function to compute Smoothing Filter

6 def compute ( img , mask ) :

Trang 23

4.2.2Smoothing Filter using OpenCVWe have the result.

Figure 4.7: Original Image Figure 4.8: Image Applying SmoothingFilter

1 # Read image in grayscale

2 image = cv2 imread ( ’/ con tent / drive / MyDri ve / DIP / Lena png ’)

Trang 24

5 # Function to compute Sharpening Filter

6 def compute ( img , mask ) :

Ngày đăng: 18/06/2024, 17:25