检测图像中的重复像素模式并使用 matlab 删除它们

Detect repetitive pixel patterns in an image and remove them using matlab

我正在使用 Matlab R2017a 并且我有一个 RGB 图像 (TIFF 128x128 uint16),如下所示为 png 图像:

实际 TIFF 图像:http://s000.tinyupload.com/index.php?file_id=13823805859248753003

如上所示,有一个非常亮的像素(黄色和浅蓝色)的重复图案。因为我使用的是像素数据,所以真正亮的像素会扭曲我的图表,所以我想 "neutralize" 它们。我到处找了找,但找不到一个干净的模式 recognition/removal 命令集,所以我最终在图像中找到了超过 10 个像素且强度值 > 1036 的行 - 有 19 行。从那里,我找到了这些最​​亮像素出现的索引,并将它们存储在一个 19 元元胞数组 - cellarray{} 中。我可以通过 运行 image(cellarray{n}) 获得那些最亮的像素值,其中 n 从 1 到 19。

从这里开始,我想通过取其上方和下方 "normal" 像素的平均值来 "neutralize" 这些超亮像素。但是如果它与另一个非常亮的像素相邻,我希望它的新像素值是最近像素的平均值 "normal"。我希望这是有道理的... 有人可以帮我处理代码或建议更简单的方法吗?非常感谢!

提出了两种方法,一种使用互相关,另一种使用亮度。它们适用于灰度和多波段图像。您应该稍微调整一下设置以改善结果。

重要提示:fillmissing 需要 Matlab 2016b 或更新版本

方法 A) 使用互相关

这是通过提取单次出现的模式并找到图像上相关性非常高的位置来实现的。虽然它比方法 B 提供了更好的结果,但它也更复杂,需要更多了解您在做什么:

I = double(yourimage);
% Show image
imagesc(I)
% You have to select a part of single occurrence of the pattern (a template) on the image! See below image.
rect = round(getrect);
% In case it is a multiband image make grayscale image
if size(I,3)>1
    BW = rgb2gray(I);
else
    BW = I;
end
% Extract template from BW
template = BW(rect(2):rect(2)+rect(4),rect(1):rect(1)+rect(3),:);
% Show template - this is the extent you selected during "getrect"
imagesc(template)

% Calculate how much said template correlates on each pixel in the image
C = normxcorr2(template,BW);
% Remove padded borders from correlation
pad = floor(size(template)./2);
center = size(I);
C = C([false(1,pad(1)) true(1,center(1))], ...
        [false(1,pad(2)) true(1,center(2))]);
% Plot the correlation
figure, surf(C), shading flat

图像上模板的相关性。请注意,它与下面的亮黄色图案和浅蓝色图案高度相关。

% Get all indexes where the correlation is high. Value read from previous figure.
% The lower the cut-off value, the more pixels will be altered
idx = C>0.5;
% Dilate the idx because else masked area is too small
idx = imdilate(idx,strel('disk',1));
% Replicate them if multiband image. Does nothing if only grayscale image
idx = repmat(idx,1,1,size(I,3));
% Replace pattern pixels with NaN
I(idx) = NaN;
% Fill Nan values with 4x4 median filter
I = fillmissing(I,'movmedian',[4 4]);
% Display new image
figure; imagesc(I)

它捕获了黄色和浅蓝色模式,但也捕获了一些误报。您必须尝试使用​​不同的模板、截止值、扩张半径和中值滤波器大小来改进结果。

方法 B) 使用图像的亮度

有点跑题了,因为没有使用模式识别,而是黄色的模式非常亮。但由于结果还不错,而且简单得多,我觉得它可能会有用。更容易避免发现误报。

% I = your image
I = double(I);

% get indexes where very bright in red channel
idx = cdata(:,:,1)>157; % 157 = brightest non-pattern pixel in your image

% From now on same as end from method A)!
% dilate the idx to also get the adjacent pixels because else too few pixels will be erased
idx = imdilate(idx,strel('disk',1));
% replacate them if multiband image. Does nothing if only grayscale image
idx = repmat(idx,1,1,size(I,3));
% replace pattern pixels with NaN
I(idx) = NaN;
% fill Nan values using 50x50 median filter
I = fillmissing(I,'movmedian',[50 50]);
% display new image
figure; imagesc(I)