Nothing Special   »   [go: up one dir, main page]

CV Lab Experiments

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

Experiments

1. Reading a color image and subtracting a square from that image

 Sure, here's an example of how you can read a color image, subtract a square region from
it, and visualize the result using Python and the OpenCV library:
 Python
import cv2
import numpy as np

# Read a color image


image = cv2.imread('color_image.jpg')

# Define the coordinates of the square region to subtract


top_left = (100, 100)
bottom_right = (300, 300)

# Create a copy of the image


modified_image = image.copy()

# Set the pixel values in the square region to black (0, 0, 0)


modified_image[top_left[1]:bottom_right[1], top_left[0]:bottom_right[0]] = [0,
0, 0]

# Display the original and modified images


cv2.imshow('Original Image', image)
cv2.imshow('Modified Image', modified_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

 In this code, you first read a color image using cv2.imread(). Then, you define the
coordinates of the top-left and bottom-right corners of the square region you want to
subtract from the image. You create a copy of the image using .copy() to avoid
modifying the original image. Finally, you set the pixel values in the specified square
region to black, effectively subtracting the square from the image..
 Make sure to replace 'color_image.jpg' with the actual path to your color image file.
You can adjust the coordinates of the square region to suit your needs.

2.erosion of an image in grayscale python code

Certainly! Here's an example of how to perform erosion on a grayscale image using Python and
the OpenCV library:

import cv2

import numpy as np
# Read a grayscale image

image = cv2.imread('grayscale_image.jpg', cv2.IMREAD_GRAYSCALE)

# Define the kernel for erosion

kernel = np.ones((5, 5), np.uint8)

# Perform erosion

erosion = cv2.erode(image, kernel, iterations=1)

# Display the original and eroded images

cv2.imshow('Original Image', image)

cv2.imshow('Eroded Image', erosion)

cv2.waitKey(0)

cv2.destroyAllWindows()

In this code, you read a grayscale image using cv2.imread() with the cv2.IMREAD_GRAYSCALE
flag to ensure the image is read as grayscale. You define a kernel using np.ones() to specify the
shape of the erosion operation. Then, you use cv2.erode() to perform the erosion operation on
the image.

Replace 'grayscale_image.jpg' with the actual path to your grayscale image file. You can
also adjust the kernel size and iterations to achieve the desired erosion effect.

3 dilation of an image in grayscale using python

Certainly! Here's an example of how to perform dilation on a grayscale image using Python and
the OpenCV library:

import cv2

import numpy as np
# Read a grayscale image

image = cv2.imread('grayscale_image.jpg', cv2.IMREAD_GRAYSCALE)

# Define the kernel for dilation

kernel = np.ones((5, 5), np.uint8)

# Perform dilation

dilation = cv2.dilate(image, kernel, iterations=1)

# Display the original and dilated images

cv2.imshow('Original Image', image)

cv2.imshow('Dilated Image', dilation)

cv2.waitKey(0)

cv2.destroyAllWindows()

In this code, you read a grayscale image using cv2.imread() with the cv2.IMREAD_GRAYSCALE
flag to ensure the image is read as grayscale. You define a kernel using np.ones() to specify the
shape of the dilation operation. Then, you use cv2.dilate() to perform the dilation operation
on the image.

Replace 'grayscale_image.jpg' with the actual path to your grayscale image file. You can
also adjust the kernel size and iterations to achieve the desired dilation effect.

4.Smoothing of an image in spatial domain using python

Sure, here's an example of how to perform image smoothing (blurring) in the spatial domain
using Python and the OpenCV library:

import cv2
# Read an image

image = cv2.imread('input_image.jpg')

# Apply Gaussian blur for smoothing

blurred = cv2.GaussianBlur(image, (5, 5), 0)

# Display the original and blurred images

cv2.imshow('Original Image', image)

cv2.imshow('Blurred Image', blurred)

cv2.waitKey(0)

cv2.destroyAllWindows()

In this code, you read an image using cv2.imread(). Then, you apply Gaussian blur using
cv2.GaussianBlur() to smooth the image. The (5, 5) argument represents the size of the
Gaussian kernel, and 0 indicates the standard deviation of the kernel in the X and Y directions.

Replace 'input_image.jpg' with the actual path to your image file. You can adjust the kernel
size and standard deviation to control the level of smoothing applied to the image.

5 Sharpening of an image in spatial domain using python

Certainly! Here's an example of how to perform image sharpening in the spatial domain using
Python and the OpenCV library:

import cv2

import numpy as np

# Read an image

image = cv2.imread('input_image.jpg', cv2.IMREAD_GRAYSCALE)


# Apply Laplacian filter for sharpening

laplacian = cv2.Laplacian(image, cv2.CV_64F)

# Display the original and sharpened images

cv2.imshow('Original Image', image)

cv2.imshow('Sharpened Image', laplacian)

cv2.waitKey(0)

cv2.destroyAllWindows()

In this code, you read a grayscale image using cv2.imread() with the cv2.IMREAD_GRAYSCALE
flag. Then, you apply the Laplacian filter using cv2.Laplacian() to enhance the edges and
details in the image.

Replace 'input_image.jpg' with the actual path to your grayscale image file. The Laplacian
filter helps sharpen the image by enhancing the edges and details, but it can also amplify noise.
You can experiment with different filters and techniques for more sophisticated sharpening
effects.

6 smoothing of an image in frequency domain using python

Smoothing in the frequency domain typically involves using filters that attenuate high-frequency
components while preserving low-frequency information. One commonly used filter for this
purpose is the Gaussian filter. Here's an example of how you can perform image smoothing in
the frequency domain using Python and the OpenCV library:

import cv2

import numpy as np

import matplotlib.pyplot as plt

# Read the image in grayscale


image = cv2.imread('input_image.jpg', cv2.IMREAD_GRAYSCALE)

# Perform Fast Fourier Transform (FFT)

f_transform = np.fft.fft2(image)

f_transform_shifted = np.fft.fftshift(f_transform)

# Create a Gaussian filter

rows, cols = image.shape

crow, ccol = rows // 2, cols // 2

gaussian_filter = np.zeros((rows, cols))

sigma = 10

for i in range(rows):

for j in range(cols):

gaussian_filter[i, j] = np.exp(-((i - crow)**2 + (j - ccol)**2) / (2 * sigma**2))

# Apply the Gaussian filter in the frequency domain

filtered_spectrum = f_transform_shifted * gaussian_filter

# Inverse FFT to get the filtered image

filtered_image = np.fft.ifft2(np.fft.ifftshift(filtered_spectrum)).real

# Display the original and smoothed images

plt.subplot(121), plt.imshow(image, cmap='gray')


plt.title('Original Image'), plt.xticks([]), plt.yticks([])

plt.subplot(122), plt.imshow(filtered_image, cmap='gray')

plt.title('Smoothed Image in Frequency Domain'), plt.xticks([]), plt.yticks([])

plt.show()

In this code, you first read an image in grayscale using cv2.imread() with the
cv2.IMREAD_GRAYSCALE flag. Then, you perform a Fast Fourier Transform (FFT) using
np.fft.fft2() to transform the image into the frequency domain. The FFT result is shifted
using np.fft.fftshift() to place the zero-frequency components at the center.

A Gaussian filter is created based on the image dimensions, and the filter is applied to the shifted
FFT. The filtered spectrum is then inverse-transformed using np.fft.ifft2() to obtain the
smoothed image in the frequency domain.

Replace 'input_image.jpg' with the actual path to your grayscale image file. You can adjust
the sigma parameter of the Gaussian filter to control the level of smoothing.

7 sharpening of an image in frequency domain using python

Sharpening in the frequency domain can be achieved by emphasizing high-frequency


components. One common sharpening filter in the frequency domain is the Laplacian filter.
Here's how you can perform image sharpening in the frequency domain using Python and the
OpenCV library:

import cv2

import numpy as np

import matplotlib.pyplot as plt

# Read the image in grayscale

image = cv2.imread('input_image.jpg', cv2.IMREAD_GRAYSCALE)

# Perform Fast Fourier Transform (FFT)

f_transform = np.fft.fft2(image)
f_transform_shifted = np.fft.fftshift(f_transform)

# Create a Laplacian sharpening filter

rows, cols = image.shape

laplacian_filter = np.array([[0, -1, 0],

[-1, 4, -1],

[0, -1, 0]])

# Apply the Laplacian filter in the frequency domain

filtered_spectrum = f_transform_shifted * laplacian_filter

# Inverse FFT to get the sharpened image

sharpened_image = np.fft.ifft2(np.fft.ifftshift(filtered_spectrum)).real

# Display the original and sharpened images

plt.subplot(121), plt.imshow(image, cmap='gray')

plt.title('Original Image'), plt.xticks([]), plt.yticks([])

plt.subplot(122), plt.imshow(sharpened_image, cmap='gray')

plt.title('Sharpened Image in Frequency Domain'), plt.xticks([]), plt.yticks([])

plt.show()

In this code, you read a grayscale image using cv2.imread() with the cv2.IMREAD_GRAYSCALE
flag. You then perform a Fast Fourier Transform (FFT) using np.fft.fft2() to transform the
image into the frequency domain. The FFT result is shifted using np.fft.fftshift() to center
the zero-frequency components.
A Laplacian sharpening filter is created and applied to the shifted FFT. The filtered spectrum is
then inverse-transformed using np.fft.ifft2() to obtain the sharpened image in the frequency
domain.

8 use of color spaces and its conversions using python

Color spaces are representations of colors in a defined way, where each color is defined by its
components. There are several color spaces used in image processing and computer vision,
including RGB, BGR, HSV, Lab, and more. Converting between color spaces is often necessary
to perform various tasks like color correction, image enhancement, and object detection. Here's
how to use color spaces and perform conversions using Python and OpenCV:

RGB to Grayscale Conversion:

python
import cv2

image = cv2.imread('input_image.jpg')
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

RGB to HSV Conversion:

python
import cv2

image = cv2.imread('input_image.jpg')
hsv_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)

RGB to Lab Conversion:

python
import cv2

image = cv2.imread('input_image.jpg')
lab_image = cv2.cvtColor(image, cv2.COLOR_BGR2Lab)

Grayscale to RGB Conversion:

python
import cv2

gray_image = cv2.imread('gray_image.jpg', cv2.IMREAD_GRAYSCALE)


color_image = cv2.cvtColor(gray_image, cv2.COLOR_GRAY2BGR)

HSV to RGB Conversion:

python
import cv2

hsv_image = cv2.imread('hsv_image.jpg')
rgb_image = cv2.cvtColor(hsv_image, cv2.COLOR_HSV2BGR)
Lab to RGB Conversion:

python
import cv2

lab_image = cv2.imread('lab_image.jpg')
rgb_image = cv2.cvtColor(lab_image, cv2.COLOR_Lab2BGR)

In these examples, replace 'input_image.jpg' with the actual path to your image file. OpenCV
provides functions like cv2.cvtColor() for color space conversions. Note that certain color
spaces like RGB, HSV, and Lab have different ranges for their components, and you might need
to adjust values after conversions.

Understanding and using color spaces and their conversions are crucial for various image
processing tasks like color-based segmentation, object tracking, and image analysis.

You might also like