如何在 Matlab 上同时旋转图像和轴? [英] How to rotate image and axes together on Matlab?

查看:51
本文介绍了如何在 Matlab 上同时旋转图像和轴?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码 1,其中垂直和/或水平翻转不影响 axes();代码 2 提出的解决方案没有产生预期的输出

全部关闭;清除所有;cl;x = [5 8];y = [3 6];C = [0 2 4 6;8 10 12 14;16 18 20 22];C2 = C(:,结束:-1:1,:);%# 水平翻转C3 = C(结束:-1:1,:,:);%# 垂直翻转C4 = C(结束:-1:1,结束:-1:1,:);%# 水平+垂直翻转% https://stackoverflow.com/a/4010203/54964子图(2,2,1),图像(x,y,C)子图(2,2,2),图像(x,y,C2)子图(2,2,3),图像(x,y,C3)子图(2,2,4),图像(x,y,C4)%% 旋转轴()不成功% https://stackoverflow.com/a/15071734/54964数字子图(2,2,1),图像(x,y,C)x = linspace(1, size(C, 2), 数字(x));% 只反转 x设置(gca,'XTick',x,'XTickLabel',x)子图(2,2,2),图像(x,y,C2)x = linspace(1, size(C, 2), 数字(x));% 反向 x设置(gca,'XTick',x,'XTickLabel',x)%反向yy = linspace(1, size(C, 1), numel(y));设置(gca,'YTick',y,'YTickLabel',翻转(y(:)))子图(2,2,3),图像(x,y,C3)x = linspace(1, size(C, 2), 数字(x));% 现在 x,y 都反转了设置(gca,'XTick',x,'XTickLabel',x)子图(2,2,4),图像(x,y,C4)

图.1 轴保持不变但图像正确翻转的输出,图 2 尝试移动 xticks/yticks

的输出

预期输出:

  • 图 1(左上角)在带有图的轴上全部正确
  • 图 2(右上)y 轴正确,但 x 轴从 8 到 5
  • 图 3(左下)y 轴从 6 到 3 但 x 轴正确
  • 图 4(右下)y 轴正确,但 x 轴从 3 到 6

尝试 2

代码

% 1 向量开始 2 向量结束 3 向量长度数字子图(2,2,1),图像(x,y,C)x = linspace(大小(C, 2), 1, 数字(x));% 只反转 x设置(gca,'XTick',x,'XTickLabel',x)子图(2,2,2),图像(x,y,C2)x = linspace(1, size(C, 2), 数字(x));% 反向 x设置(gca,'XTick',x,'XTickLabel',x)y = linspace(大小(C, 1), 1, 数字(y));% 反向 y设置(gca,'YTick',y,'YTickLabel',翻转(y(:)))子图(2,2,3),图像(x,y,C3)x = linspace(大小(C, 2), 1, 数字(x));% 现在 x,y 都反转了设置(gca,'XTick',x,'XTickLabel',x)y = linspace(1, size(C, 1), numel(y));% 反向 y设置(gca,'YTick',y,'YTickLabel',翻转(y(:)))子图(2,2,4),图像(x,y,C4)

输出

使用 matlab.graphics.axis.Axes/set 时出错在设置 'Axes' 的 'XTick' 属性时:值必须是单或双类型的向量,其值会增加test_imagesc_subplot_figure 中的错误(第 26 行)设置(gca,'XTick',x,'XTickLabel',x)

Eskapp 的提议

我没有成功执行以下操作,但图 2 没有变化;第一行数字保持与 xaxis 相同的递增顺序;我也试过代替 reverse - normal

图子图(2,2,1),图像(x,y,C)x = linspace(1, size(C, 2), 数字(x));% 只反转 x设置(gca,'xdir','反向')子图(2,2,2),图像(x,y,C2)

  • 图1和图2的输出axis保持不变

学习EBH的

Matlab:2016a
操作系统:Debian 8.5 64 位
硬件:华硕 Zenbook UX303UA

解决方案

如果我正确理解你的问题,那么这段代码就是你要找的:

x = 5:8;y = 3:6;C =重塑(0:2:22,4,3).';C2 = 翻转(C);% 水平翻转C3 = 翻转(C);% 垂直翻转C4 = rot90(C,2);% 水平+垂直翻转% 答案从这里开始:子图(2,2,1),图像(x,y,C)设置(gca,'XTick',x,'XTickLabel',x,...'YTick',y,'YTickLabel',y)子图(2,2,2),图像(x,y,C2)设置(gca,'XTick',x,'XTickLabel',fliplr(x),...'YTick',y,'YTickLabel',y)子图(2,2,3),图像(x,y,C3)设置(gca,'XTick',x,'XTickLabel',x,...'YTick',y,'YTickLabel',fliplr(y))子图(2,2,4),图像(x,y,C4)设置(gca,'XTick',x,'XTickLabel',fliplr(x),...'YTick',y,'YTickLabel',fliplr(y))

结果:

我将 xy 从 2 元素向量更改为您,但它也适用于:

x = [5 8];y = [3 6];

顺便说一句...

不用操作C并创建C2...C4,你可以写:

子图 221, imagesc(x,y,C)子图 222,imagesc(fliplr(x),y,C)子图 223,imagesc(x,fliplr(y),C)子图 224,imagesc(fliplr(x),fliplr(y),C)

并像以前一样在每次调用 subplot 之后在轴上添加操作.

<小时>

编辑:

使用向量的大小和限制:

x = linspace(0,10,6);y = linspace(0,180,19);% 不需要绘制每个标签N = 3613;C = diag(1:N)*ones(N)+rot90(diag(1:N)*ones(N));% 一些任意矩阵

上面的所有其余代码都保持不变,我得到以下结果:

Code 1 where flipping vertically and/or horizontally does not affect axes(); Code 2 where proposed solution does not yield the expected output

close all; clear all; clc;
x = [5 8];
y = [3 6];
C = [0 2 4 6; 8 10 12 14; 16 18 20 22];
C2 = C(:,end:-1:1,:);           %# horizontal flip
C3 = C(end:-1:1,:,:);           %# vertical flip
C4 = C(end:-1:1,end:-1:1,:);    %# horizontal+vertical flip

% https://stackoverflow.com/a/4010203/54964
subplot(2,2,1), imagesc(x,y,C)
subplot(2,2,2), imagesc(x,y,C2)
subplot(2,2,3), imagesc(x,y,C3)
subplot(2,2,4), imagesc(x,y,C4)

%% Rotations of axes() unsuccessfully 
% https://stackoverflow.com/a/15071734/54964
figure
subplot(2,2,1), imagesc(x,y,C)
x = linspace(1, size(C, 2), numel(x)); % reverse only x
set(gca, 'XTick', x, 'XTickLabel', x)
subplot(2,2,2), imagesc(x,y,C2)

x = linspace(1, size(C, 2), numel(x)); % reverse back x
set(gca, 'XTick', x, 'XTickLabel', x)  % reverse y
y = linspace(1, size(C, 1), numel(y));
set(gca, 'YTick', y, 'YTickLabel', flipud(y(:)))
subplot(2,2,3), imagesc(x,y,C3)

x = linspace(1, size(C, 2), numel(x)); % now both x,y reversed
set(gca, 'XTick', x, 'XTickLabel', x)
subplot(2,2,4), imagesc(x,y,C4)

Fig. 1 Output where axis stay untouched but images are flipped correctly, Fig. 2 Output from attempt with moving xticks/yticks

Expected output:

  • Fig.1 (top-left) all correct in axes with figure
  • Fig.2 (top-right) y-axis correct but x-axis from 8 to 5
  • Fig.3 (lower-left) y-axis from 6 to 3 but x-axis correct
  • Fig.4 (lower-right) y-axis correct but x-axis from 3 to 6

Attempt 2

Code

% 1 start of vector 2 end of vector 3 length of vector 
figure
subplot(2,2,1), imagesc(x,y,C)
x = linspace(size(C, 2), 1, numel(x)); % reverse only x
set(gca, 'XTick', x, 'XTickLabel', x)
subplot(2,2,2), imagesc(x,y,C2)

x = linspace(1, size(C, 2), numel(x)); % reverse back x
set(gca, 'XTick', x, 'XTickLabel', x)  
y = linspace(size(C, 1), 1, numel(y)); % reverse y
set(gca, 'YTick', y, 'YTickLabel', flipud(y(:)))
subplot(2,2,3), imagesc(x,y,C3)

x = linspace(size(C, 2), 1, numel(x)); % now both x,y reversed
set(gca, 'XTick', x, 'XTickLabel', x)
y = linspace(1, size(C, 1), numel(y)); % reverse y
set(gca, 'YTick', y, 'YTickLabel', flipud(y(:)))
subplot(2,2,4), imagesc(x,y,C4)

Output

Error using matlab.graphics.axis.Axes/set
While setting the 'XTick' property of 'Axes':
Value must be a vector of type single or double whose values increase

Error in test_imagesc_subplot_figure (line 26)
set(gca, 'XTick', x, 'XTickLabel', x)

Eskapp's proposal

I do unsuccessfully the following but no change on Fig. 2; the first row of figures stay in the same increasing order of xaxis; I also tried instead of reverse - normal

figure
subplot(2,2,1), imagesc(x,y,C)
x = linspace(1, size(C, 2), numel(x)); % reverse only x
set(gca,'xdir','reverse')
subplot(2,2,2), imagesc(x,y,C2)

  • Output of Fig. 1 and Fig. 2 axis stay the same

Studying EBH's answer

Output in the y-axis label when using set(gca,'XTick',x,'XTickLabel',x, 'YTick',y,'YTickLabel',fliplr(y)) with variables y=linspace(0,180,181); x=0:0.5:10

Matlab: 2016a
OS: Debian 8.5 64 bit
Hardware: Asus Zenbook UX303UA

解决方案

If I correctly understand your question, then this code does what you look for:

x = 5:8;
y = 3:6;
C = reshape(0:2:22,4,3).';
C2 = fliplr(C); % horizontal flip
C3 = flipud(C); % vertical flip
C4 = rot90(C,2); % horizontal+vertical flip

% the answer starts here:
subplot(2,2,1), imagesc(x,y,C)
set(gca,'XTick',x,'XTickLabel',x,...
     'YTick',y,'YTickLabel',y)
subplot(2,2,2), imagesc(x,y,C2)
set(gca,'XTick',x,'XTickLabel',fliplr(x),...
     'YTick',y,'YTickLabel',y)
subplot(2,2,3), imagesc(x,y,C3)
set(gca,'XTick',x,'XTickLabel',x,...
     'YTick',y,'YTickLabel',fliplr(y))
subplot(2,2,4), imagesc(x,y,C4)
set(gca,'XTick',x,'XTickLabel',fliplr(x),...
     'YTick',y,'YTickLabel',fliplr(y))

the result:

I changed you x and y from 2-element vectors, but it works also if:

x = [5 8];
y = [3 6];

BTW...

Instead of manipulating C and create C2...C4, you can just write:

subplot 221, imagesc(x,y,C)
subplot 222, imagesc(fliplr(x),y,C)
subplot 223, imagesc(x,fliplr(y),C)
subplot 224, imagesc(fliplr(x),fliplr(y),C)

and add the manipulation on the axis after each call to subplot like before.


Edit:

Using your sizes and limits of the vectors:

x = linspace(0,10,6);
y = linspace(0,180,19); % no need to plot each label
N = 3613;
C = diag(1:N)*ones(N)+rot90(diag(1:N)*ones(N)); % some arbitrary matrix 

where all the rest of the code above remains the same, I get the following result:

这篇关于如何在 Matlab 上同时旋转图像和轴?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆