انواع روش های آستانه گذاری در تصاویر + کدهای رایگان

پروژه ۴۵۵: انواع روش های آستانه گذاری در متلب :

در این پروژه قصد داریم انواع روش های آستانه گذاری را تشریح نماییم. بدین منظور ابتدا نیاز به یک برنامه اصلی برای فراخوانی فانکشن های مورد نظر داریم. روش هایی که قصد داریم تا نشان دهیم به شرح زیر است: 

برنامه اصلی:  


% read in image 
im1 = double(imread('image0000.jpg'));
% read in image 0025 of an image sequence
im2 = double(imread('image0025.jpg'));

% compute the difference image (Euclidian distance in RGB space)
dif = sqrt(sum((im1-im2).^2,3));

% compute difference image histogram
[h, hc] = hist(makelinear(dif), 256);

% perform thresholding to detect motion
To = hc(otsuThreshold(h));
Tk = hc(kapurThreshold(h));
Tr = hc(rosinThreshold(h));

% display results
shownormimage(dif >= To); title('Otsu result'); pause
shownormimage(dif >= Tk); title('Kapur result'); pause
shownormimage(dif >= Tr); title('Rosin result'); pause

% Alternatively, you can use
% shownormimage(dokapurthreshold(dif)); % .... etc

آستانه گذاری : otsuThreshold  
%
% T = otsuThreshold(h)
%
% compute the Otsu threshold given an input histogram
% Note that T corresponds to the histogram bin index.
%
function T = otsuThreshold(h)

L = length(h);
i = 1:L;
A = cumsum(h);
B = cumsum(h.*i);

u = B ./ A;
tmp = (A(L) - A);
v = (B(L) - B) ./ (tmp + (tmp==0));


F = A .* (A(L) - A) .* (u-v).^2;

[mF, T] = max(F);

آستانه گذاری :kapurthreshold  
%
%
% [T,F] = kapurthreshold(imhist)
%
% T = Kapur's threshold on the input histogram
% F = Sum of entropies for two class system, for all thresholds
%
% T is scaler, F is a vector of the same size as imhist
% Input histogram 'imhist' should be a 1xL vector, where L is the number of bins
%
function [T,F,E,A] = kapurthreshold(imhist)

if (size(imhist,1) ~= 1)
    imhist = imhist';
end

if (size(imhist,1) ~= 1)
    error('imhist should be a 1xL vector');
end


L = length(imhist);
i = 0:(L-1);
A = integrate(imhist);

E = cumsum(imhist .* log(imhist+(imhist==0)));

A = A + (A==0);
L2 = L-1;
F = zeros(1,L);
tmp = (A(L) - A(1:L2));
tmp=tmp + (tmp==0);
F(2:L) = (E(1:L2) ./ A(1:L2)) - log(A(1:L2)) + (E(L)-E(1:L2)) ./ tmp - log(tmp);
F(1) = 0;

[tmp, T] = min(F);

آستانه گذاری :RosinThreshold  
%
% T = RosinThreshold(imhist, picknonempty)
%
% Compute the Rosin threshold for an input histogram.
% T is the histogram index that should be used as a threshold.
% The optional second argument "picknonempty" is a binary variable
% indicating that the chosen bin should be non-zero.
%
% REF: Paul L. Rosin, "Unimodal thresholding", Pattern Recognition 34(11): 2083-2096 (2001)
%
function T = RosinThreshold(imhist, picknonempty)

% should I ensure the chosen(threshold) bin is non empty?
if (nargin < 2)
    picknonempty = 0;
end

% find best threshold

[mmax2, mpos] = max(imhist);
p1 = [mpos, mmax2];

% find last non-empty bin
L = length(imhist);
lastbin = mpos;
for i = mpos:L
    if (imhist(i) > 0)
        lastbin=i;
    end
end
    
p2 = [lastbin, imhist(lastbin)];
DD = sqrt((p2(1)-p1(1))^2 + (p2(2)-p1(2))^2);

if (DD ~= 0)
	best = -1;
	found = -1;
	for i = mpos:lastbin
        p0 = [i,  imhist(i)];
        d = abs((p2(1)-p1(1))*(p1(2)-p0(2)) - (p1(1)-p0(1))*(p2(2)-p1(2)));
        d = d / DD;
        
        if ((d > best) && ((imhist(i)>0) || (picknonempty==0)))
            best=d;
            found = i;
        end
	end
	
	if (found == -1)
        found = lastbin+1;
	end
else
    found = lastbin+1;
end


T = min(found, L);

آستانه گذاری :dootsuthreshold  
function [im2,T] = dootsuthreshold(difim, nbins, mask, usezeros)

%
% Otsu method
%


if (nargin < 2)
    nbins = 256;
end
if (nargin < 3)
    mask=1;
end
if (nargin < 4)
    usezeros=1;
end

data = removezeros(makelinear((difim+1000).*mask))-1000;

[h,hc] = hist(data, nbins);
if (usezeros==0)
    h(1)=0;
end
T = hc(otsuthreshold(h));

im2 = 0+(difim>=T);

آستانه گذاری :dokapurthreshold    
function [im2,T] = dokapurthreshold(difim, nbins, mask, usezeros)

if (nargin < 2)
    nbins = 256;
end
if (nargin < 3)
    mask=1;
end
if (nargin < 4)
    usezeros=1;
end

data = removezeros(makelinear((difim+1000).*mask))-1000;

[h,hc] = hist(data, nbins);
if (usezeros==0)
    h(1)=0;
end
T = hc(kapurthreshold(h));

im2 = 0+(difim>=T);

آستانه گذاری :dorosinthreshold 
function [im2,T] = dorosinthreshold(difim, nbins, mask, usezeros)

if (nargin < 2)
    nbins = 256;
end
if (nargin < 3)
    mask=1;
end
if (nargin < 4)
    usezeros=1;
end

data = removezeros(makelinear((difim+1000).*mask))-1000;

[h,hc] = hist(data, nbins);
%plot(hc,h); pause
if (usezeros == 0)
    h(1)=0;
end

T = hc(rosinthreshold(h,1));

im2 = 0+(difim>=T);



 p455-1

برای عضویت روی عکس زیر کلیک کنید : (آخرین اخبار مرتبط با مهندسی و سایر تکنولوژی ها)

یا آدرس لینک زیر را در تلگرام خود جستجو نمایید:

m_b_coll@


مجموعه: پردازش تصویر, مهندسی کامپیوتربرچسب ها , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,

دیدگاهتان را بنویسید

نشانی ایمیل شما منتشر نخواهد شد. بخش‌های موردنیاز علامت‌گذاری شده‌اند *