Showing posts with label MATLAB. Show all posts
Showing posts with label MATLAB. Show all posts

Sunday, 13 December 2015

Image Search using Guide in MATLAB

Inorder to run the below code , place the images of actors in actors folder,
images of actress in actress folder,images of dogs in dogs folder and place all these folders on your computer desktop

Type Guide in matlab and create a GUI as shown below and save it on desktop



Run it,
You will get below code in green automatically generated
Copy and paste the code in black to the automatically generated code.
run the code in MATLAB
 function varargout = untitled(varargin)
% UNTITLED MATLAB code for untitled.fig
%      UNTITLED, by itself, creates a new UNTITLED or raises the existing
%      singleton*.
%
%      H = UNTITLED returns the handle to a new UNTITLED or the handle to
%      the existing singleton*.
%
%      UNTITLED('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in UNTITLED.M with the given input arguments.
%
%      UNTITLED('Property','Value',...) creates a new UNTITLED or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before untitled_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to untitled_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help untitled

% Last Modified by GUIDE v2.5 13-Dec-2015 00:03:51

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @untitled_OpeningFcn, ...
                   'gui_OutputFcn',  @untitled_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before untitled is made visible.
function untitled_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to untitled (see VARARGIN)

% Choose default command line output for untitled
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes untitled wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = untitled_OutputFcn(hObject, eventdata, handles)
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;



function edit1_Callback(hObject, eventdata, handles)
% hObject    handle to edit1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit1 as text
%        str2double(get(hObject,'String')) returns contents of edit1 as a double


% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

first=get(handles.edit1,'string');
foldername=strcat('C:\Users\s\Desktop\',first,'\'); % type the pathname where the folders are located
imname=strcat(foldername,'\*.png');%images are in .png format if u r images are in other formats change this
n = length(imname);   
imgs=dir(imname);
filename=strcat(foldername,'\',imgs(1).name);
Cval = imread(filename);
handles.C = Cval;
axes(handles.axes1);
imshow(Cval);
handles.output = hObject;
guidata(hObject, handles);

filename2=strcat(foldername,'\',imgs(2).name);
Cval = imread(filename2);
handles.C = Cval;
axes(handles.axes2);
imshow(Cval);
handles.output = hObject;
guidata(hObject, handles);

=====end of code======================


Now type actress in the edit box and click on Search image button
You will get output as shown below


Saturday, 12 July 2014

making a video out of images in matlab

=====================In older versions of MATLAB========================
samples_dir='C:/data'; % data is folder name that has images
files = dir([samples_dir , '\*.jpg']);
aviObj = avifile('C:\test.avi', 'Compression', 'MSVC'); %test.avi is the name of the video and gets stored
%addframe cannot be used for true color images, so convert to gray scale and then add 
%video quality is not good
for i=1:length(files)
f_bmp=[samples_dir '\' files(i).name];
img=imread(f_bmp);
gr=rgb2gray(img);
aviObj = addframe(aviObj, gr);
end
aviObj=close(aviObj);

===================In newer versions of MATLAB===========================
 clear all;
close all;
samples_dir='C:/data'; % data is folder name that has images

Files = dir([samples_dir , '\*.jpg']);

VideoFile='C:\data\MyVideo'; %path of the video file to be saved
writerObj = VideoWriter(VideoFile);
writerObj.FrameRate =1; %increase the frame rate, here i have used 1 frame per second
open(writerObj);
for t= 1:length(Files)
Frame=imread(strcat('C:\data','\',Files(t).name));
writeVideo(writerObj,im2frame(Frame));
end
close(writerObj);

%To play the video

videoFReader = vision.VideoFileReader('C:\data\MyVideo.avi');
videoPlayer = vision.VideoPlayer;
while ~isDone(videoFReader)
   frame = step(videoFReader);
   step(videoPlayer,frame);
end
release(videoFReader);
release(videoPlayer);
                                                  (or)
implay('C:/data/MyVideo.avi');
=============================================================================
Program to read images in a directory as a video
===========================================================================
samples_dir='C:/data'; % data is folder name that has images
files = dir([samples_dir , '\*.jpg']);
hVPlayer=vision.VideoPlayer;
for i=1:length(files)
f_bmp=[samples_dir '\' files(i).name];
img=imread(f_bmp);
step(hVPlayer,img);
end
release(hVPlayer);

====Reading a 'mpg' video inbuilt in MATLAB and displaying the 36th,72nd,108th and 141th frame.======
clear all;
close all;
videobj=mmreader('xylophone.mpg');
numframes=get(videobj,'numberofframes');

x=read(videobj,36);
y=read(videobj,72);
z=read(videobj,108);
k=read(videobj,141);
subplot(221);imshow(x);title('36th frame');
subplot(222);imshow(y);title('72th frame');
subplot(223);imshow(z);title('108th frame');
subplot(224);imshow(k);title('141th frame');
=====================================================================