Matlab
Program for Cropping Binary Image : Matlab Code
Objective of the Program:
Program takes a black and white image as input. It removes the black portion and gives the white portion of the image.
Input: Gray or binary image (for example consider the image below)
Output: Gray or binary image (as below)
MATLAB Code:
img = imread('path of image');
[row col] = size(img);
for i = 1:row
if sum(img(i,:)) > 0
top = i;
break
end
end
for i = row:(-1):1
if sum(img(i,:)) > 0
bottom = i;
break
end
end
for i = 1:col
if sum(img(:,i)) > 0
left = i;
break
end
end
for i = col:(-1):1
if sum(img(:,i)) > 0
right = i;
break
end
end
output = img(top:bottom, left:right);
imshow(output);
|
Popular Articles:
1. matlab-cropping-binary-image-algorithm
Objective of the Program:
Program takes a black and white image as input. It removes the black portion and gives the white portion of the image.
This code can be optimized:
ReplyDeleteimg = imread('path of image');
[rows,cols] = find(img > 0);
output = img(min(rows):max(rows), min(cols):max(cols));
imshow(outputs,[]);
Hope it helps.
its not working, its showing 3 copies of images in vertical .
ReplyDeletesorry , horizontal
DeleteYour image should be gray image (single channel image). If color image (three channel image), you will get three copies as output.
Delete