请高手帮忙:MATLAB程序 编写一个程序,要求实现下列算法:首先将图像分割成许多8X8的子图像

请高手帮忙:MATLAB程序 编写一个程序,要求实现下列算法:首先将图像分割成许多8X8的子图像,第1张

clear all;

close all;

clc;

I=double(imread('elainbmp'));%读入图像

imshow(uint8(I));%显示图像

[Mro,Nco]=size(I);%获得读入图像的大小

fun1=@fft2;   %获得fft变换函数的句柄

Imagefft=blkproc(I,[8,8],fun1);%图像块进行fft变换

Imtemp=double(zeros(Mro,Nco));%设置临时变量用于存处理后的图像值

for ii=1:8:Mro

    for jj=1:8:Nco

        Imtemp(ii:ii+3,jj:jj+3)=Imagefft(ii:ii+3,jj:jj+3);%舍去小的变换系数

    end

end

fun2=@ifft2;

Imageifft=blkproc(Imtemp,[8,8],fun2);%分块逆变换

huifu=uint8(abs(Imageifft)); %取整

figure;

imshow(huifu);  %显示图像

1 MATLAB固定text在图中的相对位置

MATLAB如何固定text在图中的相对位置

11 text函数

语法:

text(x,y,txt)

text(___,Name,Value)

1

2

1

2

text的详细用法参见MATLAB帮助文档

一般text函数的前面两项是输入插入string的坐标位置,但若想固定string在图中的相对位置,比如说让string 一直显示在图的右上角,并不随图的坐标轴的大小的改变而改变,那这个应当如何实现呢?

利用text的Unit属性,利用normalized的单位来实现

在这里插入描述

实例如下:

text( 'string',"(a) UB-OR", 'Units','normalized','position',[075,095], 'FontSize',14,'FontWeight','Bold','FontName','Times New Roman');

1

1

在这里插入描述

据此,固定了text在图中的相对位置,在绘制一系列图表时,可移植至其他图形。

2 MATLAB绘制图像时调整坐标轴及网格线至最顶层

MATLAB绘制图像时调整坐标轴及网格线至最顶层

MATLAB在绘制图形时,会出现图像遮挡坐标轴,网格线等情况,此时,如何调整图层的先后顺序呢?

在这里插入描述

于末尾添加代码如下:

set(gca,'Layer','top');

1

1

经修改后,图形如下所示,可见问题已经解决~

在这里插入描述

3 MATLAB设置坐标范围:X/Y轴

% 设置x轴范围和刻度

set(gca,'XLim',[0 10]); % X轴的数据显示范围

set(gca,'XTick',[0:1:10]); % 设置要显示坐标刻度

set(gca,'XTickLabel',[0:1:10]); % 给坐标加标签

% 设置y轴范围和刻度

set(gca,'YLim',[95 101]); % X轴的数据显示范围

set(gca,'YTick',[95:1:101]); % 设置要显示坐标刻度

set(gca,'YTickLabel',[95:1:101]); % 给坐标加标签

% 设置当前坐标轴x轴和y轴的限制范围

axis( [xmin xmax ymin ymax] )

1

2

3

4

5

6

7

8

9

10

11

12

1

2

3

4

5

6

7

8

9

10

11

12

4 MATLAB 线型设置

参见LineSpec(线条设定)

线型 说明

| 实线

– |虚线

: | 点线

- | 点划线

MATLAB可以更改下列属性:

· LineWidth - 指定线条的宽度(以磅为单位)。

· MarkerEdgeColor - 指定标记颜色或填充标记(圆形、方形、菱形、五角形、六角形和四个三角形)的· 边颜色。

· MarkerFaceColor - 指定填充标记的面的颜色。

· MarkerSize - 指定标记的大小(以磅为单位,必须大于 0)。

5 MATLAB分区绘图subplot总标题设置

在代码末处添加以下代码,可以设置总标题:

属性修改可见MATLAB帮助-在子图网格上添加标题

sgtitle('Your Title');

1

1

对于共用横(纵)坐标标题,可以通过text函数实现:

hAxis = axes('visible','off');

hxlabel = text(-005,05,'Percentage(%)');

set(hxlabel,'fontsize',15,'rotation',90,'HorizontalAlignment','center','FontWeight','Bold','FontName','Times New Roman');

hylabel = text(05,-01,'Month');

set(hylabel,'fontsize',15,'HorizontalAlignment','center','FontWeight','Bold','FontName','Times New Roman');

1

2

3

4

5

1

2

3

4

5

效果图如下:

请添加描述

6 MATLAB分区绘图子图间距和边缘距离调整

在MATLAB中,利用subplot绘制多副子图时,会发现各子图之间的距离太远,如下图所示:

请添加描述

So,怎么调整呢?

先百度,首先尝试Matlab子图间距和边缘距离调整一文中提出的两个方法。

方法一:使用tight_subplot函数,替代subplot进行绘图设置。

下载地址:tight_subplot(Nh, Nw, gap, marg_h, marg_w)

函数调用格式:

[ha,pos]=tight_subplot(Nh,Nw,gap,marg_h,marg_w)

% ha 是坐标轴句柄,pos是每个坐标轴的原点与长宽

% Nh,Nw 可以认为是几行几列

% gap是子图的纵向和横向间距,gap(1)为纵向,gap(2)为横向

% marg_h是图件与上下边缘的距离,marg_h(1)为距下边缘的距离,marg_h(2)是距上边缘的距离

% marg_w 是图件与左右边缘的距离,marg_w(1)为距左边缘的距离,marg_w(2)是距右边缘的距离。

1

2

3

4

5

6

1

2

3

4

5

6

利用上述函数进行处理,得到图形如下:

请添加描述

相关代码如下:

figure(1)

Nh = 4; % 两行

Nw = 2; % 四列

ha = tight_subplot(Nh,Nw,[01 01],[1 01],[05 01]);

axes( ha(1) ); % 将h(a)设置为当前坐标区

hold on;

box on;

h(1) = plot(SRI3Spring{1,1},'-ob','LineWidth',15,'MarkerEdgeColor','b');

h(2) = plot(SRI3Summer{1,1},'-sr','LineWidth',15,'MarkerEdgeColor','r');

h(3) = plot(SRI3Autumn{1,1},'-g','LineWidth',15);

h(4) = plot([0,57],[0,0],'--k','LineWidth',1);

text( 'string',"(a) Huangjiagang", 'Units','normalized','position',[002,09], 'FontSize',14,'FontWeight','Bold','FontName','Times New Roman');

set(gca,'xlim',[0 57],'xtick',[1:5:57],'xticklabel',[ ]);

set(gca,'ylim',[-2 325],'ytick',[-1:1:3],'yticklabel',[-1 0 1 2 3]);

set(gca,'FontSize',12,'Fontname', 'Times New Roman');

axes( ha(2) );

hold on;

box on;

h(1) = plot(SRI3Spring{2,1},'-ob','LineWidth',15,'MarkerEdgeColor','b');

h(2) = plot(SRI3Summer{2,1},'-sr','LineWidth',15,'MarkerEdgeColor','r');

h(3) = plot(SRI3Autumn{2,1},'-g','LineWidth',15);

h(4) = plot([0,57],[0,0],'--k','LineWidth',1);

text( 'string',"(b) Huangzhuang", 'Units','normalized','position',[002,09], 'FontSize',14,'FontWeight','Bold','FontName','Times New Roman');

set(gca,'xlim',[0 57],'xtick',[1:5:57],'xticklabel',[ ]);

set(gca,'ylim',[-2 325],'ytick',[-1:1:3],'yticklabel',[ ]);

set(gca,'FontSize',12,'Fontname', 'Times New Roman');

axes( ha(3) );

hold on;

box on;

h(1) = plot(SRI3Spring{3,1},'-ob','LineWidth',15,'MarkerEdgeColor','b');

h(2) = plot(SRI3Summer{3,1},'-sr','LineWidth',15,'MarkerEdgeColor','r');

h(3) = plot(SRI3Autumn{3,1},'-g','LineWidth',15);

h(4) = plot([0,57],[0,0],'--k','LineWidth',1);

text( 'string',"(c) Baihe", 'Units','normalized','position',[002,09], 'FontSize',14,'FontWeight','Bold','FontName','Times New Roman');

set(gca,'xlim',[0 57],'xtick',[1:5:57],'xticklabel',[ ]);

set(gca,'ylim',[-2 325],'ytick',[-1:1:3],'yticklabel',[-1 0 1 2 3]);

set(gca,'FontSize',12,'Fontname', 'Times New Roman');

axes( ha(4) );

hold on;

box on;

h(1) = plot(SRI3Spring{4,1},'-ob','LineWidth',15,'MarkerEdgeColor','b');

h(2) = plot(SRI3Summer{4,1},'-sr','LineWidth',15,'MarkerEdgeColor','r');

h(3) = plot(SRI3Autumn{4,1},'-g','LineWidth',15);

h(4) = plot([0,57],[0,0],'--k','LineWidth',1);

text( 'string',"(d) Guotan", 'Units','normalized','position',[002,09], 'FontSize',14,'FontWeight','Bold','FontName','Times New Roman');

set(gca,'xlim',[0 57],'xtick',[1:5:57],'xticklabel',[ ]);

set(gca,'ylim',[-2 325],'ytick',[-1:1:3],'yticklabel',[ ]);

set(gca,'FontSize',12,'Fontname', 'Times New Roman');

axes( ha(5) );

hold on;

box on;

h(1) = plot(SRI3Spring{5,1},'-ob','LineWidth',15,'MarkerEdgeColor','b');

h(2) = plot(SRI3Summer{5,1},'-sr','LineWidth',15,'MarkerEdgeColor','r');

h(3) = plot(SRI3Autumn{5,1},'-g','LineWidth',15);

h(4) = plot([0,57],[0,0],'--k','LineWidth',1);

text( 'string',"(e) Huanglongtan", 'Units','normalized','position',[002,09], 'FontSize',14,'FontWeight','Bold','FontName','Times New Roman');

set(gca,'xlim',[0 57],'xtick',[1:5:57],'xticklabel',[ ]);

set(gca,'ylim',[-2 325],'ytick',[-1:1:3],'yticklabel',[-1 0 1 2 3]);

set(gca,'FontSize',12,'Fontname', 'Times New Roman');

axes( ha(6) );

hold on;

box on;

h(1) = plot(SRI3Spring{6,1},'-ob','LineWidth',15,'MarkerEdgeColor','b');

h(2) = plot(SRI3Summer{6,1},'-sr','LineWidth',15,'MarkerEdgeColor','r');

h(3) = plot(SRI3Autumn{6,1},'-g','LineWidth',15);

h(4) = plot([0,57],[0,0],'--k','LineWidth',1);

text( 'string',"(f)Xiangjiaping", 'Units','normalized','position',[002,09], 'FontSize',14,'FontWeight','Bold','FontName','Times New Roman');

set(gca,'xlim',[0 57],'xtick',[1:5:57],'xticklabel',[1958:5:2013]);

set(gca,'ylim',[-2 325],'ytick',[-1:1:3],'yticklabel',[ ]);

set(gca,'FontSize',12,'Fontname', 'Times New Roman');

axes( ha(7) );

hold on;

box on;

h(1) = plot(SRI3Spring{7,1},'-ob','LineWidth',15,'MarkerEdgeColor','b');

h(2) = plot(SRI3Summer{7,1},'-sr','LineWidth',15,'MarkerEdgeColor','r');

h(3) = plot(SRI3Autumn{7,1},'-g','LineWidth',15);

h(4) = plot([0,57],[0,0],'--k','LineWidth',1);

text( 'string',"(g)Xindianpu", 'Units','normalized','position',[002,09], 'FontSize',14,'FontWeight','Bold','FontName','Times New Roman');

set(gca,'xlim',[0 57],'xtick',[1:5:57],'xticklabel',[1958:5:2013]);

set(gca,'ylim',[-2 325],'ytick',[-1:1:3],'yticklabel',[-1 0 1 2 3]);

set(gca,'FontSize',12,'Fontname', 'Times New Roman');

axes( ha(8) );

axis off;

hAxis = axes('visible','off');

hxlabel = text(05,-01,'Year');

set(hxlabel,'fontsize',15,'HorizontalAlignment','center','FontWeight','Bold','FontName','Times New Roman');

hylabel = text(-014,05,'SRI');

set(hylabel,'fontsize',15,'rotation',90,'HorizontalAlignment','center','FontWeight','Bold','FontName','Times New Roman');

lgd = legend(h([1 2 3]),"Spring","Summer","Autumn",'fontsize',15,'FontWeight','Bold','FontName','Times New Roman','NumColumns',3);

set(lgd,'Box','off');

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

此外,针对多余坐标区(上图第八个分区没有图,用于放置图例),可用以下代码将其隐藏。

axes( ha(8) );

axis off;

1

2

1

2

方法二:针对2019b以后的版本,官网有tiledlayout-创建分块图布局可以用于解决间距过大的问题。

没办法,我现在也是2019a,没得使用这个方法的机会。

7 MATLAB图例设置Legend

参见MATLAB帮助-Legend在坐标区上添加图例

71 图例位置

值 说明

‘north’ 坐标区中的顶部

‘south’ 坐标区中的底部

‘east’ 坐标区中的右侧区域

‘west’ 坐标区中的左侧区域

‘northeast’ 坐标区中的右上角(二维坐标区的默认值)

‘northwest’ 坐标区中的左上角

‘southeast’ 坐标区中的右下角

‘southwest’ 坐标区中的左下角

‘northoutside’ 坐标区的上方

‘southoutside’ 坐标区的下方

‘eastoutside’ 到坐标区的右侧

‘westoutside’ 到坐标区的左侧

… …

示例: legend(‘Location’,‘northeastoutside’)

72 其它

设置列数: lgdNumColumns = 3

73 案例

MATLAB实现代码如下:

hl = legend(h([1 2]),"径流","多年平均径流",'location','northwest');

set(hl,'Box','off');

1

2

1

2

图形如下:

请添加描述

8 MATLAB设置斜体和正体

斜体(Italic)

\it

1

1

正体

\rm

1

1

案例如下:

请添加描述

相应代码如下:

figure(1)

box on;

backColor = [255 240 245]/255;

set(gca, 'color', backColor);

text( 'string',"\itZ\rm_1=\itz\rm_1+\itz\rm_2+\itz\rm_3", 'Units','normalized','position',[05,08], 'FontSize',25,'FontWeight','Bold','FontName','Times New Roman','color','r');

1

2

3

4

5

1

2

3

4

5

9 colormap相关问题

MATLAB中文帮助-colormap

MATLAB中文帮助-colorbar

MATLAB预定义的颜色图:

在这里插入描述

91 颜色图反转

举例将灰度图颜色反转,即白色部分变黑,黑色部分变白:

colormap(flipud(gray)):

1

1

10 坐标轴刻度设置

101 坐标轴刻度朝外

set(gca,'TickDir','out')

1

1

疑问:此方法只能设置将各防线坐标轴刻度朝外,如何设置某单个坐标轴刻度方向呢?

102 去除图像右边和上边的刻度线

box off

ax2 = axes('Position',get(gca,'Position'),

'Color','none',

'XAxisLocation','top',

'YAxisLocation','right',

'XColor','k','YColor','k');

set(ax2,'YTick', []);

set(ax2,'XTick', []);

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/12171203.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-21
下一篇 2023-05-21

发表评论

登录后才能评论

评论列表(0条)

保存