{"id":61,"date":"2006-06-06T08:33:59","date_gmt":"2006-06-06T12:33:59","guid":{"rendered":"https:\/\/blogs.mathworks.com\/steve\/?p=61"},"modified":"2019-10-22T12:27:36","modified_gmt":"2019-10-22T16:27:36","slug":"batch-processing","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/steve\/2006\/06\/06\/batch-processing\/","title":{"rendered":"Batch processing"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n<p> <em>23-Jun-2015 update: See the new <a href=\"https:\/\/www.mathworks.com\/help\/images\/batch-processing-using-the-image-batch-processor-app.html\">Image Batch Processor App<\/a> (added to R2015a) and my <a href=\"https:\/\/blogs.mathworks.com\/steve\/2015\/06\/23\/new-image-batch-processor-app-in-r2015a\/\">blog post about it<\/a>.<\/em>\r\n<\/p>\r\n      <p>A couple of months ago I was working with a bunch of pictures I had taken at home.  I had about 40 of them, and I needed to\r\n         crop and resize them all the same way.  Naturally, I wrote a MATLAB script to do it.\r\n      <\/p>\r\n      <p>This experience reminded me that customers sometimes ask \"How do I use the Image Processing Toolbox to do batch processing\r\n         of my images?\" Really, though, this isn't a toolbox question; it's a MATLAB question. In other words, the basic MATLAB techniques\r\n         for batch processing apply to any domain, not just image processing.\r\n      <\/p>\r\n   <\/introduction>\r\n   <h3>Contents<\/h3>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"#1\">Step 1: Get a list of filenames<\/a><\/li>\r\n         <li><a href=\"#4\">Step 2: Determine the processing steps to follow for each file<\/a><\/li>\r\n         <li><a href=\"#9\">Step 3: Put everything together in a for loop<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>Step 1: Get a list of filenames<a name=\"1\"><\/a><\/h3>\r\n   <p>If you use the <tt>dir<\/tt> function with an output argument, you get back a structure array containing the filenames, as well as other information about\r\n      each file.\r\n   <\/p>\r\n   <p>Let's say I want to process all files ending in .jpg:<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">files = dir(<span style=\"color: #A020F0\">'*.jpg'<\/span>)<\/pre><pre style=\"font-style:oblique\">files = \r\n42x1 struct array with fields:\r\n    name\r\n    date\r\n    bytes\r\n    isdir\r\n<\/pre><p>The <tt>files<\/tt> struct array has 42 elements, indicating that there are 42 files matching \"*.jpg\" in the current directory.  Let's look at\r\n      the details for a couple of these files.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">files(1)<\/pre><pre style=\"font-style:oblique\">ans = \r\n     name: 'IMG_0175.jpg'\r\n     date: '12-Feb-2006 10:49:30'\r\n    bytes: 962477\r\n    isdir: 0\r\n<\/pre><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">files(end)<\/pre><pre style=\"font-style:oblique\">ans = \r\n     name: 'IMG_0216.jpg'\r\n     date: '12-Feb-2006 11:09:10'\r\n    bytes: 1004842\r\n    isdir: 0\r\n<\/pre><h3>Step 2: Determine the processing steps to follow for each file<a name=\"4\"><\/a><\/h3>\r\n   <p>There are four basic steps to follow for each file:\r\n   <\/p>\r\n   <p>1. Read in the data from the file.<\/p>\r\n   <p>2. Process the data.<\/p>\r\n   <p>3. Construct the output filename.<\/p>\r\n   <p>4. Write out the processed data.<\/p>\r\n   <p>Here's what my read and processing steps looked like:<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">rgb = imread(<span style=\"color: #A020F0\">'IMG_0175.jpg'<\/span>);  <span style=\"color: #228B22\">% or rgb = imread(files(1).name);<\/span>\r\nrgb = rgb(1:1800, 520:2000, :);\r\nrgb = imresize(rgb, 0.2, <span style=\"color: #A020F0\">'bicubic'<\/span>);<\/pre><p>You have many options to consider for constructing the output filename. In my case, I wanted to use the same name but in a\r\n      subfolder:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">output_name = [<span style=\"color: #A020F0\">'cropped\\'<\/span> files(1).name] <span style=\"color: #228B22\">% Use fullfile instead if you want<\/span>\r\n                                         <span style=\"color: #228B22\">% multiplatform portability<\/span><\/pre><pre style=\"font-style:oblique\">output_name =\r\ncropped\\IMG_0175.jpg\r\n<\/pre><p>Here's another example of output name construction.  You might use something like this if you want to change image formats.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">input_name = files(1).name<\/pre><pre style=\"font-style:oblique\">input_name =\r\nIMG_0175.jpg\r\n<\/pre><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">[path, name, extension] = fileparts(input_name)<\/pre><pre style=\"font-style:oblique\">path =\r\n     ''\r\nname =\r\nIMG_0175\r\nextension =\r\n.jpg\r\n<\/pre><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">output_name = fullfile(path, [name <span style=\"color: #A020F0\">'.tif'<\/span>])<\/pre><pre style=\"font-style:oblique\">output_name =\r\nIMG_0175.tif\r\n<\/pre><h3>Step 3: Put everything together in a for loop<a name=\"9\"><\/a><\/h3>\r\n   <p>Here's my complete batch processing loop:<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">files = dir(<span style=\"color: #A020F0\">'*.JPG'<\/span>);\r\n<span style=\"color: #0000FF\">for<\/span> k = 1:numel(files)\r\n   rgb = imread(files(k).name);\r\n   rgb = rgb(1:1800, 520:2000, :);\r\n   rgb = imresize(rgb, 0.2, <span style=\"color: #A020F0\">'bicubic'<\/span>);\r\n   imwrite(rgb, [<span style=\"color: #A020F0\">'cropped\\'<\/span> files(k).name]);\r\n<span style=\"color: #0000FF\">end<\/span><\/pre><script language=\"JavaScript\"> \r\n<!--\r\n    function grabCode_61() {\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='61 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 61';\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_61()\"><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.2<br><\/p>\r\n<\/div>\r\n<!--\r\n61 ##### SOURCE BEGIN #####\r\n%%\r\n% A couple of months ago I was working with a bunch of pictures I had taken\r\n% at home.  I had about 40 of them, and I needed to crop and resize them\r\n% all the same way.  Naturally, I wrote a MATLAB script to do it.\r\n%\r\n% This experience reminded me that customers sometimes ask \"How do I use \r\n% the Image Processing Toolbox to do batch processing of my images?\"  \r\n% Really, though, this isn't a toolbox question; it's a MATLAB question. In\r\n% other words, the basic MATLAB techniques for batch processing apply to \r\n% any domain, not just image processing.\r\n\r\n%% Step 1: Get a list of filenames\r\n% If you use the |dir| function with an output argument, you get back a\r\n% structure array containing the filenames, as well as other information\r\n% about each file.\r\n%\r\n% Let's say I want to process all files ending in .jpg:\r\n\r\nfiles = dir('*.jpg')\r\n\r\n%%\r\n% The |files| struct array has 42 elements, indicating that there are 42\r\n% files matching \"*.jpg\" in the current directory.  Let's look at the\r\n% details for a couple of these files.\r\n\r\nfiles(1)\r\n\r\n%%\r\nfiles(end)\r\n\r\n%% Step 2: Determine the processing steps to follow for each file\r\n% There are four basic steps to follow for each file:\r\n%\r\n% 1. Read in the data from the file.\r\n%\r\n% 2. Process the data.\r\n%\r\n% 3. Construct the output filename.\r\n%\r\n% 4. Write out the processed data.\r\n%\r\n% Here's what my read and processing steps looked like:\r\n\r\nrgb = imread('IMG_0175.jpg');  % or rgb = imread(files(1).name);\r\nrgb = rgb(1:1800, 520:2000, :);\r\nrgb = imresize(rgb, 0.2, 'bicubic');\r\n\r\n%%\r\n% You have many options to consider for constructing the output filename.\r\n% In my case, I wanted to use the same name but in a subfolder:\r\n\r\noutput_name = ['cropped\\' files(1).name] % Use fullfile instead if you want\r\n                                         % multiplatform portability\r\n\r\n%%\r\n% Here's another example of output name construction.  You might use\r\n% something like this if you want to change image formats.\r\n\r\ninput_name = files(1).name\r\n\r\n%%\r\n\r\n[path, name, extension] = fileparts(input_name)\r\n\r\n%%\r\n\r\noutput_name = fullfile(path, [name '.tif'])\r\n\r\n%% Step 3: Put everything together in a for loop\r\n% Here's my complete batch processing loop:\r\n\r\nfiles = dir('*.JPG');\r\nfor k = 1:numel(files)\r\n   rgb = imread(files(k).name);\r\n   rgb = rgb(1:1800, 520:2000, :);\r\n   rgb = imresize(rgb, 0.2, 'bicubic');\r\n   imwrite(rgb, ['cropped\\' files(k).name]);\r\nend\r\n\r\n##### SOURCE END ##### 61\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n 23-Jun-2015 update: See the new Image Batch Processor App (added to R2015a) and my blog post about it.\r\n\r\n      A couple of months ago I was working with a bunch of pictures I had taken at... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/steve\/2006\/06\/06\/batch-processing\/\">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":[154,158,160,76,156,164,162],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/61"}],"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=61"}],"version-history":[{"count":2,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/61\/revisions"}],"predecessor-version":[{"id":1345,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/61\/revisions\/1345"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media?parent=61"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/categories?post=61"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/tags?post=61"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}