Digital Image Processing Basics
Digital image processing is the acquisition, analysis, and performing of a wide range of operations on an image.
Digital image processing is the acquisition, analysis, and performing of a wide range of operations on an image.
In digital image processing, there are three types of images:
- Binary
- Grayscale
- RGB
The digital image can be represented within the two-dimensional array.
A binary image is represented by a dimensional array where each cell stores 0 (black) and 1 (white) values.
A grayscale image is a two-dimensional array where values in the array lay in a range from 0 to 255
The RGB has the same range of values as grayscale but has 3 two-dimensional arrays representing each color — Red, Green, and Blue.
With this knowledge, we can, therefore, calculate how many bits are required to store an image:
For binary images, width * height, since each pixel requires 1 bit, we can omit it.
For grayscale image: width * height * 8, since each pixel is represented by 8 bits
For RGB image: width * height * 8 * 3, because it has 3 planes, each plane contains values in a range from 0 to 255
Let’s start with image acquisition. In this article, I will be using Python and the OpenCV library. We can use the method imread from the OpenCV library for image acquisition.
import cv2 as cv
img = cv.imread('gray.jpg')
cv.waitKey()
cv.destroyAllWindows()
As you can see, we have two additional lines at the end of the file. These code lines are needed to stop the execution of the program.
The code is working, but we cannot see anything useful from the current code. Let’s fix it — show our image.
import cv2 as cv
img = cv.imread('gray.jpg')
cv.imshow('Grayscale image', img)
cv.waitKey()
cv.destroyAllWindows()
Now, you should see the image.
It’s time for more exciting things. We can do the following mathematical operations on an image:
- Addition — blend two images together
- Subtraction — find the absolute differences between two images
- Multiplication — adding color to line drawing
- Division — find the relative differences between two images.
Addition
import cv2 as cv
img_one = cv.imread('img/image1.png')
img_two = cv.imread('img/image2.png')
img_sum = img_one + img_two
cv.imshow('Sum', img_sum)
cv.waitKey()
cv.destroyAllWindows()
Substraction
Let’s proceed with subtraction; now we will subtract image1 from image 2
import cv2 as cv
img_one = cv.imread('img/image1.png')
img_two = cv.imread('img/image2.png')
img_subtract = img_two - img_one
cv.imshow('Subtract', img_subtract)
cv.waitKey()
cv.destroyAllWindows()
The result of subtraction is the absolute difference between the two images.
Division
The result of division is the relative.
Multiplication
The result of multiplication is adding colors to lines.
These operations cannot be performed on different images with different sizes. For example, you cannot add images 640 x 480 pixels to 320 x 320 pixels.
References:
- Rafael C. Gonzalez • Richard E. Woods, Digital Image Processing
- https://docs.opencv.org/4.x/