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

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

18 2 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 đề Assignment Report Digital Image Processing Image Fundamental (Week: 1-2-3-4)
Tác giả Trinh Trong Tinh
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 18
Dung lượng 5,05 MB

Nội dung

OpenCV, an open-source computer vision library, provides awealth of functions and tools to facilitate image processing tasks, making it an invaluable resource forresearchers and practiti

Trang 1

HANOI UNIVERSITY OF SCIENCE AND TECHNOLOGY SCHOOL OF ELECTRICAL AND ELECTRONIC ENGINEERING

————————————————

ASSIGNMENT REPORT

DIGITAL IMAGE PROCESSING IMAGE FUNDAMENTAL (Week: 1-2-3-4)

Instructor: Prof Tran Thi Thanh Hai

Hanoi, 2023

Trang 2

3 The First Step in Image Processing 3

3.1 Import library 3

3.2 Reading and Loading of an Image: cv2.imread() 4

3.3 Display an image: cv2 imshow() 5

3.4 Saving or Writing image: cv2.imwrite() 5

4 Histogram 5 4.1 How to calculate Histogram 5

4.2 Code,Methodology and Formulas 6

4.3 Linear Transform 7

4.4 Gamma correction 9

4.5 Histogram equalization 10

5 Filter 12 5.1 Convolutional function 12

5.2 Median filter 13

5.3 Smoothing(Gaussian filter) 15

5.4 Sharpening(Laplacian filter 16

I propose to undertake an exercise in the field of Digital Image Processing, designed by Instructor Assoc Prof Thanh-Hai Tran This exercise encompasses nine main tasks, each requiring students

to submit both a report and Python code for implementation While students have the option to use libraries for code implementation, those who create their own functions will receive a 30% higher score The primary objective of this exercise is to consolidate the knowledge acquired over four weeks of study It aims to empower students to develop functions independently and utilize functions available within the OpenCV library Throughout this exercise, I will write and implement my code

on the Colab platform In this report, I will detail the methodologies chosen for each task and compare the functions I develop with those readily available in the OpenCV library For reference,

my code is accessible at this URL: Link to Code https://colab.research.google.com/drive/ 13XQnGXm-qKfI-qLbFvJapskEasGHu-YI#scrollTo=cu7QItw9vkVW

Digital image processing plays a pivotal role in various computer vision and image analysis appli-cations It encompasses a broad range of techniques and methods that allow us to manipulate and analyze images to extract valuable information Among the essential tools for digital image process-ing, OpenCV stands as a cornerstone OpenCV, an open-source computer vision library, provides a wealth of functions and tools to facilitate image processing tasks, making it an invaluable resource for researchers and practitioners in the field of computer vision

In this report, we delve into the practical aspects of utilizing OpenCV to accomplish various image processing tasks, marking the initial steps on the journey towards a deeper understanding of com-puter vision We aim to demonstrate the importance of OpenCV in image processing and highlight its versatility in handling different image-related challenges By working through a series of exercises,

we will develop functions to read, display, and save images, compute and display image histograms, perform linear transformations, apply gamma correction, and equalize histograms Moreover, we will explore techniques to reduce noise through median filtering, smooth images with Gaussian filters, and

Trang 3

with the fundamentals, such as importing essential libraries like OpenCV, matplotlib, and NumPy Additionally, we address the nuances of working with Google Colab, a popular cloud-based environ-ment, which necessitates specific considerations for image display The journey starts with the basics, illustrating how to read and display images while highlighting the importance of image histograms As

we progress, we explore various techniques to enhance image quality, including linear transformations and gamma correction Histogram equalization is employed to achieve better image contrast We also delve into more advanced topics, such as noise reduction through median filtering and image smooth-ing via Gaussian filters Finally, we sharpen images ussmooth-ing Laplacian and high-pass filters to produce enhanced results

By the end of this report, you will have gained valuable insights into the practical applications

of OpenCV in digital image processing This knowledge serves as a foundation for more advanced computer vision tasks, and we hope it inspires further exploration in the fascinating world of computer vision

3.1 Import library

Embarking on the captivating journey of image processing is an exhilarating experience To navigate the rich and intricate landscapes of digital imagery, we must first lay the foundation This preliminary step is where we introduce three indispensable libraries that serve as the building blocks for our exploration: OpenCV, NumPy, and Matplotlib OpenCV: Illuminating the Path to Image Processing Excellence The first cornerstone of our journey is OpenCV, the Open Source Computer Vision Library Its name resonates with the world of computer vision, and rightfully so This remarkable library is

a treasure trove of versatile tools that empower us to seamlessly interact with images By including OpenCV in our project, we unlock a realm of possibilities, allowing us to perform a myriad of operations

on images With the simple import statement below, we open the gateway to a world of image processing possibilities:

1 [ im port cv2 ]

3 [ i mpo rt c v2 as cv ]

OpenCV is the Swiss Army knife of image processing, offering the ability to read images, apply filters, detect objects, recognize patterns, and engage in complex computer vision tasks It stands as

a cornerstone of the image processing world, beckoning us to explore its potential

NumPy: The Canvas of Image Data Our second companion on this journey is NumPy, the numerical computing powerhouse of Python It serves as the canvas on which we paint our image data NumPy

is indispensable for image processing, allowing us to store, manipulate, and transform image channels such as Red (R), Green (G), and Blue (B) To bring NumPy into our fold, we need only employ the following elegant syntax:

1 [ i mpo rt nu mpy as np ]

NumPy arrays form the bedrock of our image data, enabling operations like image filtering, his-togram computation, and pixel-level manipulations It provides the bridge between the visual world and the numerical realm, essential for image data analysis and transformation Matplotlib: Crafting Visual Narratives

Our third ally in this visual odyssey is Matplotlib, a renowned library for data visualization in Python Matplotlib empowers us to tell compelling visual stories with our image data Its capabilities extend from displaying images to crafting intricate plots and generating informative graphs that enrich our understanding of the visual world To invite Matplotlib into our creative realm, we utilize the following incantation:

1 [ im po rt ma tp l ot lib p yp lo t as plt ]

Trang 4

ing it a versatile choice for creating publication-ready images and showcasing our image processing accomplishments

In summary, our triumvirate of OpenCV, NumPy, and Matplotlib serves as the compass and map for our journey into the world of image processing These libraries, when wielded skillfully, transform digital imagery into a playground for exploration and creative expression With these tools, we are poised to unleash the full potential of image processing, be it for scientific research, artistic endeavors,

or any field that seeks to unveil the secrets hidden within visual data As we move forward, these foundational pillars will enable us to delve deeper into the realm of image manipulation and analysis, ultimately shaping our understanding of the visual world

3.2 Reading and Loading of an Image: cv2.imread()

To read an image ,opencv provide a cv2.imread() function This function support various file format like BMP, PNG, JPEG, TIFF etc And returns a NumPy array containing the image data

1 [ cv2 i mrea d ( path , f la g )]

Parameters:

”path”: The image should be in the working directory or a full path of image should be given

”flag”: The flags option is used to control how the image is read

Implement with14:

1 ima ge = cv2 im re ad ( ’ lenn a png ’)

2 if im ag e is not No ne :

3 pri nt (" Im ag e read su c ce s s fu l l y ")

4 else :

5 pri nt (" Un ab le to r ead the im age ")

Output: Image read successfully

This output indicates that we have successfully loaded an image with 3 color channels (typically Red, Green, and Blue, or RGB) When the cv2.imread() function is called without specifying any additional flags, it defaults to loading the image as a color image This means that each pixel in the image is represented by three values (one for each channel), allowing it to display in full color

However, if you were to specify a ’flag’ of 0 when reading the image using cv2.imread(), it would load the image as a grayscale image Grayscale images have only one channel, where each pixel is

Trang 5

image (3 channels) to a grayscale image (1 channel) typically involves an algorithm that calculates a weighted average of the RGB values to determine the pixel’s grayscale intensity Like this image15

3.3 Display an image: cv2 imshow()

To display an image in a new window, we use the cv2.imshow() function This function takes two arguments: the window name and the image to be displayed

1 [ c v2 _ im sho w ( i mag e ) ]

Parameters: ”image”: It is the image that is to be displayed Implement with14

1 # Read an i mag e fro m a file

2 ima ge = cv2 im re ad ( ’ Lenn a png ’)

3 # Di sp lay the ima ge in a wi nd ow nam ed " O ri gina l I mag e "

4 cv 2 _i msh ow ( imag e )

Output of this command was displayed as14

3.4 Saving or Writing image: cv2.imwrite()

To save an image after processing it, we utilize the cv2.imwrite() function, passing the storage path and the image to be saved as arguments

1 [ cv2 i mwri te ( fi le na me , im age ) ]

Parameters:

”filename”: A string representing the file name The filename must include image format like jpg, png, etc

”image”: It is the image that is to be saved Implement with15

1 # Read an i mag e fro m a file

2 ima ge = cv2 imr ea d (’ g r a y_ i ma g e png ’)

3 # Di sp lay the ima ge in a wi nd ow nam ed " O ri gina l I mag e "

4 cv 2 _i msh ow ( imag e )

Output of this command was displayed as15

The histogram of a digital image with intensity levels in the range[0,L-1] is a discrete function

, where rkis thekthintensity value and is the number of pixels in the image with intensity rk It is common practice to normalize a histogram by dividing each of its components by the total number of pixels in the image, denoted by the product MN, where, as usual, M and N are the row and column dimensions of the image

4.1 How to calculate Histogram

The cumulative histogramhc(i) for a data setX with nbins is calculated using the following formula:

h ic( ) =

i

X

k=0

where hc(i) belongs to the interval [0,Nx× Ny] In this formula:

Trang 6

h(k) is the histogram value for bin k,

Nx×Nyis the total number of samples in the data set,

i is the current bin index,

k ranges from 0 to i

4.2 Code,Methodology and Formulas

Figure 3: Gray image

Figure 4: Gray image

1 def C A L_ DIS _hi s t o g ra m (img , n u m_ b in s ) :

2 # T n h h i st o g ra m

3 va lu e_r an ge = (0 , 255)

4

5 # K h i t o hi s to g ra m v bi n _e d ge s

6 hi s to g ra m = [0] * nu m_ b in s

7 bi n _e d ge s = [ i * ( val u e _r a n ge [1] - v a lu e _r a n ge [ 0]) / n um _ bi n s for

i in ra ng e ( nu m _b in s + 1) ]

8

9 # T n h t o n hi s to g ra m

10 for row in img :

11 for p ix el_ val ue in row :

12 bi n _i n de x = int (( pi x el _ v al u e - va l u e_ r a ng e [0 ]) * n um _ bi n s

/ ( va lu e_r ang e [1] - v alu e_r a ng e [ 0]) )

13 if b i n_ i nd e x == n um _b i ns :

14 bi n _i n de x = n u m_ b in s - 1

15 hi s to g ra m [ b in _ in d ex ] += 1

16 hi s to g ra m = np ar ra y ( h is tog ra m )

17 bi n _e d ge s = np ar ra y ( b in _ed ge s )

Trang 7

20 plt tit le ( ’ Hi sto gra m ’)

21 plt x la be l ( ’ G i t r Pixel ’)

22 plt y la be l ( ’ T n s ’)

23 plt sav ef ig ( ’ his to g ra m png ’)

24 plt sho w ()

The code calculates the histogram using the following methodology:

• The image is divided into bins, typically 256 bins for an 8-bit grayscale image

• A value range, in this case, (0, 255), represents the range of possible pixel values

• The histogram and bin edges are initialized as arrays

• The code iterates through each pixel in the image, calculates the corresponding bin index, and updates the histogram

The formula used to compute the bin index for a given pixel value is as follows:

bin index = pixel value − value range[0]

value range[1] − value range[0]× num bins

In comparing the results of calculating the histogram using our custom function with the one available in the Matplotlib library(plt.hist(gray mage.raveli (),bins = 256,range= [0,256]), we have observed that the results from both functions are identical This indicates that the algorithm used in the library’s function likely precisely employs the formula described for histogram computation This finding not only attests to the reliability of the built-in function but also helps optimize the time and effort required for histogram calculations in our application

4.3 Linear Transform

Linear transformation, in the context of image processing and computer vision, is a fundamental concept that involves applying a linear function to the pixel values of an image It’s a technique used to modify the appearance and characteristics of an image while preserving the linearity of the mapping Here are some key points about linear transformation, Linear Mapping, Brightness and Contrast Adjustments, Histogram Modifications, Normalization, Image Enhancement

Now, let’s proceed with ”Change the histogram by linear transform,” which involves applying linear transformation to adjust the histogram of an image to enhance its visual characteristics

1 Calculate the minimum (Smin) and maximum (Smax) pixel values in the original image

2 For each pixel (i, j) in the original image, apply the linear transformation using the formula you provided:

I′(i,j) = 255 I(i,j) − Smin

Smax−Smin

if I′(i,j) ≤ 0, then I′(i,j) = 0

if I′(i,j) ≥ 255, then I′(i,j) = 255 With min(I(i,j)) ≤ Smin<Smax≤max(I(i,j)) Implement a function linear transform(image)

1 def l in ear _t ra nsf or m ( image , minI , m axI ) :

2 # Cr ea te the LUT

3 LUT = np empty (256 , dty pe = np ui nt8 )

4 for i in r ang e (25 6) :

5 LUT [i ] = int (255 * ( i - mi nI ) / ( maxI - mi nI ) )

6

Trang 8

8 tr an sf orm ed _i ma ge = L UT [ imag e ]

9

10 r e tu r n t r a n s f o r m e d _ i m a g e

The purpose of the linear transform function is to seamlessly and intuitively adjust the contrast of

an image This process is executed through a series of specific steps Initially, the function identifies the minimum and maximum pixel values within the original image, which are crucial for determining Smin and Smax Subsequently, it iterates through each pixel within the image, computing a new pixel value (stretched pixel) using a linear contrast stretching formula The transformation ensures that pixel values remain within the desired range of [0, 255] The function systematically applies conditions

to safeguard that the transformed pixel values do not exceed these bounds In conclusion, the function returns the image with contrast adjustment after completing the entire process, providing a more visually appealing representation

Figure 5: Images Before and After Linear Transform After applying the linear transformation using the custom function with the mathematical formulas outlined above, we can observe that the initial image appeared quite dark However, following the linear transformation process, the resulting image exhibits remarkable clarity and detail Moreover, when inspecting the histogram plot, it becomes evident that the original image had its pixel values due to the limited spread of pixel values In contrast, when examining the histogram of the image

Trang 9

after the linear transformation, we can see that the pixel values are evenly distributed across the entire grayscale range of [0, 255], resulting in clear and distinct details being displayed on the image Furthermore, when comparing this custom transformation with the result obtained using the built-in OpenCV function ”transf ormedimage= cv2.convertScaleAbs image,alpha = alpha,beta( = beta), ” we find that they exhibit similar characteristics in terms of the distribution of grayscale values

Gamma correction6is a fundamental image processing technique used to adjust the luminance and contrast of digital images It is based on the power-law relationship between the pixel values of an image and their corresponding brightness levels In essence, gamma correction allows for the fine-tuning of an image’s appearance, particularly in terms of enhancing or reducing its overall brightness and contrast The gamma correction process involves raising the pixel values to a certain power (gamma value), which serves to either amplify or dampen the differences between the dark and light areas of an image A gamma value less than 1 ( 1) enhances contrast and makes the image appear brighter, while a gamma< value greater than 1 ( 1) decreases contrast and darkens the image Gamma correction is particularly> useful in various applications, including photography, medical imaging, and computer graphics, where precise control over image contrast and brightness is essential By employing gamma correction, one can improve the visibility of details in both underexposed and overexposed images, making it a versatile tool in image enhancement and visualization It offers a flexible and non-destructive means of adjusting the tonal range of images, making them more visually appealing and informative

• γ is a constant

• c is a constant

Here are my code and outputs after apply gamma correction:

Code

1 def g am ma_ co rr ect io n ( image , g amma =1 0) :

2 if ga mm a <= 0:

3 rai se Va lu e Er r o r (" Gam ma v al ue mu st be gr ea t er t han ze ro ") 4

5 # Nor ma l i ze t he ima ge to the rang e [0 , 1]

Trang 10

6 no r m a l iz e d _ i ma g e = ima ge / 25 5 0

7

8 # App ly g am ma c o rr e c ti o n

9 co rre cte d_i ma ge = np powe r ( n or ma liz ed _i mage , gam ma )

10

11 re tu rn c o r re c t e d_ i m a g e

I will choose 4 values in order to image’s name Contrast.png:

1 ga mma _ va lue s = [0.5 , 1.0 , 1.5 , 2 0]

Output was illustrated in:

The results we can observe in image (7) with a series of gamma values less than 1 and greater than 1 When observing the original image, it’s easy to notice that the image becomes brighter as the gamma value increases This means that when we use a gamma value less than 1, the image becomes darker and increases the contrast of darker areas Conversely, when we use a gamma value greater than 1, the image becomes brighter and decreases the contrast of darker areas

To better understand this, it’s important to observe the corresponding graph of the gamma cor-rection function The graph typically shows the relationship between the gamma value and how the pixels in the image are transformed With a low gamma value, the graph has an upward-sloping line, indicating a darker image with increased contrast With a high gamma value, the graph has a decreas-ing slope, showdecreas-ing a brighter image with decreased contrast Adjustdecreas-ing the gamma value is a powerful way to modify the brightness and contrast of an image and is often used to enhance image display or create special artistic effects

4.5 Histogram equalization

Histogram equalization is used to enhance contrast It is not necessary that contrast will always be increase in this There may be some cases were histogram equalization can be worse In that cases the contrast is decreased We have simple image and the histogram of this image has been shown aside:

Ngày đăng: 30/05/2024, 14:55

w