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

Identifying the objects based on count(bwlabel)

Identifying the objects based on labeling



Steps To Be Performed:


  1. Convert the RGB image to binary image.
  2. Fill the holes in the image.
  3. Label the objects in the image based on connectivity 8
  4. Display the images in a RGB format
4.1.  Store the co-ordinates(x,y) of the object 1 which is  labeled as 1.
4.2.  Calculate the image size for object one. The length can be found by subtracting the maximum and the minimum of y values. Similary for x find the width by subtracting the maximum and minimum of x values.
4.3.  Now map the pixel value to the new image.






Original Image






A=imread('num2.jpg');
figure,imshow(A);
title('Original image');
C=~im2bw(A);
B=imfill(C,'holes');
label=bwlabel(B,8);
for j=1:max(max(label))

[row, col] = find(label==j);

len=max(row)-min(row)+2;
breadth=max(col)-min(col)+2;
target=uint8(zeros([len breadth 3] ));
sy=min(col)-1;
sx=min(row)-1;

for i=1:size(row,1)
    x=row(i,1)-sx;
    y=col(i,1)-sy;
    target(x,y,:)=A(row(i,1),col(i,1),:);
end
mytitle=strcat('Object Number:',num2str(j));
figure,imshow(target);title(mytitle);
end





The objects that are displayed in a RGB format.













like button Like "IMAGE PROCESSING" page

Image extensions in MATLAB

Graphics Interchange Format
To extract frames from GIF file.
The image is usually an animated GIF file.
The multiframes are read using the syntax: imread(filename, ‘frames’, ‘all’)
To read ‘n’ number of frames from a GIF file: imread(filename, ‘frames’, 1:n)

Example:
Here, the GIF image is an animated file and contains 8 frames.
The frames that are read will be stored in a 4-D format.



 [A,map]=imread('cute.gif','frames','all');
mov=immovie(A,map);
implay(mov);



To animate the muliframes: use  ‘immovie’ to make a movie from the muliframe image.
Play the movie using ‘implay’.

To view a particular frame:

imshow(A(:,:,4),map);  Here the 4th frame is viewed.

To create avi file from the movie:

movie2avi(mov, 'cute_girl.avi', 'compression', 'None','fps',3);
Here frames per second(fps) is 3. 



video
like button Like "IMAGE PROCESSING" page

Image Shading in MATLAB

         
              Here I tried to shade the RGB image which results in a pencil sketched type image. The user selects the image from the pop-up menu and by adjusting the line value in the slider, the edge thickness is made.
By adjusting the thresh value in the slider the image shade is increased or decreased.
Additional Information:
To make your own pencil sketch without using MATLAB try Autodesk Sketchbook.
Based on the customers review, this software is best to try on low pixel camera images.
Steps To Be Performed:

1.     Input a RGB image
2.     Convert the image to grayscale image.
3.     Find the edges using Sobel Method.[gradient Estimation]
4.     Filter the Image to obtain the image without noise
4.1.          The blurred or unsharp image is subtracted from the image to obtain the sharpened image.
5. Blend the edge and the sharpened image.



MATLAB Code:

function image_shade

The GUI is designed.

global filename A;
scz=get(0,'ScreenSize');
figure('Position',[round(scz(1,3)/4) round(scz(1,4)/8) 700 500],'MenuBar','None','NumberTitle','off','Name','Pencil sketch Application','Resize','off');
axes('Position',[0 0 .7 1],'xtick',[],'ytick',[]);
shade=uicontrol('Style','slider','Position',[500,310 200 20],'Max',1 ,'Min',0.01,'Value',0.56,'Callback',@draw);
thresh=uicontrol('Style','slider','Position',[500,370 200 20],'Max',255,'Min',0,'Value',30,'Callback',@draw);
directory=dir('*.jpg');
files={directory.name}';
tval=uicontrol('style','text','Position',[500,340 100 20]','String','Thresh :');
line=uicontrol('style','text','Position',[500,395 100 20]','String','Line :');
uicontrol('style','text','Position',[500,455 100 20]','String','Filename:');
uicontrol('Style','popupmenu','position',[500 420 160 30],'Value',1,'String',files,'Callback',@displayfile);

    function displayfile(obj,~)
        ptr=get(obj,'value');
        filename=char(files(ptr));
        A=imread(filename);                      
        imshow(A);
    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(A))
        A=imread(filename);
        B=rgb2gray(A);


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






Shaded Image

Original Image




like button Like "IMAGE PROCESSING" page

Image Arithmetic in MATLAB with example

Image Arithmetic

    An image is represented in a matrix format.
    To perform image arithmetic the size of the two matrices should be same.
    The operation on two images results in a new image.
    Consider two images A and B with same size.


Image Addition

    In a RGB image, the addition of two images can be done using the ‘+’ operator. C=A+B;
Here, the minimum value of A+B and 255 is taken.

(i.e) C(i,j,1)=min(A(i,j,1)+B(i,j,1),255) where (i,j) represents the pixel position.
Image addition can be used to add the components from one image into other image.


Image Subtraction

A new image is obtained as a result of the difference between the pixels in the same location of the two images being subtracted.
C=A-B; ie. Maximum value of A-B and zero.
C(i,j,:) =max(A(i,j,:)-B(i,j,:),0).

Image subtraction is widely used for change detection.
For detecting the missing components in product assembly,
Example:To detect the defects in the PCB.



Image subtraction is most beneficial in the area of medical image processing called mask mode radiography.
   

Image Multiplication

Image multiplication is used to increase the average gray level of the image by multiplying with a constant.
It is used for masking operations.
C=A.*B;

Image Division

Image division can be considered as multiplication of one image and the reciprocal of other image.
C=A.\B;

Logical Operations:

Logical operations are done on pixel by pixel basis.
The AND and OR operations are used for selecting subimages in an image .
This masking operation is referred as Region Of Interest processing.

Logical AND

To isolate the interested region from rest of the image portion logical AND or OR is used. Consider a mask image L for the image A.
To obtain the interested area, D= and(L,A) ;
We can use L&A also.
The resulting image will be stored in D which contains the isolated image part.

Logical OR

Syntax: D=or(L,A). We can also use L|A



background=imread('back.jpg');
A=imread('tommy1.bmp');
B=imread('jerry1.bmp');

%Image addition
%Both A and B are of same size
object=A+B;


background=imresize(background,[size(object,1) size(object,2)]);
Im3=uint8(zeros(size(object)));
whiteImg=uint8(ones(size(object)));
%Array right division. A./B is the matrix with elements A(i,j)/B(i,j). A and B must
%have the same size, unless one of them is a scalar.
%Image Division
mask=whiteImg./object;
%Logical AND
im3=uint8(mask&background);%uint8(and(mask,background));
figure,imshow(mask);
%Array multiplication. A.*B is the element-by-element product of the arrays A and B.

%Image multiplication
%Multiply the background and the mask image
%And the result with the foreground image to obtain the final Image.
finalImg=(background.*im3)+object;
figure,imshow(finalImg);










Example:2
img61.jpg

Sample_Image.jpg

D=imread('img61.jpg');
E=imread('Sample_Image.jpg');
 D=imresize(D,[size(E,1) size(E,2)]);
 A=double(E);
 C=uint8(zeros(size(A)));
 F=E;


 [x,y]=find((A(:,:,1)+A(:,:,2)+A(:,:,3))>650);

 for i=1:size(x,1)
    C(x(i),y(i),:)=1; 
    F(x(i),y(i),:)=0;    
 end



 C=C.*D;

  
  C=C+F;
  imshow(C);



like button Like "IMAGE PROCESSING" page

Paint application in MATLAB

Let’s Paint


This paint application will fill the closed boundary with the specified color.
First, Create image with the well defined boundary value.
                                 

In my application, I created images with boundary/border with black pixel value ie [R,G,B]=[0,0,0] and the rest of the area with white [R,G,B]=[255,255,255].




How to use the application:
1.     Select one image file from the pop up menu.
2.     Select a color and click the image portion to fill the clicked area.
3.     If you need to fill the image portion with another color then click another color button
4.     Click undo button to come back to the previous point.
5.     Click save button to save the file.








Code explanation:

    First create a figure with color buttons, axis and the pop-up menu for selecting the image.
 I am showing only .png(Portable Network Graphics) files in the pop-up menu.
directory=dir('*.png');

When the user selects the image file then the function ‘displayfile’ is called.





function paintapp
scz=get(0,'ScreenSize');
figure('Position',[round(scz(1,3)/4) round(scz(1,4)/8) 700 500],'MenuBar','None','NumberTitle','off','Name','Paint Application','Resize','off');
inputs=['RED   ';'GREEN ';'BLUE  ';'YELLOW';'BLACK ';'WHITE ';'ORANGE';'PURPLE';'BROWN ';'PINK  ';'GRAY  ';'LGREEN';'Save  ';'undo  '];
global top targetcolor replacecolor x y A color undocolor originalcolor point filename;
top=1;
targetcolor=[255 255 255];
xaxis=500;
yaxis=400;
for k=1:size(inputs,1)
uicontrol('Style','pushbutton','position',[xaxis yaxis 80 30],'String',inputs(k,:),'Callback',@paintme);
xaxis=xaxis+90;
if(mod(k,2)==0)
    yaxis=yaxis-50;
    xaxis=500;
end
end
ax=axes('Position',[0 0 .7 1],'xtick',[],'ytick',[]);
directory=dir('*.png');
files={directory.name}';
uicontrol('Style','popupmenu','position',[500 450 160 30],'Value',1,'String',files,'Callback',@displayfile);


The file is obtained from the string array ‘files’ and the image is displayed.

function displayfile(obj,~)
        ptr=get(obj,'value');
        filename=char(files(ptr));
        A=imread(filename);
        image(A);
    end
    

When the user clicks the color button the function ‘paintme’ is called.
Here, targetcolor is the color value at the point where the user clicks.


The replacecolor is the color the user selects from the colors displayed.


Based on the color selected the color value to be filled in the area is stored in ‘replacecolor’. If the user selects the undo button the color to be replaced will the replacecolor and the target color will be filled at the place.
                                                                 

                                                                                                                      






function paintme(object,~)
   color=get(object,'String');
   if(~isempty(A))
   switch(color(1,:))
       case 'RED   '
           while(strcmp(color,'RED   ')==1)
           replacecolor=[254 1 1];
            getpoints;
           end
       case 'GREEN '
           while(strcmp(color,'GREEN ')==1)
           replacecolor=[1 60 21];
          getpoints;
           end
       case 'BLUE  '
           while(strcmp(color,'BLUE  ')==1)
               replacecolor=[20 120 120];
           %replacecolor=[1 1 254];
            getpoints;
           end
       case 'YELLOW'
           while(strcmp(color,'YELLOW')==1)
           replacecolor=[240 240 1];
           getpoints;
           end
       case 'BLACK '
           while(strcmp(color,'BLACK ')==1)
           replacecolor=[1 1 1];
            getpoints;
           end
       case 'WHITE '
           while(strcmp(color,'WHITE ')==1)
           replacecolor=[254 254 254];
            getpoints;
           end
       case 'ORANGE'
          while(strcmp(color,'ORANGE')==1)
          replacecolor=[250 90 1];
         getpoints;
          end
       case 'PURPLE'
           while(strcmp(color,'PURPLE')==1)
           replacecolor=[90 20 60];
            getpoints;
           end
       case 'BROWN '
           replacecolor=[90 20 10];
           while(strcmp(color,'BROWN ')==1)
            getpoints;
           end
       case 'PINK  '
           while(strcmp(color,'PINK  ')==1)
           replacecolor=[220 90 90];
            getpoints;
           end
          
            case 'LGREEN'
           while(strcmp(color,'LGREEN')==1)
            replacecolor=[20 240 10];
            getpoints;
           end
       case 'GRAY  '
      
           while(strcmp(color,'GRAY  ')==1)
           replacecolor=[20 20 20];
            getpoints;
           end
          
        case 'undo  '
                floodfill(point(1),point(2),undocolor,originalcolor);
       case 'Save  '
           fname=strcat('my',filename);
           msg=strcat('File name:',fname);
           msgbox(msg,'FILE SAVED');
          
   end
   end
   
end

 If the user clicks the save button the file is saved.




The user should click on the image to fill with the replace color.
The ‘waitforbuttonpress’ function is used to get the user input graphically by a mouse click.
When user clicks a particular point, the x and y co-ordinate value is obtained using ‘get(ax,'CurrentPoint')’.
   
function getpoints
      
     try
         waitforbuttonpress;
         f=get(ax,'CurrentPoint');
         x=round(f(1,2));
         y=round(f(1,1));
        
         % check whether x and y is less than the size of the image
         targetcolor(1)=A(x,y,1);
         targetcolor(2)=A(x,y,2);
         targetcolor(3)=A(x,y,3);
        
         point(1)=x;
         point(2)=y;
         undocolor=replacecolor;
         originalcolor=targetcolor;
         floodfill(x,y,targetcolor,replacecolor);
        % end
    
     catch
         color='null';
     end
        
       
    end
    
Floodfill or bucketfill algorithm is used to fill the image portion.

function floodfill(x,y,targetcolor,replacecolor)
    top=1;
    queue=zeros(1);
   
    if((A(x,y,1)==targetcolor(1))&&(A(x,y,2)==targetcolor(2))&&(A(x,y,3)==targetcolor(3)))
        queue(top,1)=x;
        queue(top,2)=y;
        top=top+1;
        i=1;
     while(i~=top)
          l= queue(i,1);
          m=queue(i,2);
        
             
         if((A(l,m,1)==targetcolor(1))&&(A(l,m,2)==targetcolor(2))&&(A(l,m,3)==targetcolor(3)))
            
             w=m;
             e=m;
             wnode=1;
             enode=1;
             while((A(l,w,1)==targetcolor(1))&&(A(l,w,2)==targetcolor(2))&&(A(l,w,3)==targetcolor(3)))
                 wnode=wnode+1;
                 w=w-1;
             end
             while((A(l,e,1)==targetcolor(1))&&(A(l,e,2)==targetcolor(2))&&(A(l,e,3)==targetcolor(3)))
                 enode=enode+1;
                 e=e+1;
             end
             w=m+1;
             e=m-1;
             for j=1:wnode-1
                 A(l,w-j,1)=replacecolor(1);
                 A(l,w-j,2)=replacecolor(2);
                A(l,w-j,3)=replacecolor(3);
             end
             
             for j=1:enode-1
                A(l,e+j,1)=replacecolor(1);
                A(l,e+j,2)=replacecolor(2);
                A(l,e+j,3)=replacecolor(3);
             end
             snode=m-wnode+2;
             for j=snode:(snode+enode+wnode-4)
                if((A(l+1,j,1)==targetcolor(1))&&(A(l+1,j,2)==targetcolor(2))&&(A(l+1,j,3)==targetcolor(3)))
                   queue(top,1)=l+1;
                   queue(top,2)=j;
                   top=top+1;
               end
                if((A(l-1,j,1)==targetcolor(1))&&(A(l-1,j,2)==targetcolor(2))&&(A(l-1,j,3)==targetcolor(3)))
               queue(top,1)=l-1;
               queue(top,2)=j;
               top=top+1;
                end
                end
        
         end
        i=i+1;
    end
    end
    image(A);
end
end


References:
-------------------------------------------------------------------------------------------------------------
GET FREE IMAGES AT:http://www.clker.com/





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