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

Select your favorite topic

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

1 comments:

TBATOU DRISS said... Reply to comment

nice Greeting from Morocco.
& Thank you so much for the effort you did to help others.

Enjoyed Reading? Share Your Views

Previous Post Next Post Home
Related Posts Plugin for WordPress, Blogger...
Google ping Hypersmash.com