How to get exact position of text with respect to figure row, column in Matlab

24 vues (au cours des 30 derniers jours)
Amir
Amir le 29 Mar 2012
In the following code segment I am trying to get the exact position of the text bounding box with respect to the figure pixel coordinates( row and column) to eventually be able to crop off that part of figure ( from array img ). However what I get from textBox is not very helpful! some negative numbers!! can anyone provide me some tips
hFigure = figure('Color', 'w','position',...
[1600 200 600 250]...
,'MenuBar', 'none', 'ToolBar', 'none');
axis off
axis([0 1 0 1]);
hText=text('String','T','fontsize',100,'color','r',...
'fontname','Times New Roman',...
'HorizontalAlignment','left','VerticalAlignment','bottom',...
'BackgroundColor',[.8 .8 .8],'EdgeColor','r');
set(hText, 'Units','Pixels');
textBox=get(hText, 'Extent');%[left,bottom,width,height]
figBox = get(hFigure,'Position');
imageData = getframe(hFigure);
img = imageData.cdata;
%using textBox and imgBox:
imgText=img(?:?,?:?,3); **% this is what I want to do**
Please refer to this image:

Réponses (2)

Geoff
Geoff le 29 Mar 2012
I'm not sure about the general case, but here you could exploit the fact that your image bounds are not white:
[y,x] = find( img(:,:,1)~=255 & img(:,:,2)~=255 & img(:,:,3)~=255 );
tl = min([y x]);
br = max([y x]);
image(img(tl(1):br(1), tl(2):br(2),:));

per isakson
per isakson le 29 Mar 2012
The parent of an text-object is an axes-object. The extent of the text-object is relative to the axes-object. Use pixels for all objects. This is a start
axis off
axis([0 1 0 1]);
axh = gca;
set( axh, 'units', 'pixel' )
ax_pos = get( axh, 'Position' );
hText=text( 'units', 'pixel', ...

Catégories

En savoir plus sur Characters and Strings dans Help Center et File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by