{"id":4058,"date":"2014-08-19T18:00:21","date_gmt":"2014-08-19T23:00:21","guid":{"rendered":"https:\/\/blogs.mathworks.com\/seth\/?p=4058"},"modified":"2014-08-15T18:39:32","modified_gmt":"2014-08-15T23:39:32","slug":"a-matlab-vs-simulink-battle-im-in","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/simulink\/2014\/08\/19\/a-matlab-vs-simulink-battle-im-in\/","title":{"rendered":"A MATLAB vs. Simulink battle? I&#8217;m in!"},"content":{"rendered":"<p>When I saw the <a href=\"https:\/\/blogs.mathworks.com\/steve\/2014\/08\/12\/it-aint-easy-seeing-green-unless-you-have-matlab\">latest post by Matt Tearle<\/a> on <a href=\"https:\/\/blogs.mathworks.com\/steve\/\">Steve's blog<\/a>, I had to do something.<\/p>\r\n\r\n<p>Since the video at the center of this post is about a rap battle between MATLAB and Simulink, I thought it would be interesting to bring that to a higher level and see how the video processing could have been implemented in Simulink instead of MATLAB.<\/p>\r\n\r\n<p>Seriously... who wants to write code when you can simply connect blocks?<\/p>\r\n\r\n<p><strong>Overview<\/strong><\/p>\r\n\r\n<p>Since I have absolutely zero knowledge in image processing, I simply re-implemented the algorithm suggested by Matt in Simulink. The resulting model looks like:<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2014Q3\/battleModel.png\" alt=\"Green Screen processing\" \/><\/p>\r\n\r\n<p>Let's look at the details.<\/p>\r\n\r\n<p><strong>Importing the video<\/strong><\/p>\r\n\r\n<p>To begin, I used the <a href=\"https:\/\/www.mathworks.com\/help\/dsp\/ref\/frommultimediafile.html\">From Multimedia File<\/a> from the <a href=\"https:\/\/www.mathworks.com\/products\/dsp-system\/\">DSP System Toolbox<\/a> to directly read the AVI-file with our rappers in front of a green background. Then I use the <a href=\"https:\/\/www.mathworks.com\/help\/vision\/ref\/resize.html\">resize<\/a> block from the <a href=\"https:\/\/www.mathworks.com\/products\/computer-vision\/\">Computer Vision System Toolbox<\/a>. This allows me to work on images of a relatively small size. After, I normalize the data, as suggested by Matt.<\/p> \r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2014Q3\/importImage.png\" alt=\"Reading Video\" \/><\/p>\r\n\r\n<p><strong>The Greenness factor<\/strong><\/p>\r\n\r\n<p>I really like the simple equation the Matt figured out to calculate the \"greenness\" of each pixel in the image. In MATLAB, it looks like:<\/p>\r\n\r\n<p><code><span class=\"comment\">% Greenness = G*(G-R)*(G-B)<\/span>\r\ngreenness = yd(:,:,2).*(yd(:,:,2)-yd(:,:,1)).*(yd(:,:,2)-yd(:,:,3));<\/code><\/p>\r\n\r\nIn Simulink, I can use <a href=\"https:\/\/www.mathworks.com\/help\/simulink\/slref\/selector.html\">Selector<\/a> blocks to separate the three colors (third dimension of the image signal) and implement Matt's equation:\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2014Q3\/greenness.png\" alt=\"greenness\" \/><\/p>\r\n\r\n<p><strong>Thresholding<\/strong><\/p>\r\n\r\n<p>The next piece of code is something that many users struggle to figure out:<\/p>\r\n\r\n<p><code>thresh = 0.3*mean(greenness(greenness&gt;0));<\/code><\/p>\r\n\r\n<p>To implement logical indexing (e.g. <tt>A(A>0)<\/tt>) in Simulink, we need to combine the <a title=\"https:\/\/www.mathworks.com\/help\/simulink\/slref\/find.html (link no longer works)\">Find<\/a> block with a <a href=\"https:\/\/www.mathworks.com\/help\/simulink\/slref\/selector.html\">Selector<\/a> block. This results in a <a href=\"https:\/\/www.mathworks.com\/help\/simulink\/ug\/variable-size-signal-basics.html\">variable-size signal<\/a><\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2014Q3\/thresholding.png\" alt=\"thresholding\" \/><\/p>\r\n\r\n\r\n<p><strong>Combining the images<\/strong><\/p>\r\n\r\n<p>The last line we need to implement is to replace the pixels identified as green in the foreground image but their values in the background image. In MATLAB, this is accomplished using this line:<\/p>\r\n\r\n<p><code>rgb2(isgreen) = rgb1(isgreen);<\/code><\/p>\r\n\r\n<p>In Simulink, we need to extend the logic seen at the previous step. We first select the desired pixels from the background image, and then assign them to the foreground image:<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2014Q3\/combining.png\" alt=\"combining\" \/><\/p>\r\n\r\n<p>Note that the <a href=\"https:\/\/www.mathworks.com\/products\/computer-vision\/\">Computer Vision System Toolbox<\/a> has a <a href=\"https:\/\/www.mathworks.com\/help\/vision\/ref\/compositing.html\">Compositing<\/a> block implementing this functionality.<\/p>\r\n\r\n<p>Here is what the final result looks like:<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2014Q3\/battle.gif\" alt=\"battle\" \/><\/p>\r\n\r\n\r\n<p><strong>Now it's your turn<\/strong><\/p>\r\n\r\n<p>What do you think? Is it more convenient to implement this algorithm in MATLAB or Simulink? How do you typically decide between a <em>code-based<\/em> or a <em>block-based<\/em> implementation? Let us know by leaving a <a href=\"https:\/\/blogs.mathworks.com\/seth\/?p=4058&#comment\">comment here<\/a>.<\/p>","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2014Q3\/battle.gif\" onError=\"this.style.display ='none';\" \/><\/div><p>When I saw the latest post by Matt Tearle on Steve's blog, I had to do something.\r\n\r\nSince the video at the center of this post is about a rap battle between MATLAB and Simulink, I thought it would... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/simulink\/2014\/08\/19\/a-matlab-vs-simulink-battle-im-in\/\">read more >><\/a><\/p>","protected":false},"author":41,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[50,71],"tags":[402,194],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/4058"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/users\/41"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/comments?post=4058"}],"version-history":[{"count":15,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/4058\/revisions"}],"predecessor-version":[{"id":4083,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/4058\/revisions\/4083"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/media?parent=4058"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/categories?post=4058"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/tags?post=4058"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}