如何在Matlab上一起旋转图像和轴? [英] How to rotate image and axes together on Matlab?
问题描述
代码1,其中垂直和/或水平翻转不会影响axes()
;
代码2中提出的解决方案未产生预期的输出
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)
图. 1输出,其中轴保持不变,但图像正确翻转,
图2尝试移动xticks
/yticks
Fig. 1 Output where axis stay untouched but images are flipped correctly,
Fig. 2 Output from attempt with moving xticks
/yticks
预期输出:
- 图1(左上)全部在带有图的轴上
- 图2(右上)的y轴正确,但x轴从8到5
- 图3(左下)的y轴从6变为3,但x轴正确
- 图4(右下)的y轴正确,但x轴从3到6.
代码
% 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)
输出
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的提议
我未成功完成以下操作,但未对图2进行任何更改;第一行数字按xaxis
的升序排列;我也尝试了reverse
-normal
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)
-
图1和图2
axis
的输出保持不变Output of Fig. 1 and Fig. 2
axis
stay the same将
set(gca,'XTick',x,'XTickLabel',x, 'YTick',y,'YTickLabel',fliplr(y))
与变量y=linspace(0,180,181); x=0:0.5:10
Matlab:2016a
操作系统:Debian 8.5 64位
硬件:华硕Zenbook UX303UAMatlab: 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))
结果:
我从2元素向量中更改了
x
和y
,但在以下情况下也可以使用I changed you
x
andy
from 2-element vectors, but it works also if:x = [5 8]; y = [3 6];
顺便说一句...
代替操纵
C
并创建C2
...C4
,您只需编写:Instead of manipulating
C
and createC2
...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)
并像以前一样在每次调用
subplot
之后在轴上添加操作.and add the manipulation on the axis after each call to
subplot
like before.修改:
使用向量的大小和限制:
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屋!