Unit 6: Image Processing Using Python - Subjective Questions
CAP776 — Programming In Python • Practice Questions with Detailed Answers
20 questions
Define digital image processing. Explain how a digital image is represented in Python.
Digital image processing is the use of computer algorithms to manipulate, analyze, enhance, or extract information from digital images.
A digital image is represented as a two-dimensional or three-dimensional array of pixel values:
- A grayscale image is generally a 2D array with one intensity value per pixel.
- A color image is generally a 3D array containing multiple color channels.
- In an 8-bit image, pixel intensity values normally range from to .
- For grayscale images, represents black and represents white.
- RGB images contain red, green, and blue channels.
If an RGB image has width and height , its array shape is commonly . Python libraries such as Pillow, OpenCV, and NumPy are frequently used to load and process these arrays.
Explain image cropping and describe how an image can be cropped using Pillow.
Cropping removes unwanted outer portions of an image and retains a selected rectangular region. It is useful for focusing on an object, changing image composition, or preparing data for analysis.
In Pillow, a crop box is specified as:
(left, upper, right, lower)
Example:
from PIL import Image
image = Image.open("photo.jpg")
cropped = image.crop((100, 50, 500, 350))
cropped.save("cropped.jpg")
The coordinates are measured from the top-left corner. The selected width and height are:
Cropping usually does not modify the retained pixel values; it selects a smaller region from the original image.
What is image resizing? Explain the role of interpolation methods during resizing.
Image resizing changes the width and height of an image. It may be used to reduce storage, create thumbnails, standardize dataset dimensions, or enlarge an image.
Since output pixels may not correspond exactly to original pixels, an interpolation method estimates their values:
- Nearest-neighbor interpolation: Selects the closest source pixel. It is fast but may produce blocky results.
- Bilinear interpolation: Uses the four nearest pixels and produces smoother output.
- Bicubic interpolation: Uses a larger neighborhood and generally produces better-quality enlargements.
- Lanczos interpolation: Uses a high-quality resampling filter and is effective for downscaling.
Pillow example:
resized = image.resize((800, 600), Image.Resampling.LANCZOS)
The chosen method affects processing speed, edge quality, smoothness, and image detail.
Describe image rotation. Explain how the coordinates of a point change when an image is rotated through an angle .
Image rotation turns an image clockwise or anticlockwise around a selected center. It is used for orientation correction, augmentation, and geometric transformation.
For rotation about the origin, a point is transformed into as follows:
The matrix form is:
For rotation about the image center, coordinates are first translated to the center, rotated, and translated back. Interpolation is required because transformed coordinates may not be integers. Empty regions created after rotation may be filled with a background color, and the canvas may optionally be expanded.
Distinguish between horizontal flipping, vertical flipping, and image rotation.
Horizontal flipping reflects an image across a vertical axis:
- Left and right sides are exchanged.
- A pixel at horizontal coordinate moves approximately to .
- It produces a mirror-like image.
Vertical flipping reflects an image across a horizontal axis:
- Top and bottom regions are exchanged.
- A pixel at vertical coordinate moves approximately to .
Rotation turns an image through a specified angle:
- It can use any angle, such as , , or .
- Arbitrary-angle rotation normally requires interpolation.
- It may create empty areas around image boundaries.
A horizontal or vertical flip is a reflection, not a general rotation. However, applying both horizontal and vertical flips is equivalent to a rotation.
Explain brightness adjustment. How can brightness be increased or decreased mathematically and with Pillow?
Brightness adjustment changes the overall lightness or darkness of an image.
An additive brightness transformation may be written as:
where is the original pixel value, is the output, and is the brightness offset. Values must be clipped to the valid range, such as .
A multiplicative model is:
where increases brightness and decreases it.
Pillow provides ImageEnhance.Brightness:
from PIL import ImageEnhance
enhancer = ImageEnhance.Brightness(image)
brighter = enhancer.enhance(1.5)
A factor of 1.0 keeps the original brightness, a factor above 1.0 brightens the image, and a factor below 1.0 darkens it.
What is contrast adjustment? Explain how it differs from brightness adjustment.
Contrast represents the difference between dark and bright regions. Increasing contrast makes dark pixels darker and bright pixels brighter, while decreasing contrast brings pixel intensities closer together.
A common contrast transformation is:
where is a midpoint or mean intensity and is the contrast factor.
- If , contrast increases.
- If , contrast decreases.
- If , the image remains unchanged.
Difference from brightness:
- Brightness mainly shifts or scales the overall intensity level.
- Contrast changes the separation between intensity values.
- Excessive brightness may cause clipping in bright areas.
- Excessive contrast may remove detail from highlights and shadows.
In Pillow, contrast can be changed with ImageEnhance.Contrast(image).enhance(factor).
Explain color adjustment in a digital image. Describe saturation adjustment and grayscale conversion.
Color adjustment modifies the appearance or balance of an image's color channels. It may include changing saturation, hue, channel intensity, white balance, or color temperature.
Saturation indicates the purity or vividness of colors:
- Increasing saturation makes colors more vivid.
- Decreasing saturation makes colors duller.
- Zero saturation produces a grayscale-like image.
Pillow example:
from PIL import ImageEnhance
enhanced = ImageEnhance.Color(image).enhance(1.5)
A grayscale intensity can be estimated from RGB values using a weighted formula:
The weights differ because human vision is more sensitive to green than to blue. Grayscale conversion reduces color information and is useful for thresholding, edge detection, document processing, and other intensity-based operations.
Define image filtering and explain the process of spatial convolution.
Image filtering modifies pixel values by considering each pixel and, usually, its neighboring pixels. Filters are used for smoothing, noise removal, sharpening, and feature detection.
In spatial convolution, a small matrix called a kernel, mask, or filter moves across the image. At each position, neighboring image values are multiplied by corresponding kernel coefficients and summed.
For image and kernel , the output is:
The main steps are:
- Place the kernel over a pixel and its neighborhood.
- Multiply overlapping values.
- Add the products.
- Store the result at the output position.
- Repeat across the image.
Boundary pixels can be handled through zero padding, reflection, replication, or by ignoring incomplete neighborhoods.
Compare mean blur, Gaussian blur, and median blur.
Mean blur:
- Replaces a pixel with the average of neighboring values.
- Uses a kernel with equal coefficients.
- Is simple and fast but can blur edges significantly.
A mean kernel is:
Gaussian blur:
- Uses a weighted average based on a Gaussian distribution.
- Gives greater weight to pixels near the center.
- Produces natural smoothing and reduces Gaussian-like noise.
Median blur:
- Replaces a pixel with the median of neighborhood values.
- Is nonlinear because it does not use weighted summation.
- Is especially effective against salt-and-pepper noise.
- Preserves edges better than a mean filter in many cases.
Thus, filter selection depends on the noise type, required edge preservation, and computational cost.
Explain image sharpening and describe the use of a sharpening kernel.
Image sharpening emphasizes edges and fine details by increasing the difference between neighboring pixel intensities. It is useful for improving visual clarity after acquisition or blurring.
A common sharpening kernel is:
The positive center coefficient preserves and strengthens the current pixel, while the negative neighboring coefficients emphasize local intensity changes.
Another approach is unsharp masking:
- Create a blurred version of the image.
- Subtract the blurred image from the original to obtain a detail mask.
- Add a scaled mask back to the original.
Mathematically:
where controls sharpening strength. Excessive sharpening can amplify noise and create halos or unnatural edges.
What is edge detection? Explain the working of Sobel edge detection.
Edge detection identifies locations where image intensity changes sharply. Such changes often represent object boundaries, lines, corners, or transitions between regions.
The Sobel operator estimates horizontal and vertical intensity gradients using two kernels:
After convolution, the gradient magnitude is calculated as:
An approximate magnitude may also be computed as . The gradient direction is:
A threshold can then be applied to retain strong edges. Smoothing before Sobel processing often reduces false edges caused by noise.
Describe the main stages of the Canny edge detection algorithm.
The Canny edge detector is a multi-stage algorithm designed to detect thin and reliable edges.
Its main stages are:
- Grayscale conversion: Converts a color image into intensity values.
- Gaussian smoothing: Reduces noise that could create false edges.
- Gradient calculation: Finds edge strength and direction, commonly using Sobel derivatives.
- Non-maximum suppression: Retains only local gradient maxima, producing thin edge candidates.
- Double thresholding: Classifies pixels as strong, weak, or non-edge pixels using high and low thresholds.
- Edge tracking by hysteresis: Retains weak edges connected to strong edges and discards isolated weak responses.
OpenCV usage is:
edges = cv2.Canny(gray, 100, 200)
The two values are the lower and upper thresholds. Poor threshold selection may omit real edges or retain excessive noise.
Explain the basic procedure for reading, displaying, and saving an image using OpenCV.
OpenCV provides functions for image input, display, processing, and output.
Example:
import cv2
image = cv2.imread("photo.jpg")
cv2.imshow("Image", image)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.imwrite("copy.jpg", image)
Explanation:
cv2.imread()reads an image and returns a NumPy array.cv2.imshow()displays the image in a window.cv2.waitKey(0)waits indefinitely for a key press.cv2.destroyAllWindows()closes OpenCV windows.cv2.imwrite()saves an image to a file.
By default, OpenCV loads a color image in BGR channel order rather than RGB. If loading fails, cv2.imread() returns None, so programs should check the returned value before processing.
What is Pillow? Describe basic image operations performed using the Image module.
Pillow is a widely used Python imaging library and is the maintained successor to the Python Imaging Library. Its central PIL.Image module represents images and provides common file and transformation operations.
Basic operations include:
- Opening an image with
Image.open(). - Displaying it with
image.show(). - Inspecting
image.size,image.mode, andimage.format. - Converting modes with
image.convert(). - Cropping with
image.crop(). - Resizing with
image.resize(). - Rotating with
image.rotate(). - Flipping with
ImageOpsortranspose(). - Saving with
image.save().
Example:
from PIL import Image
image = Image.open("input.png")
gray = image.convert("L")
gray.save("gray.png")
Pillow supports formats such as JPEG, PNG, BMP, GIF, and TIFF, depending on installed components.
Compare OpenCV and Pillow for image processing in Python.
Pillow:
- Designed primarily for general image manipulation.
- Provides a simple and beginner-friendly interface.
- Is suitable for cropping, resizing, rotating, format conversion, drawing, and basic enhancement.
- Uses RGB channel order for typical color images.
- Represents images as Pillow
Imageobjects.
OpenCV:
- Designed for image processing and computer vision.
- Provides advanced algorithms for filtering, edge detection, feature extraction, video processing, and object analysis.
- Uses NumPy arrays directly.
- Loads color images in BGR order by default.
- Usually offers stronger support for real-time vision applications.
When converting an OpenCV image for use with Pillow, BGR should be changed to RGB. Pillow is often preferred for straightforward file and display operations, while OpenCV is preferred for numerical processing and computer vision.
Explain image modes and color-channel ordering. Why can color errors occur when images are exchanged between Pillow and OpenCV?
An image mode defines how each pixel is represented.
Common Pillow modes include:
1: Binary image.L: 8-bit grayscale image.RGB: Red, green, and blue channels.RGBA: RGB plus an alpha channel.CMYK: Cyan, magenta, yellow, and black channels.
Pillow commonly uses RGB, whereas OpenCV commonly stores loaded color images as BGR. If a BGR array is interpreted directly as RGB, red and blue are exchanged, causing visibly incorrect colors.
Conversion examples in OpenCV are:
rgb = cv2.cvtColor(bgr, cv2.COLOR_BGR2RGB)
gray = cv2.cvtColor(bgr, cv2.COLOR_BGR2GRAY)
When converting a NumPy array to a Pillow image, channel order and data type must be correct. Most 8-bit color images use unsigned integer values from to .
Describe a complete enhancement pipeline for a noisy, dark, and low-contrast image.
A suitable enhancement pipeline may contain the following stages:
- Load and validate the image: Confirm that the file exists and inspect its dimensions and color mode.
- Correct orientation or crop: Rotate, flip, or crop the image if required.
- Reduce noise: Apply Gaussian blur for smooth noise or median blur for salt-and-pepper noise.
- Adjust brightness: Increase intensity carefully while avoiding clipping.
- Improve contrast: Use contrast scaling or histogram-based enhancement.
- Correct color: Adjust saturation, white balance, or individual color channels.
- Sharpen: Apply mild sharpening after noise reduction to restore details.
- Inspect the result: Compare the processed image with the original and check for lost details or artifacts.
- Save appropriately: Choose a suitable format and compression quality.
The order matters. Sharpening before denoising can amplify noise, and repeated JPEG saving can introduce compression artifacts. Processing parameters should be selected according to image content rather than applied excessively.
Write and explain an OpenCV-based procedure to crop, resize, flip, and rotate an image.
An OpenCV procedure is:
import cv2
image = cv2.imread("input.jpg")
cropped = image[50:350, 100:500]
resized = cv2.resize(cropped, (400, 300))
flipped = cv2.flip(resized, 1)
center = (flipped.shape[1] // 2, flipped.shape[0] // 2)
matrix = cv2.getRotationMatrix2D(center, 30, 1.0)
rotated = cv2.warpAffine(
flipped, matrix, (flipped.shape[1], flipped.shape[0])
)
cv2.imwrite("output.jpg", rotated)
Explanation:
- NumPy slicing uses
[y1:y2, x1:x2]to crop. cv2.resize()changes output dimensions.cv2.flip(image, 1)performs a horizontal flip;0performs a vertical flip, and-1flips both axes.cv2.getRotationMatrix2D()creates the affine transformation matrix.cv2.warpAffine()applies the rotation.
Cropping coordinates must remain within the image boundaries, and rotation may remove corner regions unless the output canvas is expanded.
Discuss the factors that affect the quality of filtering and image enhancement operations.
The quality of filtering and enhancement depends on several factors:
- Kernel size: Larger smoothing kernels remove more noise but also remove more detail.
- Filter type: Median, Gaussian, and mean filters behave differently for different noise distributions.
- Parameter strength: Excessive brightness, contrast, saturation, or sharpening can create unnatural output.
- Image bit depth: Low bit depth may cause banding after strong adjustments.
- Clipping: Values below the minimum or above the maximum must be clipped, which can destroy shadow or highlight detail.
- Processing order: Denoising should generally occur before sharpening or edge detection.
- Interpolation method: Resizing and rotation quality depend on interpolation.
- Compression: Lossy formats such as JPEG can introduce block and ringing artifacts.
- Boundary handling: Padding choices influence filter output near the image edges.
- Noise level and image content: Fine textures can be mistaken for noise, while strong denoising can erase meaningful features.
Effective enhancement balances noise reduction, detail preservation, visual quality, and computational cost.
Define digital image processing. Explain how a digital image is represented in Python.
Digital image processing is the use of computer algorithms to manipulate, analyze, enhance, or extract information from digital images.
A digital image is represented as a two-dimensional or three-dimensional array of pixel values:
- A grayscale image is generally a 2D array with one intensity value per pixel.
- A color image is generally a 3D array containing multiple color channels.
- In an 8-bit image, pixel intensity values normally range from to .
- For grayscale images, represents black and represents white.
- RGB images contain red, green, and blue channels.
If an RGB image has width and height , its array shape is commonly . Python libraries such as Pillow, OpenCV, and NumPy are frequently used to load and process these arrays.
Did this save you a night before the exam?
LPU Notes is free, and it stays free. Ads cover part of the server bill. The rest comes out of a student's own pocket: the domain, the storage, and keeping the site up through the weeks everyone needs it at once.
The payment button didn't load. An ad blocker or a filtered network is the usual reason. to try again.
Nothing here is ever locked, and nothing unlocks. Chip in only if it was worth it. What it pays for →