Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views

R Programming

The document outlines various image processing techniques including point-to-point transformation, thresholding, histogram equalization, geometric transformations, linear and non-linear filtering, and edge detection using Python and OpenCV. It provides code examples for each technique, demonstrating how to manipulate images, apply filters, and analyze frequency components. The document serves as a practical guide for implementing these techniques in image processing tasks.

Uploaded by

Krishna Soni
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

R Programming

The document outlines various image processing techniques including point-to-point transformation, thresholding, histogram equalization, geometric transformations, linear and non-linear filtering, and edge detection using Python and OpenCV. It provides code examples for each technique, demonstrating how to manipulate images, apply filters, and analyze frequency components. The document serves as a practical guide for implementing these techniques in image processing tasks.

Uploaded by

Krishna Soni
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

1. Point-to-point transformation.

This laboratory experiment provides for thresholding an image


and the evaluation of its histogram. Histogram equalization. This experiment illustrates the
relationship among the intensities (gray levels) of an image and its histogram.

Ans. Certainly! The point-to-point transformation, thresholding, and histogram equalization are
fundamental image processing techniques that can be implemented using various programming
languages and libraries. I'll provide examples using Python and the OpenCV library.

### 1. Point-to-Point Transformation:

```python

import cv2

import numpy as np

import matplotlib.pyplot as plt

# Load an image

image = cv2.imread('path/to/your/image.jpg', cv2.IMREAD_GRAYSCALE)

# Apply point-to-point transformation (example: contrast adjustment)

alpha = 1.5 # Scaling factor

beta = 30 # Brightness offset

transformed_image = cv2.convertScaleAbs(image, alpha=alpha, beta=beta)

# Display the original and transformed images

plt.subplot(1, 2, 1), plt.imshow(image, cmap='gray'), plt.title('Original Image')

plt.subplot(1, 2, 2), plt.imshow(transformed_image, cmap='gray'), plt.title('Transformed Image')

plt.show()

```

Replace 'path/to/your/image.jpg' with the actual path to your image file. This example adjusts the
contrast and brightness using the point-to-point transformation.

### 2. Thresholding and Histogram:

```python

import cv2

import numpy as np

import matplotlib.pyplot as plt

# Load an image
image = cv2.imread('path/to/your/image.jpg', cv2.IMREAD_GRAYSCALE)

# Apply thresholding

_, binary_image = cv2.threshold(image, 128, 255, cv2.THRESH_BINARY)

# Compute and plot the histogram

hist = cv2.calcHist([image], [0], None, [256], [0, 256])

plt.plot(hist)

plt.title('Original Image Histogram')

plt.show()

# Display the original and binary images

plt.subplot(1, 2, 1), plt.imshow(image, cmap='gray'), plt.title('Original Image')

plt.subplot(1, 2, 2), plt.imshow(binary_image, cmap='gray'), plt.title('Binary Image (Thresholded)')

plt.show()

```

Replace 'path/to/your/image.jpg' with the actual path to your image file. This example applies a simple
binary thresholding technique.

### 3. Histogram Equalization:

```python

import cv2

import numpy as np

import matplotlib.pyplot as plt

# Load an image

image = cv2.imread('path/to/your/image.jpg', cv2.IMREAD_GRAYSCALE)

# Apply histogram equalization

equalized_image = cv2.equalizeHist(image)

# Compute and plot the histograms

hist_original = cv2.calcHist([image], [0], None, [256], [0, 256])

hist_equalized = cv2.calcHist([equalized_image], [0], None, [256], [0, 256])

# Display the original and equalized images

plt.subplot(2, 2, 1), plt.imshow(image, cmap='gray'), plt.title('Original Image')


plt.subplot(2, 2, 2), plt.plot(hist_original), plt.title('Original Image Histogram')

plt.subplot(2, 2, 3), plt.imshow(equalized_image, cmap='gray'), plt.title('Equalized Image')

plt.subplot(2, 2, 4), plt.plot(hist_equalized), plt.title('Equalized Image Histogram')

plt.show()

```

Replace 'path/to/your/image.jpg' with the actual path to your image file. This example applies histogram
equalization to enhance the contrast of the image.

These examples use the OpenCV library for image processing. Ensure you have OpenCV installed:

```bash

pip install opencv-python

```

Feel free to adjust the parameters and explore more sophisticated image processing techniques based
on your specific needs.
2. Geometric transformations. This experiment shows image rotation, scaling, and translation. Two-
dimensional Fourier transform.

Ans. Certainly! Geometric transformations, including rotation, scaling, and translation, are common
operations in image processing. Additionally, the two-dimensional Fourier transform is a powerful tool
for analyzing the frequency components of an image. I'll provide examples using Python and the OpenCV
library.

### 1. Image Rotation, Scaling, and Translation:

```python

import cv2

import numpy as np

import matplotlib.pyplot as plt

# Load an image

image = cv2.imread('path/to/your/image.jpg')

# Rotation

rows, cols = image.shape[:2]

rotation_matrix = cv2.getRotationMatrix2D((cols/2, rows/2), 45, 1) # Rotate by 45 degrees

rotated_image = cv2.warpAffine(image, rotation_matrix, (cols, rows))

# Scaling

scaling_factor = 1.5

scaled_image = cv2.resize(image, None, fx=scaling_factor, fy=scaling_factor)

# Translation

translation_matrix = np.float32([[1, 0, 50], [0, 1, 30]]) # Translate by (50, 30) pixels

translated_image = cv2.warpAffine(image, translation_matrix, (cols, rows))

# Display the original and transformed images

plt.subplot(2, 2, 1), plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB)), plt.title('Original Image')

plt.subplot(2, 2, 2), plt.imshow(cv2.cvtColor(rotated_image, cv2.COLOR_BGR2RGB)), plt.title('Rotated


Image')

plt.subplot(2, 2, 3), plt.imshow(cv2.cvtColor(scaled_image, cv2.COLOR_BGR2RGB)), plt.title('Scaled


Image')

plt.subplot(2, 2, 4), plt.imshow(cv2.cvtColor(translated_image, cv2.COLOR_BGR2RGB)),


plt.title('Translated Image')
plt.show()

```

Replace 'path/to/your/image.jpg' with the actual path to your image file. This example demonstrates
rotation by 45 degrees, scaling by a factor of 1.5, and translation by (50, 30) pixels.

### 2. Two-Dimensional Fourier Transform:

```python

import cv2

import numpy as np

import matplotlib.pyplot as plt

# Load an image

image = cv2.imread('path/to/your/image.jpg', cv2.IMREAD_GRAYSCALE)

# Compute the 2D Fourier Transform

fourier_transform = np.fft.fft2(image)

magnitude_spectrum = np.log(np.abs(fourier_transform) + 1) # Log transform for better visualization

# Display the original image and its magnitude spectrum

plt.subplot(1, 2, 1), plt.imshow(image, cmap='gray'), plt.title('Original Image')

plt.subplot(1, 2, 2), plt.imshow(magnitude_spectrum, cmap='gray'), plt.title('Magnitude Spectrum')

plt.show()

```

Replace 'path/to/your/image.jpg' with the actual path to your grayscale image file. This example
computes the 2D Fourier Transform and displays the original image and its magnitude spectrum.

These examples use the OpenCV library for image processing. Ensure you have OpenCV installed:

```bash

pip install opencv-python

```

Feel free to modify the parameters and explore further geometric transformations and Fourier
transform-based analyses based on your specific requirements.
3. Linear filtering using convolution. Highly selective filters.

Ans. Linear filtering using convolution is a common technique in image processing. Highly selective filters
are filters designed to enhance or suppress specific features in an image. Let's implement linear filtering
with selective filters using Python and the OpenCV library.

### Linear Filtering with Convolution:

```python

import cv2

import numpy as np

import matplotlib.pyplot as plt

# Load an image

image = cv2.imread('path/to/your/image.jpg', cv2.IMREAD_GRAYSCALE)

# Define a Gaussian filter (Example: Highly selective filter for blurring)

gaussian_filter = np.array([[1, 4, 6, 4, 1],

[4, 16, 24, 16, 4],

[6, 24, 36, 24, 6],

[4, 16, 24, 16, 4],

[1, 4, 6, 4, 1]]) / 256

# Apply convolution with the Gaussian filter

filtered_image = cv2.filter2D(image, -1, gaussian_filter)

# Display the original and filtered images

plt.subplot(1, 2, 1), plt.imshow(image, cmap='gray'), plt.title('Original Image')

plt.subplot(1, 2, 2), plt.imshow(filtered_image, cmap='gray'), plt.title('Filtered Image')

plt.show()

```
Replace 'path/to/your/image.jpg' with the actual path to your image file. This example uses a Gaussian
filter for blurring, which is a commonly used highly selective filter.

You can design different filters for various purposes, such as edge detection, sharpening, or smoothing,
based on the specific features you want to enhance or suppress.

### Highly Selective Filter Example: Edge Detection (Sobel Filter):

```python
import cv2

import numpy as np

import matplotlib.pyplot as plt

# Load an image

image = cv2.imread('path/to/your/image.jpg', cv2.IMREAD_GRAYSCALE)

# Apply Sobel filter for edge detection

sobel_x = cv2.Sobel(image, cv2.CV_64F, 1, 0, ksize=3)

sobel_y = cv2.Sobel(image, cv2.CV_64F, 0, 1, ksize=3)

magnitude = np.sqrt(sobel_x**2 + sobel_y**2)

# Display the original and edge-detected images

plt.subplot(1, 2, 1), plt.imshow(image, cmap='gray'), plt.title('Original Image')

plt.subplot(1, 2, 2), plt.imshow(magnitude, cmap='gray'), plt.title('Edge-Detected Image')

plt.show()

```

This example uses the Sobel filter for edge detection. You can adjust the filter parameters based on your
requirements.

Feel free to experiment with different filters and their parameters to achieve the desired effects.
4. Ideal filters in the frequency domain. Non Linear filtering using convolutional masks. Edge
detection. This experiment enables students to understand the concept of edge detectors
and their operation in noisy images.

Ans. Certainly! Edge detection is a crucial task in image processing, and ideal filters in the frequency
domain can be used for this purpose. Additionally, non-linear filtering using convolutional masks can
enhance the results, especially in the presence of noise. I'll provide examples using Python and the
OpenCV library.

### Ideal Filters in the Frequency Domain:

```python

import cv2

import numpy as np

import matplotlib.pyplot as plt

# Load an image

image = cv2.imread('path/to/your/image.jpg', cv2.IMREAD_GRAYSCALE)

# Compute the 2D Fourier Transform

fourier_transform = np.fft.fft2(image)

frequencies = np.fft.fftshift(fourier_transform)

# Create an ideal high-pass filter

rows, cols = image.shape

center_row, center_col = rows // 2, cols // 2

radius = 30 # Adjust the radius based on your requirements

mask = np.ones((rows, cols), np.uint8)

cv2.circle(mask, (center_col, center_row), radius, 0, -1)

# Apply the ideal high-pass filter

frequencies_high_pass = frequencies * mask

filtered_image_high_pass = np.fft.ifft2(np.fft.ifftshift(frequencies_high_pass)).real

# Display the original and filtered images

plt.subplot(1, 2, 1), plt.imshow(image, cmap='gray'), plt.title('Original Image')

plt.subplot(1, 2, 2), plt.imshow(filtered_image_high_pass, cmap='gray'), plt.title('Filtered Image (High


Pass)')

plt.show()
```

Replace 'path/to/your/image.jpg' with the actual path to your image file. This example demonstrates the
application of an ideal high-pass filter in the frequency domain for edge enhancement.

### Non-Linear Filtering for Edge Detection (Using Laplacian Mask):

```python

import cv2

import numpy as np

import matplotlib.pyplot as plt

# Load an image

image = cv2.imread('path/to/your/image.jpg', cv2.IMREAD_GRAYSCALE)

# Apply a Laplacian mask for edge detection

laplacian_mask = np.array([[0, 1, 0],

[1, -4, 1],

[0, 1, 0]])

filtered_image_laplacian = cv2.filter2D(image, -1, laplacian_mask)

# Display the original and filtered images

plt.subplot(1, 2, 1), plt.imshow(image, cmap='gray'), plt.title('Original Image')

plt.subplot(1, 2, 2), plt.imshow(filtered_image_laplacian, cmap='gray'), plt.title('Edge-Detected Image


(Laplacian)')

plt.show()

```

This example applies a Laplacian mask using convolution for edge detection. You can experiment with
other non-linear filters such as the Sobel operator, Prewitt operator, etc.

Feel free to adjust parameters, experiment with different masks, and explore more non-linear filters to
understand their effects on edge detection in noisy images.

You might also like