how to save the segmented region as a separate image?

I have used the code which I have found in online and divided an image into several segments. I want to extract green parts from the image and store them. I am able to display the segmented image separately with black mask. but I want to display and save the green small segmented region only as a separate image (like 9 separate .jpg images ) without black regions.
This is the code I have found in online
segmented_images = cell(1,3);
rgb_label = repmat(pixel_labels,[1 1 3]);
for k = 1:nColors
color = img;
color(rgb_label ~= k) = 0;
segmented_images{k} = color;
end
Thank you in advance.

2 Comments

you can use
imwrite(segmented_images{k},as ur list)
No. That is saving the region with black part. you can see it in greenpart.jpg image. I have used imshow() instead of imwrite().

Sign in to comment.

 Accepted Answer

First get the outline, then the bounding box and use imcrop(). Then use imwrite(). Let me know if you can't figure it out.

7 Comments

Hello @Image Analyst Thank you so much for your answer but I am very new MATLAB I am not able to do what you have suggested. Please provide me starting code if possible.
Thanks in advance.
In order to get greenpart.jpg, you must have had a mask to mask it with. So just get the bounding box from your mask image, and call imcrop()
props = regionprops(mask, 'BoundingBox');
croppedImage = imcrop(rgbImage, props.BoundingBox);
save(croppedImage, filename);
this will help to extract similar size and color images? What if i want to extract images with random shape ,size and colors?
@image analyst
Please suggest a procedure to apply edge detector on a geotiff image and then extract each segmented region separately. please
If you want random regions for some reason, use randi() to get random coordinates, then you can use poly2mask() to make a binary image mask that you can use to extract colors from.
To use an edge detector, you simply apply it to the image using edge(), imgradient(), stdfilt(), or whatever edge filter you're using. Then it depends on what you mean by "extract". Like crop out to it's own smaller image using imcrop(), or extract values from like grayImage(mask), or something else.
john
john on 31 Jan 2022
Edited: john on 31 Jan 2022
Hello how do I get those things which you mentioned? Outline bounding box then crop? please could you give any reference example which is there in matlab in which this is done? @image analyst
You should click on the "Show older comments" link. Then you'd see
props = regionprops(mask, 'BoundingBox');
croppedImage = imcrop(rgbImage, props.BoundingBox);
save(croppedImage, filename);
If you need more help with your image, post a new question after reading this:

Sign in to comment.

More Answers (1)

I have a gray set of 20 images I want to convert gray to color and every time I work on the conversion colors vary I want to install the color for all images for example, The area in red color is still red in image1 but change blue in image 2 The problem: the changed the color with repeated 20 images

1 Comment

I have no idea what you're asking. Please try to rephrase, and attach some screenshots, in a new question (not back here). Basically I think you want to use ind2rgb() and somehow want to change the colormap with each image, though I'm not sure how you're going to decide how to change the colormap each time.

Sign in to comment.

Categories

Find more on Images in Help Center and File Exchange

Asked:

Ad
on 28 Oct 2017

Commented:

on 31 Jan 2022

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!