DIP Lab File
DIP Lab File
1
Experiment 1 – To Study the Image Processing concept
Digital Image Processing means processing digital image by means of a digital computer. We can also
say that it is a use of computer algorithms, in order to get enhanced image either to extract some
useful information.
Image acquisition: This involves capturing an image using a digital camera or scanner, or importing an
existing image into a computer.
Image enhancement: This involves improving the visual quality of an image, such as increasing
contrast, reducing noise, and removing artifacts.
Image restoration: This involves removing degradation from an image, such as blurring, noise, and
distortion.
Image segmentation: This involves dividing an image into regions or segments, each of which
corresponds to a specific object or feature in the image.
Image representation and description: This involves representing an image in a way that can be
analyzed and manipulated by a computer, and describing the features of an image in a compact and
meaningful way.
Image analysis: This involves using algorithms and mathematical models to extract information from
an image, such as recognizing objects, detecting patterns, and quantifying features.
Image synthesis and compression: This involves generating new images or compressing existing
images to reduce storage and transmission requirements.
Digital image processing is widely used in a variety of applications, including medical imaging, remote
sensing, computer vision, and multimedia.
Types of an image
BINARY IMAGE– The binary image as its name suggests, contain only two pixel elements i.e 0 &
1,where 0 refers to black and 1 refers to white. This image is also known as Monochrome.
BLACK AND WHITE IMAGE– The image which consist of only black and white color is called BLACK
AND WHITE IMAGE.
8 bit COLOR FORMAT– It is the most famous image format.It has 256 different shades of colors in it
and commonly known as Grayscale Image. In this format, 0 stands for Black, and 255 stands for
white, and 127 stands for gray.
16 bit COLOR FORMAT– It is a color image format. It has 65,536 different colors in it.It is also known
as High Color Format. In this format the distribution of color is not as same as Grayscale image.
Image as a Matrix
As we know, images are represented in rows and columns we have the following syntax in which
images are represented:
2
The right side of this equation is digital image by definition. Every element of this matrix is called
image element , picture element , or pixel.
Steps Involved
Algorithm
1. Compute the histogram of pixel values of the input image. The histogram places the value of
each pixel 𝑓[𝑥,𝑦] into one of L uniformly-spaced buckets ℎ[𝑖]
2. Where 𝐿=2^8 and the image dimension is 𝑀×𝑁
3. Calculate the cumulative distribution function
4. Scale the input image using the cumulative distribution function to produce the output
image.
Where CDFmin is the smallest non-zero value of the cumulative distribution function
Spatial Filtering technique is used directly on pixels of an image. Mask is usually considered to be
added in size so that it has a specific center pixel. This mask is moved on the image such that the
center of the mask traverses all image pixels.
3
Neighborhood processing in spatial domain: Here, to modify one pixel, we consider values of the
immediate neighboring pixels also. For this purpose, 3X3, 5X5, or 7X7 neighborhood mask can be
considered. An example of a 3X3 mask is shown below.
4
2. If X is a subset of Z then XoY is a subset of ZoY
3.(XoY)oY = XoY
2. (X.Y).Y = X.Y
# organizing imports
import cv2
import numpy as np
5
cv2.imshow('Mask', mask)
cv2.imshow('Opening', opening)
# organizing imports
import cv2
import numpy as np
6
# over the image and structuring element
closing = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel)
For example, let's say that we have a car and there is a camera centered in the middle of the car.
Let's say we want to establish autonomous features in the car like lane centering assistance. So, in
order to do this, we need to be able to identify lanes on the image. Now when you're looking for
lanes with the camera centered maybe in the center of the car, you're not going to look for lanes in
the sky or in all the way to the right or the left. The lane will be somewhat to the left and right and
near the ground, not in the sky. So establishing a region of interest in the image is important because
it allows us to zoom into a certain region to look for something, which helps to narrow our focus.
You can also think of it like searching for something. If you lost your keys and you know you've only
been to the kitchen and your bedroom, you'll only search the kitchen and bedroom. You're not going
to search the bathroom or living room when you know you didn't leave it there.
Program in Python
import cv2
import numpy as np
image= cv2.imread('Road-lanes.jpg')
blank= np.zeros_like(image_gray)
region_of_interest= cv2.fillPoly(blank, ROI,255)
region_of_interest_image= cv2.bitwise_and(image_gray, region_of_interest)
7
cv2.waitKey()
cv2.destroyAllWindows()
Program in Matlab
8
% Canny Edge Detection
N = edge(I, 'Canny');
subplot(2, 4, 7),
imshow(N);
title("Canny");
Sharpening an image using a gradient mask involves enhancing the high-frequency components,
which correspond to edges in the image. One common method for sharpening is to use a Laplacian
or high-pass filter. Here's a Python script using OpenCV to sharpen an image using a gradient mask:
Program in Python
import cv2
import numpy as np
from matplotlib import pyplot as plt
plt.subplot(1, 3, 1)
plt.title('Original Image')
plt.imshow(cv2.cvtColor(original_image, cv2.COLOR_BGR2RGB))
plt.axis('off')
plt.subplot(1, 3, 2)
plt.title('Laplacian Filter')
plt.imshow(laplacian, cmap='gray')
plt.axis('off')
9
plt.subplot(1, 3, 3)
plt.title('Sharpened Image')
plt.imshow(sharpened_image, cmap='gray')
plt.axis('off')
plt.show()
Working of erosion:
A pixel in the original image (either 1 or 0) will be considered 1 only if all the pixels under the kernel
are 1, otherwise, it is eroded (made to zero).
Thus all the pixels near the boundary will be discarded depending upon the size of the kernel.
So the thickness or size of the foreground object decreases or simply the white region decreases in
the image.
Working of dilation:
A pixel element in the original image is ‘1’ if at least one pixel under the kernel is ‘1’.
It increases the white region in the image or the size of the foreground object increases
Program in Python
10
img_dilation = cv2.dilate(img, kernel, iterations=1)
cv2.imshow('Input', img)
cv2.imshow('Erosion', img_erosion)
cv2.imshow('Dilation', img_dilation)
cv2.waitKey(0)
Program in C++
#include <bits/stdc++.h>
using namespace std;
#define pi 3.142857
const int m = 8, n = 8;
int dctTransform(int matrix[][n])
{
int i, j, k, l;
11
sum = 0;
for (k = 0; k < m; k++) {
for (l = 0; l < n; l++) {
dct1 = matrix[k][l] *
cos((2 * k + 1) * i * pi / (2 * m)) *
cos((2 * l + 1) * j * pi / (2 * n));
sum = sum + dct1;
}
}
dct[i][j] = ci * cj * sum;
}
}
12