# separating an image into two images

조회 수: 23(최근 30일)
basma alshaibah 2018년 4월 27일
답변: John BG 2018년 5월 6일
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 ?

댓글을 달려면 로그인하십시오.

### 답변(2개)

Rik 2018년 4월 27일
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표시숨기기 없음
Rik 2018년 5월 6일
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.

댓글을 달려면 로그인하십시오.

John BG 2018년 5월 6일
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
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
thanks in advance for time and attention
John BG

댓글을 달려면 로그인하십시오.

### 범주

Find more on Get Started with Image Processing Toolbox in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by