Split one image into multiple images

I have 1 image size 10x10 like this:
image = [ 0 0 1 1 1 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 1 1 1 0 0 0 0 0 0
0 1 1 1 0 0 0 1 1 1
0 1 1 1 0 0 0 1 1 0
0 1 1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 1 0 0 0 0 0 ]
I want to split this image into many other images, like:
I1 = [ 0 0 1 1 1 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 ]
I2 = [ 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 1 1 1 0 0 0 0 0 0
0 1 1 1 0 0 0 0 0 0
0 1 1 1 0 0 0 0 0 0
0 1 1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 ]
...and so on. Each splited image contains 1 part of feature of the origin.
I tried to find local but the result wasn't as expected
[y_peaks x_peaks] = find(image);
np = 1;
for ii=1:length(y_peaks)-1
if ((y_peaks(ii+1)==(y_peaks(ii)-1))&&((x_peaks(ii+1)==(x_peaks(ii)-1))))||...
((y_peaks(ii+1)==(y_peaks(ii)-1))&&((x_peaks(ii+1)==(x_peaks(ii)+0))))||...
((y_peaks(ii+1)==(y_peaks(ii)-1))&&((x_peaks(ii+1)==(x_peaks(ii)+1))))||...
((y_peaks(ii+1)==(y_peaks(ii)+0))&&((x_peaks(ii+1)==(x_peaks(ii)-1))))||...
((y_peaks(ii+1)==(y_peaks(ii)+0))&&((x_peaks(ii+1)==(x_peaks(ii)+1))))||...
((y_peaks(ii+1)==(y_peaks(ii)+1))&&((x_peaks(ii+1)==(x_peaks(ii)-1))))||...
((y_peaks(ii+1)==(y_peaks(ii)+1))&&((x_peaks(ii+1)==(x_peaks(ii)+0))))||...
((y_peaks(ii+1)==(y_peaks(ii)+1))&&((x_peaks(ii+1)==(x_peaks(ii)+1))))
local_y(ii,np) = y_peaks(ii);
local_x(ii,np) = x_peaks(ii);
local_y(ii+1,np) = y_peaks(ii+1);
local_x(ii+1,np) = x_peaks(ii+1);
else
np = np + 1;
local_y(ii+1,np) = y_peaks(ii+1);
local_x(ii+1,np) = x_peaks(ii+1);
end
end

 採用された回答

Matt J
Matt J 2022 年 10 月 11 日
編集済み: Matt J 2022 年 10 月 11 日

1 投票

Assuming you have the Image Processing Toolbox,
image = [ 0 0 1 1 1 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 1 1 1 0 0 0 0 0 0
0 1 1 1 0 0 0 1 1 1
0 1 1 1 0 0 0 1 1 0
0 1 1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 1 0 0 0 0 0 ];
reg=regionprops(logical(image),'PixelIdxList'); n=numel(reg);
I=false(size(image)); I(:,:,n)=0;
for i=1:n
tmp=I(:,:,i);
tmp(reg(i).PixelIdxList)=1;
I(:,:,i)=tmp;
end
I
I = 10×10×4 logical array
I(:,:,1) = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 I(:,:,2) = 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 I(:,:,3) = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 I(:,:,4) = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

2 件のコメント

Ho
Ho 2022 年 10 月 11 日
Thank you! This helps so much
Matt J
Matt J 2022 年 10 月 11 日
You are quite welcome, but please Accept-click the answer to indicate so.

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

その他の回答 (1 件)

Image Analyst
Image Analyst 2022 年 10 月 12 日

1 投票

Here is an alternate way. Use bwlabel and ismember:
binaryImage = [ 0 0 1 1 1 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 1 1 1 0 0 0 0 0 0
0 1 1 1 0 0 0 1 1 1
0 1 1 1 0 0 0 1 1 0
0 1 1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 1 0 0 0 0 0 ]
binaryImage = 10×10
0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 1 1 1 0 0 0 1 1 1 0 1 1 1 0 0 0 1 1 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0
[labeledImage, numRegions] = bwlabel(binaryImage); % Identify (label) each individual blob.
subplot(numRegions+1, 1, 1);
imshow(binaryImage); % Display original image.
title('Original image')
% Create individual images and display them.
for k = 1 : numRegions
thisImage = ismember(labeledImage, k); % Create image.
subplot(numRegions+1, 1, k+1);
imshow(thisImage); % Display this image.
end
See my Image Processing Tutorial:

質問済み:

Ho
2022 年 10 月 11 日

回答済み:

2022 年 10 月 12 日

Community Treasure Hunt

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

Start Hunting!

Translated by