Clear Filters
Clear Filters

Adaptive Threshold for Segmentation

4 views (last 30 days)
nazneen
nazneen on 20 Feb 2015
Commented: Image Analyst on 24 Sep 2020
I have a piece of code which works perfectly fine but i am not able to understand the part where it creates a threshold matrix. Can someone please take time out and explain it to me ?
% A1 is the input image
A1max=ordfilt2(A1,512*512,ones(512,512)); %my max value % 512x512 Maximum Filter
A1min=ordfilt2(A1,1,ones(512,512)); %mIN value % 512 X 512 Minimum Filter
A1dif=A1max-A1min;
avg=filter2(fspecial('average',7),A1)';
z=0.9;%%GAMMA
c=0.5;%%ALPHA
[ka, kb]=size(A1);
for i=1:ka
for j=1:kb
if avg(i,j)> c*A1dif(i,j) && (abs(avg(i,j)-A1(i,j))/(avg(i,j)))<(1-c)
if avg(i,j)>=A1(i,j)
TH(i,j)=c*avg(i,j);
else
TH(i,j)=avg(i,j);
end
else
TH(i,j)= avg(i,j)+z*A1dif(i,j);
end
end
end
How does one pick the Gamma and Alpha values?

Answers (1)

Image Analyst
Image Analyst on 20 Feb 2015
Edited: Image Analyst on 20 Feb 2015
I'd say it's just by trial and error to see what looks good. You might want to use rangefilt() instead of doing ordfilt twice. Also, you vectorize the loops to speed it up and make it more compact.
Man, that's one weird filter. What's its purpose?
clc;
clear all;
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
% A1 is the input image
A1 = imread('cameraman.tif');
% Display the original gray scale image.
subplot(1, 2, 1);
imshow(A1, []);
title('Original Grayscale Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
A1max=ordfilt2(A1,512*512,ones(512,512)); %my max value % 512x512 Maximum Filter
A1min=ordfilt2(A1,1,ones(512,512)); %mIN value % 512 X 512 Minimum Filter
A1dif=A1max-A1min;
avg=filter2(fspecial('average',7),A1)';
z=0.9;%%GAMMA
c=0.5;%%ALPHA
[ka, kb]=size(A1);
for i=1:ka
for j=1:kb
if avg(i,j)> c*A1dif(i,j) && (abs(avg(i,j)-A1(i,j))/(avg(i,j)))<(1-c)
if avg(i,j)>=A1(i,j)
TH(i,j)=c*avg(i,j);
else
TH(i,j)=avg(i,j);
end
else
TH(i,j)= avg(i,j)+z*A1dif(i,j);
end
end
end
subplot(1, 2, 2);
imshow(TH, []);
title('TH Image', 'FontSize', fontSize);
It looks like it filters twice, once on the transpose of the image and adds them together. What is this for?
  4 Comments
Rakshith Nayak
Rakshith Nayak on 24 Sep 2020
how did you get alpha ,gamma and 'average' 7(line 23) values?
Image Analyst
Image Analyst on 24 Sep 2020
Personally, I don't know, and nazneen hasn't been seen here in 5 years.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!