Digital image processing is a significant aspect of data science. It is used in image modification and enhancement so that image attributes can be acquired to lead to a greater understanding of data.
An image is made up of elements called pixels, the smallest pieces of information in images. There are three main types of images:
Each pixel contains three values for the red, green, and blue color and is stored in three bytes. Each value is in the range 0
-255
. The values combined make up the resultant color of the pixel.
Values range from 0
-255
and represent the pixel intensity. Each pixel is stored in eight bits. 0
depicts a white pixel, while 255
depicts a black pixel.
Each pixel is stored in one bit and can have 0
or 255
as its value. 0
depicts a white pixel, while 255
depicts a black pixel.
This refers to transforming a grayscale image to a binary form (black-and-white). To binarize an image:
for
loops.0
(white) to it. If it is greater than or equal to the threshold, we assign 255
(black) to it.Thus, a black-and-white image is obtained.
In the code below, the Python imaging library, PIL, is used to read the image.
#import librariesimport PILfrom PIL import Imageimg=Image.open('og_image.png')#binarization functiondef binarize(img):#initialize thresholdthresh=200#convert image to greyscaleimg=img.convert('L')width,height=img.size#traverse through pixelsfor x in range(width):for y in range(height):#if intensity less than threshold, assign whiteif img.getpixel((x,y)) < thresh:img.putpixel((x,y),0)#if intensity greater than threshold, assign blackelse:img.putpixel((x,y),255)return imgbin_image=binarize(img)bin_image
Original image:
Image after binarization: