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

 Akzeptierte Antwort

Matt J
Matt J am 11 Okt. 2022
Bearbeitet: Matt J am 11 Okt. 2022
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 Kommentare

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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Image Analyst
Image Analyst am 12 Okt. 2022
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:

Gefragt:

Ho
am 11 Okt. 2022

Beantwortet:

am 12 Okt. 2022

Community Treasure Hunt

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

Start Hunting!

Translated by