ALCANTARAuLaboratory-6-Image-Processing-Student_031006
ALCANTARAuLaboratory-6-Image-Processing-Student_031006
Samonte
BScPE 4-1
Processing To introduce basic image processing techniques using Python and OpenCV.
a. Create a folder on preferred destination and upload selfie image with “.jpg” file
extension.
b. Create a “.py” file and save it on the same destination
of images. c. Import image using open OpenCV.
d. Display the image using OpenCV ‘imshow’
function. e. Save the displayed image as a
new file.
Code:
import
cv2
# Load the image file (replace "abi.jpg" with your file name)
image = cv2.imread("abi.jpg")
# Wait until a key is pressed and then close the display window
cv2.waitKey(0)
cv2.destroyAllWindows()
CODE DISCUSSION:
This Python code uses OpenCV to load, display, and save an image. To do this, it starts with
loading the image through the command cv2.imread("abi.jpg"), where the image file, (abi.jpg), is found
in the same folder from where the Python script has been invoked. Replace "abi.jpg" with the name of
the image that you want to use. The loaded image is then shown in a window with the title of "Image" by
cv2.imshow ("Image", image). That is where you can preview the image before saving. It saves a copy of
the image as "new_selfie.jpg" using cv2.imwrite ("new_selfie.jpg", image). Thus, creating a new image
copy and giving it another name but keeping the previous image safe. After showing the picture, the
script waits until a key is pressed (cv2.waitKey(0)) and then, after detecting a pressed key, closes a
window with cv2.destroyAllWindows(). It's such an easy flow to load your image, see it, and save a new
version using just a few lines of code.
Code:
import cv2
import numpy as np
CODE DISCUSSION:
The first code changes the brightness and contrast of an image. The function
cv2.convertScaleAbs scales the pixel values of the input image to enhance contrast by setting alpha
and brightness by beta. There are three output versions of the original image with their brightness
increased by 50, then 100, and lastly by 200 with a contrast factor of 1.5. These adjustments make
the image appear progressively brighter while maintaining contrast enhanced. The results are
displayed using OpenCV's imshow and saved as separate image files for reference.
b. Apply color
filters.
Figure 1: Original
Image
Figure 2: Sobel Filtered Image
Figure 3: Gaussian Filtered Ima
ge Figure 4:
Laplacian Filtered Image
Code:
import cv2
import numpy as np
# Sobel Filter
sobel = cv2.Sobel(image, cv2.CV_64F, 1, 1, ksize=3)
# Gaussian Filter
gaussian = cv2.GaussianBlur(image, (5, 5), 0)
# Laplacian Filter
laplacian = cv2.Laplacian(image, cv2.CV_64F)
CODE DISCUSSION:
The second code demonstrates three types of image filters: Sobel, Gaussian, and Laplacian,
showing how they each affect an image. The Sobel filter is applied with cv2.Sobel, which calculates
changes in pixel intensity both horizontally and vertically, making it good for detecting edges and
boundaries. The Gaussian filter is applied using cv2.GaussianBlur, which smooths the image by
averaging pixel values based on a Gaussian distribution, helping reduce noise and fine details. The
Laplacian filter is applied with cv2.Laplacian to calculate the second derivative of pixel intensity,
highlighting rapid intensity changes, like edges. The results from each filter are converted into an 8-
bit format for easy display and saving. Each filtered image is saved and shown in separate windows
so you can compare how each filter changes the image. This code is a practical way to demonstrate
techniques like edge detection, noise reduction, and feature enhancement
.
a. Filter image using convolution and
kernels b. Use 2x2, 5x5, and 10x10 kernel.
Code:
import cv2
import numpy as np
CODE DISCUSSION:
This code applies convolution to an image using normalized kernels of sizes 2x2, 5x5, and 10x10.
Convolution blurs the image by averaging the pixel values within the area covered by the kernel.
Smaller kernels (like 2x2) produce a gentle blur, while larger kernels (like 10x10) create a stronger
smoothing effect. The convolution is performed using the cv2.filter2D function. The resulting blurred
images are then saved and displayed, allowing you to see the varying degrees of smoothing based on
the different kernel sizes.
Code:
import cv2
import numpy as np
# Create a binary mask for the object (white background is treated as background)
_, mask = cv2.threshold(gray, 240, 255, cv2.THRESH_BINARY_INV)
CODE DISCUSSION:
This code replaces the white background of an image with different solid colors like yellow, blue, and
red. First, the image is converted to grayscale to easily identify the white background. A binary mask
is created using cv2.threshold, where the white areas are identified as the background and the other
areas as the object. Solid color backgrounds are generated using NumPy arrays, and the object is
combined with the new background using bitwise operations (cv2.bitwise_or and cv2.bitwise_and).
The result is an image where the original background is replaced with bold colors, which is then
saved and displayed in OpenCV windows for reference.
Conclusion
The document, Laboratory Module No. 6 – Image Processing, introduces basic image processing
techniques using Python and OpenCV. It covers various applications of OpenCV, including image
manipulation, brightness and contrast adjustments, filtering with Sobel, Gaussian, and Laplacian
methods, kernel-based convolution for smoothing, and background replacement with solid colors.
The code examples demonstrate fundamental concepts, such as reading, displaying, and
saving images, applying transformations, and filtering images using kernels and masks. The
exercises provide hands-on experience in areas like edge detection, noise reduction, and
background modification, highlighting the versatility of OpenCV for image processing tasks.
This module effectively bridges theory and practice, making it a valuable resource for
understanding image processing fundamentals with Python and OpenCV