1. Trang chủ
  2. » Luận Văn - Báo Cáo

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

26 5 0
Tài liệu đã được kiểm tra trùng lặp

Đ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

Thông tin cơ bản

Tiêu đề Image Fundamental
Tác giả Phung Mac Quan
Người hướng dẫn Prof. Tran Thi Thanh Hai
Trường học Hanoi University of Science and Technology
Chuyên ngành Digital Image Processing
Thể loại Assignment Report
Năm xuất bản 2023
Thành phố Hanoi
Định dạng
Số trang 26
Dung lượng 7,08 MB

Nội dung

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 PROCESSING

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

Student ID: 20213584

Hanoi, 2023

Trang 2

2.1 Histogram by My Function 5

2.2 Histogram using OpenCV 6

2.3 Histogram Conclusion 7

3 Transformation 8 3.1 Linear transform 8

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 18 4.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 2

Histogram

2.1 Histogram by My Function

We have the result

Figure 2.1: Gray image Figure 2.2: Histogram

Code

1 # Read image in grayscale

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

3 cv2_imshow ( image )

4

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

5

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.2 Histogram 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 )

4

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 ’)

13 plt ylab el ( ’ Frequency ’)

14 plt show ()

2.3 Histogram Conclusion

From the results above, we can see that whether we use manual function or OpenCV

to draw the histogram, both methods yield the same results

7

Trang 8

Chapter 3

Transformation

3.1 Linear transform

3.1.1 Linear Transfrom using Numpy

We have the result

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 Linear

Transform

Code

1

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 ()

26

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.2 Linear Transfrom using OpenCV

We 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 Linear

Transform

11

Trang 12

1

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 )

7

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 ()

22

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

3.2 Gamma correction

3.2.1 Gamma Correction by My Function

We have the result

Figure 3.9: Original Image Figure 3.10: Image Applying Gamma

CorrectionCode

1

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 )

7

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 ))

12

13 for x in range ( width ) :

14 for y in range ( height ):

13

Trang 14

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 )

24 cv2_imshow ( gamma_img_cv2 )

3.2.2 Gamma Correction using OpenCV

We have the result

Figure 3.11: Original Image Figure 3.12: Image Applying Gamma

Correction

Trang 15

1

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 )

7

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 )

11 return result

12 gamma_img = To_Gamma (image , 1.5)

13 cv2_imshow ( gamma_img )

3.3 Histogram Equalization

3.3.1 Histogram Equalization by My Fuction

We have the result

Figure 3.13: Original Image Figure 3.14: Image Applying HistogramEqualization

15

Trang 16

1

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 )

7

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

13

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

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

23

24 # Map pixel intensities

25 equalized_image = cdf_normalized [ image ]

26 cv2_imshow ( equalized_image )

Trang 17

3.3.2 Histogram Equalization using OpenCV

We have the result

Figure 3.15: Original Image Figure 3.16: Image Applying HistogramEqualization

Code

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 )

6

7 # Equalize image by openCV

8 equalized_image = cv2 equalizeHist ( image )

9 cv2_imshow ( equalized_image )

3.4 Transformation 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 4

Filters

4.1 Median Filter

4.1.1 Median Filter by My Function

We have the result

Figure 4.1: Original Image Figure 4.2: Image Applying Median

Fil-ter

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 )

4

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 the

median

22 return img_new

23 img_median = median_fil ter ( image )

24 cv2_imshow ( img_median )

4.1.2 Median Filter using OpenCV

We have the result

19

Trang 20

Figure 4.3: Original Image Figure 4.4: Image Applying Median

Fil-ter

Code

1 # Read image in grayscale

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

Trang 21

4.2 Smoothing Filter

4.2.1 Smoothing Filter by My Function

We have the result

Figure 4.5: Original Image Figure 4.6: Image Applying Smoothing

FilterCode

1 # Read image in grayscale

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

3 cv2_imshow ( image )

4

5 # Function to compute Smoothing Filter

6 def compute ( img , mask ) :

Trang 23

4.2.2 Smoothing Filter using OpenCV

We have the result

Figure 4.7: Original Image Figure 4.8: Image Applying Smoothing

FilterCode

1 # Read image in grayscale

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

Trang 24

4.3 Sharpening Filter

4.3.1 Sharpening Filter by My Function

We have the result

Figure 4.9: Original Image Figure 4.10: Image Applying Sharpening

FilterCode

1 # Read image

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

3 cv2_imshow ( image )

4

5 # Function to compute Sharpening Filter

6 def compute ( img , mask ) :

Trang 25

4.3.2 Sharpening Filter using OpenCV

We have the result

Figure 4.11: Original Image Figure 4.12: Image Applying Sharpening

FilterCode

25

Trang 26

be achieved However, in the case of the Sharpening Filter, it seems that I may have made

an error somewhere, as the results differ when implementing it manually and using theOpenCV library

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

w