- Read the image using the "imread" function and convert it to grayscale if necessary.
- Use a suitable thresholding method to convert the image into a binary image, such as "imbinarize".
- Use the "regionprops" function to obtain the centroid coordinates of each object in the binary image.
- Iterate over the centroids and extract the hyperbola images based on the centroid coordinates and a specified size around it.
- Save the extracted hyperbola images to separate files using the "imwrite" function
Program to extract single objects from a image based on the centers of gravity of each object (+ 2-3 pixels outside the hyperbola).
3 views (last 30 days)
Show older comments
Hello,
I need help with the following task:
Write a program to extract single objects from a image based on the centers of gravity of each object (+ 2-3 pixels outside the hyperbola). Automatically save the extracted hyperbola images to a file.
I already used regionprops with centroid, to search for these centers of gravity of each object (hyperboles), but I don't know what now.
0 Comments
Answers (2)
Maneet Kaur Bagga
on 6 Oct 2023
Hi Kamil,
As per my understanding, you want to extract single objects from an image based on the centres of gravity. To achieve this kindly refer to the following steps:
Refer to the following code snippet for better understanding of the mentioned steps:
% Read the image
image = imread('your_image.jpg');
% Convert the image to grayscale if necessary
if size(image, 3) > 1
grayImage = rgb2gray(image);
else
grayImage = image;
end
% Convert the grayscale image to binary
binaryImage = imbinarize(grayImage);
% Get the properties of each object in the binary image
props = regionprops(binaryImage, 'Centroid');
% Set the size around the centroid to extract the hyperbola image
sizeAroundCentroid = 3; % Adjust this value as needed
% Iterate over the centroids and extract the hyperbola images
for i = 1:numel(props)
centroid = round(props(i).Centroid);
x = centroid(1);
y = centroid(2);
% Extract the hyperbola image based on centroid coordinates and size
hyperbolaImage = image(y-sizeAroundCentroid:y+sizeAroundCentroid, x-sizeAroundCentroid:x+sizeAroundCentroid, :);
% Save the extracted hyperbola image to a file
imwrite(hyperbolaImage, sprintf('hyperbola_%d.jpg', i));
end
Refer to the following MATLAB Documentation for further information on the functions:
I hope this provides you with the required information regarding your query.
Regards,
Maneet Bagga
0 Comments
Image Analyst
on 6 Oct 2023
Since you're already using regionprops, you have already segmented the image and have a binary mask. I don't know what a hyperbola image is, but I guess hyperbolas are the shapes of the blobs in your image.
So to get a region 3 pixels larger than your originally segmented blobs you must call imdilate. Then call regionprops and ask for "bounding box" to get the bounding box of each blob. Then in a loop call imwrite to save the extracted original image to disk. Something like (untested) where mask is your binary image that you already have said you have.
se = strel('disk', 1, 0); % Create a structuring element
mask = imdilate(mask, se); % Enlarge by 3 pixels (a layer of one pixel all the way around).
% Find the bounding box of each blob.
props = regionprops(mask, 'BoundingBox');
% Crop each blob out of the image and save the cropped image to disk.
for k = 1 : numel(props)
% Get bounding box for this blob.
thisBB = props(k).BoundingBox;
% Crop it out from original image.
croppedImage = imcrop(grayImage, thisBB); % grayimage is your original image.
% Save to disk
fileName = sprintf('Region %2.2d.png', k); % Construct the base filename.
imwrite(croppedImage, fileName); % Save to current folder. Use fullfile() if you want it in some other folder.
end
This will keep the shape of the blob, unlike the other, chatbot answer that just gives you a 3x3 matrix around the centroid. Also, you would think it would have known better than to use "image" as the name of the variable since that is already a built-in function name.
It's a generic, general purpose demo of how to threshold an image to find blobs, and then measure things about the blobs, and extract certain blobs based on their areas or diameters. It then extracts each blob into it's own cropped image.
0 Comments
See Also
Categories
Find more on Image Segmentation and Analysis in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!