CV Lab Experiments
CV Lab Experiments
CV Lab Experiments
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
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.
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
# Perform 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.
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
# Perform 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.
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')
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.
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
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.
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
f_transform = np.fft.fft2(image)
f_transform_shifted = np.fft.fftshift(f_transform)
sigma = 10
for i in range(rows):
for j in range(cols):
filtered_image = np.fft.ifft2(np.fft.ifftshift(filtered_spectrum)).real
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.
import cv2
import numpy as np
f_transform = np.fft.fft2(image)
f_transform_shifted = np.fft.fftshift(f_transform)
[-1, 4, -1],
sharpened_image = np.fft.ifft2(np.fft.ifftshift(filtered_spectrum)).real
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.
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:
python
import cv2
image = cv2.imread('input_image.jpg')
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
python
import cv2
image = cv2.imread('input_image.jpg')
hsv_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
python
import cv2
image = cv2.imread('input_image.jpg')
lab_image = cv2.cvtColor(image, cv2.COLOR_BGR2Lab)
python
import cv2
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.