从MATLAB中的ab / w图像中删除白色像素?
信息处理
图像处理
2022-02-23 23:16:12
1个回答
所以......你想从黑色区域周围删除胖白色边框?
您可以尝试使用 (eg) 搜索第一行和最后一行,第二个图像中为 0 的列find:
% assuming BW2 is 2D, one channel.
% the reason the first has a BW2' and the second is BW
% is because of Matlab being column-major when it does `find`:
% have to make sure we actually get the first/last indices.
[col1,row1] = find(BW2'==0,1,'first'); % top-left corner
[row2,col2] = find(BW2==0,1,'last'); % bottom-right corner
BW3 = BW(row1:row2, col1:col2);
imshow(BW3)
这确实假设图像的左上角和右下角不会成为轮廓的一部分 - 这是一个非常低的机会。
但是,如果您想确定,则可以改为查找并非全部为 255 的第一行和不全部为 255 的第一列,并且类似地查找最后一行/列(即通过查找整个行/列来定位边距255) 并像这样进行裁剪:
cols = sum(abs(255-BW2),1); % work out difference from 255
rows = sum(abs(255-BW2),2);
col1 = find(cols,1,'first');
col2 = find(cols,1,'last');
row1 = find(rows,1,'first');
row2 = find(rows,1,'last');
BW3 = BW(row1:row2,col1:col2);
(请注意,由于它使用的压缩,这将在您的 jpeg 上失败 - 边框不是唯一的 255,那里有几个 251。但是如果您不BW先保存为 jpeg 并且它会在您的代码上工作并且然后将其读回。如果要保存图像,请使用无损格式,如 TIF 或 PNG)。
其它你可能感兴趣的问题

