⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 affinetransformation.m

📁 具体实现三维图像的仿射变换
💻 M
📖 第 1 页 / 共 2 页
字号:
% Hint: edit controls usually have a white background on Windows.%       See ISPC and COMPUTER.if ispc    set(hObject,'BackgroundColor','white');else    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));endfunction ytrans_Callback(hObject, eventdata, handles)% hObject    handle to ytrans (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 ytrans as text%        str2double(get(hObject,'String')) returns contents of ytrans as a double% --- Executes on button press in btrans.function btrans_Callback(hObject, eventdata, handles)% hObject    handle to btrans (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    structure with handles and user data (see GUIDATA)% Hint: get(hObject,'Value') returns toggle state of btrans% --- Executes during object creation, after setting all properties.function yscale_CreateFcn(hObject, eventdata, handles)% hObject    handle to yscale (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    set(hObject,'BackgroundColor','white');else    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));endfunction yscale_Callback(hObject, eventdata, handles)% hObject    handle to yscale (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 yscale as text%        str2double(get(hObject,'String')) returns contents of yscale as a double% --- Executes on button press in bscale.function bscale_Callback(hObject, eventdata, handles)% hObject    handle to bscale (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    structure with handles and user data (see GUIDATA)% Hint: get(hObject,'Value') returns toggle state of bscale% --- Executes on button press in Transform.function Transform_Callback(hObject, eventdata, handles)% hObject    handle to Transform (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    structure with handles and user data (see GUIDATA)global gg gc;
gc=0;
h=guidata(gcbo);
if  gg~=2 
    h=guidata(gcbo);
    image_1=h.image_1;  % Get Image 1
    hb(1)=num_check(h.rot); % Rotation
    hb(2)=num_check(h.xscale); % Scale in x-direction
    hb(3)=num_check(h.yscale); % Scale in y-direction
    hb(4)=num_check(h.xshear); % Shear
    hb(5)=num_check(h.yshear);
    hc=sum(hb);
    switch hc
        case 0
            a=str2num(get(h.xscale,'String'));  
            b=str2num(get(h.xshear,'String'));
            c=str2num(get(h.yshear,'String'));
            d=str2num(get(h.yscale,'String'));
            th=str2num(get(h.rot,'String'));  %2*pi/10;
            ch=get(h.deg,'Value');
            switch ch
                case 0
                    th=th;
                otherwise
                    th=th*pi/180;;
            end   
            k=1;
            TT=k*[cos(th) -sin(th); sin(th) cos(th)]*[a b; c d];
            TTT=[ 1 0 0; 0 1 0; 0 0 1];
            TTT(1:2,1:2)=TT;
            set(h.message_text,'String','Transformaing .... Please Wait');
            tform=maketform('affine',TTT);
            [image_2] = imtransform(image_1,tform);
            h.image_2=image_2;
            axes(h.axes2);
            set(h.figure2_title,'Visible','on');
            imshow(image_2,[]);
            gc=1;
            axes(h.axes2);
            g2=h.axis12;
            switch g2
                case 0
                    axes(h.axes1);   
                    axis on;
                    axes(h.axes2);   
                    axis on;
                otherwise
                    axes(h.axes1);   
                    axis off;
                    axes(h.axes2);   
                    axis off;
            end
            set(h.message_text,'String','Done');
            set(h.Showaxes,'Visible','on');
            guidata(gcbo,h);    
        otherwise
            set(h.message_text,'String','Invalid value: enter a real number only');
    end
end

% --- Executes on button press in Save_image.function Save_image_Callback(hObject, eventdata, handles)% hObject    handle to Save_image (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    structure with handles and user data (see GUIDATA)global gg gc;
h=guidata(gcbo);
set(h.message_text,'String',''); 
if   gc~=0
    dot='.';
    pathname=h.image_pathname;
    [filename, pathname] = uiputfile('*.*', 'Save Transformed Image');
    if filename~=0         try_again=find(filename==dot);
        if ~isempty(try_again) 
            image_ex=filename(try_again+1:end);
            valid_ex=['BMP'; 'TIF'; 'JPG'; 'PCX'; 'PNG'; 'HDF'; 'XWD'; 'RAS'; 'PBM'; 'PGM'; 'PPM'; 'PNM';];
            p=['\<' image_ex '\>'];
            g=regexpi(valid_ex,p);  % Match Exact extension, ignoring case.      
            b=cell2mat(g);           % Convert cell to matrix  
            if b~=0 & length(image_ex)==3
                  CMap=colormap(h.axes1);
                  h.savedimage_filename=filename;
                  h.savedimage_pathname=pathname;
                  imwrite(h.image_2,CMap,[pathname filename]);
                  savedto=['Image Saved to:    ' pathname filename];
                  set(h.message_text,'String',savedto); 
             else
                  set(h.message_text,'String','Invalid file extension: Image NOT saved, use one of these extensions, BMP TIF JPG PCX PNG HDF XWD RAS PBM PGM PPM PNM');      
            end
        else
            set(h.message_text,'String','Empty file extension: Image NOT saved, use one of these extensions, BMP TIF JPG PCX PNG HDF XWD RAS PBM PGM PPM PNM'); 
        end
   
    end
end% --- Executes on button press in pushbutton4.function pushbutton4_Callback(hObject, eventdata, handles)% hObject    handle to pushbutton4 (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    structure with handles and user data (see GUIDATA)close;% --- Executes during object creation, after setting all properties.function yshear_CreateFcn(hObject, eventdata, handles)% hObject    handle to yshear (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    set(hObject,'BackgroundColor','white');else    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));endfunction yshear_Callback(hObject, eventdata, handles)% hObject    handle to yshear (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 yshear as text%        str2double(get(hObject,'String')) returns contents of yshear as a doublefunction hb=num_check(ha);
% This function checks the validation of the entered transformation
% parameters
% ha is the handle to be checked
% hb is a flag, 1 for correct range, 0 for incorrect range.
h=guidata(gcbo);
ba=get(ha,'String');
bb=str2num(ba);
hb=0;
if isempty(bb) | ba=='i' | ba=='j'
    axes(h.axes2);
    cla;
    set(h.message_text,'String','Invalid value: enter a real number only');
    switch ha
        case h.xscale
            set(ha,'String','1');
        case h.yscale
            set(ha,'String','1');
        otherwise
            set(ha,'String','0');
    end
    hb=1;
    %break
end
%hb=ha;
%guidata(gcbo,h);% --- Executes on button press in Showaxes.function Showaxes_Callback(hObject, eventdata, handles)% hObject    handle to Showaxes (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    structure with handles and user data (see GUIDATA)h=guidata(gcbo);sh=get(h.Showaxes,'String');
switch sh
    case 'Show Axes'
        set(h.Showaxes,'String','Hide Axes');
        h.axis12=0;
    otherwise
        set(h.Showaxes,'String','Show Axes'); 
        h.axis12=1;
end
g1=get(h.axes1,'visible');
axes(h.axes1);
switch g1
    case 'off'
        axis on;
        set(h.axes1,'visible','on');
    otherwise
        axis off;
        set(h.axes1,'visible','off');
endg2=get(h.axes2,'visible');
axes(h.axes2);
switch g2
    case 'off'     
        axis on;
        set(h.axes2,'visible','on');
    otherwise
        axis off;
        set(h.axes2,'visible','off');
end;
guidata(gcbo,h);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -