separating an image into two images

8 views (last 30 days)
basma alshaibah
basma alshaibah on 27 Apr 2018
Answered: John BG on 6 May 2018
i have this image and I want to separate it into two images the first called left and the other called right can someone provide me with the appropriate code ?

Answers (2)

John BG
John BG on 6 May 2018
Hi Basma
1.-
In order to split the image as requested there's no need to convert the image to greyscale.
The key is to spot the white gap between both images.
2.-
Acquiring image
clear all;close all;clc
A=imread('001.jpg');
figure(1);imshow(A)
[sz1 sz2 sz3]=size(A)
sz1 =
754
sz2 =
1148
sz3 =
3
Only along the split area all pixels are white meaning that only there the sum is about
.
sz1*255
=
192270
It's a bit less because not all gap pixels are 255 white, but their values are the closest ones to the top value 255.
.
.
3.-
The following calculates the sum of all pixels column by column.
L=zeros(1,sz2);
for k=1:1:sz2
L(k)=sum(A(:,k,1)+A(:,k,2)+A(:,k,3));
end
L=L/1e5;
figure(2);plot(L);grid on
[rows,cols]=find(L(L>1.9))
hold on
stem(cols,ones(1,numel(cols)),'g')
.
.
4.-
The gap to split the image is the middle one
nrange=[1:1:sz2]
n1=nonzeros(nrange.*(L>1.9))
n1 =
1
2
3
4
5
6
568
569
570
571
572
573
574
575
576
1141
1142
1143
1144
1145
1146
1147
1148
A way to get right the mid point of the split gap is with command kmeans.
.
n2=kmeans(n1,3)
n2 =
2
2
2
2
2
2
1
1
1
1
1
1
1
1
1
3
3
3
3
3
3
3
3
n3=find(n2==1)
n_split=n1(floor(.5*(min(n3)+max(n3))))
n_split =
572
5.-
Now you can cut:
Aleft=A(:,[1:n_split],:);
Aright=A(:,[n_split+1:end],:);
6.-
Saving each part
imwrite(Aleft,'name_image_left.jpg')
imwrite(Aright,'name_image_right.jpg')
.
Basma
if you find this answer useful would you please be so kind to consider marking my answer as Accepted Answer?
To any other reader, if you find this answer useful please consider clicking on the thumbs-up vote link
thanks in advance for time and attention
John BG

Rik
Rik on 27 Apr 2018
Convert the image to a grayscale (e.g. with im2double), invert (just do IM=1-IM;), and find the columns that sum to 0. Then you can use that as indices to separate the images.
  1 Comment
Rik
Rik on 6 May 2018
Did this suggestion solve your problem? If so, please consider marking it as accepted answer. It will make it easier for other people with the same question to find an answer, as well as give me reputation points. If this didn't solve your question, please comment with what problems you are still having.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!