问题描述
我正在使用Matlab R2017a,我有一个RGB图像(TIFF 128x128 uint16),如下图所示为png图像:
实际TIFF图像:
%计算模板中每个像素的相关程度image
C = normxcorr2(模板,BW);
%从关联中删除填充边框
pad = floor(size(template)./ 2);
center = size(I);
C = C([false(1,pad(1))true(1,center(1))],...
[false(1,pad(2))true(1,中心(2))]);
%绘制相关性
数字,冲浪(C),阴影平衡
模板在图像上的相关性。请注意,它与亮黄色图案和浅蓝色图案高度相关。
%获取相关性较高的所有索引。从上图中读取的值。
%截止值越低,将改变的像素越多
idx = C> 0.5;
%扩展idx因为其他掩盖区域太小
idx = imdilate(idx,strel('disk',1));
%如果是多波段图像,则复制它们。如果只有灰度图像
idx = repmat(idx,1,1,size(I,3)),则什么都不做;
%用NaN
替换模式像素I(idx)= NaN;
%用4x4中值滤波器填充Nan值
I = fillmissing(I,'movmedian',[4 4]);
%显示新图片
figure; imagesc(I)
它同时捕捉黄色和浅蓝色图案,但也有一些误报。您必须尝试不同的模板,截止值,扩张半径和中值滤波器大小以改善结果。
方法B)使用图像亮度
有点offtopic因为没有使用模式识别,而是黄色模式非常明亮。但由于结果不是太糟糕而且简单得多,我觉得它可能有用。更容易避免发现误报。
%I =你的形象
I = double(I);
%获得红色通道中非常明亮的索引
idx = cdata(:,:,1)> 157; %157 =图像中最亮的非图案像素
%从现在开始,与方法A)结束相同!
%扩大idx以获得相邻像素,因为否则将删除太少的像素
idx = imdilate(idx,strel('disk',1));如果是多波段图像,
%会替换它们。如果只有灰度图像
idx = repmat(idx,1,1,size(I,3)),则什么都不做;
%用NaN
替换模式像素I(idx)= NaN;
%使用50x50中值过滤器填充Nan值
I = fillmissing(I,'movmedian',[50 50]);
%显示新图片
figure; imagesc(I)
I'm using Matlab R2017a and I have a RGB image (TIFF 128x128 uint16), shown below as a png image:
Actual TIFF image: http://s000.tinyupload.com/index.php?file_id=13823805859248753003
As shown above, there's a repeating pattern of really light pixels (yellow and light blue). Because I'm using pixel data, the really light pixels are skewing my graphs, so I want to "neutralize" them. I looked everywhere but I couldn't find a clean pattern recognition/removal set of commands, so I ended up finding the rows in the image where there were more than 10 pixels with intensity value > 1036 - there were 19 rows. From there, I found the indices where these brightest pixels occur, and stored them in a 19-cell cell array - cellarray{}. I can get those brightest pixel values by running image(cellarray{n}), where n goes from 1-19.
From here, I want to "neutralize" these super bright pixels by taking the average of the "normal" pixels above and below it. But if it is adjacent to another really bright pixel, I want its new pixel value to be the average of the closest pixels that are "normal". I hope that makes sense...Can someone help me with the code or suggest an easier method? Thanks so much!
Two methods are proposed, one using cross correlation the other using brightness. They work with both grayscale and multiband images. You should play with the settings a bit to improve the result.
Important: fillmissing
requires Matlab 2016b or newer
Method A) Using cross correlation
This works by extracting a single occurrence of the pattern and finding the location on the images where the correlation is very high. While it provides better results than Method B, it is also more complicated and needs a bit more knowledge about what you are doing:
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
Correlation of the template on the image. Note that it both highly correlates with the bright yellow patterns and the light blue pattern bellow.
% 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)
It catches both the yellow and light blue pattern but also some false positives. You have to experiment with different templates, cut-off values, dilation radii and median filter sizes to improve the result.
Method B) Using brightness of image
A bit offtopic because no pattern recognition is used but instead that the yellow patterns are just very bright. But since the result isn't too bad and a lot simpler I felt it might be useful. A lot easier to avoid finding false positives.
% 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)
这篇关于检测图像中的重复像素图案并使用matlab删除它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!