{"id":89,"date":"2006-10-19T15:49:44","date_gmt":"2006-10-19T19:49:44","guid":{"rendered":"https:\/\/blogs.mathworks.com\/steve\/?p=89"},"modified":"2019-10-22T16:29:47","modified_gmt":"2019-10-22T20:29:47","slug":"hough-transform-coordinate-system","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/steve\/2006\/10\/19\/hough-transform-coordinate-system\/","title":{"rendered":"Hough transform coordinate system"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <p>Reader JP asked me about my <a href=\"https:\/\/blogs.mathworks.com\/steve\/?p=81\">recent blog post on the Hough transform<\/a>. Specifically, she wanted to know exactly how the rho-theta coordinate system was defined.  When I answered her question,\r\n      I realized that our documentation isn't very clear on this point, so I thought I'd post the answer here.\r\n   <\/p>\r\n   <p>Suppose you have a pixel at (x=25, y=25).  (Note that the Image Processing Toolbox convention is that the x-axis is the horizontal\r\n      axis, the y-axis is the vertical axis, and the origin is at the upper left.)  There is a family of lines that all go through\r\n      (25,25).  The solid blue line in the diagram below is one such line.  Draw a perpendicular projection from the upper left\r\n      pixel to the line; that's the dashed blue line below.  The length of the projection is rho.  The clockwise angle between the\r\n      projection and the horizontal axis is theta.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">x = [1 50 50 1 1];\r\ny = [1 1 50 50 1];\r\nplot(x,y,<span style=\"color: #A020F0\">'k'<\/span>,<span style=\"color: #A020F0\">'LineWidth'<\/span>,2,<span style=\"color: #A020F0\">'Clipping'<\/span>,<span style=\"color: #A020F0\">'off'<\/span>)\r\naxis <span style=\"color: #A020F0\">equal<\/span>\r\naxis <span style=\"color: #A020F0\">ij<\/span>\r\naxis <span style=\"color: #A020F0\">off<\/span>\r\n\r\nhold <span style=\"color: #A020F0\">on<\/span>\r\nplot(25,25,<span style=\"color: #A020F0\">'k'<\/span>,<span style=\"color: #A020F0\">'Marker'<\/span>,<span style=\"color: #A020F0\">'o'<\/span>,<span style=\"color: #A020F0\">'MarkerSize'<\/span>,8,<span style=\"color: #A020F0\">'MarkerFaceColor'<\/span>,<span style=\"color: #A020F0\">'k'<\/span>)\r\n\r\nplot([1 25], [1 25], <span style=\"color: #A020F0\">'b'<\/span>, <span style=\"color: #A020F0\">'LineWidth'<\/span>, 2, <span style=\"color: #A020F0\">'LineStyle'<\/span>, <span style=\"color: #A020F0\">'--'<\/span>)\r\n\r\nplot([1 49], [49 1], <span style=\"color: #A020F0\">'b'<\/span>, <span style=\"color: #A020F0\">'LineWidth'<\/span>, 2)\r\n\r\ntext(10,20,<span style=\"color: #A020F0\">'\\rho = 33.9'<\/span>)\r\ntext(5,3,<span style=\"color: #A020F0\">'\\theta = 45^\\circ'<\/span>)\r\ntext(27,25,<span style=\"color: #A020F0\">'(25,25)'<\/span>)\r\ntext(-3,-1,<span style=\"color: #A020F0\">'(1,1)'<\/span>)\r\n\r\nhold <span style=\"color: #A020F0\">off<\/span><\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/89\/hough_coordinates_01.png\"> <p>Let's try this with an image.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">a = zeros(50,50);\r\na(25,25) = 1;\r\n[H, theta, rho] = hough(a);\r\n\r\nimshow(H, [], <span style=\"color: #A020F0\">'XData'<\/span>, theta, <span style=\"color: #A020F0\">'YData'<\/span>, rho, <span style=\"color: #A020F0\">'InitialMag'<\/span>, <span style=\"color: #A020F0\">'fit'<\/span>);\r\naxis <span style=\"color: #A020F0\">on<\/span>\r\nxlabel(<span style=\"color: #A020F0\">'\\theta'<\/span>)\r\nylabel(<span style=\"color: #A020F0\">'\\rho'<\/span>)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/89\/hough_coordinates_02.png\"> <p>Zooming in around the theta = 0 point gives us this:<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">axis([-5 5 21 26])<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/89\/hough_coordinates_03.png\"> <p>There is a vertical line (theta = 0 degrees) that goes through the (25,25) pixel, and this line is 24 units away (perpendicular\r\n      distance) from the upper left pixel.\r\n   <\/p>\r\n   <p>Let's look at another spot.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">axis([71 79 27 33])<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/89\/hough_coordinates_04.png\"> <p>There is another line through (25,25) whose perpendicular projection from (1,1) is 75 degrees from the horizontal axis.  The\r\n      length of perpendicular projection to this line from (1,1) is about 29 units.\r\n   <\/p>\r\n   <p>That's about it.  I'll ask our writer to add more information about the Hough transform coordinate system to the Users Guide.<\/p>\r\n   <p>(Note: The coordinate systems shown in <i>Digital Image Processing<\/i> by Gonzalez and Woods, as well as <i>Digital Image Processing Using MATLAB<\/i> by Gonzalez, Woods, and Eddins, are different from what I've shown here.  In those sources, the x-axis is the vertical axis.)\r\n   <\/p><script language=\"JavaScript\"> \r\n<!--\r\n    function grabCode_0224e1dc6b0a434faef745dbe520477c() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='0224e1dc6b0a434faef745dbe520477c ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 0224e1dc6b0a434faef745dbe520477c';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        author = 'Steve Eddins';\r\n        copyright = 'Copyright 2006 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<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');\r\n      \r\n      d.title = title + ' (MATLAB code)';\r\n      d.close();\r\n      } \r\n-->  \r\n      <\/script>\r\n<noscript>\r\n<em>A JavaScript-enabled browser is required to use the \"Get the MATLAB code\" link.<\/em>\r\n<\/noscript>\r\n<p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_0224e1dc6b0a434faef745dbe520477c()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n            the MATLAB code<\/span><\/a><br><br>\r\n      Published with MATLAB&reg; 7.3<br><\/p>\r\n<\/div>\r\n<!--\r\n0224e1dc6b0a434faef745dbe520477c ##### SOURCE BEGIN #####\r\n%% Hough transform coordinate system\r\n%\r\n% Reader JP asked me about my <https:\/\/blogs.mathworks.com\/steve\/?p=81 \r\n% recent blog post on the Hough transform>.\r\n% Specifically, she wanted to know exactly how the rho-theta coordinate\r\n% system was defined.  When I answered her question, I realized that our\r\n% documentation isn't very clear on this point, so I thought I'd post the\r\n% answer here.\r\n%\r\n% Suppose you have a pixel at (x=25, y=25).  (Note that the Image Processing\r\n% Toolbox convention is that the x-axis is the horizontal axis, the y-axis\r\n% is the vertical axis, and the origin is at the upper left.)  There is a\r\n% family of lines that all go through (25,25).  The solid blue line in the\r\n% diagram below is one such line.  Draw a perpendicular projection from the\r\n% upper left pixel to the line; that's the dashed blue line below.  The\r\n% length of the projection is rho.  The clockwise angle between the\r\n% projection and the horizontal axis is theta.\r\n\r\nx = [1 50 50 1 1];\r\ny = [1 1 50 50 1];\r\nplot(x,y,'k','LineWidth',2,'Clipping','off')\r\naxis equal\r\naxis ij\r\naxis off\r\n\r\nhold on\r\nplot(25,25,'k','Marker','o','MarkerSize',8,'MarkerFaceColor','k')\r\n\r\nplot([1 25], [1 25], 'b', 'LineWidth', 2, 'LineStyle', 'REPLACE_WITH_DASH_DASH')\r\n\r\nplot([1 49], [49 1], 'b', 'LineWidth', 2)\r\n\r\ntext(10,20,'\\rho = 33.9')\r\ntext(5,3,'\\theta = 45^\\circ')\r\ntext(27,25,'(25,25)')\r\ntext(-3,-1,'(1,1)')\r\n\r\nhold off\r\n\r\n%%\r\n% Let's try this with an image.\r\n\r\na = zeros(50,50);\r\na(25,25) = 1;\r\n[H, theta, rho] = hough(a);\r\n\r\nimshow(H, [], 'XData', theta, 'YData', rho, 'InitialMag', 'fit');\r\naxis on\r\nxlabel('\\theta')\r\nylabel('\\rho')\r\n\r\n%%\r\n% Zooming in around the theta = 0 point gives us this:\r\n\r\naxis([-5 5 21 26])\r\n\r\n%%\r\n% There is a vertical line (theta = 0 degrees) that goes through the\r\n% (25,25) pixel, and this line is 24 units away (perpendicular distance)\r\n% from the upper left pixel.\r\n\r\n%%\r\n% Let's look at another spot.\r\n\r\naxis([71 79 27 33])\r\n\r\n%%\r\n% There is another line through (25,25) whose perpendicular projection from\r\n% (1,1) is 75 degrees from the horizontal axis.  The length of\r\n% perpendicular projection to this line from (1,1) is about 29 units.\r\n%\r\n% That's about it.  I'll ask our writer to add more information about\r\n% the Hough transform coordinate system to the Users Guide.\r\n%\r\n% (Note: The coordinate systems shown in \r\n% _Digital Image Processing_ by Gonzalez and Woods, as well as _Digital Image \r\n% Processing Using MATLAB_ by\r\n% Gonzalez, Woods, and Eddins, are different from\r\n% what I've shown here.  In those sources, the x-axis is the vertical\r\n% axis.)\r\n\r\n##### SOURCE END ##### 0224e1dc6b0a434faef745dbe520477c\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   Reader JP asked me about my recent blog post on the Hough transform. Specifically, she wanted to know exactly how the rho-theta coordinate system was defined.  When I answered her question,\r\n   ... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/steve\/2006\/10\/19\/hough-transform-coordinate-system\/\">read more >><\/a><\/p>","protected":false},"author":42,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[50,90,230,36,68,78,94,96,130],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/89"}],"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=89"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/89\/revisions"}],"predecessor-version":[{"id":3510,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/89\/revisions\/3510"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media?parent=89"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/categories?post=89"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/tags?post=89"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}