How to crop an portion fro image

I have a code below
clc
clear all
close all
I = imread('rice.png');
imshow(I)
background = imopen(I,strel('disk',15));
I2 = I - background;
imshow(I2)
I3 = imadjust(I2);
imshow(I3);
level = graythresh(I3);
bw = im2bw(I3,level);
bw = bwareaopen(bw, 50);
imshow(bw)
cc = bwconncomp(bw, 4)
grain = false(size(bw));
grain(cc.PixelIdxList{50}) = true;
grain(cc.PixelIdxList{30}) = true;
grain(cc.PixelIdxList{80}) = true;
grain(cc.PixelIdxList{75}) = true;
grain(cc.PixelIdxList{70}) = true;
imshow(grain);
now want to crop the grains shown in binary image from original image ,kindly help

 採用された回答

Guillaume
Guillaume 2014 年 11 月 13 日

1 投票

Use regionprops to extract all sort of information on your connected components. In particular, the Image field of the structure array returned by regionprops will contain the binary image of each grain.
props = regionprops(cc, 'Image'); %to just get the image of each grain.
%props(1).Image will be the image of the first grain,
%props(2).Image will be the image of the second grain,
%etc.

その他の回答 (1 件)

Siam
Siam 2014 年 11 月 13 日

1 投票

You can try with imcrop()

4 件のコメント

nkumar
nkumar 2014 年 11 月 13 日
but i want to do it automatically ,there was 5 obects,if tere were many i cany use imcrop to do all times,so i want t do automatically
Siam
Siam 2014 年 11 月 13 日
Your question was not clear as it says how to crop a portion from image. Not sure what actually you want to do? your code gives you 5 grains in white color and background in black. So why do you have to crop them? Can you explain a bit more?
nkumar
nkumar 2014 年 11 月 14 日
es my image has 5 grains ,now i want to crop them from original image,i.e i need the grains which in in binary image from original image,
I have 50,30,80,75,70th grains in binary image,same i need i in original image,how can i crop it
Guillaume
Guillaume 2014 年 11 月 14 日
Doesn't my answer do what you want?
Note that crop is probably not the appropriate word. It sounds like what you want is to extract part of an image.

サインインしてコメントする。

タグ

質問済み:

2014 年 11 月 13 日

コメント済み:

2014 年 11 月 14 日

Community Treasure Hunt

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

Start Hunting!

Translated by