Binarization image does not have good effect

243 views Asked by At

I have a problem when binarizing an image:

enter image description here

The walker in the picture is lost after binarizing. Could anyone offer help? Here's the code I used:

clc;
clear;
video = VideoReader('C:\Users\Small_Bird\Desktop\ch02_20170323193606~2.avi');
nFrames = video.NumberOfFrames;
H = video.Height;
W = video.Width;

Rate = video.Preallocate movie structure.

for frameNum = 3500:nFrames
     P = read(video,frameNum);
     grayImage=rgb2gray(P);
     cannyEdge=edge(grayImage,'canny');
     [m,n]=size(grayImage);
     for i=1:m
         for j=1:n
             if 1==cannyEdge(i,j)
                 h(i,j)=grayImage(i,j)+3;
             else 
                 h(i,j)=grayImage(i,j);
             end
         end
     end

     thresh=graythresh(h);
     I2=im2bw(h,thresh);   
     subplot(2,2,1);
     imshow(grayImage),title('original image');
     subplot(2,2,2);
     imshow(cannyEdge),title('image after extracting edge');
     subplot(2,2,3);
     imshow(h),title('image after strengthening edge');
     subplot(2,2,4);
     imshow(I2),title('image after binaryzation'); 
end
1

There are 1 answers

1
gnovice On BEST ANSWER

The issue is the choice of threshold for im2bw. You're using the function graythresh to compute a global threshold across the whole image, which your results show only succeeds in separating the black parts of the image from the gray-or-higher parts of the image. You'll need to choose a higher threshold, either an absolute one you use for all images or one computed from some features of each image.

If you have MATLAB version R2016a or newer you have options for computing a locally adaptive threshold using either adaptthresh or im2binarize (the replacement for the im2bw function in newer versions) using the 'adaptive' method. This may give you better results than a simple global threshold.