{"id":1753,"date":"2016-04-14T14:14:52","date_gmt":"2016-04-14T18:14:52","guid":{"rendered":"https:\/\/blogs.mathworks.com\/steve\/?p=1753"},"modified":"2019-11-01T16:37:24","modified_gmt":"2019-11-01T20:37:24","slug":"jpeg2000-and-specifying-a-target-compression-ratio","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/steve\/2016\/04\/14\/jpeg2000-and-specifying-a-target-compression-ratio\/","title":{"rendered":"JPEG2000 and specifying a target compression ratio"},"content":{"rendered":"<div class=\"content\"><p>I was looking at some documentation yesterday and saw something that I had forgotten. When you write a JPEG2000 image file using <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/imwrite.html\"><tt>imwrite<\/tt><\/a>, you can specify a desired compression ratio.<\/p><p>OK, that sounds fun. Let's try it with the peppers image.<\/p><pre class=\"codeinput\">rgb = imread(<span class=\"string\">'peppers.png'<\/span>);\r\nimshow(rgb)\r\ntitle(<span class=\"string\">'Original image'<\/span>)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/steve\/files\/TargetCompressionRatioExample_01.jpg\" alt=\"\"> <p>Shall we start with a modest compression ratio of 10?<\/p><pre class=\"codeinput\">imwrite(rgb,<span class=\"string\">'peppers_10.j2k'<\/span>,<span class=\"string\">'CompressionRatio'<\/span>,10);\r\nimshow(<span class=\"string\">'peppers_10.j2k'<\/span>)\r\ntitle(<span class=\"string\">'Target compression ratio: 10'<\/span>)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/steve\/files\/TargetCompressionRatioExample_02.jpg\" alt=\"\"> <p>That looks the same.<\/p><p>Hold on, though. Let's make sure we agree on what compression ratio means. And while we're at it, let's check the output file to verify the actual ratio.<\/p><p>The in-memory storage format for this image is 3 color values for each pixel, and each color value is stored using 1 byte. So the total number of in-memory bytes to represent the image is the number of rows times the number of columns times 3.<\/p><pre class=\"codeinput\">size(rgb)\r\n<\/pre><pre class=\"codeoutput\">\r\nans =\r\n\r\n   384   512     3\r\n\r\n<\/pre><pre class=\"codeinput\">num_mem_bytes = prod(ans)\r\n<\/pre><pre class=\"codeoutput\">\r\nnum_mem_bytes =\r\n\r\n      589824\r\n\r\n<\/pre><p>Now let's figure out the size of the JPEG2000 file we just created.<\/p><pre class=\"codeinput\">s = dir(<span class=\"string\">'peppers_10.j2k'<\/span>);\r\nnum_file_bytes = s.bytes\r\n<\/pre><pre class=\"codeoutput\">\r\nnum_file_bytes =\r\n\r\n       58384\r\n\r\n<\/pre><p>The compression ratio is the ratio of those two numbers.<\/p><pre class=\"codeinput\">r = num_mem_bytes \/ num_file_bytes\r\n<\/pre><pre class=\"codeoutput\">\r\nr =\r\n\r\n   10.1025\r\n\r\n<\/pre><p>That's pretty close to the specified target.<\/p><p>Let's compress the image by a factor of 20.<\/p><pre class=\"codeinput\">imwrite(rgb,<span class=\"string\">'peppers_20.j2k'<\/span>,<span class=\"string\">'CompressionRatio'<\/span>,20)\r\nimshow(<span class=\"string\">'peppers_20.j2k'<\/span>)\r\ntitle(<span class=\"string\">'Target compression ratio: 20'<\/span>)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/steve\/files\/TargetCompressionRatioExample_03.jpg\" alt=\"\"> <p>I'm still seeing very little difference. Let's dial it all the way up to 100.<\/p><pre class=\"codeinput\">imwrite(rgb,<span class=\"string\">'peppers_100.j2k'<\/span>,<span class=\"string\">'CompressionRatio'<\/span>,100)\r\nimshow(<span class=\"string\">'peppers_100.j2k'<\/span>)\r\ntitle(<span class=\"string\">'Target compression ratio: 100'<\/span>)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/steve\/files\/TargetCompressionRatioExample_04.jpg\" alt=\"\"> <p>Now that's getting pretty bad. Let's zoom into a region and compare more closely.<\/p><pre class=\"codeinput\">subplot(1,2,1)\r\nimshow(rgb)\r\nxlim([310 440])\r\nylim([30 130])\r\ntitle(<span class=\"string\">'Original'<\/span>)\r\n\r\nsubplot(1,2,2)\r\nimshow(<span class=\"string\">'peppers_100.j2k'<\/span>)\r\nxlim([310 440])\r\nylim([30 130])\r\ntitle(<span class=\"string\">'Target compression ratio: 100'<\/span>)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/steve\/files\/TargetCompressionRatioExample_05.jpg\" alt=\"\"> <p>How extreme can we get? Let's try 1000.<\/p><pre class=\"codeinput\">imwrite(rgb,<span class=\"string\">'peppers_1000.j2k'<\/span>,<span class=\"string\">'CompressionRatio'<\/span>,1000)\r\nclf\r\nimshow(<span class=\"string\">'peppers_1000.j2k'<\/span>)\r\ntitle(<span class=\"string\">'Target compression ratio: 1000'<\/span>)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/steve\/files\/TargetCompressionRatioExample_06.jpg\" alt=\"\"> <p>That's pretty ugly. But how big is the file? Did we get close to the target?<\/p><pre class=\"codeinput\">s = dir(<span class=\"string\">'peppers_1000.j2k'<\/span>);\r\ns.bytes\r\n<\/pre><pre class=\"codeoutput\">\r\nans =\r\n\r\n   545\r\n\r\n<\/pre><pre class=\"codeinput\">num_mem_bytes \/ s.bytes\r\n<\/pre><pre class=\"codeoutput\">\r\nans =\r\n\r\n   1.0822e+03\r\n\r\n<\/pre><p>That last image is stored using just 545 bytes! That's a compression ratio of about 1082.<\/p><p>I know JPEG2000 is used in some medical imaging systems, and I have also heard that the Library of Congress uses it.<\/p><p>Do you use JPEG2000? If so, please leave a comment. I'd be interested to hear about your application.<\/p><script language=\"JavaScript\"> <!-- \r\n    function grabCode_1502e1add6094638beb22f92f95fca6d() {\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='1502e1add6094638beb22f92f95fca6d ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 1502e1add6094638beb22f92f95fca6d';\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        copyright = 'Copyright 2016 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 copyright line at the bottom if specified.\r\n        if (copyright.length > 0) {\r\n            d.writeln('');\r\n            d.writeln('%%');\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     --> <\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_1502e1add6094638beb22f92f95fca6d()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n      the MATLAB code <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; R2016a<br><\/p><\/div><!--\r\n1502e1add6094638beb22f92f95fca6d ##### SOURCE BEGIN #####\r\n%%\r\n% I was looking at some documentation yesterday and saw something that I had\r\n% forgotten. When you write a JPEG2000 image file using \r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/imwrite.html |imwrite|>, you can\r\n% specify a desired compression ratio.\r\n%\r\n% OK, that sounds fun. Let's try it with the peppers image.\r\n\r\nrgb = imread('peppers.png');\r\nimshow(rgb)\r\ntitle('Original image')\r\n\r\n%%\r\n% Shall we start with a modest compression ratio of 10?\r\n\r\nimwrite(rgb,'peppers_10.j2k','CompressionRatio',10);\r\nimshow('peppers_10.j2k')\r\ntitle('Target compression ratio: 10')\r\n\r\n%%\r\n% That looks the same.\r\n%\r\n% Hold on, though. Let's make sure we agree on what compression ratio means.\r\n% And while we're at it, let's check the output file to verify the actual\r\n% ratio.\r\n%\r\n% The in-memory storage format for this image is 3 color values for each\r\n% pixel, and each color value is stored using 1 byte. So the total number of\r\n% in-memory bytes to represent the image is the number of rows times the\r\n% number of columns times 3.\r\n\r\nsize(rgb)\r\n\r\n%%\r\nnum_mem_bytes = prod(ans)\r\n\r\n%%\r\n% Now let's figure out the size of the JPEG2000 file we just created.\r\n\r\ns = dir('peppers_10.j2k');\r\nnum_file_bytes = s.bytes\r\n\r\n%%\r\n% The compression ratio is the ratio of those two numbers.\r\n\r\nr = num_mem_bytes \/ num_file_bytes\r\n\r\n%%\r\n% That's pretty close to the specified target.\r\n%\r\n% Let's compress the image by a factor of 20.\r\n\r\nimwrite(rgb,'peppers_20.j2k','CompressionRatio',20)\r\nimshow('peppers_20.j2k')\r\ntitle('Target compression ratio: 20')\r\n\r\n%%\r\n% I'm still seeing very little difference. Let's dial it all the way up to\r\n% 100.\r\n\r\nimwrite(rgb,'peppers_100.j2k','CompressionRatio',100)\r\nimshow('peppers_100.j2k')\r\ntitle('Target compression ratio: 100')\r\n\r\n%%\r\n% Now that's getting pretty bad. Let's zoom into a region and compare more\r\n% closely.\r\n\r\nsubplot(1,2,1)\r\nimshow(rgb)\r\nxlim([310 440])\r\nylim([30 130])\r\ntitle('Original')\r\n\r\nsubplot(1,2,2)\r\nimshow('peppers_100.j2k')\r\nxlim([310 440])\r\nylim([30 130])\r\ntitle('Target compression ratio: 100')\r\n\r\n%%\r\n% How extreme can we get? Let's try 1000.\r\n\r\nimwrite(rgb,'peppers_1000.j2k','CompressionRatio',1000)\r\nclf\r\nimshow('peppers_1000.j2k')\r\ntitle('Target compression ratio: 1000')\r\n\r\n%%\r\n% That's pretty ugly. But how big is the file? Did we get close to the\r\n% target?\r\n\r\ns = dir('peppers_1000.j2k');\r\ns.bytes\r\n\r\n%%\r\n\r\nnum_mem_bytes \/ s.bytes\r\n\r\n%%\r\n% That last image is stored using just 545 bytes! That's a\r\n% compression ratio of about 1082.\r\n%\r\n% I know JPEG2000 is used in some medical imaging systems, and I have also\r\n% heard that the Library of Congress uses it.\r\n%\r\n% Do you use JPEG2000? If so, please leave a comment. I'd be interested to\r\n% hear about your application.\r\n\r\n##### SOURCE END ##### 1502e1add6094638beb22f92f95fca6d\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img src=\"https:\/\/blogs.mathworks.com\/steve\/files\/TargetCompressionRatioExample_05.jpg\" class=\"img-responsive attachment-post-thumbnail size-post-thumbnail wp-post-image\" alt=\"\" decoding=\"async\" loading=\"lazy\" \/><\/div><p>I was looking at some documentation yesterday and saw something that I had forgotten. When you write a JPEG2000 image file using imwrite, you can specify a desired compression ratio.OK, that sounds... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/steve\/2016\/04\/14\/jpeg2000-and-specifying-a-target-compression-ratio\/\">read more >><\/a><\/p>","protected":false},"author":42,"featured_media":1759,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[178,154,76,36,164,625,190,72,52,360,298],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/1753"}],"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=1753"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/1753\/revisions"}],"predecessor-version":[{"id":1754,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/1753\/revisions\/1754"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media\/1759"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media?parent=1753"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/categories?post=1753"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/tags?post=1753"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}