Matlab Proje Ornegi

Matlab programlama kullanarak bir fotoğrafın pixel ayarlarını değiştiren script kodları aşağıdadır. Kodları kopyalayıp matlab programında "Ctrl+N" tuşuna basıp yeni scipt oluşturma sayfasına yapıştırın. Script'i Ezresize diye bir dosya açıp kaydedin ve o dosyanın içine car.jpg adında bir fotoğraf koyup matlab de "Ezresize" yazıp çağırın.. 

function EZresize

%Ezresize script'i size linerenterpolasyon algoritması yolu ile
%fotoğrafları kolay ve çabukça yeniden boyutlandırma yani
%pixeli ile oynama şansı verir
%kodlama :"Joe Blaine --- Image Resizing"

close all
clc

%Değişkenler


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

%Kolları

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=genişlik
        % H=yukseklik
        % 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

%Credits: Andreas Klimke, for an image resizing algorithm

{ 0 yorum... Skip ke Kotak Komentar }

Tambahkan Komentar Anda

 

Blogger news

Blogroll

About

Muhterem Blog © 2012 | Template By Jasriman Sukri