Mahesh Ranjan
Mahesh Ranjan

Reputation: 35

MATLAB Thresholding

I am using the following MATLAB code for Niblack Binarization.

    mean= averagefilter2(image1);
    meanSquare = averagefilter2(image1.^2);
    standardDeviation = (meanSquare - mean.^2).^0.5;
    binaryImage = image1 >=  (mean + k_threshold * standardDeviation);

   function img=averagefilter2(image1)
       meanFilter = fspecial('average',[60 60]);
       img = imfilter (image1,meanFilter);
   end

But when I implement it, Original Image becomes Binarised Image.

(Ignore the black border..it is just to highlight the white patch on the edge of the image)

That is, near the edges some data pixels go missing and becomes white (the white patch at the top and right edges). Am I wrong anywhere in this implementation? Is there a better "MATLAB way" of implementing it, or should I do it manually using nested loops for calculating average and standard deviation?

Upvotes: 2

Views: 106

Answers (1)

Ander Biguri
Ander Biguri

Reputation: 35525

Likely this is due to boundary conditions of the imfilter function, and perharps from your own function averagefilter2.

When you filter, in the edge cases, you need to access pixels that are outside the image. That means that you need to make assumptions on what happens outside the boundary.

imfilter has a parameter to choose what is assumed to be outside, and it is assumed to be zero by default. That would definetly cause a smaller value for the mean and perhaps that makes the binarization get "deleted" there.

Try different values, and surely implement that for your own function also.

I suggest starting with 'symmetric'

Upvotes: 2

Related Questions