将图保存到新图像

时间:2017-06-05 06:25:28

标签: matlab image-processing

我已从图像中删除了线条,我想将其保存(图)到新图像中。

删除了行:

Filled by White Rectangle

所以这是我的代码。

clc;              % Clear the command window.
close all;        % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures if you have the Image Processing Toolbox.
clear;            % Erase all existing variables. Or clearvars if you want.
workspace;        % Make sure the workspace panel is showing.
format short g;
format compact;
fontSize = 20;

grayImage = imread('tab1.png');
% Save this figure handle.
hFig1 = gcf;
% Get the dimensions of the image.  
% numberOfColorBands should be = 1.
[rows, columns, numberOfColorBands] = size(grayImage);
if numberOfColorBands > 1
    % It's not really gray scale like we expected - it's color.
    % Convert it to gray scale by taking only the green channel.
    grayImage = grayImage(:, :, 2); % Take green channel.
end

binaryImage = grayImage < 240;
binaryImage(: ,1: 20) = false;
imshow(grayImage, []);
hold on;
cc = bwconncomp(binaryImage);
% Measure the bounding box of all blobs.
measurements = regionprops(cc, 'BoundingBox');
fprintf('Found %d regions\n', cc.NumObjects);
numSkinnyRegions = 0;
bboxes=cat(1,measurements.BoundingBox);
for k = 1 : cc.NumObjects
    figure(hFig1); % Switch to figure 1.
    print(gcf,'Figuree','-dpng');
    thisBB = measurements(k).BoundingBox
    % Draw a box around the region in cyan.
    hRect = rectangle('Position', thisBB, 'EdgeColor', 'c', 'LineWidth', 1);
    aspectRatio(k) = thisBB(4)/thisBB(3);
    if (thisBB(4) <= 3 || thisBB(3) <= 3) && (aspectRatio(k) > 4 || aspectRatio(k) < 1/4)
        numSkinnyRegions = numSkinnyRegions + 1;
        % Save it to a cell array, just in case we want to use it after the loop is done.
        croppedImages{numSkinnyRegions} = imcrop(binaryImage, thisBB);      
        % Draw skinny regions in a different color
        delete(hRect); % Get rid of old one.
        hRect = rectangle('Position', thisBB);
        hRect.FaceColor = 'w';
        hRect.EdgeColor = 'w';
        hRect.LineWidth = 2;
   end
end

figure; imshow(grayImage) 

当我将图形保存到png文件中时,结果是图像包含黑色线条,与我在图中的不同。 saved image

那么我的代码出了什么问题?我是否将代码print(gcf,'Figuree','-dpng');置于错误的行中?

1 个答案:

答案 0 :(得分:-1)

好的,现在我得到了自己的答案!只需使用mathworks file exchange中的export_fig即可。干杯!