Open In App

What is Image shading in MATLAB?

Last Updated : 14 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

As the name suggests, Image shading means modifying the original image into its sharpened form by detecting the edges of the image. Here two words are combined to define a process one is “image” which means picture and another word is “Shading” which is defined as a process of redrawing or modifying the image in thin or thick lines or changing the color and saturation of the image to increase its quality or visibility. 

Basically MATLAB is a programming language that also supports the GUI (Graphical user interface) in which we will work on Images. In it the formation of images is also possible by the points by the help of GUI we can redraw the original image into its reconstructed image or form.

As we know that all the concepts are about images so it’s better if we can understand them with the help of examples. Let’s see some examples for a better Understanding.

Before going to examples and their outputs let’s see the steps which will be performed or followed in the process of Image Shading in the MATLAB IDE. These will be the important steps to perform Image shading of any original image because the program and process will be the same only we have to change the images or take images of different objects for sharpening of that images. let’s see the steps that we have to follow :

  1. In the first step we have to take the original RBG image of any object.
  2. Now we have to convert that original image into a grayscale image by using the line label.
  3. Now in the third step, we have to find all the sharp edges of the image by a method which is called Sobel Method.
  4. Now we have to Filter the image to get a sharp image or to get an image without noise.
  5. Now the blurred image is subtracted from the original image to get the sharpened image or highly-edged image.
  6. Now in the last step blend the edges and the sharped image basically we get the sharpened image.

Now we will write the code for image shading which works on different types of images Let’s see the code:

Example 1:

Matlab




% MATLAB code that will work for various objects and images.
function sharpened_image
global filename GFG;
scz=get(0,'ScreenSize');
figure('Position',[round(scz(1,3)/8) round(scz(1,4)/9)
400 500],'MenuBar','NumberTitle','off','Name',
'Pencil sketch Application','Resize','off');
axes('Position',[0 0 .8 1],'atick',[],'btick',[]);
shade=uicontrol('Style','slider','Position',
[400,310 100 20],'Max',1 ,'Min',0.01,'
Value',0.56,'Callback',@draw);
thresh=uicontrol('Style','slider','Position',
[400,370 100 20],'Max',255,'Min',0,
'Value',30,'Callback',@draw);
directory=dir('*.jpg');
files={directory.name};
tval=uicontrol('style','text','Position',
[400,340 700 20]','Thresh :');
line=uicontrol('style','text','Position',
[400,395 800 20]','String','Line :');
uicontrol('style','text','Position',
[400,455 100 200]','Filename:');
uicontrol('Style','popupmenu','position',
[400 420 180 30],'Value',1,'String',
files,'Callback',@displayfile);
  
    function file_display(obj,~)
        ptr=get(obj,'value');
        filename=char(files(ptr));
        X1=imread(filename);                      
        imshow(X1);
    end
    function draw(~,~)
        sh=get(shade,'Value');
        thr=get(thresh,'Value');
        thval=strcat('Thresh :', num2str(sh));
        set(tval,'String',thval);
         
        lineval=strcat('Line :', num2str(thr));
        set(line,'String',lineval);
       if(~isempty(B1))
        X1=imread(filename);
        B=rgb2gray(X1);
% The Edge of the image is detected using 
% the sobel edge detection method.
C3=~edge(B,'sobel','VERTICAL');
C2=~edge(B,'sobel','HORIZONTAL');
C=uint8(C3.*C2);
  
% The image is sharpened by subtracting the blur image.
F1=uint8(imfilter(B,fspecial('unsharp')/sh));
  
% The blending of the edge and the filtered image is done.
  
for m=1:size(F1,1)
    for n=1:size(F1,2)
        if(C(m,n)==0)
           F1(m,n)=B(m,n)-thr;
        end
    end
end
imshow(F1);
       end
    end
end


Output:

Original Image

Output 1:

Final Output

Let’s see another example for a better understanding:

Original Image (GFG)

Output 2:

Final Output



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads