- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Auto Cropping Based on Labeling the Connected Components using MATLAB
In digital image processing, the auto cropping based labeling the connected components is a cropping technique in which different regions in an image are determined by using labeled connected component. After that the image is automatically cropped by extracting the surrounding area of each connecting component.
The main advantage of the auto-cropping based on labeling the connected components is that it allows to isolate and extract individual regions of interest within an image.
Algorithm
The step-by-step process to execute the auto-cropping based on labeling the connected components is given below:
Step 1 - Process the image if necessary. This step can involve applying filters, contrast adjustment, rgb to grayscale conversion, etc.
Step 2 - Create a binary image. This is done by using threshold operation. In the binary image, the regions of interest within the image are represented as foreground.
Step 3 - Perform connected component labeling on the binary image.
Step 4 - Find the properties of interest like area, bounding box, etc. by analyzing the labeled connected components. In MATLAB, this operation can be performed by using “regionprops()” function.
Step 5 - Select the desired connected components for cropping depending on a specific condition.
Step 6 - Perform cropping of each selected component by using “imcrop()” function in MATLAB.
Step 7 - Display the cropped image by using “imshow()” function in MATLAB.
Let us now understand this auto cropping of image based on labeling the connected components with the help of a MATLAB program.
Example
% MATLAB program to demonstrate auto cropping based on labeling the connected components % Call imread() function to read the input image img = imread('https://www.tutorialspoint.com/assets/questions/media/14304-1687425323.jpg'); % Convert the input image to grayscale grayimg = rgb2gray(img); % Create a binary image using a threshold % Set the threshold value as per need threshold = 0.35; binaryimg = imbinarize(grayimg, threshold); % Perform connected component labeling labeledimg = bwlabel(binaryimg); % Extract the properties of connected components prop = regionprops(labeledimg, 'BoundingBox'); % Select and crop each connected component for i = 1 : numel(prop) boundingbox = prop(i).BoundingBox; croppedimg = imcrop(img, boundingbox); % Display the cropped image imshow(croppedimg); title('Cropped Image'); end
Conclusion
In this MATLAB program, we call “imread” function to read the input image. Then, the input image is processed by using “rgb2gray” function to convert to grayscale. Next, we call “imbinarize” function to create a binary image of the grayscale image using a threshold. Then, we call “bwlabel” function to perform connected component labeling. After that we find the properties of the connected components by using “regionprops” function. In this example program, we have extracted the bounding boxes of the components. Next, we run the “for” loop to process each connected component and crop the region from the original image by using “imcrop” function. Finally, we call “imshow” function to display the cropped image.