{"id":122,"date":"2007-03-16T07:00:59","date_gmt":"2007-03-16T11:00:59","guid":{"rendered":"https:\/\/blogs.mathworks.com\/steve\/2007\/03\/16\/computing-distance-using-image-file-information\/"},"modified":"2019-10-23T08:46:20","modified_gmt":"2019-10-23T12:46:20","slug":"computing-distance-using-image-file-information","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/steve\/2007\/03\/16\/computing-distance-using-image-file-information\/","title":{"rendered":"Computing distance using image file information"},"content":{"rendered":"<div class=\"content\">\n<p><a href=\"https:\/\/blogs.mathworks.com\/steve\/2007\/01\/15\/a-year-of-blogging\/#comment-6462\">Patrick asked<\/a> for an example on how to compute distances between objects based on resolution information stored in a file. The TIFF format<br \/>\nis one that can store resolution information, and the <tt>imfinfo<\/tt> function can tell you about it.<\/p>\n<p>Let's look at a simple example:<\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\">url = <span style=\"color: #a020f0;\">'https:\/\/blogs.mathworks.com\/images\/steve\/122\/blobs.tif'<\/span>;\r\nbw = imread(url);\r\nimshow(bw)<\/pre>\n<p>&nbsp;<\/p>\n<p>Here's the output of <tt>imfinfo<\/tt>:<\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\">info = imfinfo(url)<\/pre>\n<pre style=\"font-style: oblique;\">info = \r\n\r\n                     Filename: 'C:\\TEMP\\tp281629'\r\n                  FileModDate: '06-Mar-2007 09:28:16'\r\n                     FileSize: 2176\r\n                       Format: 'tif'\r\n                FormatVersion: []\r\n                        Width: 308\r\n                       Height: 242\r\n                     BitDepth: 1\r\n                    ColorType: 'grayscale'\r\n              FormatSignature: [73 73 42 0]\r\n                    ByteOrder: 'little-endian'\r\n               NewSubFileType: 0\r\n                BitsPerSample: 1\r\n                  Compression: 'CCITT 1D'\r\n    PhotometricInterpretation: 'WhiteIsZero'\r\n                 StripOffsets: [10x1 double]\r\n              SamplesPerPixel: 1\r\n                 RowsPerStrip: 26\r\n              StripByteCounts: [10x1 double]\r\n                  XResolution: 100\r\n                  YResolution: 100\r\n               ResolutionUnit: 'Inch'\r\n                     Colormap: []\r\n          PlanarConfiguration: 'Chunky'\r\n                    TileWidth: []\r\n                   TileLength: []\r\n                  TileOffsets: []\r\n               TileByteCounts: []\r\n                  Orientation: 1\r\n                    FillOrder: 1\r\n             GrayResponseUnit: 0.0100\r\n               MaxSampleValue: 1\r\n               MinSampleValue: 0\r\n                 Thresholding: 1\r\n\r\n<\/pre>\n<p>Notice the XResolution and YResolution fields, as well as the ResolutionUnit field. According to the TIFF specification, the XResolution and YResolution fields are the number of pixels per resolution unit.<\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\">info.XResolution<\/pre>\n<pre style=\"font-style: oblique;\">ans =\r\n\r\n   100\r\n\r\n<\/pre>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\">info.YResolution<\/pre>\n<pre style=\"font-style: oblique;\">ans =\r\n\r\n   100\r\n\r\n<\/pre>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\">info.ResolutionUnit<\/pre>\n<pre style=\"font-style: oblique;\">ans =\r\n\r\nInch\r\n\r\n<\/pre>\n<p>Now let's use <tt>bwlabel<\/tt> and <tt>regionprops<\/tt> to compute the centroids of the objects.<\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\">L = bwlabel(bw);\r\ns = regionprops(L, <span style=\"color: #a020f0;\">'Centroid'<\/span>)<\/pre>\n<pre style=\"font-style: oblique;\">s = \r\n\r\n4x1 struct array with fields:\r\n    Centroid\r\n\r\n<\/pre>\n<p>Compute the distance (in pixel units) between the first two objects.<\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\">delta_x = s(1).Centroid(1) - s(2).Centroid(1);\r\ndelta_y = s(1).Centroid(2) - s(2).Centroid(2);\r\npixel_distance = hypot(delta_x, delta_y)<\/pre>\n<pre style=\"font-style: oblique;\">pixel_distance =\r\n\r\n  103.6742\r\n\r\n<\/pre>\n<p>Finally, use the resolution information from the file to convert to physical distance. (Note: this calculation assumes that<br \/>\nthe horizontal and vertical resolutions are the same.)<\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\">physical_distance = pixel_distance \/ info.XResolution<\/pre>\n<pre style=\"font-style: oblique;\">physical_distance =\r\n\r\n    1.0367\r\n\r\n<\/pre>\n<p><script>\/\/ <![CDATA[\nfunction grabCode_e13b9309c2524fccb276164308193672() {\n        \/\/ Remember the title so we can use it in the new page\n        title = document.title;\n\n        \/\/ Break up these strings so that their presence\n        \/\/ in the Javascript doesn't mess up the search for\n        \/\/ the MATLAB code.\n        t1='e13b9309c2524fccb276164308193672 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\n        t2='##### ' + 'SOURCE END' + ' #####' + ' e13b9309c2524fccb276164308193672';\n    \n        b=document.getElementsByTagName('body')[0];\n        i1=b.innerHTML.indexOf(t1)+t1.length;\n        i2=b.innerHTML.indexOf(t2);\n \n        code_string = b.innerHTML.substring(i1, i2);\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\n\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \n        \/\/ in the XML parser.\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\n        \/\/ doesn't go ahead and substitute the less-than character. \n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\n\n        author = 'Steve Eddins';\n        copyright = 'Copyright 2007 The MathWorks, Inc.';\n\n        w = window.open();\n        d = w.document;\n        d.write('\n\n\n\n<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add author and copyright lines at the bottom if specified.\r\n        if ((author.length > 0) || (copyright.length > 0)) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (author.length > 0) {\r\n                d.writeln('% _' + author + '_');\r\n            }\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\n\n\n\n\n\\n');\n      \n      d.title = title + ' (MATLAB code)';\n      d.close();\n      }\n\/\/ ]]><\/script><\/p>\n<p style=\"text-align: right; font-size: xx-small; font-weight: lighter; font-style: italic; color: gray;\">\n<a><span style=\"font-size: x-small; font-style: italic;\">Get<br \/>\nthe MATLAB code<br \/>\n<noscript>(requires JavaScript)<\/noscript><\/span><\/a><\/p>\n<p>Published with MATLAB\u00ae 7.4<\/p>\n<\/div>\n<p><!--\ne13b9309c2524fccb276164308193672 ##### SOURCE BEGIN #####\n%% Computing distance using image file information\n% <https:\/\/blogs.mathworks.com\/steve\/2007\/01\/15\/a-year-of-blogging\/#comment-6462 % Patrick asked> for an example on how to compute distances between objects\n% based on resolution information stored in a file.  The TIFF format is one\n% that can store resolution information, and the |imfinfo| function can\n% tell you about it.\n%\n% Let's look at a simple example:\n\nurl = 'https:\/\/blogs.mathworks.com\/images\/steve\/122\/blobs.tif';\nbw = imread(url);\nimshow(bw)\n\n%%\n% Here's the output of |imfinfo|:\n\ninfo = imfinfo(url)\n\n%%\n% Notice the XResolution and YResolution fields, as well as the\n% ResolutionUnit field. According to the\n% <http:\/\/partners.adobe.com\/public\/developer\/en\/tiff\/TIFF6.pdf % TIFF specification>, the\n% XResolution and YResolution fields are the number of pixels per\n% resolution unit.\n\ninfo.XResolution\n\n%%\n\ninfo.YResolution\n\n%%\n\ninfo.ResolutionUnit\n\n%%\n% Now let's use |bwlabel| and |regionprops| to compute the centroids of the\n% objects.\n\nL = bwlabel(bw);\ns = regionprops(L, 'Centroid')\n\n%%\n% Compute the distance (in pixel units) between the first two objects.\n\ndelta_x = s(1).Centroid(1) - s(2).Centroid(1);\ndelta_y = s(1).Centroid(2) - s(2).Centroid(2);\npixel_distance = hypot(delta_x, delta_y)\n\n%%\n% Finally, use the resolution information from the file to convert to\n% physical distance.  (Note: this calculation assumes that the horizontal\n% and vertical resolutions are the same.)\n\nphysical_distance = pixel_distance \/ info.XResolution\n##### SOURCE END ##### e13b9309c2524fccb276164308193672\n--><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\nPatrick asked for an example on how to compute distances between objects based on resolution information stored in a file. The TIFF format<br \/>\nis one that can store resolution information, and the... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/steve\/2007\/03\/16\/computing-distance-using-image-file-information\/\">read more >><\/a><\/p>\n","protected":false},"author":42,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[166,334,332,76,36,168],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/122"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/users\/42"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/comments?post=122"}],"version-history":[{"count":2,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/122\/revisions"}],"predecessor-version":[{"id":2579,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/122\/revisions\/2579"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media?parent=122"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/categories?post=122"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/tags?post=122"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}