Lab Detail


Sno Back Back Subject subject date title note
1 1 Back to subject AI for Image Analysis - 20A30702b (Theory) Sept. 7, 2025 Unit - 4 Part01

 

1. Blending Two Images
 ðŸ‘‰ Blend two images of the same size using weighted addition.

 

 

import cv2
import matplotlib.pyplot as plt

img1 = cv2.imread("images/parrot.jpg")
img2 = cv2.imread("images/antelops.jpeg")

# Resize second image to match first
img2 = cv2.resize(img2, (img1.shape[1], img1.shape[0]))

# Blend images
blended = cv2.addWeighted(img1, 0.7, img2, 0.3, 0)

# Convert BGR → RGB for matplotlib
blended_rgb = cv2.cvtColor(blended, cv2.COLOR_BGR2RGB)

plt.imshow(blended_rgb)
plt.axis('off')
plt.title("Blended Image")
plt.show()

 

2. Changing Contrast and Brightness

👉 Use cv2.convertScaleAbs().

Contrast

The contrast stretching operation takes a low-contrast image as input and stretches the narrower range of the intensity values to span a desired wider range of values in order to output a high-contrast output image, thereby enhancing the image's contrast. It is just a linear scaling function that is applied to image pixel values, and hence the image enhancement is less drastic (than its more sophisticated counterpart histogram equalization, to be described shortly). The following screenshot shows the point transformation function for contrast stretching:

As can be seen from the previous screenshot, the upper and lower pixel value limits (over which the image is to be normalized), need to be specified before the stretching can be performed (for example, for a gray-level image, the limits are often set to 0 and 255, in order for the output image to span the entire range of available pixel values). All we need to find is a suitable value of m from the CDF of the original image. The contrast stretching transform produces higher contrast than the original by darkening the levels below the value m (in other words, stretching the values toward the lower limit) in the original image and brightening the levels previous to value m (stretching the values toward the upper limit) in the original image. The following sections describe how to implement contraststretching using the PIL library

# # alpha: contrast, beta: brightness
# adjusted = cv2.convertScaleAbs(img1, alpha=1.5, beta=50)

# cv2.imshow("Contrast & Brightness", adjusted)
# cv2.waitKey(0)
import cv2
import matplotlib.pyplot as plt

# Load image
img1 = cv2.imread("images/parrot.jpg")

# Adjust contrast (alpha) and brightness (beta)
adjusted = cv2.convertScaleAbs(img1, alpha=1.5, beta=50)

# Convert BGR → RGB for matplotlib
adjusted_rgb = cv2.cvtColor(adjusted, cv2.COLOR_BGR2RGB)

# Display safely in notebook
plt.imshow(adjusted_rgb)
plt.axis('off')
plt.title("Contrast & Brightness Adjusted")
plt.show()

 

Smoothing Images:

Online Reference Source

 

 

Problem with Smoothing 
    -Does Not remove outlies(Noise)
    - Smooths edges(Blur)

 

Median filter:

Median Filtering

1.Sort the K2 Values in window Centered at The Pixel
2.Assign The Middle Value(Median) to Pixel 

Non-Linear Operation
(Cannot be Implemented using convolution)

 

 

 

It is the best order statistic filter; it replaces the value of a pixel by the median of gray levels
 
in the Neighborhood of the pixel.

The original of the pixel is included in the computation of the median of the filter are quite possible  because for certain types of random noise, the provide excellent noise reduction capabilities with considerably less blurring then smoothing filters of similar size. These are effective for bipolar and unipolar impulse noise.

 

GAUSSIAN LOWPASS FILTERS:
 
Same Gaussian Kernel is used everywhere.
Blurs across edges
 
"Bias" Gaussian Kernel such that Pixcels not similar in Intensity to
Center Pixel Receive a lower  weight
 
The form of these filters in two dimensions is given by
 
ï‚· This transfer function is smooth, like Butterworth filter.
ï‚· Gaussian in frequency domain remains a Gaussian in spatial domain
ï‚· Advantage: No ringing artifacts.
Where D0 is the cutoff frequency. When D(u,v) = D0, the GLPF is down to 0.607 of its maximum
value. This means that a spatial Gaussian filter, obtained by computing the IDFT of above
equation., will have no ringing. Fig..Shows a perspective plot, image display and radial cross
sections of a GLPF function.

 

 

 

magnified sections in (b) and(c).
Fig. shows an application of lowpass filtering for producing a smoother, softer-looking
result from a sharp original. For human faces, the typical objective is to reduce the sharpness of
fine skin lines and small blemished.
 
 

Bilateral Filter

 
 
 
 
Non-Linear Operation
(Cannot be Implemented using Convolution)