如何裁剪没有图片等数据的二进制图像(matlab)

How can I crop binary images which dont have data like pics (matlab)

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

输入图像:

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

输出图像:

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

我需要这个的 matlab 代码,任何代码和想法都是 welcome.thanks

这是我的错误代码:

  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')

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

将图片裁剪成这些尺寸并保存。

下面是一些简单的代码。基本上,您想消除图像边缘仅包含 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);