Image processing - pixel comparison

조회 수: 11 (최근 30일)
Archit Sethi
Archit Sethi 2021년 4월 30일
댓글: Archit Sethi 2021년 5월 3일
Suppose there is a 5X5 filter window to traverse a image with zero padding.
My Question:
I'm trying to compare all the pixels in row-1 in 1 iteration. If pixels in 1st row are all similar, then RC for row-1 is 1.
similarly, for row-2, comparing pixels in 2nd row. If similar, then RC for that row-2 is 1.
Can anyone suggest some methodology/approach for this implement?
Even if not in one iteration, some other approach?
  댓글 수: 3
Archit Sethi
Archit Sethi 2021년 5월 1일
I would elucidiate my question & approach:-
Similarity of the pixels in row or column is done by comparing only their 4 most significant bits.
Step-1: algo compares the pixels in a row and column. If pixels in a row are similar(equal), the row comparison signal for that particular row is 1. Similarly do for column.
Then, if pixels in all rows are similar, R signal =1. similarly if pixels in all columns are similar, C signal = 1.
Step-2: Median is computed.
Row_Comparison = compare(MSB 4 bits of pixels in each row)
Column_Comparison = compare(MSB 4 bits of pixels in each column)
R_signal = (RC[0] & RC[1] & RC[2] & RC[3] & RC[4])
C_signal = (CC[0] & CC[1] & CC[2] & CC[3] & CC[4])
If not able to understand, I would share my script to see current approach.
Archit Sethi
Archit Sethi 2021년 5월 2일
Can anyone help me with adaptive median filter code with these steps:
window_size = 5;
adaptive_MF(window_size){
Row_Comparison = compare(MSB 4 bits of pixels in each row)
Column_Comparison = compare(MSB 4 bits of pixels in each column)
R_signal = (RC[0] & RC[1] & RC[2] & RC[3] & RC[4])
C_signal = (CC[0] & CC[1] & CC[2] & CC[3] & CC[4])
if (R_signal is 1 and C_signal is1)
Median = window(2,2)
elseif (R_signal is 1 or C_signal is 1)
Median = median(diagonal pixels of filter window 5x5)
else{
m1 = median(diagonal pixels of filter window 5x5)
m2 = median(Horizontal pixels from 2nd row of filter window 5x5)
m3 = median(Verticla pixels from 2nd column of filter window 5x5)
Median = median(m1,m2,m3)
}
window(2,2) = Median
}
I've tried many times this algo in MATLAB, but array index out of bounds error or outlined error pops ups? I've no clue how to tackle this problem with above pseudo code.
Also, i did msb = mod(floor(image/128),2); %for MSB extraction so that i can do pixel comparisons. But no luck.
Please help!

댓글을 달려면 로그인하십시오.

답변 (2개)

Image Analyst
Image Analyst 2021년 5월 2일
For what it's worth, see my attached modified median filter demo.
  댓글 수: 1
Archit Sethi
Archit Sethi 2021년 5월 3일
Hi Image Analyst!
Yeah I had a look at ur code. But, I'm following the above mentioned algo for 2D adaptive median filter approach. Can you help with that code/Algo?
Please that would be very helpful!

댓글을 달려면 로그인하십시오.


DGM
DGM 2021년 5월 1일
편집: DGM 2021년 5월 1일
The filter you describe doesn't make use of its height. If it's only evaluating each row within the window independently, it's a 1-D filter and only needs to be 1x5.
It's unclear what type of image you're working with. If we assume this is a binary image, then the answer is simple:
% read a test image and clean it up
inpict = im2double(imread('circles.png'));
inpict = inpict>0.1; % make it logical
anytrue = imdilate(inpict,ones(1,5));
anyfalse = imdilate(~inpict,ones(1,5));
outpict = xor(anytrue,anyfalse);
If you want to do this for a grayscale or color image, you're going to have to define some tolerance and test for equality within said tolerance. You may be able to use rangefilt() for grayscale inputs.
inpict = im2double(imread('cameraman.tif'));
inpict = medfilt2(inpict,[3 3]); % clean it up a bit
tol = 0.03;
rangemap = rangefilt(inpict,ones(1,5));
outpict = rangemap<tol;
For RGB inputs, you could use rangefilt() to generate range maps, test against the tolerance to get three logical maps, then logically combine them.
  댓글 수: 5
Archit Sethi
Archit Sethi 2021년 5월 1일
For a 5x5 filter, can you explain or write some piece of script to know more about how i can code in above mentioned algorithm.
I'm struggling to write this for quite some time...but no success till now.
DGM
DGM 2021년 5월 2일
편집: DGM 2021년 5월 2일
This example uses nlfilter():
inpict = rgb2gray(imread('sources/lena.tif'));
rowpict = nlfilter(inpict,[5 5],@rowfilt);
colpict = nlfilter(inpict,[5 5],@colfilt);
imshow(cat(1,rowpict,colpict))
function out = rowfilt(in)
tol = 16;
out = all(range(in,2)<tol);
end
function out = colfilt(in)
tol = 16;
out = all(range(in,1)<tol);
end
I'm not really sure what should be done with the two outputs at this point. Either way, that's how nlfilter() can be used.
This is the same thing using a simple sliding window written with loops:
inpict = rgb2gray(imread('sources/lena.tif'));
filtsize = [5 5];
tol = 16;
s0 = size(inpict);
padsize = floor(filtsize/2);
% nlfilter() uses zero-padding, but you could replicate, etc
inpict = padarray(inpict,padsize,0,'both');
s = size(inpict);
rowpict = false(s0);
colpict = false(s0);
osm = filtsize(1)-1;
osn = filtsize(2)-1;
for n = 1:(s(2)-2*padsize(2))
for m = 1:(s(1)-2*padsize(1))
sample = inpict(m:(m+osm),n:(n+osn));
rowpict(m,n) = all(range(sample,2)<tol);
colpict(m,n) = all(range(sample,1)<tol);
end
end
imshow(cat(1,rowpict,colpict))
As much as everyone rags on writing things with loops, this is still ~3x as fast as using nlfilter()

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by