-
Notifications
You must be signed in to change notification settings - Fork 45
/
scalebar.m
61 lines (52 loc) · 2.55 KB
/
scalebar.m
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
clc;clear all;close all;
[ filename, pathname] = ...
uigetfile({'*.jpg;*.tif;*.png;*.gif','All Image Files';...
'*.*','All Files' },'Choose Input Image');
image=imread([pathname filesep filename]);
InputDialog={'Length of the Scale Bar to draw [cm]'};
Name='Input the Details';
NumLines=1;
Defaults={'21'};
UserInput=inputdlg(InputDialog,Name,NumLines,Defaults);
scale = str2num(UserInput{1});
scale = scale * 10; % to convert from cm to mm
figure
imshow(image)
hold on
h = helpdlg('choose start-point of scalebar','ScaleBar');
uiwait(h);
[ x1,y1 ] = ginput(1);
h = helpdlg('choose end-point of scalebar','ScaleBar');
uiwait(h);
[ x2,y2 ] = ginput(1);
line([x1,x2],[y1,y2]);
length = sqrt((x1-x2)^2+(y1-y2)^2);
dpixel = scale / length * 100;
um = 100 / dpixel * 50;
title([ 'Vectorlength: ' num2str(round(length)) 'px = ' num2str(scale) ...
'mm, 100px = ' num2str(round(dpixel)) 'mm, ' num2str(round(um)) ...
'px = 5 cm'])
h = helpdlg(['Image Width = ' num2str(size(image,2)) ' px - (x1,y1) = (' ...
num2str(round(x1)) ',' num2str(round(y1)) ') - (x2, y2) = (' ...
num2str(round(x2)) ',' num2str(round(y2)) ')'],'Positions');
disp('copy the stuff below to your .tex-file')
disp('%-------------')
disp('\newcommand{\imsize}{\linewidth} % makes life easier with images')
disp('\newlength\imagewidth % needed for scalebars')
disp('\newlength\imagescale % needed for scalebars')
disp('\pgfmathsetlength{\imagewidth}{\imsize} % desired displayed width of image')
disp(['\pgfmathsetlength{\imagescale}{\imagewidth/' num2str(size(image,2)) '} % pixel width of imagefile used below'])
disp('%-------------')
disp('\begin{tikzpicture}[x=\imagescale,y=-\imagescale]')
disp(' \def\x{100}')
disp(' \def\y{100}')
disp(' \node[anchor=north west,inner sep=0pt,outer sep=0pt] at (0,0)')
disp([' {\includegraphics[width=\imagewidth]{' filename '}};' ])
disp([' % ' num2str(round(length)) 'px = ' num2str(scale) 'mm > 100px = ' num2str(round(dpixel)) 'mm > ' num2str(round(um)) 'px = 50mm'])
disp([' \draw[|-|,thick] (' num2str(round(x1)) ',' num2str(round(y1)) ...
') -- (' num2str(round(x2)) ',' num2str(round(y2)) ') node [sloped,midway,above] '...
'{\SI{' num2str(scale) '}{\milli\meter}};' ])
disp([' \draw[|-|,thick] (\x,\y) -- (\x+' num2str(round(um)) ',\y) node [midway,above] {\SI{5}{\centi\meter}};'])
disp('\end{tikzpicture}%');
disp('%-------------')
disp('copy the stuff above to your .tex-file')