Image resizing project in matlab (GUI)

EZresize allows you to resize an image easily and quickly by way of a
bilinear interpolation algorithm. Before running, place the images that
you wish to resize with in the folder that contains the program.
To run,
at the command prompt, enter EZresize. Once the program is opened, enter
the filename of the image (ex. myfirstpic.jpg) in your MatLab current
directory and click get image. Enter your image size in the height and
width boxes accordingly and, when ready, click 'Resize It!'. If you
decide you would like to save the image, enter a file name (ex:
myresizedpic.jpg) in the 'File Name' field. When ready, click 'Save it'
and your image will be saved in your current MatLab directory. To edit a
new image, close all picture windows, and then repeat the above process.



%%%%%%source code%%%%%%%%%%


close all
clear all
clc




figure_color=[1 1 1];
panel_color=[0.5 0.5 0.5];
entryField_color=[1 1 1];

%Handles

hFigure=figure(...
'Units','Pixels',...
'Position',[1000 300 198 348],...
'Toolbar','none',...
'MenuBar','none',...
'NumberTitle','off',...
'Color',[.5 .5 .5],...
'Name','EZresize {Cadence Software Deveolpment}');

hPanel=uipanel(...
'Parent', hFigure,...
'Units','Pixels',...
'Position',[0 0 200 350],...
'BackgroundColor',panel_color);

hImageChoice=uicontrol(...
'Style','Edit',...
'Parent',hPanel,...
'Units','Pixel',...
'Position',[10 250 180 25],...
'String','car.jpg',...
'BackgroundColor',entryField_color);

hButton=uicontrol(...
'Style','pushbutton',...
'Parent',hPanel,...
'Units','Pixels',...
'Position',[25 300 150 20],...
'String','Get Image',...
'BackgroundColor',panel_color,...
'Callback',@imageChoice_callback);

hImageChoiceText=uicontrol(...
'Style','Text',...
'Parent',hPanel,...
'Units','Pixels',...
'Position',[10 270 180 20],...
'String','Enter the name of your image(.jpg):',...
'BackgroundColor',panel_color,...
'HorizontalAlignment','Center');


hWidthEditBox=uicontrol(...
'Style','Edit',...
'Parent',hPanel,...
'Units','Pixel',...
'Position',[10 200 180 25],...
'String','300',...
'BackgroundColor',entryField_color);


hWidthText=uicontrol(...
'Style','Text',...
'Parent',hPanel,...
'Units','Pixels',...
'Position',[10 220 180 20],...
'String','Enter Desired Width (in Pixels)',...
'BackgroundColor',panel_color,...
'HorizontalAlignment','Center');

hHeightEditBox=uicontrol(...
'Style','Edit',...
'Parent',hPanel,...
'Units','Pixel',...
'Position',[10 150 180 25],...
'String','200',...
'BackgroundColor',entryField_color);

hHeightText=uicontrol(...
'Style','Text',...
'Parent',hPanel,...
'Units','Pixels',...
'Position',[10 170 180 20],...
'String','Enter Desired Height (in Pixels)',...
'BackgroundColor',panel_color,...
'HorizontalAlignment','Center');


hButton2=uicontrol(...
'Style','pushbutton',...
'Parent',hPanel,...
'Units','Pixels',...
'Position',[25 120 150 20],...
'String','Resize It!',...
'BackgroundColor',panel_color,...
'Callback',@resizeButton_callback);

hImageWriteEditBox=uicontrol(...
'Style','Edit',...
'Parent',hPanel,...
'Units','Pixel',...
'Position',[10 60 180 25],...
'String','',...
'BackgroundColor',entryField_color);

hButton3=uicontrol(...
'Style','pushbutton',...
'Parent',hPanel,...
'Units','Pixels',...
'Position',[25 30 150 20],...
'String','Save Image',...
'BackgroundColor',panel_color,...
'Callback',@imageWrite_callback);

hImageWriteText=uicontrol(...
'Style','Text',...
'Parent',hPanel,...
'Units','Pixels',...
'Position',[10 80 180 20],...
'String','Enter a filename',...
'BackgroundColor',panel_color,...
'HorizontalAlignment','Center');

handle_list=...
[hFigure,hPanel,hImageChoice,...
hButton,hButton2,hButton3,...
hImageChoiceText,...
hWidthEditBox,...
hWidthText,...
hHeightEditBox,...
hHeightText,...
hImageWriteEditBox,...
hImageWriteText];

set(handle_list,...
'Units','Normalized');

%Callback Functions

function imageChoice_callback(hObject,eventdata)

img=imread(get(hImageChoice,'String'),'jpg');

% W=width
% H=height
% C=3 for RGB color images

[H W C]=size(img);
oldsize=size(img);

hFigure=figure(...
'Units','Pixels',...
'Position',[200 200 W H],...
'Toolbar','none',...
'MenuBar','none',...
'NumberTitle','off',...
'Color',figure_color,...
'Name','EZresize {Cadence Software Deveolpment}');

hAxes=axes(...
'visible','off',...
'Units','Pixels',...
'Position',[0 0 W H],...
'NextPlot','ReplaceChildren',...
'XLimMode','Manual',...
'YLimMode','Manual',...
'XLim',[1 W],...
'YLim',[1 H],...
'YDir','Reverse');

imagesc(img);

setappdata(hFigure,'UserData',img);

end %imageChoice_callback

function resizeButton_callback(hObject, eventdata)

%Get data
img=getappdata(hFigure,'UserData');
img=double(img);
oldsize=size(img);
H=str2double(get(hWidthEditBox,'String'));
W=str2double(get(hHeightEditBox,'String'));
newsize=[W H];

%Routine

% Scaling factor
factor = (oldsize(1:2)-1)./(newsize-1);

% Create new grid (foundation for image)
u = 0:newsize(1)-1;
v = 0:newsize(2)-1;
[U, V] = ndgrid(u, v);

% Make a conection between the new grid and the old size
u = u.*factor(1) + 1;
v = v.*factor(2) + 1;

% Compute the location of each new point relative to one nearest
% neighbor of the original image
U = U.*factor(1); U = U - fix(U);
V = V.*factor(2); V = V - fix(V);

% Perform interpolation element by element
U = repmat(U, [1 1 3]);
V = repmat(V, [1 1 3]);
N = (V-1).*((U-1).*img(floor(u), floor(v), :) - ...
U.*img(ceil(u), floor(v), :)) - ...
V.*((U-1).*img(floor(u), ceil(v), :) - ...
U.*img(ceil(u), ceil(v), :));

N=uint8(N);

[H W C]=size(N);

hFigure=figure(...
'Units','Pixels',...
'Position',[100 100 W H],...
'Toolbar','none',...
'MenuBar','none',...
'NumberTitle','off',...
'Color',figure_color,...
'Name','Resized Image');

hAxes=axes(...
'visible','off',...
'Units','Pixels',...
'Position',[0 0 W H],...
'NextPlot','ReplaceChildren',...
'XLimMode','Manual',...
'YLimMode','Manual',...
'XLim',[1 W],...
'YLim',[1 H],...
'YDir','Reverse');

imagesc(N)

setappdata(hFigure,'UserData',N);

end %resizeButton_Callback

function imageWrite_callback(hObject, eventdata)

F=getappdata(hFigure,'UserData');
H=get(hImageWriteEditBox,'String')
imwrite(F,H)

end %imageWrite_callback


end %EZresize

No comments:

Post a Comment

 
;