【问题标题】:How can I crop binary images which dont have data like pics (matlab) [duplicate]如何裁剪没有图片等数据的二进制图像(matlab)[重复]
【发布时间】:2015-10-26 02:56:20
【问题描述】:

我有一些二进制图像,我想删除无用的区域,只保留具有如下示例数据的区域:

输入图像:

http://s1.upload7.ir/downloads/UsenferJ3Nb964UkpfABPC8YAyKPwAmh/1.bmp

输出图像:

http://s1.upload7.ir/downloads/TQHGaGCWCofspM2AEK3myfETc9ZwAMbm/2.bmp

我需要matlab代码,欢迎任何代码和想法。谢谢

这是我出错的代码:

  clear all;close all;clc;
  I=imread('I111 p.bmp');
  figure,imshow(I),title('I')
  I=~I;
  [row,col]=size(I);

  Ix=I;

  % [x]=find(0);

  for i=1:row
      for j=1:col

         if I(i,j)==0
             Ix(i,:)=[];

      end
  end
  end
   figure,imshow(~Ix),title('Ix')

【问题讨论】:

  • @RafaelMonteiro 我使用 2 个 for 和一个 if 来检测黑色像素,但这是不正确的。我不知道这样做。

标签: image matlab image-processing binary


【解决方案1】:

下面是一些简单的代码。基本上,您想消除图像边缘上仅包含 1 的所有行和列,因为黑色为 0,白色为 1。

im = imread('1.bmp'); % insert actual path to image here
any_zeros = any(im==0, 1); % checks every column to see if any elements in column are zero
column_start = find(any_zeros==1, 1); % first column that contains 0
column_end = find(any_zeros==1, 1, 'last'); % last column that contains 0

any_zeros = any(im==0, 2); % checks every row to see if any elements in row are zero
row_start = find(any_zeros==1, 1); % first row that contains 0
row_end = find(any_zeros==1, 1, 'last'); % last row that contains 0

% final image has excess "white" on edges eliminated
final_im = im(row_start:row_end, column_start:column_end);

【讨论】:

  • 这个答案比我的好得多(解释得更好)。
  • @It's Magic ,谢谢你的好回答。我很感激你为我所做的一切。
【解决方案2】:

使用加载图像,转换为黑白并使用矩阵运算符将整个矩阵与 0 进行比较,然后在任一方向求和以找到任一方向上的第一个和最后一个索引(使用 find),这是真的。

将图像裁剪到这些尺寸并保存。

【讨论】:

  • 感谢您的回复,亲爱的@user,您能帮我写下matlab代码的find()部分吗?
  • find(sum(Im==0,1)>0,1,'first')
  • 我也非常感谢您为我所做的一切。非常感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-04-10
  • 1970-01-01
  • 1970-01-01
  • 2015-12-03
  • 2013-04-20
  • 2016-12-05
  • 2012-02-12
相关资源
最近更新 更多