{"id":6807,"date":"2016-04-08T09:00:34","date_gmt":"2016-04-08T13:00:34","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=6807"},"modified":"2016-12-22T11:35:14","modified_gmt":"2016-12-22T16:35:14","slug":"subpixel-motion-estimation-without-interpolation","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2016\/04\/08\/subpixel-motion-estimation-without-interpolation\/","title":{"rendered":"Subpixel Motion Estimation without Interpolation"},"content":{"rendered":"<a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/4291457-avi-nehemiah\">Avi<\/a>'s pick of the week is <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/44244-subpixel-motion-estimation-without-interpolation\">Subpixel Motion Estimation without Interpolation<\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/3256890-stanley-chan\">Stanley Chan<\/a>.\r\n\r\n<br class=\"clear\" \/><br class=\"clear\" \/> When I work on video processing problems one of the tasks I perform most frequently is estimating the velocity of individual pixels\u00a0in a video stream. It was while solving one of these problems that I stumbled\u00a0upon Stanley Chan's submission on sub-pixel motion estimation without interpolation.\r\n<h2><strong>Contents<\/strong><\/h2>\r\n<div>\r\n<ul>\r\n\t<li><a href=\"#1\">Read Input Video<\/a><\/li>\r\n\t<li><a href=\"#2\">Setup Motion Estimation Parameter<\/a><\/li>\r\n\t<li><a href=\"#3\">Estimate Motion Between First Two Frames<\/a><\/li>\r\n\t<li><a href=\"#4\">Quiver plot to view motion vectors<\/a><\/li>\r\n\t<li><a href=\"#5\">Other motion estimation techniques<\/a><\/li>\r\n<\/ul>\r\n<\/div>\r\n<h2><strong>Read Input Video<\/strong><a name=\"1\"><\/a><\/h2>\r\nLet's start by reading in a video which we'll use to estimate the motion between individual pixels from frame to frame.\r\n<pre class=\"codeinput\">vidReader = VideoReader(<span class=\"string\">'visiontraffic.avi'<\/span>,<span class=\"string\">'CurrentTime'<\/span>,11);<\/pre>\r\n<h2><strong>Setup Motion Estimation Parameters<\/strong><a name=\"2\"><\/a>\r\n<br class=\"clear\" \/>We then setup the parameters for the motion estimation, in this case we set the block size to 8 and the pixel search limit to 10.<\/h2>\r\n<pre class=\"codeinput\">opts.BlockSize   = 8;\r\nopts.SearchLimit = 10;\r\n<\/pre>\r\n<h2><strong>Estimate Motion Between First Two Frames<\/strong><a name=\"3\"><\/a><\/h2>\r\nNow lets read the first two frames of video and and estimate the motion using the Bidirectional_ME function from the FileExchange entry.\r\n<pre class=\"codeinput\">frameRGBLast = readFrame(vidReader);\r\nframeGrayLast = rgb2gray(frameRGBLast);\r\nframeRGB = readFrame(vidReader);\r\nframeGray = rgb2gray(frameRGB);\r\n[MVx, MVy] = Bidirectional_ME(im2double(frameGray), im2double(frameGrayLast)<span class=\"keyword\">...<\/span>\r\n            , opts);\r\n\r\nfigure;\r\nsubplot(2,2,1)\r\nimagesc(MVx.^2);\r\ntitle(<span class=\"string\">'Motion Field X-Direction'<\/span>);\r\naxis <span class=\"string\">image<\/span>;\r\nsubplot(2,2,2)\r\nimagesc(MVy.^2);\r\ntitle(<span class=\"string\">'Motion Field Y-Direction'<\/span>);\r\naxis <span class=\"string\">image<\/span>;\r\nsubplot(2,2,3);\r\nimshow(frameRGBLast);\r\ntitle(<span class=\"string\">'Previous Frame'<\/span>);\r\nsubplot(2,2,4);\r\nimshow(frameRGB);\r\ntitle(<span class=\"string\">'Current Frame'<\/span>);\r\n<\/pre>\r\n<a href=\"https:\/\/blogs.mathworks.com\/pick\/files\/POTW_Post_01.png\" rel=\"attachment wp-att-6809\"><img decoding=\"async\" loading=\"lazy\" width=\"560\" height=\"420\" class=\"aligncenter size-full wp-image-6809\" src=\"https:\/\/blogs.mathworks.com\/pick\/files\/POTW_Post_01.png\" alt=\"POTW_Post_01\" \/><\/a>\r\n<h2><\/h2>\r\n<h2><strong>Quiver plot to view motion vectors<\/strong><a name=\"4\"><\/a><\/h2>\r\nYou can also view the motion vectors using a quiver plot.\r\n<pre class=\"codeinput\">figure;\r\nquiver(MVx(end:-1:1,:), MVy(end:-1:1,:));\r\naxis <span class=\"string\">image<\/span>;\r\ntitle(<span class=\"string\">'Motion Vector Field'<\/span>);\r\n<a href=\"https:\/\/blogs.mathworks.com\/pick\/files\/POTW_Post_02.png\" rel=\"attachment wp-att-6810\"><img decoding=\"async\" loading=\"lazy\" width=\"560\" height=\"420\" class=\"aligncenter size-full wp-image-6810\" src=\"https:\/\/blogs.mathworks.com\/pick\/files\/POTW_Post_02.png\" alt=\"POTW_Post_02\" \/><\/a><\/pre>\r\n<h2><strong>Other motion estimation techniques<\/strong><a name=\"5\"><\/a><\/h2>\r\nThe Computer Vision System Toolbox provides several other ways to do motion estimation include some variants of optical flow and block matching. To learn more type\r\n<pre class=\"codeinput\">doc <span class=\"string\">vision<\/span>\r\n<\/pre>\r\n<h2><\/h2>\r\n<h2><\/h2>","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/pick\/files\/POTW_Post_01.png\" onError=\"this.style.display ='none';\" \/><\/div><p>Avi's pick of the week is Subpixel Motion Estimation without Interpolation by Stanley Chan.\r\n\r\n When I work on video processing problems one of the tasks I perform most frequently is estimating the... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2016\/04\/08\/subpixel-motion-estimation-without-interpolation\/\">read more >><\/a><\/p>","protected":false},"author":132,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/6807"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/users\/132"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=6807"}],"version-history":[{"count":37,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/6807\/revisions"}],"predecessor-version":[{"id":8214,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/6807\/revisions\/8214"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=6807"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=6807"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=6807"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}