Lets Learn together... Happy Reading

" Two roads diverged in a wood, and I,
I took the one less traveled by,
And that has made all the difference "-Robert Frost

Recent Posts

Simple Image Thresholding using GUI component Slider

Thresholding is one of the steps performed on the image during image conversion. 
Also Learn: Otsu's thresholding method
It is used for separating the background from the foreground.
The user can drag the slider to adjust the Thresholding. The slider value ranges between 0 and 255.

MATLAB CODE:


function f=mainthresh()
%The window size of the image is adjusted based on the screen size.


global T1;
ssz = get(0,'ScreenSize');
figure('Visible','on','Name','IMAGE THRESHOLDING','NumberTitle','off','Position',ssz);

axes('units','pixels','Position',[ssz(3)/45 ssz(4)/4 ssz(3)-(ssz(3)/4) ssz(4)-(ssz(4)/3)]);

in=imread('coins.png');
imshow(in);
set(gca,'xtick',[],'ytick',[])

%The Slider is one of the GUI component in MATLAB. The Value of the slider is between 0 and 255.
%These values can be changed based on the image type.


slid=uicontrol('Style','Slider','Visible','on','Value',1,'Max',255,'Min',0,'Sliderstep',[1 1],'Position',[ssz(3)/35 ssz(4)/5 ssz(3)-(ssz(3)/3) 20],'Callback', @threshing_image);
ent=uicontrol('Style','pushbutton','Visible','on','String','THRESHOLD VALUE','Position',[ssz(3)-(ssz(3)/4) ssz(4)-(ssz(4)/8) 105 30]);
ed=uicontrol('Style','edit','Visible','on','String','0','Value',1,'Position',[ssz(3)-(ssz(3)/4) ssz(4)-(ssz(4)/6) 90 20]);


%When the slider is moved the corresponding position value is passed to the function 'threshing_image' .

%The original image is passed to another function 'Imthreshold'.



      function threshing_image(object,~)
        val=get(object,'value');
        in=imread('coins.png');
        T1=Imthreshold(in,val);
        imshow(T1);
        set(gca,'xtick',[],'ytick',[])
       
        set(ed,'String',val);
      end

    

%Based on the value passed, the image background is separated from the foreground.

%In this example, the background of the coins image contains pixel values less than 80.
%By adjusting the slider to value 80 the background area gets value zero and the coins appear as it is.




function Im=Imthreshold(Image,Tvalue)

%Replace the pixel value either with 0 or 255.


Image( find ( Image(:,:,1) < Tvalue ) )=255;

Im=Image;
end
f=T1;
end















Thresholded Image



Here is another example , This Image is RGB. So we have to consider the RED, GREEN and BLUE pixel values.



like button Like "IMAGE PROCESSING" page

MATLAB PROGRAM : 2D MEDIAN FILTERING FOR SALT AND PEPPER NOISE WITHOUT USING medfilt2 FUNCTION

MEDIAN FILTER:

In digital Image processing, removing the noise is one of the preprocessing techniques.
The image noise may be termed as random variation of brightness or color information.
There are various types of image noise. Here a matlab program to remove 'salt and pepper noise' using median filtering is given.
 The random occurrence of black and white pixels is ‘salt and pepper noise’.

The procedural steps for 2D median filtering:


Learn how to pad with zeros using MATLAB built_in function padarray.

FLOW CHART:


MATLAB CODE:

%READ AN 2D IMAGE
A=imread('zebra.jpg');
title('IMAGE WITH SALT AND PEPPER NOISE');
figure,imshow(A);

%PAD THE MATRIX WITH ZEROS ON ALL SIDES
modifyA=zeros(size(A)+2);
B=zeros(size(A));

%COPY THE ORIGINAL IMAGE MATRIX TO THE PADDED MATRIX
        for x=1:size(A,1)
            for y=1:size(A,2)
                modifyA(x+1,y+1)=A(x,y);
            end
        end
      %LET THE WINDOW BE AN ARRAY
      %STORE THE 3-by-3 NEIGHBOUR VALUES IN THE ARRAY
      %SORT AND FIND THE MIDDLE ELEMENT
       
for i= 1:size(modifyA,1)-2
    for j=1:size(modifyA,2)-2
        window=zeros(9,1);
        inc=1;
        for x=1:3
            for y=1:3
                window(inc)=modifyA(i+x-1,j+y-1);
                inc=inc+1;
            end
        end
       
        med=sort(window);
        %PLACE THE MEDIAN ELEMENT IN THE OUTPUT MATRIX
        B(i,j)=med(5);
       
    end
end
%CONVERT THE OUTPUT MATRIX TO 0-255 RANGE IMAGE TYPE
B=uint8(B);
title('IMAGE AFTER MEDIAN FILTERING');
figure,imshow(B);












Learn how to add 'salt and pepper noise to an image'.Median filtering preserves the image without getting blurred. Median filtering is done on an image matrix by   finding the median of the neighborhood pixels by using a window that slides pixel by pixel.


  

like button Like "IMAGE PROCESSING" page
Previous Post Next Post Home
Related Posts Plugin for WordPress, Blogger...
Twitter Bird Gadget Google ping Hypersmash.com