Programming lesson
Mastering Image Warping and Morphological Processing: A MATLAB Guide for EE569 Homework 3
Learn geometric image modification, homographic stitching, and morphological operations with MATLAB. This tutorial covers spatial warping, panorama creation, and defect detection using real-world examples.
Introduction to Image Warping and Morphological Processing
In this tutorial, we explore key concepts from Ee569 homework #3, focusing on geometric image modification, homographic transformation, and morphological processing. These techniques are fundamental in computer vision and image processing, with applications ranging from panorama stitching in smartphones to defect detection in manufacturing. By understanding these methods, you'll be equipped to handle real-world image manipulation tasks.
Geometric Image Modification: Spatial Warping
Understanding the Square-to-Disk Mapping
Problem 1 requires transforming a square image into a disk-shaped image. The mapping must preserve boundaries: square edges map to the circle's circumference, the center remains fixed, and the transformation is one-to-one (reversible). This is a classic example of spatial warping, where each pixel in the output is computed from the input using a coordinate transformation.
Implementing the Warping Algorithm in MATLAB
To implement this, you can use a polar coordinate approach. For each output pixel (x,y) inside the disk, compute its distance from the center and angle. Then map these to input coordinates using a scaling function that ensures boundary alignment. For instance, if the input square has side length L, the radius of the output disk is L/2. The mapping can be: r_out = sqrt(x^2 + y^2), and the input coordinate r_in = (r_out / (L/2)) * (L/2). However, to satisfy the boundary condition, you need a non-linear scaling. A common method is to use the inverse mapping: for each output pixel, find the corresponding input pixel via u = x * (L/2) / sqrt(x^2+y^2) and v = y * (L/2) / sqrt(x^2+y^2) for points inside the circle. This ensures that points on the square boundary (where sqrt(x^2+y^2) = L/2) map to the circle boundary.
Applying the Warp to Test Images
Apply your algorithm to the provided images: Dog in basket, Forky, and Twenty-two. Display the resulting disk-shaped images. Then, perform reverse warping to recover the original square image. Compare the recovered image with the original. You will likely notice differences due to interpolation artifacts (e.g., bilinear or nearest-neighbor interpolation) and the fact that the mapping is not perfectly reversible due to discretization. Discuss these distortions in your report.
Homographic Transformation and Image Stitching
Building Panoramas with Homography
Problem 2 involves stitching multiple images into a panorama using homographic transformations. A homography is a 3x3 matrix that maps points from one image to another, assuming the scene is planar or the camera rotates about its optical center. The steps include: selecting control points (using SIFT/SURF and FLANN matching), estimating the homography matrix using at least 4 point pairs, and warping one image onto the other.
Step-by-Step Implementation
You are allowed to use OpenCV for feature detection. In MATLAB, you can use the detectSURFFeatures and extractFeatures functions. Match features using matchFeatures and then use estimateGeometricTransform to find the homography. For the lunchroom images (left, middle, right), stitch them sequentially: first left and middle, then the result with right. Use backward mapping and bilinear interpolation to warp images. Create a large canvas and composite by averaging overlapping pixels.
Discussion Points
Answer the questions: (1) How many control points were used? Show the corresponding points between left-middle and middle-right pairs. (2) How did you select control points? Explain how you used matched features to create the panorama. Include intermediate results showing the matched points.
Morphological Processing: Shrinking, Thinning, and Skeletonizing
Basic Morphological Operations
Problem 3 covers morphological processing. You will implement shrinking, thinning, and skeletonizing using pattern tables (provided in the assignment). These operations reduce binary shapes to their essential structure. For the spring, flower, and jar images, apply each filter and show intermediate results. Explain how the pattern table works: for each 3x3 neighborhood, a lookup table determines the output pixel value.
Solving a Maze with Morphological Processing
Part (b) asks you to find a solution to a maze. Use morphological thinning iteratively to reduce the maze paths to single-pixel-wide lines. Then, you can trace the path from start to end. Alternatively, use a combination of dilation and subtraction to identify the solution. Show your method and the final path.
Defect Detection in a Horse Image
Part (c) involves a binary image of a horse with black defects. You need to: (1) Count the total number of disconnected defects. Use connected component labeling (e.g., bwconncomp in MATLAB) to count objects. (2) Determine how many different defect sizes exist and plot a histogram of defect pixel counts. (3) Correct the defects by filling them (e.g., using morphological closing or region filling). (4) Suggest alternative methods, such as using a median filter or machine learning for classification.
Practical Tips and Common Pitfalls
- Always use backward mapping for warping to avoid holes.
- For homography, ensure enough point pairs and use RANSAC to remove outliers.
- When morphological operations, be careful with border handling.
- Document all assumptions clearly in your report.
Conclusion
This tutorial has covered the core tasks of Ee569 homework #3. By implementing spatial warping, homographic stitching, and morphological processing, you gain hands-on experience with essential image processing techniques. These skills are directly applicable to modern AI and computer vision systems, such as those used in autonomous vehicles, medical imaging, and augmented reality. Good luck with your assignment!