19CS49 Computer Vision Lab File PDF
19CS49 Computer Vision Lab File PDF
WebSite: www.lingayasuniversity.edu.in
Program-1
What is Python?
Python is a popular programming language. It was created by Guido van
Rossum, and released in 1991.
It is used for:
• web development (server-side),
• software development,
• mathematics,
• system scripting.
Why Python?
• Python works on different platforms (Windows, Mac, Linux, Raspberry
Pi, etc).
• Python has a simple syntax similar to the English language.
• Python has syntax that allows developers to write programs with fewer
lines than some other programming languages.
• Python runs on an interpreter system, meaning that code can be
executed as soon as it is written. This means that prototyping can be
very quick.
• Python can be treated in a procedural way, an object-oriented way or a
functional way.
Good to know
• The most recent major version of Python is Python 3, which we shall be
using in this tutorial. However, Python 2, although not being updated
with anything other than security updates, is still quite popular.
• In this tutorial Python will be written in a text editor. It is possible to
write Python in an Integrated Development Environment, such as
Thonny, Pycharm, Netbeans or Eclipse which are particularly useful
when managing larger collections of Python files.
Python Syntax compared to other programming
languages
• Python was designed for readability, and has some similarities to the
English language with influence from mathematics.
• Python uses new lines to complete a command, as opposed to other
programming languages which often use semicolons or parentheses.
• Python relies on indentation, using whitespace, to define scope; such as
the scope of loops, functions and classes. Other programming languages
often use curly-brackets for this purpose.
Example
print("Hello, World!")
Example
if 5 > 2:
print("Five is greater than two!") if 5
> 2: print("Five is greater than
two!")
PROGRAM – 2
Aim:- Write a Program to display the Negative of a Digital Image
Negative of an image in MATLAB:-
The negative of an image is achieved by replacing the intensity 'i' in the original image by 'i-1',
i.e., the darkest pixels will become the brightest and the brightest pixels will become the
darkest. Image negative is produced by subtracting each pixel from the maximum intensity
value.
For example, in an 8-bit grayscale image, the max intensity value is 255, thus each pixel is
subtracted from 255 to produce the output image.
S = T(r) = (L1) – r
Where L 1 is the max intensity value, - s is the output pixel value and r is the input pixel value
Algorithm
1. Read RGB color image into the MATLAB environment using Matlab inbuilt function
imread()
3. Use the formula stated above on every pixel of the image to get the corresponding negative
pixel value.
4. Convert each RGB pixel value at location j) to its negative image values and assign it to the
corresponding location (i, j) of another matrix
5 with arguments:
PROGRAM – 3
Aim:- Write a Program to perform thresholding on an input Image
Thresholding is a type of image segmentation, where we change the pixels of an image to make the
image easier to analyze. In thresholding, we convert an image from colour or grayscale into a binary
image, i.e., one that is simply black and white. Most frequently, we use thresholding as a way to select
areas of interest of an image, while ignoring the parts we are not concerned with. We have already
done some simple thresholding, in the “Manipulating pixels” section of the Image Representation in
the skimage episode. In that case, we used a simple NumPy array manipulation to separate the pixels
belonging to the root system of a plant from the black background. In this episode, we will learn how
to use skimage functions to perform thresholding. Then, we will use the masks returned by these
functions to select the parts of an image we are interested in.
Next, we would like to apply the threshold t such that pixels with grayscale values on one side
of t will be turned “on”, while pixels with grayscale values on the other side will be turned
“off”. How might we do that? Remember that grayscale images contain pixel values in the
range from 0 to 1, so we are looking for a threshold t in the closed range [0.0, 1.0]. We see in
the image that the geometric shapes are “darker” than the white background but there is also
some light gray noise on the background. One way to determine a “good” value for t is to look
at the grayscale histogram of the image and try to identify what grayscale ranges correspond
to the shapes in the image or the background
PROGRAM-3
max_range = 60
# Loop over the input image and if pixel value lies in desired range set it to 255
otherwise set it to 0. for i in range(row):
for j in range(column):
if img[i,j]>min_range and img[i,j]<max_range: img1[i,j] = 255
q else: img1[i,j] = 0
PROGRAM-6
AIM: Write a Program to perform bit-plane slicing
#as variable name says ,it's list of pixel values in binary , but in 1
#dimension
imgIn1D = intToBitArray(img)
#reshaping above 1D array to a matrix aka image
imgIn2D = np.reshape(imgIn1D , (360,640) ) def
bitplane(bitImgVal , img1D ):
'''
this function extracts the specific bit out of each binary pixel values of the
matrix
for example , if bitImgVal = 3 , then , third bit of each pixel is extracted
return bitList
#i don't know why but the multiplication factor is : 2^(n-1) where n is the bit
number
#example, if binary pixel value is 11001010 and n = 3 , factor = 2^(3-1)
#image represented by 8th bit plane
eightbitimg = np.array( bitplane(0, imgIn1D ) ) * 128
In our histogram, it looks like there’s distribution of intensity all over image
Black and White pixels as grayscale image.
From the histogram, we can conclude that dark region is more than brighter
region.
Now, we will deal with an image which consist of intensity distribution of pixels
where pixel value varies. First, we need to calculate histogram using OpenCV
in-built function.
Histogram Calculation
Here, we use cv2.calcHist()(in-built function in OpenCV) to find the histogram.
cv2.calcHist(images, channels, mask, histSize, ranges[, hist[,
accumulate]])
Input
Output:-
import cv2
from matplotlib import pyplot as plt
img = cv2.imread('ex.jpg',0)
# alternative way to find histogram of an image
plt.hist(img.ravel(),256,[0,256])
plt.show()
PROGRAM-8
Log transformation
Logarithmic transformation of an image is one of the gray level
image transformations. Log transformation of an image means
replacing all pixel values, present in the image, with its
logarithmic values. Log transformation is used for image
enhancement as it expands dark pixels of the image as
compared to higher pixel values.
The formula for applying log transformation in an image is,
S = c * log (1 + r)
where,
R = input pixel value,
C = scaling constant and
S = output pixel value
The value of ‘c’ is chosen such that we get the maximum output
value corresponding to the bit size used. So, the formula for
calculating ‘c’ is as follows:
c = 255 / (log (1 + max_input_pixel_value))
When we apply log transformation in an image and any pixel
value is ‘0’ then its log value will become infinite. That’s why we
are adding ‘1’ to each pixel value at the time of log
transformation so that if any pixel value is ‘0’, it will become ‘1’
and its log value will be ‘0’.
Let’s apply log transformation in an image using Python.
Input:-
# Read an image
image = cv2.imread('GFG.png')
# Apply log transformation method c =
255 / np.log(1 + np.max(image))
log_image = c * (np.log(image + 1))
# Specify the data type so that # float value will
be converted to int log_image =
np.array(log_image, dtype = np.uint8)
output:-
PROGRAM-9
AIM: Write a Program to implement an Ideal low pass filter
In the field of Image Processing, Ideal Lowpass Filter (ILPF) is used for
image smoothing in the frequency domain. It removes high-frequency noise
from a digital image and preserves low-frequency components.\
Input:-
Output:-
Program -10
Where,
Do is a positive constant. BLPF passes all the frequencies less than Do value without
attenuation and cuts off all the frequencies greater than it.
as cutoff frequency. But instead of making a sharp cut-off (like, Ideal Lowpass Filter
(ILPF)), it introduces a smooth transition from 1 to 0 to reduce ringing artifacts.
D(u, v) is the Euclidean Distance from any point (u, v) to the origin of the
Approach:
INPUT:-
OUTPUT:-