18DIP Lab 3

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

Lab Manual # 3

“To Write and Execute Image Processing Programs using Point Processing Method”

Objective:

• Obtain Negative Image


• Obtain Flip Image
• Thresholding
• Contrast Stretching
Apparatus:
• PC/Laptop with Windows 7/8/8.1/10
• Anaconda softwareLatest Anaconda 3 Release - Anaconda 3.7
Theory:

OpenCV

OpenCV is the huge open-source library for computer vision, machine learning, and
image processing and now it plays a major role in real-time operation which is very important in
today’s systems. By using it, one can process images and videos to identify objects, faces, or
even the handwriting of a human.

Point Operation:

Point operations are often used to change the grayscale range and distribution. The
concept of point operation is to map every pixel onto a new image with a predefined
transformation function.
g(x, y) = T(f(x, y))

• g (x, y) is the output image


• T is an operator of intensity transformation
• f (x, y) is the input image
We all already know that images are simply represented digitally as a 2D ordered matrix.
Operations that are used to modify a pixel value without affecting the neighboring pixels are
known as Point Operations. Point operations will:

• Not change the size of the image


• Not change the geometry of the image
• Not change the local structure of the image
• Not affect the neighbor pixels
Point processing in Spatial Domain:

All the processing done on the pixel values. Point processing operations take the form:

s = T (r)

Here, T is referred to as a grey level transformation function or a point processing


operation, s refers to the processed image pixel value and r refers to the original image pixel
value.

I. Negative Image:
This is a photographic image of a dull and dark part, as well as the bright areas in the photos.
Plastics and glass, for instance, contain negative material most often. Unlike an ordinary image, a
negative one reflects on light-dark areas in both cases. Similarly, negative color images offer the
choice of altering the areas within them; from cyan, to magenta, toblues, and vice versa.

s = (L-1) – r,

where L= number of grey levels.

Input Code:

Output:
II. Flip Image:
A flipped image or reversed image, the more formal term, is a static or moving image that is
generated by a mirror-reversal of an original across a horizontal axis. A flopped image is
mirrored across the vertical axis. The syntax for this is:

img.transpose(Image.TRANSPOSE)

Input Code:

Output:

Original Image: Flipped Image:

III. Thresholding:
Thresholding is a technique in OpenCV, which is the assignment of pixel values in relation to
the threshold value provided. In thresholding, each pixel value is compared with the threshold
value.If the pixel value is smaller than the threshold, it is set to 0, otherwise, it is set to a
maximum value (generally 255).Thresholding is a very popular segmentation technique, used for
separating an object considered as a foreground from its background. In Computer Vision, this
technique of thresholding is done on grayscale images. So initially, the image must be converted
in grayscale color space. The syntax is:

cv2.threshold(source, thresholdValue, maxVal, thresholdingTechnique)

Input Code:
Output:

Obtained different Thresholds


IV. Contrast Stretching:
Contrast stretching (often called normalization) is a simple image enhancement technique
that attempts to improve the contrast in an image by `stretching’ the range of intensity values it
contains to span a desired range of values, e.g., the full range of pixel values that the image type
concerned allows. By changing the location of points (r1, s1) and (r2, s2), we can control the
shape of the transformation function. For example:

• When r1 =s1 and r2=s2, transformation becomes a Linear function.


• When r1=r2, s1=0 and s2=L-1, transformation becomes a thresholding function.
• When (r1, s1) = (rmin, 0) and (r2, s2) = (rmax, L-1), this is known as Min-Max
Stretching.
• When (r1, s1) = (rmin + c, 0) and (r2, s2) = (rmax – c, L-1), this is known
as Percentile Stretching.
In Min-Max Stretching, the lower value of the input image is mapped to 0 and the upper
value is mapped to 255.When Min-Max is performed, the tail ends of the histogram become long
resulting in no improvement in the image quality. So, it is better to clip a certain percentage like
1%, 2% of the data from the tail ends of the input image histogram. This is known as Percentile
Stretching.

Input Code:
Output:

Q1: Write down an algorithm to find negative of an image?

function findNegative(image):
// Assuming the image is in grayscale

// Get image dimensions


rows = image.rows
cols = image.cols

// Iterate over each pixel


for each pixel (i, j) in image:
// Get pixel value
pixel_value = image[i, j]

// Compute negative value


negative_value = 255 - pixel_value

// Update pixel value in image


image[i, j] = negative_value

return image
Q2: Write down a program for performing Min-Max Stretching of an image
using open CV. Also paste its output.
Q3: Write down the names of simple Thresholding techniques
Simple thresholding techniques in image processing are fundamental for segmenting images
into foreground and background by converting grayscale images into binary images. Here are some of
the common simple thresholding techniques:

• Global Thresholding (Simple Thresholding)


• Adaptive Thresholding
• Otsu’s Thresholding

Q4: How to Flip and rotate an image using Open CV

• Import the required libraries OpenCV and matplotlib. Make sure you have already installed
them.
• Read the input image using cv2.imread() method. Specify the full path of the image.
• Rotate the input image using cv2.rotate() function. Flipped using the function cv2. flip().
• Display the rotated and flipped image.

Conclusion:

You might also like