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

TILE SWAPPING GAME

             The little one at my home likes to play the hidden object games. There are also mini games as a part of the hidden object game. One such game I came across was tile swapper, which is of course a good old one. I thought of trying this game with MATLAB and to my surprise ,it came out better than I expected.

Method I used:

I divided the image into equal parts and randomized the position of the images.
I created push buttons and placed these random parts on it.
When a button is clicked followed by another button click, the image on the previous button click is swapped with the last button clicked image. This process continues until all the image parts are at the correct position.

How to play:

1.       Select an image from the popup menu



2.       Select the number of tiles from the popup menu

3.       Click an image part

4.       Click another image part

The selected  image parts are swapped



5.       Continue the process until all the image parts are in correct position.


6.       If the image parts are in correct position then msgbox pops up with a message.


MATLAB CODE:


function tile_swap_game
clear all;
clc
%FIGURE WINDOW
figure('Position',[250 150 700 450],'Name','TILE SWAP GAME','NumberTitle','off','MenuBar','None','Resize','off');
%DECLARE THE GLOBAL VARIABLES
global indexvalue currvalue prevalue correctPos Pname x1 y1 flag A Rnum presentPos files I TImg;

%GET THE JPEG IMAGES IN THE CURRENT FOLDER
directory=dir('*.jpg');
files={directory.name}';
%TITLE SIZE
msize={'2X2';'3X3';'4X4';'5X5';'6X6';'7X7';'8X8'};
%DEFINE THE GUI POPMENUS
uicontrol('Style','text','position',[495 400 75 25],'String','SELECT IMAGE');
fctrl=uicontrol('Style','popupmenu','position',[495 350 160 40],'Value',1,'String',files,'Callback',@displayfile);
uicontrol('Style','text','position',[495 320 75 25],'String','# TILES');
sctrl=uicontrol('Style','popupmenu', 'position',[495 290 160 20],'Value',1,'String',msize,'Callback',@tile);

%DISPLAY THE SELECTED IMAGE FROM THE POPMENU OPTION
    function displayfile(~,~)

        ptr=get(fctrl,'Value');
        filename=char(files(ptr));
        I=imread(filename);
        %RESIZE IMAGE TO STANDARD SIZE
        I=imresize(I,[448 447]);
        TImg=im2double(I);
       uicontrol('Style','Pushbutton','Position',[1 1 448 447],'CData' ,TImg);
       
    end

%GET THE TITLE SIZE AND DIVIDE THE IMAGE BASED ON THE SIZE
    function tile(~,~)
         
        if(isempty(I))
         
            displayfile;
        end
        ptr1=get(sctrl,'Value');
        num=ptr1+1;
        mvalue=mod(size(I),num);
        A=imresize(I,[size(I,1)-mvalue(1,1) size(I,2)-mvalue(1,2)]);

      
        m=size(A,1)/num;
        n=size(A,2)/num;
        x=1;
        y=1;
        x1=m;
        y1=n;
        indexvalue=1;

        correctPos=zeros([num^2 5]);
        presentPos=zeros([num^2 5]);
        inc=1;
        %DIVIDE THE IMAGE SIZE INTO EQUAL PARTS
        %EXAMPLE: IF #TITLE IS 4X4 THEN IMAGE WILL BE DIVIDED INTO 16 EQUAL
        %PARTS
        for i = 1:num
            m=x1*i;
            for j=1:num
   
                n=y1*j;
                correctPos(inc,1:4)=[x y m n];
               inc=inc+1;
               y=n;
            end
            x=m;
            y=1;
        end
        %RANDOMIZE THE IMAGE PARTS
        Rnum=randperm(num^2);

        %RE-POSITION THE [X,Y] CO-ORDINATES FOR THE UICONTROL FIELD
        %'POSITION'
        presentPos=sortrows(correctPos,-2);
        correctPos(:,5)=[1:(inc-1)]';
        presentPos(:,5)=[1:(inc-1)]';

        for j = 1 : inc-1
            %GET THE RANDOM NUMBER
            i=Rnum(j);
            %READ THE IMAGE PART BASED ON THE RANDOM NUMBER
            Ind=im2double(A(correctPos(i,1):correctPos(i,3),correctPos(i,2):correctPos(i,4),:));
            %DISPLAY THE IMAGE PART ON THE UICONTROL OBJECT 'PUSHBUTTON'
            Pname(j)=uicontrol('Style','Pushbutton','Position',[presentPos(j,1) presentPos(j,2) x1 y1],'CData' ,Ind,'UserData',j,'callback',@swap);
   
        end
    end
   
    %FUNCTION TO SWAP TWO TITLES
    function swap(obj,~)
        currvalue=get(obj,'UserData');

        if indexvalue == 2
   
           Temp=presentPos(currvalue,:);
           presentPos(currvalue,:)=presentPos(prevalue,:);
           set(Pname(currvalue),'Position',[presentPos(currvalue,1) presentPos(currvalue,2) x1 y1]);
           presentPos(prevalue,:)=Temp;
           set(Pname(prevalue),'Position',[presentPos(prevalue,1) presentPos(prevalue,2) x1 y1]);
   
           Evaluate_Position;
   
           if(flag==1)
             winner;
           end
   
           indexvalue=1;
        else
           prevalue=currvalue;
           indexvalue=indexvalue+1;
        end
   
    end

    %CHECK IF ALL THE IMAGE PARTS ARE PLACED IN THE CORRECT POSITION
    function Evaluate_Position
       
        flag=1;      
      
        tot=find(presentPos(:,5)==Rnum');
        if(numel(tot)==size(Rnum,2))
           flag=1;
        else
           flag=0;
        end
    end

    %DISPLAY THE IMAGE AND 
    function winner
        uicontrol('Style','Pushbutton','Position',[1 1 448 447],'CData' ,TImg);
        msgbox('YOU WIN!!!','CONGRATS');
    end
end




NOTE:  

a. Check whether the current directory contains JPEG images before executing the code
b. If you find the code is broken or unable to execute, mail me.I will mail you the code. Happy Weekend:-)

like button Like "IMAGE PROCESSING" page

Pop-Up Menu


A very simple example to show how the pop-up menu / Drop down list in MATLAB works.



MATLAB CODE:
function Popup_menu
global filename Img  I T;
scz=get(0,'ScreenSize');

figure('Position',[round(scz(1,3)/4) round(scz(1,4)/8) 800 500],'MenuBar','None','NumberTitle','off','Name','PopUpMenu Example','Resize','off');
axes('Position',[0 0 .7 1],'xtick',[],'ytick',[]);

directory=dir('*.jpg');
files={directory.name}';

%Get the File Name
%Position =[X Y length width]
uicontrol('style','text','Position',[565,453 60 20]','String','Filename:');
%The file names are the options here.
uicontrol('Style','popupmenu','position',[628 445 170 30],'Value',1,'String',files,'Callback',@displayfile);

%Select the Conversion type
uicontrol('style','text','Position',[565,415 90 20]','String','Image 
Conversion:');

%The options for image conversion are : Original Image, Convert to grayscale,
Convert to binary, Edge detected Image.
uicontrol('Style','popupmenu','position',[565 375 200 30],'Value',1,'String',{'Convert to GrayScale','Convert to Binary','Original Image','Edge detected Image'},'Callback',@convert);

%Select the edge type
T=uicontrol('Style','popupmenu','position',[565 305 200 30],'Value',1,'String',{'Canny','Sobel','Prewitt','log'},'callback',@findedge);
F=uicontrol('style','text','Position',[565,345 90 20]','String','Edge Type:');

%These components can be made visible or invisible
set(T,'Visible','off');
set(F,'Visible','off');

function displayfile(obj,eve)
        ptr=get(obj,'value');
        filename=char(files(ptr));
        Img=imread(filename);
        imshow(Img);
end

%Switch case
%Value 1 performs the conversion of the orginal image to grayscale image
%Value 2 performs the conversion of the orginal image to binary image
%Value 3 shows the orginal image
%Value 4 performs the edge detection of the image based on the type.




    function convert(obj,eve)
        ptr=get(obj,'value');
        switch ptr
            case {1}
                set(T,'Visible','off');
                set(F,'Visible','off');
                I=rgb2gray(Img);
                imshow(I);
            case {2}
                set(T,'Visible','off');
                set(F,'Visible','off');
                I=im2bw(Img);
                imshow(I);
            case {3}
                set(T,'Visible','off');
                set(F,'Visible','off');
                imshow(Img);
            case {4}
            set(T,'Visible','on');
            set(F,'Visible','on');
   
                           
        end
    end

                                                              
                                                             
                                                              


                                      
                                 
                                      
                                     


       
  %To perform edge detection based on the type
    function findedge(obj,event)
            ptr=get(obj,'value');
            B=rgb2gray(Img);
            switch ptr
                case {1}
                    imshow(~edge(B,'canny'));
                case {2}
                    imshow(~edge(B,'sobel'));
                case {3}
                    imshow(~edge(B,'Prewitt'));
                case {4}
                    imshow(~edge(B,'log'));
                   
            end
           
           
        end
               
   
end




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