{"id":111,"date":"2007-01-01T20:39:44","date_gmt":"2007-01-02T01:39:44","guid":{"rendered":"https:\/\/blogs.mathworks.com\/steve\/?p=111"},"modified":"2019-10-23T08:41:09","modified_gmt":"2019-10-23T12:41:09","slug":"superimposing-line-plots","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/steve\/2007\/01\/01\/superimposing-line-plots\/","title":{"rendered":"Superimposing line plots on images"},"content":{"rendered":"<div class=\"content\">\r\n\r\nSeveral people have asked me recently how to plot some kind of shape on top of an image, so I thought I'd show the basic technique\r\nhere. Essentially, you display the image, then call <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/hold.html\"><tt>hold on<\/tt><\/a>, and then use <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/plot.html\"><tt>plot<\/tt><\/a> or some other graphics command to superimpose the desired shape.\r\n\r\nFor my first example, I'll superimpose the boundaries found by the <a href=\"https:\/\/www.mathworks.com\/help\/images\/ref\/bwboundaries.html\"><tt>bwboundaries<\/tt><\/a> function on top of the original binary image.\r\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\">bw = imread(<span style=\"color: #a020f0;\">'circles.png'<\/span>);\r\nb = bwboundaries(bw);\r\nimshow(bw)<\/pre>\r\n<img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/111\/superimposing_plots_01.jpg\" alt=\"\" hspace=\"5\" vspace=\"5\" \/>\r\n\r\nNow call <tt>hold on<\/tt>. This causes subsequent plotting commands to add to what's already in the figure, instead of replacing it.\r\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\">hold <span style=\"color: #a020f0;\">on<\/span><\/pre>\r\n<img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/111\/superimposing_plots_02.jpg\" alt=\"\" hspace=\"5\" vspace=\"5\" \/>\r\n\r\nFinally, call plot to superimpose the boundary locations.\r\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\"><span style=\"color: #0000ff;\">for<\/span> k = 1:numel(b)\r\n    plot(b{k}(:,2), b{k}(:,1), <span style=\"color: #a020f0;\">'r'<\/span>, <span style=\"color: #a020f0;\">'Linewidth'<\/span>, 3)\r\n<span style=\"color: #0000ff;\">end<\/span><\/pre>\r\n<img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/111\/superimposing_plots_03.jpg\" alt=\"\" hspace=\"5\" vspace=\"5\" \/>\r\n\r\nIt's good to get in the habit of calling <tt>hold off<\/tt> when you're done adding plot elements. That way, the next high-level plotting command will start over, which is usually\r\nwhat is expected.\r\n\r\nMy second example shows how to superimpose a 25-pixel-by-25-pixel grid on an image. (See <a href=\"https:\/\/blogs.mathworks.com\/steve\/?p=94#comments\">Natasha's blog comment<\/a>.) Display the image first, and then call <tt>hold on<\/tt>.\r\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\">clf\r\nrgb = imread(<span style=\"color: #a020f0;\">'peppers.png'<\/span>);\r\nimshow(rgb)\r\nhold <span style=\"color: #a020f0;\">on<\/span><\/pre>\r\n<img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/111\/superimposing_plots_04.jpg\" alt=\"\" hspace=\"5\" vspace=\"5\" \/>\r\n\r\nNow superimpose the grid. To make sure the grid is visible over all pixel colors, I'll use the trick of superimposing two\r\nline objects with contrasting colors and line styles. The <a href=\"https:\/\/www.mathworks.com\/help\/images\/ref\/impixelregion.html\">Pixel Region Tool<\/a> in the Image Processing Toolbox uses this same trick.\r\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\">M = size(rgb,1);\r\nN = size(rgb,2);\r\n\r\n<span style=\"color: #0000ff;\">for<\/span> k = 1:25:M\r\n    x = [1 N];\r\n    y = [k k];\r\n    plot(x,y,<span style=\"color: #a020f0;\">'Color'<\/span>,<span style=\"color: #a020f0;\">'w'<\/span>,<span style=\"color: #a020f0;\">'LineStyle'<\/span>,<span style=\"color: #a020f0;\">'-'<\/span>);\r\n    plot(x,y,<span style=\"color: #a020f0;\">'Color'<\/span>,<span style=\"color: #a020f0;\">'k'<\/span>,<span style=\"color: #a020f0;\">'LineStyle'<\/span>,<span style=\"color: #a020f0;\">':'<\/span>);\r\n<span style=\"color: #0000ff;\">end<\/span>\r\n\r\n<span style=\"color: #0000ff;\">for<\/span> k = 1:25:N\r\n    x = [k k];\r\n    y = [1 M];\r\n    plot(x,y,<span style=\"color: #a020f0;\">'Color'<\/span>,<span style=\"color: #a020f0;\">'w'<\/span>,<span style=\"color: #a020f0;\">'LineStyle'<\/span>,<span style=\"color: #a020f0;\">'-'<\/span>);\r\n    plot(x,y,<span style=\"color: #a020f0;\">'Color'<\/span>,<span style=\"color: #a020f0;\">'k'<\/span>,<span style=\"color: #a020f0;\">'LineStyle'<\/span>,<span style=\"color: #a020f0;\">':'<\/span>);\r\n<span style=\"color: #0000ff;\">end<\/span>\r\n\r\nhold <span style=\"color: #a020f0;\">off<\/span><\/pre>\r\n<img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/111\/superimposing_plots_05.jpg\" alt=\"\" hspace=\"5\" vspace=\"5\" \/><script language=\"JavaScript\">\/\/ <![CDATA[\r\n\r\n\r\n    function grabCode_4e84b66ff0f34061a1d4bc1af568543e() {\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='4e84b66ff0f34061a1d4bc1af568543e ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 4e84b66ff0f34061a1d4bc1af568543e';\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('\r\n\r\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>\r\n\r\n\r\n\\n');\r\n\r\n      d.title = title + ' (MATLAB code)';\r\n      d.close();\r\n      }   \r\n\r\n\/\/ ]]><\/script>\r\n<p style=\"text-align: right; font-size: xx-small; font-weight: lighter; font-style: italic; color: gray;\"><a><span style=\"font-size: x-small; font-style: italic;\">Get\r\nthe MATLAB code\r\n<noscript>(requires JavaScript)<\/noscript><\/span><\/a><\/p>\r\nPublished with MATLAB\u00ae 7.3\r\n\r\n<\/div>\r\n<!--\r\n4e84b66ff0f34061a1d4bc1af568543e ##### SOURCE BEGIN #####\r\n%% Superimposing line plots on images\r\n% Several people have asked me recently how to plot some kind of shape on\r\n% top of an image, so I thought I'd show the basic technique here.\r\n% Essentially, you display the image, then call\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/hold.html\r\n% |hold on|>, and then use\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/plot.html\r\n% |plot|> or some other graphics command to superimpose the desired shape.\r\n%\r\n% For my first example, I'll superimpose the boundaries found by the\r\n% <https:\/\/www.mathworks.com\/help\/images\/index.htmlbwboundaries.html\r\n% |bwboundaries|> function on top of the original binary image.\r\n\r\nbw = imread('circles.png');\r\nb = bwboundaries(bw);\r\nimshow(bw)\r\n\r\n%%\r\n% Now call |hold on|.  This causes subsequent plotting commands to add to\r\n% what's already in the figure, instead of replacing it.\r\n\r\nhold on\r\n\r\n%%\r\n% Finally, call plot to superimpose the boundary locations.\r\n\r\nfor k = 1:numel(b)\r\nplot(b{k}(:,2), b{k}(:,1), 'r', 'Linewidth', 3)\r\nend\r\n\r\n%%\r\n% It's good to get in the habit of calling |hold off| when you're done\r\n% adding plot elements.  That way, the next high-level plotting command\r\n% will start over, which is usually what is expected.\r\n\r\n%%\r\n% My second example shows how to superimpose a 25-pixel-by-25-pixel grid on\r\n% an image.  (See\r\n% <https:\/\/blogs.mathworks.com\/steve\/?p=94#comments Natasha's blog comment>.)\r\n% Display the image first, and\r\n% then call |hold on|.\r\n\r\nclf\r\nrgb = imread('peppers.png');\r\nimshow(rgb)\r\nhold on\r\n\r\n%%\r\n% Now superimpose the grid.  To make sure the grid is visible over all\r\n% pixel colors, I'll use the trick of superimposing two line objects with\r\n% contrasting colors and line styles.  The\r\n% <https:\/\/www.mathworks.com\/help\/images\/ref\/impixelregion.html\r\n% Pixel Region Tool> in the Image\r\n% Processing Toolbox uses this same trick.\r\n\r\nM = size(rgb,1);\r\nN = size(rgb,2);\r\n\r\nfor k = 1:25:M\r\nx = [1 N];\r\ny = [k k];\r\nplot(x,y,'Color','w','LineStyle','-');\r\nplot(x,y,'Color','k','LineStyle',':');\r\nend\r\n\r\nfor k = 1:25:N\r\nx = [k k];\r\ny = [1 M];\r\nplot(x,y,'Color','w','LineStyle','-');\r\nplot(x,y,'Color','k','LineStyle',':');\r\nend\r\n\r\nhold off\r\n##### SOURCE END ##### 4e84b66ff0f34061a1d4bc1af568543e\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n\r\nSeveral people have asked me recently how to plot some kind of shape on top of an image, so I thought I'd show the basic technique\r\nhere. Essentially, you display the image, then call hold on,... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/steve\/2007\/01\/01\/superimposing-line-plots\/\">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":[88,178,90,76,36,162,68,190],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/111"}],"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=111"}],"version-history":[{"count":3,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/111\/revisions"}],"predecessor-version":[{"id":1290,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/111\/revisions\/1290"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media?parent=111"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/categories?post=111"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/tags?post=111"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}