如何将加载的图像传递给matlab中的另一个功能按钮?

时间:2017-07-31 04:22:36

标签: image matlab m

将图像加载到Matlab GUI后,如何使用按钮将该图像传递给另一个功能按钮?当我按下GUI中的按钮时,图像应该传递给我的Matlab代码。

这是我的GUI代码

% --- 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)
[nama_file, nama_path]=uigetfile(... L3
    {'*.bmp;*.jpg','File Citra(*.bmp,*.jpg)';
    '*.bmp','File Bitmap(*.bmp)';...
    '*.jpg','File jpeg (*.jpg)';
    '*.*','Semua File(*.*)'},...
    'Pilih Citra');
if ~isequal(nama_file,0)
    handles.dat=imread(fullfile(nama_path,nama_file));
    guidata(hObject, handles);
    handles.current_dat=handles.dat;
    axes(handles.axes1);
    imshow (handles.current_dat);
else
return;
end

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

searchImageHist('redflower.jpg', 'model1Hist', 10);

我想要的是searchImageHist不是图像文件名而是来自button1 / axes1的图片。 顺便说一下这是searchImageHist.m的代码

function searchImageHist(imageName, modelName, nResults)

% function searchImageHist(imageName, modelName, nResults)
% Image retrieval m-file
%
% 
%

% load train model:
load(modelName);

% compute 3-D image histograms (HSV color space):
fprintf('Computing 3-D (HSV) histogram for query image...\n');
[Hist, RGBQ] = getImageHists(imageName);

% number of training samples:
Nfiles = length(Hists);

% decision thresholds:
t = 0.010;
t2 = 0.8;

fprintf('Searching...\n');

range = 0.0:0.1:1.0;
rangeNew = 0.0:0.05:1.0;
[x,y,z]    = meshgrid(range);
[x2,y2,z2] = meshgrid(rangeNew);

Hist = interp3(x,y,z,Hist,x2,y2,z2);

Similarity = zeros(Nfiles, 1);

for (i=1:Nfiles) % for each file in database:

    % compute (normalized) eucledean distance for all hist bins:
    HistT = interp3(x,y,z,Hists{i},x2,y2,z2);
    DIFF = abs(Hist-HistT) ./ Hist;

    % keep distance values for which the corresponding query image's values
    % are larger than the predefined threshold:    
    DIFF = DIFF(Hist>t);

    % keep error values which are smaller than 1:
    DIFF2 = DIFF(DIFF<t2);
    L2 = length(DIFF2);

    % compute the similarity meaasure:
    Similarity(i) = length(DIFF) * mean(DIFF2) / (L2^2);

    % (interface): plot images with small similarity measures:
    plotThres = 0.5 * 10 / length(DIFF);
    if (Similarity(i) < plotThres)
%        fprintf('%70s %5.2f %5d %5d\n', files{i}, median(DIFF2),
%        length(DIFF), L2);
        subplot(2,2,1);imshow(RGBQ);
        title('Query image');        
        RGB = imread(files{i});
        subplot(2,2,2);imshow(RGB);
        title('A similar image ... Still Searching ...');        
        subplot(2,2,3);
        plot(DIFF)

        if (length(DIFF2)>1)
            subplot(2,2,4); plot(DIFF2);
            axis([1 length(DIFF2) 0.2 1])
        end
        drawnow
    end
end

% find the nResult "closest" images:
[Sorted, ISorted] = sort(Similarity);

NRows = ceil((nResults+1) / 3);

% plot query image:
subplot(NRows,3,1); imshow(RGBQ); title('Query Image');

% ... plot similar images:
for (i=1:nResults)
    RGB = imread(files{ISorted(i)});
    str = sprintf('Im %d: %.3f',i,100*Sorted(i));
    subplot(NRows,3,i+1); imshow(RGB);  title(str);
end

fprintf('Done\n');

0 个答案:

没有答案