I am working on image segmentation, and I need to segment image while keeping the same image size. I have a code which is able to segment the image, but, the segmented the size is different from the original image. I would like your help in segmenting the image while keeping the image size same as original image as shown in the example image below.
[![original image][1]][1] [1]: https://i.stack.imgur.com/Wrf2A.jpg [![Segmented image][2]][2] [2]: https://i.stack.imgur.com/i4q8O.png
function S = im2segment(im) % im is the original image
nrofsegments = 5;
S = cell(1,nrofsegments);
% Get sum of intensities along the columns
S_sum=sum(im,1);
%300 is decided as the threshold and image is binarised
S_sum(S_sum<300)=0; %pixels having intensity lesser than 300 is made 0
S_sum(S_sum>=300)=1; % pixels having intensity greater than 300 is made 1
B = bwboundaries(S_sum); %identifies the boundary
for kk = 1:nrofsegments
seg_start=min(B{kk}(:,2)); %detects the first column for segmentation2
seg_end = max(B{kk}(:,:)); % detects the last column for segmentation
S_sum = im(:,seg_start: seg_end); %image displayed for detected column width
S{kk} = S_sum;
end
