Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Matlab 将数字写在图像上_Matlab_Text_Matlab Figure - Fatal编程技术网

Matlab 将数字写在图像上

Matlab 将数字写在图像上,matlab,text,matlab-figure,Matlab,Text,Matlab Figure,使用Matlab,我想在imshow显示的图像中的特定位置写一个数字。目前,我有: myimage = imread('Route of my image'); myimage = im2double(myimage); imshow(myimage) MyBox = uicontrol('style','text'); set(MyBox,'String',mynumber); set(MyBox,'Position',[25,25,15,15]); 我的问题是,“集合”中给出的位置与所

使用Matlab,我想在imshow显示的图像中的特定位置写一个数字。目前,我有:

myimage = imread('Route of my image');
myimage = im2double(myimage);

imshow(myimage)

MyBox = uicontrol('style','text');
set(MyBox,'String',mynumber);
set(MyBox,'Position',[25,25,15,15]);

我的问题是,“集合”中给出的位置与所有管理体形窗口的窗口相关,因此它还包括灰色边框。如何仅相对于图形(没有灰色边框)写入它们?

您可以按照所述步骤从图形中删除灰色边框,以便在放置文本时获得正确的坐标。基本上获取图形和包含图像的轴的尺寸,并使图形与轴精确匹配

请注意,当指定
uicontrol
对象的位置时,0位置位于左下角,而图像内的像素坐标从左上角开始。因此,您需要获得图像的尺寸,并从形成图像的行数中减去实际的y坐标,即第一个尺寸

以下是一个例子:

clear
clc
close all

myimage = imread('coins.png');
myimage = im2double(myimage);

imshow(myimage);

[r,c,~] = size(myimage);

%// Copied/pasted from http://www.mathworks.com/matlabcentral/answers/100366-how-can-i-remove-the-grey-borders-from-the-imshow-plot-in-matlab-7-4-r2007a
set(gca,'units','pixels'); %// set the axes units to pixels
x = get(gca,'position'); %// get the position of the axes
set(gcf,'units','pixels'); %// set the figure units to pixels
y = get(gcf,'position'); %// get the figure position
set(gcf,'position',[y(1) y(2) x(3) x(4)]);% set the position of the figure to the length and width of the axes
set(gca,'units','normalized','position',[0 0 1 1]) % set the axes units to pixels

%// Example
hold on
mynumber = 20;

%// Set up position/size of the box
Box_x = 25;
Box_y = 25;
Box_size = [15 15];
%// Beware! For y-position you want to start at the top left, i.e. r -
%// Box_y
BoxPos = [Box_x r-Box_y Box_size(1) Box_size(2)];
MyBox = uicontrol('style','text','Position',BoxPos);

set(MyBox,'String',mynumber);
和输出:

你可以用它来代替吗


上面的答案在figure窗口中添加文本。如果要修改图像本身并更改像素,以便在显示图像的位置获得文本,可以使用计算机视觉工具箱中提供的insertText函数

myimage = imread('Route of my image');
myimage = im2double(myimage);

myimage = insertText(myimage, [25 25], 'your text');
imshow(myimage)

更快更简单。太好了。当人们写下如此深入的回复,却得不到信任时,我感到很难过。应该有一点警告:“有人输入了200行或文本,请稍候……”但我认为他从你那里学到的比从我那里学到的更多;有时候最好的解决方案是最简单的!非常感谢。很抱歉,我不能勾选你的答案,但ninehundred的答案更简单:)哈哈,当然没问题!事实上,这比我想象的要简单得多:)
myimage = imread('Route of my image');
myimage = im2double(myimage);

myimage = insertText(myimage, [25 25], 'your text');
imshow(myimage)