保存直方图

% --------------------------------------------------------------------
function SaveHist_Callback(hObject, eventdata, handles)

% hObject    handle to SaveHist (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% 直方图对比
if isequal(handles.Img1, 0)
    msgbox('请载入图像!', '提示信息');
    return;
end
if isequal(handles.Img2, 0)
    msgbox('请进行去雾处理!', '提示信息');
    return;
end
figure('Name', '直方图对比', 'NumberTitle', 'Off', ...
    'Units', 'Normalized', 'Position', [0.1 0.1 0.5 0.5]);
subplot(2, 2, 1); imshow(handles.Img1); title('原图像', 'FontWeight', 'Bold');
subplot(2, 2, 2); imshow(handles.Img2); title('处理后的图像', 'FontWeight', 'Bold');
Q = rgb2gray(handles.Img1);
W = rgb2gray(handles.Img2);
subplot(2, 2, 3); imhist(Q, 64); title('原灰度直方图', 'FontWeight', 'Bold');
subplot(2, 2, 4); imhist(W, 64); title('处理后的灰度直方图', 'FontWeight', 'Bold');

f = getframe(gcf);
f  = frame2im(f);
SaveHistImage(f);
function SaveHistImage(Img)
imagesPath = '.\\hist_results';
if ~exist(imagesPath, 'dir')
    mkdir(imagesPath);
end

[FileName,PathName,FilterIndex] = uiputfile({'*.jpg;*.tif;*.png;*.gif','All Image Files';...
          '*.*','All Files' },'保存直方图',...
          '.\\hist_results\\result.jpg');
if isequal(FileName, 0) || isequal(PathName, 0)
    return;
end
fileStr = fullfile(PathName, FileName);
imwrite(Img, fileStr);
msgbox('处理结果保存成功!', '提示信息');
    原文作者:Jakai
    原文地址: https://www.jianshu.com/p/48516264e2a2
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞