How can i remove borders image?

5 次查看(过去 30 天)
Ehsan R
Ehsan R 2013-4-18
评论: DGM 2024-3-5
hi
i have a picture containing a figure(digit). i'd like to remove the borders of the picture from all four direction so that just the figure is left(no borders).
meanwhile please don'tuse imclearborder and imcrop.
  1 个评论
Image Analyst
Image Analyst 2013-4-18
编辑:Image Analyst 2013-4-18
Why do you say that? Why "please don't use" the functions that can accomplish what you want to accomplish? It just doesn't make sense. Do you want to get the job done or not? Try posting your image to snag.gy or tinypic.com.

请先登录,再进行评论。

回答(2 个)

Image Analyst
Image Analyst 2013-4-18
Your image did not come through for me - for some reason that site is banned by my company. Try the command imclearborder() in the Image Processing Toolbox. Or just use imcrop() or extraction
subimage = fullImage(row1:row2, column1:column2);

Guilherme Freire Roberto
Not the fanciest solution, but this worked for me:
%Remove borders from upper rows
checkRow = 0;
while(checkRow == 0)
if any(IMG(:,1))
checkRow = 1;
else
IMG = IMG(:,2:end);
end
end
%Remove borders from bottom rows
checkRow = 0;
while(checkRow == 0)
if any(IMG(:,size(IMG,2)))
checkRow = 1;
else
IMG = IMG(:,1:end-1);
end
end
%Remove borders from left columns
checkColumn = 0;
while(checkColumn == 0)
if any(IMG(1,:))
checkColumn = 1;
else
IMG = IMG(2:end,:);
end
end
%Remove borders from right columns
checkColumn = 0;
while(checkColumn == 0)
if any(IMG(size(IMG,1),:))
checkColumn = 1;
else
IMG = IMG(1:end-1,:);
end
end
  1 个评论
DGM
DGM 2024-3-5
It works, but if you're already familiar with any(), you can simplify a lot.
% find ROI geometry
inpict = any(inpict,3); % collapse channels
mkr = any(inpict,2); % find rows with nonzero elements
mkc = any(inpict,1); % find cols with nonzero elements
r1 = find(mkr,1,'first');
r2 = find(mkr,1,'last');
c1 = find(mkc,1,'first');
c2 = find(mkc,1,'last');
% crop original image to extents
inpict = inpict(r1:r2,c1:c2,:); % all channels

请先登录,再进行评论。

标签

尚未输入任何标签。

Community Treasure Hunt

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

Start Hunting!

Translated by