如何从 Matlab 图像中去除直线?

How to remove straight lines from a Matlab image?

我有一个来自名为 Two_dim 的矩阵的 matlab 图像,如下图所示。

我想从图像中删除所有 3 条 底部水平直线。我在 Whosebug 上查找使用 regionprops 来消除水平线并获得此代码。但这似乎并没有消除线条。

rp = regionprops(Two_dim, 'PixelIdxList', 'Eccentricity', 'Orientation');
rp = rp([rp.Eccentricity]>0.95 & (abs([rp.Orientation])<2 | abs([rp.Orientation])>89));
Two_dim(vertcat(rp.PixelIdxList)) = false;

regionprops 需要一个二值图像,每个像素都是两个值之一。

你可以这样做

Two_dim(Two_dim<0.5) = 0;
Two_dim(Two_dim>=0.5) = 1; # the actual value doesn't matter

Two_dim = logical(Two_dim);

您可以查看行强度总和。只要线条保持水平,它们就会脱颖而出。

grayI = rgb2gray(I);

rowSums = sum(grayI,2);

plot(rowSums);

filterRows = rowSums > 1*10^5

I(filterRows,:,:) = 255;

这是一个使用霍夫变换方法的答案。稍后我将在下面的代码中添加更多解释:

% I crop only the intresting part for illustration:
BW = edge(Two_dim(1:1000,:),'canny');
subplot 131
imagesc(Two_dim(1:1000,:))
title('Original image')
axis xy
[H,theta,rho] = hough(BW); % preform Hough transform
subplot 132
P = houghpeaks(H,10,'NHoodSize',[1 1]); % find the peaks in the transformation
lines_found = houghlines(BW,theta,rho,P,...
    'FillGap',50,'MinLength',1); % convert the peaks to line objects
imagesc(Two_dim(1:1000,:)), hold on
result = Two_dim(1:1000,:);
for k = 1:length(lines_found)
   % extract one line:
   xy = [lines_found(k).point1; lines_found(k).point2];
   % Plot the detected lines:
   plot(xy(:,1),xy(:,2),'LineWidth',1,'Color','green');
   % remove the lines from the image:
   % note that I take a buffer of 3 to the 'width' of the line
   result(xy(1,2):xy(1,2)+3,xy(1,1):xy(2,1)) = 0;
end
title('Detected lines')
axis xy
subplot 133
imagesc(result)
title('Corrected image')
axis xy

输出: