0

有一个原始图像test1.jpg。问题是在图像中显示轴并保持图像质量。我正在运行以下代码,该代码取自matlab、图像像素和实际距离的多轴或比例尺

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,但它不会在图像中保存轴。

请指教,我该如何解决这些问题。我将非常感谢任何帮助。

原始图像和获得的图像附在此消息中。 原始图像

获得的图像

4

2 回答 2

1

在此处输入图像描述原则上你可以用 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);
于 2019-04-03T09:48:17.003 回答
0

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

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')

请在附件中找到获得的图像。获得的图像

于 2019-04-02T12:48:34.563 回答