MATLAB 降低图像质量

MATLAB degrades image quality

有一张原图test1.jpg。问题是在图像中显示轴并保持图像质量。 我 运行 以下代码取自 :

img = imread('test1.jpg');

% define variables
imgsize1 = size(img,1);  % size on screen
imgsize2 = size(img,2);
xreal = 1;      % meter
yreal = 1;      % meter

% create figure
figure('Position',[0,0,imgsize1,imgsize2]);

% pixel axes and actual plot
a=axes('Position',[.2 .2 .7 .7]);
set(a,'Units','normalized');
iptsetpref('ImshowAxesVisible','on');
imshow(img,'Parent',a);

% real world axis (below)
b=axes('Position',[.2 .1 .7 1e-12]);
set(b,'Units','normalized');
set(b,'Color','none');
set(b,'xlim',[0 xreal]);

% real world axis (left)
c=axes('Position',[.09 .2 1e-12 .7 ]);
set(c,'Units','normalized');
set(c,'Color','none');
set(c,'ylim',[0 yreal],'YDir','reverse');

% set labels
xlabel(a,'Pixels')
xlabel(b,'Real distance (m)')
ylabel(a,'Pixels');
ylabel(c,'Real distance (m)');
saveas(gcf,'test2.jpg');

1) 获得的图像 test2.jpg 质量很差 - 它变得强烈像素化。 2) 横轴比图片大

我尝试使用 imwrite,但它没有在图像中保存坐标轴。

请指教,我该如何解决这些问题。 如果有任何帮助,我将不胜感激。

原始和获得的图像附在本邮件中。 original image

obtained image

另一个论坛上的一个人提出了解决水平轴问题的代码,但最终图像的分辨率仍然很差。这是代码:

img = imread('test1.jpg');

% define variables
imgsize1 = size(img,1);  % size on screen
imgsize2 = size(img,2);
xreal = 1;      % meter
yreal = 1;      % meter

% create figure
h = figure(); % !!!


% pixel axes and actual plot
a=axes('Position',[.2 .2 .7 .7]);
set(a,'Units','normalized');
iptsetpref('ImshowAxesVisible','on');

im = image(img); % !!!

% real world axis (below)
b=axes('Position',[.2 .1 .7 0]);
set(b,'Units','normalized');
set(b,'Color','none');
set(b,'xlim',[0 xreal]);

% real world axis (left)
c=axes('Position',[.09 .2 1e-12 .7 ]);
set(c,'Units','normalized');
set(c,'Color','none');
set(c,'ylim',[0 yreal],'YDir','reverse');

% set labels
xlabel(a,'Pixels')
xlabel(b,'Real distance (m)')
ylabel(a,'Pixels');
ylabel(c,'Real distance (m)');

print('test4','-djpeg','-r300')

获取的图片请见附件。obtained image

原则上你可以用imwrite做你想做的事。问题是您必须将所有轴写入图像数据的矩形中。该解决方案制作了一个棕色蒙版,稍后将用地图图像填充。您可以看到那里有些不准确,因为最终输出中保留了一些棕色像素。希望这能给你一个线索。

% read map
img = imread('test1.jpg');
imgsize1 = size(img,1);  % size on screen
imgsize2 = size(img,2);
% create figure small enough for the screen
h = figure('Position',[0,0,imgsize2/3,imgsize1/3],'units','pixels');
a=axes('Position',[.2 .2 .7 .7]);
iptsetpref('ImshowAxesVisible','on');
% make a brown rectangle to mark map location in figure
pix = [];
pix(1,1,1) = 200;
pix(1,1,2) = 50;
pix(1,1,3) = 50;
img1 = uint8(repmat(pix,imgsize1,imgsize2,1));
imshow(img1,'Parent',a);
set(a,'Units','normalized','fontsize',13);
%iptsetpref('ImshowAxesVisible','on');
b=axes('Position',[.2 .1 .7 1e-12]);
set(b,'xlim',[0 1],'Color','none','fontsize',13);
c=axes('Position',[.09 .2 1e-12 .7 ]);
set(c,'ylim',[0 1],'YDir','reverse','Color','none','fontsize',13);
% set labels
xlabel(a,'Pixels')
xlabel(b,'Real distance (m)')
ylabel(a,'Pixels');
ylabel(c,'Real distance (m)');
% save brown map
saveas(h,'test2.jpg');
img2 = imread('test2.jpg');
% find map size in saved image
x = find(img2(:,700,1) == 200,1,'last')-find(img2(:,700,1) == 200,1);
y = find(img2(700,:,1) == 200,1,'last')-find(img2(700,:,1) == 200,1);
ratio = (imgsize1/x + imgsize2/y)/2;
% correct size of image
img3 = imresize(img2,ratio);
% find map coord in image
x3 = find(img3(:,1700,1) == 200,1);
y3 = find(img3(1700,:,1) == 200,1);
% fill brown rectangle with real map
img3(x3:x3+imgsize1-1,y3:y3+imgsize2-1,:) = img;
imwrite(img3,'test3.jpg')
figure;
imshow(img3);