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

时间:2017-07-11 18:07:37

标签: image matlab image-processing normalization pattern-recognition

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

enter image description here

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

如上所示,有一个非常轻的像素(黄色和浅蓝色)的重复图案。因为我使用了像素数据,所以真正的亮像素正在扭曲我的图形,所以我想要中和#34;他们。我到处寻找,但我找不到干净的模式识别/删除命令集,所以我最终找到了图像中的行,其中有超过10个像素,强度值> 1036 - 共有19行。从那里,我找到了这些最​​亮像素出现的指数,并将它们存储在一个19格的单元格数组中 - cellarray {}。我可以通过运行图像(cellarray {n})获得那些最亮的像素值,其中n从1到19。

从这里,我想"中和"这些超亮像素取平均值"正常"它上面和下面的像素。但如果它与另一个非常明亮的像素相邻,我希望它的新像素值是最接近像素的平均值,这是正常的#34;。我希望这是有道理的... 有人可以帮助我使用代码或建议一个更简单的方法吗?非常感谢!

1 个答案:

答案 0 :(得分:4)

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

重要提示: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)

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

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

correlation

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

endresult

它捕获黄色和浅蓝色图案,但也有一些误报。您必须尝试不同的模板,截止值,扩张半径和中值滤波器大小,以改善结果。

方法B)使用图像亮度

有点offtopic因为没有使用模式识别,而是黄色模式非常明亮。但由于结果不是太糟糕而且更简单,我觉得它可能有用。更容易避免发现误报。

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

enter image description here