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

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 TECHNOLOGYSCHOOL OF ELECTRICAL AND ELECTRONIC ENGINEERING

ASSIGNMENT REPORT DIGITAL IMAGE PROCESSINGIMAGE FUNDAMENTAL (Week: 1-2-3-4)

Instructor: Prof Tran Thi Thanh Hai

Hanoi, 2023

Trang 2

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 54.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 125.1 Convolutional function 12

Digital image processing plays a pivotal role in various computer vision and image analysis cations It encompasses a broad range of techniques and methods that allow us to manipulate andanalyze 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 awealth of functions and tools to facilitate image processing tasks, making it an invaluable resource forresearchers and practitioners in the field of computer vision.

appli-In this report, we delve into the practical aspects of utilizing OpenCV to accomplish various imageprocessing 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 highlightits 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 willexplore 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 Aswe progress, we explore various techniques to enhance image quality, including linear transformationsand gamma correction Histogram equalization is employed to achieve better image contrast We alsodelve into more advanced topics, such as noise reduction through median filtering and image smooth-ing via Gaussian filters Finally, we sharpen images using Laplacian and high-pass filters to produceenhanced results.

By the end of this report, you will have gained valuable insights into the practical applicationsof OpenCV in digital image processing This knowledge serves as a foundation for more advancedcomputer vision tasks, and we hope it inspires further exploration in the fascinating world of computervision.

3.1Import library

Embarking on the captivating journey of image processing is an exhilarating experience To navigatethe rich and intricate landscapes of digital imagery, we must first lay the foundation This preliminarystep is where we introduce three indispensable libraries that serve as the building blocks for ourexploration: OpenCV, NumPy, and Matplotlib OpenCV: Illuminating the Path to Image ProcessingExcellence 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 isa treasure trove of versatile tools that empower us to seamlessly interact with images By includingOpenCV in our project, we unlock a realm of possibilities, allowing us to perform a myriad of operationson images With the simple import statement below, we open the gateway to a world of imageprocessing possibilities:

NumPy: The Canvas of Image Data Our second companion on this journey is NumPy, the numericalcomputing powerhouse of Python It serves as the canvas on which we paint our image data NumPyis indispensable for image processing, allowing us to store, manipulate, and transform image channelssuch as Red (R), Green (G), and Blue (B) To bring NumPy into our fold, we need only employ thefollowing elegant syntax:

1 [ i mpo rt nu mpy as np ]

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

his-Our third ally in this visual odyssey is Matplotlib, a renowned library for data visualization inPython Matplotlib empowers us to tell compelling visual stories with our image data Its capabilitiesextend from displaying images to crafting intricate plots and generating informative graphs that enrichour understanding of the visual world To invite Matplotlib into our creative realm, we utilize thefollowing 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 processingaccomplishments.

In summary, our triumvirate of OpenCV, NumPy, and Matplotlib serves as the compass and mapfor our journey into the world of image processing These libraries, when wielded skillfully, transformdigital imagery into a playground for exploration and creative expression With these tools, we arepoised 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, thesefoundational pillars will enable us to delve deeper into the realm of image manipulation and analysis,ultimately shaping our understanding of the visual world.

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

To read an image ,opencv provide a cv2.imread() function This function support various file formatlike BMP, PNG, JPEG, TIFF etc And returns a NumPy array containing the image data.1 [ cv2 i mrea d ( path , f la g )]

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 additionalflags, it defaults to loading the image as a color image This means that each pixel in the image isrepresented 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 wouldload 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 aweighted average of the RGB values to determine the pixel’s grayscale intensity Like this image15

3.3Display an image: cv2 imshow()

To display an image in a new window, we use the cv2.imshow() function This function takes twoarguments: 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 file2 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.4Saving or Writing image: cv2.imwrite()

To save an image after processing it, we utilize the cv2.imwrite() function, passing the storage pathand 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

4.1How to calculate Histogram

The cumulative histogramhc(i) for a data setX with nbins is calculated using the following 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.2Code,Methodology and Formulas

Figure 3: Gray image

Figure 4: Gray image1 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 m3 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 s6 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 fori in ra ng e ( nu m _b in s + 1) ]

9 # T n h t o n hi s to g ra m10 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 - 115 hi s to g ra m [ b in _ in d ex ] += 116 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, andupdates 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 oneavailable in the Matplotlib library(plt.hist(gray mage.raveli (),bins = 256,range= [0,256]), we haveobserved that the results from both functions are identical This indicates that the algorithm used inthe library’s function likely precisely employs the formula described for histogram computation Thisfinding not only attests to the reliability of the built-in function but also helps optimize the time andeffort required for histogram calculations in our application.

4.3Linear Transform

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

Now, let’s proceed with ”Change the histogram by linear transform,” which involves applying lineartransformation 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

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 ofan image This process is executed through a series of specific steps Initially, the function identifiesthe minimum and maximum pixel values within the original image, which are crucial for determiningSmin and Smax Subsequently, it iterates through each pixel within the image, computing a new pixelvalue (stretched pixel) using a linear contrast stretching formula The transformation ensures thatpixel values remain within the desired range of [0, 255] The function systematically applies conditionsto safeguard that the transformed pixel values do not exceed these bounds In conclusion, the functionreturns the image with contrast adjustment after completing the entire process, providing a morevisually appealing representation.

Figure 5: Images Before and After Linear Transform

After applying the linear transformation using the custom function with the mathematical formulasoutlined above, we can observe that the initial image appeared quite dark However, following thelinear 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 valuesdue 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 entiregrayscale 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 thebuilt-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 andcontrast of digital images It is based on the power-law relationship between the pixel values of an imageand their corresponding brightness levels In essence, gamma correction allows for the fine-tuning of animage’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), whichserves to either amplify or dampen the differences between the dark and light areas of an image Agamma 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, whereprecise control over image contrast and brightness is essential By employing gamma correction, onecan improve the visibility of details in both underexposed and overexposed images, making it a versatiletool in image enhancement and visualization It offers a flexible and non-destructive means of adjustingthe 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 07

Output was illustrated in:

The results we can observe in image (7) with a series of gamma values less than 1 and greaterthan 1 When observing the original image, it’s easy to notice that the image becomes brighter as thegamma value increases This means that when we use a gamma value less than 1, the image becomesdarker and increases the contrast of darker areas Conversely, when we use a gamma value greaterthan 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 rection function The graph typically shows the relationship between the gamma value and how thepixels 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, showing a brighter image with decreased contrast Adjusting the gamma value is a powerfulway to modify the brightness and contrast of an image and is often used to enhance image display orcreate special artistic effects.

cor-4.5Histogram equalization

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

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

Xem thêm:

w