TikZ for Image Analysis

File: beamer-list-example.tex(6157 bytes; Thu, Jul/10/2008) download 'beamer-list-example.tex' to disk
File: colorblobs.jpg(27.15 kb; Wed, Sep/12/2007) download 'colorblobs.jpg' to disk

What I want to do:

Display a digital image and add some TikZ-Overlays, e.g. marking a region of interest or some extracted features. Preferably, image features are drawn using convenient pixel coordinates.
original image

This is the resulting image with overlay:

rendering of resulting PDF

The following is the code I came up with so far (download link at the top of this page).* Another thing that may be useful is to attach [use as bounding box] to the image node; this will allow you to center a figure nicely even if labels stand out differently to the left and to the right (not in this example).

\documentclass{article}
\usepackage{graphicx,tikz}

\usepackage[graphics,tightpage,active]{preview}
\PreviewEnvironment{tikzpicture}
\newlength\imagewidth
\newlength\imagescale

\begin{document}

\pgfmathsetlength{\imagewidth}{10cm} % desired displayed width of image
\pgfmathsetlength{\imagescale}{\imagewidth/600} % pixel width of image
%
% adjust scale of tikzpicture (and direction of y) such that pixel
% coordinates can be used for drawing overlays:
\begin{tikzpicture}[x=\imagescale,y=-\imagescale]

% place image (integer coordinates refer to pixel centers):
\node[anchor=north west,inner sep=0pt,outer sep=0pt] at (-0.5,-0.5)
  {\includegraphics[width=\imagewidth]{colorblobs.jpg}};

% draw overlays:
\draw[red]
  (599,220.25) -- (581.13,227.69) -- (563.04,237.51) -- (555.44,244.20) -- (552.18,249.94) -- (551.55,254.72) -- (552.13,259.31) -- (555.01,268.30) -- (568.63,287.98) -- (585.88,304.68) -- (594.88,310.29) -- (599,311.97)
  (430.50,-0) -- (432.90,7.39) -- (433.80,18.94) -- (432.14,28.08) -- (427.71,40.66) -- (421.03,52.82) -- (412.55,63.90) -- (400.02,75.31) -- (396.75,76.72) -- (392.70,77.02) -- (386.99,76.28) -- (382.48,72.97) -- (359.89,44.73) -- (334.04,23.86) -- (329.17,18.85) -- (328.03,14.93) -- (329.12,10.54) -- (333.39,6.43) -- (340.95,1.49) -- (341.45,0)
  (405.01,244.37) -- (394.08,235.35) -- (377.01,214.60) -- (353.80,189.88) -- (345.80,178.20) -- (338.04,162.10) -- (333.05,147.31) -- (331.24,138.07) -- (331.86,123) -- (334.79,116.09) -- (339.26,110.84) -- (344.37,106.37) -- (352.25,102.12) -- (367.47,98.80) -- (382.01,98.67) -- (393.35,101.27) -- (403.16,106.24) -- (420.16,119.03) -- (441.38,139.79) -- (465.76,159.14) -- (477.89,171.77) -- (481.61,178.75) -- (482.57,182.71) -- (482.31,188.39) -- (476.72,203.12) -- (466.65,216.40) -- (453.18,229.50) -- (435.78,242.32) -- (424.32,247.29) -- (414.10,247.76) -- cycle
  (105.30,0) -- (104.91,1.76) -- (101.55,3.91) -- (93.67,12.39) -- (89.77,21.55) -- (87.58,31.92) -- (84.08,83.20) -- (84.86,87.35) -- (86.28,89.86) -- (88.49,91.30) -- (91.84,91.83) -- (96.88,91.28) -- (120.96,85.35) -- (145.76,82.88) -- (148.70,81.36) -- (150.88,78.98) -- (151.50,72.80) -- (145.83,44.42) -- (142.26,18.89) -- (139.77,8) -- (135.97,-0)
  (102.89,479) -- (102.42,477.92) -- (98.70,475.82) -- (94.61,468.89) -- (91.32,453.23) -- (91.11,438.77) -- (89.96,436.67) -- (82.02,428.77) -- (81.12,424.73) -- (82.24,421.24) -- (91.20,412.16) -- (119.25,389.21) -- (138.04,379.02) -- (151.34,370.17) -- (170.58,355.62) -- (180.06,350.44) -- (186.52,348.82) -- (190.78,349.68) -- (195.25,352.20) -- (198.93,355.69) -- (205.20,363.96) -- (210.47,377.54) -- (213.80,391.91) -- (214.51,410.07) -- (210.92,447.92) -- (208.39,456.85) -- (203.13,466.74) -- (200.25,476.80) -- (200.64,479)
  (61.95,0) -- (62.35,1.50) -- (67.50,6.26) -- (71.15,11.34) -- (74.11,20) -- (75.25,27.03) -- (74.95,37.76) -- (72.81,45.36) -- (69.42,51.33) -- (65.60,55.83) -- (59.60,61.78) -- (51.30,66.11) -- (42.61,69.96) -- (33,72.10) -- (15.50,72.26) -- (7.64,69.22) -- (2.72,66.28) -- (0,65.74)
  (146.39,176.61) -- (142.42,170.59) -- (137.76,157.59) -- (134.63,142.31) -- (134.42,129.31) -- (137.42,116.56) -- (143.01,105.36) -- (149.03,100.61) -- (158.42,98.27) -- (175.12,100.33) -- (202.81,106.90) -- (211.18,107.96) -- (217.98,108) -- (247.40,105.21) -- (257.61,105.32) -- (271.96,108.15) -- (282.89,112.23) -- (286.83,115.59) -- (289.18,118.90) -- (290.09,122.40) -- (289.97,127.43) -- (284.88,141.82) -- (273.98,166.93)
  (0,202.31) -- (1.90,202.40) -- (11.28,196.42) -- (28.92,187.95) -- (40.12,185.67) -- (45.02,186.05) -- (49.12,188.52) -- (51.34,192.19) -- (52,198.12) -- (51.12,208.49) -- (44.76,232.86) -- (39.57,247.31) -- (35.64,253.33) -- (30.07,257.49) -- (22.93,258.53) -- (12.47,256.74) -- (6.57,254.62) -- (0,251.02)
  (103.38,315.30) -- (103.47,327.92) -- (113.56,357.42) -- (113.55,360.72) -- (111.60,363.43) -- (106.13,364.96) -- (73,363.24) -- (51.64,364.25) -- (45.61,363.71) -- (40.97,362.20) -- (35.57,357.38) -- (31.45,350.02) -- (19.86,312.97) -- (17.37,297.12) -- (18.68,286.95) -- (21.10,278.50) -- (26.70,271.53) -- (33.13,268.96) -- (38.86,269.34) -- (59.69,275.26) -- (71.90,276.58) -- (110.12,270.51) -- (113.77,270.94) -- (116.15,273.30) -- (116.40,276.95) -- (105.27,305.53) -- cycle
  (273.98,166.93) -- (266.65,195.77) -- (262.97,205.82) -- (259.19,211.51) -- (254.68,213.32) -- (246.90,212.63) -- (224.30,202.81) -- (209.38,197.34) -- (170.64,192.80) -- (164.12,191.17) -- (153.78,185.91) -- (151.29,183.75) -- (146.39,176.61)
  (273.98,166.93) -- (292.03,177.62) -- (304.34,190.41) -- (312.65,206.01) -- (314.01,213.49) -- (313.53,219.76) -- (310.62,225.82) -- (304.91,232.04) -- (277.89,248.74) -- (267.22,258.68) -- (260.31,270.41) -- (250.67,298.19) -- (244.29,311.68) -- (240.60,316.54) -- (236.01,320.14) -- (229.43,323.23) -- (224.32,324.11) -- (215.02,323.90) -- (208.36,322.57) -- (201.18,318.50) -- (197.13,314.57) -- (193.81,309.93) -- (191.39,304.28) -- (189.89,297.83) -- (186.15,264.10) -- (181.68,247.14) -- (171.50,228.63) -- (149.07,200.91) -- (146.29,196.14) -- (145.50,193.07) -- (145.98,189.90) -- (151.13,183.79) -- (146.39,176.61)
  (273.98,166.93) -- (256.67,162.76) -- (246.22,162.62) -- (236.85,163.58) -- (221.61,167.41) -- (197.41,175.43) -- (163.08,181.50) -- (155.73,183.12) -- (152.94,184.82) -- (151.29,183.75) -- (146.39,176.61)
  (61.95,0) -- (105.30,0)
  (105.30,0) -- (135.97,-0)
  (135.97,-0) -- (341.45,0)
  (341.45,0) -- (430.50,-0)
  (430.50,-0) -- (599,-0) -- (599,220.25)
  (599,220.25) -- (599,311.97)
  (599,311.97) -- (599,479) -- (200.64,479)
  (200.64,479) -- (102.89,479)
  (102.89,479) -- (-0,479) -- (0,251.02)
  (0,251.02) -- (0,202.31)
  (0,202.31) -- (0,65.74)
  (0,65.74) -- (-0,-0) -- (61.95,0)
;

\tikzstyle{every node}=[circle,fill=white,anchor=center,inner sep=1.2pt]
\draw (576,257) node {1};
\draw (358,357) node {2};
\draw (394,33) node {3};
\draw (422,189) node {5};
\draw (117,55) node {6};
\draw (160,427) node {7};
\draw (35,35) node {8};
\draw (174,140) node {10};
\draw (24,217) node {11};
\draw (63,317) node {13};
\draw (225,248) node {14};
\draw (244,186) node {15};

\end{tikzpicture}

\end{document}
[*]This has been updated using the \pgfmathsetlength idea from Martin Heller and the outer sep=0pt setting by K.M.Fauske (thanks!).

Valid XHTML 1.0! Valid CSS!
This page was last modified: Thursday, July 10, 2008 hacker emblem