{"id":1108,"date":"2014-07-23T13:52:31","date_gmt":"2014-07-23T17:52:31","guid":{"rendered":"https:\/\/blogs.mathworks.com\/steve\/?p=1108"},"modified":"2019-11-01T11:14:37","modified_gmt":"2019-11-01T15:14:37","slug":"image-types","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/steve\/2014\/07\/23\/image-types\/","title":{"rendered":"Image Types"},"content":{"rendered":"\r\n<div class=\"content\"><!--introduction--><p><i>Today's post is part of an <a href=\"https:\/\/blogs.mathworks.com\/steve\/category\/dipum-tutorials\/\">ongoing (but long delayed) tutorial series on digital image processing using MATLAB<\/a>. I'm covering topics in roughly the order used in the book <a href=\"http:\/\/imageprocessingplace.com\/DIPUM-2E\/dipum2e_main_page.htm\">Digital Image Processing Using MATLAB<\/a>.<\/i><\/p><p>In the previous post in this series, I discussed the different numeric data types that commonly come into play when doing image processing in MATLAB. Today I want to talk about the four main <i>image types<\/i>:<\/p><div><ul><li>Gray-scale images<\/li><li>Binary images<\/li><li>Indexed images<\/li><li>RGB images<\/li><\/ul><\/div><!--\/introduction--><h3>Contents<\/h3><div><ul><li><a href=\"#624f38ad-33f5-44e5-a741-17521c52bdb4\">Gray-Scale Images<\/a><\/li><li><a href=\"#6a1ab15a-13b1-4616-a2f4-f4b36364d686\">Binary Images<\/a><\/li><li><a href=\"#b54e5696-a19d-4223-8b46-178c7657f0e5\">Indexed Images<\/a><\/li><li><a href=\"#8d1f29ff-020b-4b85-a6b3-693f1ebbce45\">RGB Images<\/a><\/li><\/ul><\/div><h4>Gray-Scale Images<a name=\"624f38ad-33f5-44e5-a741-17521c52bdb4\"><\/a><\/h4><p>A <i>gray-scale image<\/i> is a matrix whose values represent shades of gray. When the matrix is of type <tt>uint8<\/tt>, the integer-valued elements are in the range [0,255]. By convention, the value 0 is displayed as black, and the value 255 is displayed as white. Values in-between are displayed as intermediate shades of gray.<\/p><p>When the matrix is of type <tt>uint16<\/tt>, then 0 is displayed as black and 65535 is displayed as white.<\/p><p>For a floating-point matrix, either of type <tt>double<\/tt> or <tt>single<\/tt>, the value 1.0 is displayed as white.<\/p><p>Originally, the Image Processing Toolbox documentation called these <i>intensity images<\/i>, and you might still find this term used in some places. Some of our color scientist users complained, though, that the term <i>intensity image<\/i> meant something slightly different in their field, so we (mostly) changed our terminology.<\/p><pre class=\"codeinput\">I = imread(<span class=\"string\">'rice.png'<\/span>);\r\nwhos <span class=\"string\">I<\/span>\r\n<\/pre><pre class=\"codeoutput\">  Name        Size             Bytes  Class    Attributes\r\n\r\n  I         256x256            65536  uint8              \r\n\r\n<\/pre><pre class=\"codeinput\">imshow(I)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2014\/image_types_01.jpg\" alt=\"\"> <h4>Binary Images<a name=\"6a1ab15a-13b1-4616-a2f4-f4b36364d686\"><\/a><\/h4><p>In image processing, the term <i>binary image<\/i> refers to a two-valued image whose pixes are either black or white. (Or, a bit more generally, the pixels are either background or foreground.) In MATLAB and the Image Processing Toolbox, we have adopted the convention that binary images are represented as <i>logical matrices<\/i>. Here's an example of constructing a matrix whose type is logical and then displaying the result as a binary (black-and-white) image.<\/p><pre class=\"codeinput\">[xx,yy] = meshgrid(-100:100);\r\nbw = hypot(xx,yy) &lt;= 50;\r\nwhos <span class=\"string\">bw<\/span>\r\n<\/pre><pre class=\"codeoutput\">  Name        Size             Bytes  Class      Attributes\r\n\r\n  bw        201x201            40401  logical              \r\n\r\n<\/pre><pre class=\"codeinput\">imshow(bw)\r\ntitle(<span class=\"string\">'Binary image'<\/span>)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2014\/image_types_02.jpg\" alt=\"\"> <p>Note that a common mistake is to create a <tt>uint8<\/tt> matrix and fill it with 0s and 1s, thinking that it will display as black and white.<\/p><pre class=\"codeinput\">bw = zeros(30,30,<span class=\"string\">'uint8'<\/span>);\r\nbw(10:20,10:20) = 1;\r\nimshow(bw,<span class=\"string\">'InitialMagnification'<\/span>,<span class=\"string\">'fit'<\/span>)\r\ntitle(<span class=\"string\">'Help, my image is all black!'<\/span>)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2014\/image_types_03.jpg\" alt=\"\"> <p>When a matrix is of type <tt>uint8<\/tt>, then the value 1 is not white; it's almost completely black!<\/p><h4>Indexed Images<a name=\"b54e5696-a19d-4223-8b46-178c7657f0e5\"><\/a><\/h4><p>An <i>indexed image<\/i> has two components: an <i>index matrix<\/i> of integers, commonly called <tt>X<\/tt>, and a <tt>color map matrix<\/tt>, commonly called <tt>map<\/tt>. The matrix <tt>map<\/tt> is an M-by-3 matrix of floating-point values (either <tt>double<\/tt> or <tt>single<\/tt>) in the range [0.0,1.0]. Each row of <tt>map<\/tt> specifies the red, green, and blue components of a single color. An indexed image is displayed by mapping values in the index matrix to colors in the color map. A quirk of MATLAB is that this mapping is data-type specific. If the index matrix is floating-point, then the value 1.0 corresponds to the first color in the color map. But if the index matrix is <tt>uint8<\/tt> or <tt>uint16<\/tt>, then the value 0 corresponds to the first color. (Maybe I'll explain the reasons for that on another day.)<\/p><p>You display an indexed image by passing both the index matrix and the color map matrix to <tt>imshow<\/tt>, like this:<\/p><pre class=\"codeinput\">[X,map] = imread(<span class=\"string\">'trees.tif'<\/span>);\r\nwhos <span class=\"string\">X<\/span> <span class=\"string\">map<\/span>\r\n<\/pre><pre class=\"codeoutput\">  Name        Size             Bytes  Class     Attributes\r\n\r\n  X         258x350            90300  uint8               \r\n  map       256x3               6144  double              \r\n\r\n<\/pre><pre class=\"codeinput\">imshow(X,map)\r\ntitle(<span class=\"string\">'Indexed image'<\/span>)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2014\/image_types_04.jpg\" alt=\"\"> <h4>RGB Images<a name=\"8d1f29ff-020b-4b85-a6b3-693f1ebbce45\"><\/a><\/h4><p>An RGB image is an M-by-N-by-3 array. For a particular pixel at row <tt>r<\/tt> and column <tt>c<\/tt>, the three values <tt>RGB(r,c,1)<\/tt>, <tt>RGB(r,c,2)<\/tt>, and <tt>RGB(r,c,3)<\/tt> specify the red, green, and blue color components of that pixel. A pixel whose color components are [0,0,0] is displayed as black. For a floating-point array, a pixel whose color components are [1.0,1.0,1.0] is displayed as white. For a <tt>uint8<\/tt> or <tt>uint16<\/tt> array, either [255,255,255] or [65535,65535,65535] is displayed as white.<\/p><pre class=\"codeinput\">RGB = imread(<span class=\"string\">'peppers.png'<\/span>);\r\nwhos <span class=\"string\">RGB<\/span>\r\n<\/pre><pre class=\"codeoutput\">  Name        Size                Bytes  Class    Attributes\r\n\r\n  RGB       384x512x3            589824  uint8              \r\n\r\n<\/pre><pre class=\"codeinput\">imshow(RGB)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2014\/image_types_05.jpg\" alt=\"\"> <p>You can think of an RGB image as a \"stack\" of three gray-scale images. These gray-scale images are commonly called the <i>component images<\/i>.<\/p><pre class=\"codeinput\">imshow(RGB(:,:,1))\r\ntitle(<span class=\"string\">'Red component image'<\/span>)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2014\/image_types_06.jpg\" alt=\"\"> <pre class=\"codeinput\">imshow(RGB(:,:,2))\r\ntitle(<span class=\"string\">'Green component image'<\/span>)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2014\/image_types_07.jpg\" alt=\"\"> <pre class=\"codeinput\">imshow(RGB(:,:,3))\r\ntitle(<span class=\"string\">'Blue component image'<\/span>)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2014\/image_types_08.jpg\" alt=\"\"> <p>In my first year of blogging (2006), I wrote a <a href=\"https:\/\/blogs.mathworks.com\/steve\/category\/pixel-colors\/\">series of blog posts<\/a> explaining in great detail how the MATLAB image display model works for various image and data types. The series is still up-to-date and worth reading today. Or, to see it all in one place, take a look at my MATLAB Digest article, <a href=\"https:\/\/www.mathworks.com\/company\/newsletters\/articles\/how-matlab-represents-pixel-colors.html\">\"How MATLAB Represents Pixel Colors.\"<\/a><\/p><p>For more information, see Sections 2.7 and 7.1 of <a href=\"http:\/\/imageprocessingplace.com\/DIPUM-2E\/dipum2e_main_page.htm\"> <i>Digital Image Processing Using MATLAB<\/i><\/a>.<\/p><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2011\/dipum-cover.png\" alt=\"\"> <\/p><script language=\"JavaScript\"> <!-- \r\n    function grabCode_3eb34b21a4af46ae9328134b3f06b0f9() {\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='3eb34b21a4af46ae9328134b3f06b0f9 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 3eb34b21a4af46ae9328134b3f06b0f9';\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 2014 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_3eb34b21a4af46ae9328134b3f06b0f9()\"><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; R2014a<br><\/p><\/div><!--\r\n3eb34b21a4af46ae9328134b3f06b0f9 ##### SOURCE BEGIN #####\r\n%% Images and data types\r\n% _Today's post is part of an\r\n% <https:\/\/blogs.mathworks.com\/steve\/category\/dipum-tutorials\/ ongoing (but\r\n% long delayed)\r\n% tutorial series on digital image processing using MATLAB>. I'm covering\r\n% topics in roughly the order used in the book\r\n% <http:\/\/imageprocessingplace.com\/DIPUM-2E\/dipum2e_main_page.htm Digital\r\n% Image Processing Using MATLAB>._\r\n%\r\n% In the previous post in this series, I discussed the different numeric\r\n% data types that commonly come into play when doing image processing in\r\n% MATLAB. Today I want to talk about the four main _image types_:\r\n%\r\n% * Gray-scale images\r\n% * Binary images\r\n% * Indexed images\r\n% * RGB images\r\n%\r\n%% Gray-Scale Images\r\n% A _gray-scale image_ is a matrix whose values represent shades of gray.\r\n% When the matrix is of type |uint8|, the integer-valued elements are in\r\n% the range [0,255]. By convention, the value 0 is displayed as black, and\r\n% the value 255 is displayed as white. Values in-between are displayed as\r\n% intermediate shades of gray.\r\n% \r\n% When the matrix is of type |uint16|, then 0 is displayed as black and\r\n% 65535 is displayed as white.\r\n%\r\n% For a floating-point matrix, either of type |double| or |single|, the\r\n% value 1.0 is displayed as white.\r\n%\r\n% Originally, the Image Processing Toolbox documentation called these\r\n% _intensity images_, and you might still find this term used in some\r\n% places. Some of our color scientist users complained, though, that the\r\n% term _intensity image_ meant something slightly different in their field,\r\n% so we (mostly) changed our terminology.\r\n\r\nI = imread('rice.png');\r\nwhos I\r\n\r\n%%\r\nimshow(I)\r\n\r\n%% Binary Images\r\n% In image processing, the term _binary image_ refers to a two-valued image\r\n% whose pixes are either black or white. (Or, a bit more generally, the\r\n% pixels are either background or foreground.) In MATLAB and the Image\r\n% Processing Toolbox, we have adopted the convention that binary images are\r\n% represented as _logical matrices_. Here's an example of constructing a\r\n% matrix whose type is logical and then displaying the result as a binary\r\n% (black-and-white) image.\r\n\r\n[xx,yy] = meshgrid(-100:100);\r\nbw = hypot(xx,yy) <= 50;\r\nwhos bw\r\n\r\n%%\r\nimshow(bw)\r\ntitle('Binary image')\r\n\r\n%%\r\n% Note that a common mistake is to create a |uint8| matrix and fill it with 0s\r\n% and 1s, thinking that it will display as black and white.\r\n\r\nbw = zeros(30,30,'uint8');\r\nbw(10:20,10:20) = 1;\r\nimshow(bw,'InitialMagnification','fit')\r\ntitle('Help, my image is all black!')\r\n\r\n%%\r\n% When a matrix is of type |uint8|, then the value 1 is not white; it's\r\n% almost completely black!\r\n%\r\n%% Indexed Images\r\n% An _indexed image_ has two components: an _index matrix_ of integers,\r\n% commonly called |X|, and a |color map matrix|, commonly called |map|. The\r\n% matrix |map| is an M-by-3 matrix of floating-point values (either\r\n% |double| or |single|) in the range [0.0,1.0]. Each row of |map| specifies\r\n% the red, green, and blue components of a single color. An indexed image\r\n% is displayed by mapping values in the index matrix to colors in the color\r\n% map. A quirk of MATLAB is that this mapping is data-type specific. If the\r\n% index matrix is floating-point, then the value 1.0 corresponds to the first\r\n% color in the color map. But if the index matrix is |uint8| or |uint16|,\r\n% then the value 0 corresponds to the first color. (Maybe I'll explain the\r\n% reasons for that on another day.)\r\n%\r\n% You display an indexed image by passing both the index matrix and the\r\n% color map matrix to |imshow|, like this:\r\n\r\n[X,map] = imread('trees.tif');\r\nwhos X map\r\n\r\n%%\r\nimshow(X,map)\r\ntitle('Indexed image')\r\n\r\n%% RGB Images\r\n% An RGB image is an M-by-N-by-3 array. For a particular pixel at row |r|\r\n% and column |c|, the three values |RGB(r,c,1)|, |RGB(r,c,2)|, and\r\n% |RGB(r,c,3)| specify the red, green, and blue color components of that\r\n% pixel. A pixel whose color components are [0,0,0] is displayed as black.\r\n% For a floating-point array, a pixel whose color components are\r\n% [1.0,1.0,1.0] is displayed as white. For a |uint8| or |uint16| array,\r\n% either [255,255,255] or [65535,65535,65535] is displayed as white.\r\n\r\nRGB = imread('peppers.png');\r\nwhos RGB\r\n\r\n%%\r\nimshow(RGB)\r\n\r\n%%\r\n% You can think of an RGB image as a \"stack\" of three gray-scale images.\r\n% These gray-scale images are commonly called the _component images_.\r\n\r\nimshow(RGB(:,:,1))\r\ntitle('Red component image')\r\n\r\n%%\r\nimshow(RGB(:,:,2))\r\ntitle('Green component image')\r\n\r\n%%\r\nimshow(RGB(:,:,3))\r\ntitle('Blue component image')\r\n\r\n%%\r\n% In my first year of blogging (2006), I wrote a\r\n% <https:\/\/blogs.mathworks.com\/steve\/category\/pixel-colors\/ series of blog\r\n% posts> explaining in great detail how the MATLAB image display model\r\n% works for various image and data types. The series is still up-to-date\r\n% and worth reading today. Or, to see it all in one place, take a look at\r\n% my MATLAB Digest article,\r\n% <https:\/\/www.mathworks.com\/company\/newsletters\/articles\/how-matlab-represents-pixel-colors.html \"How MATLAB Represents Pixel Colors.\">\r\n\r\n%%\r\n% For more information, see Sections 2.7 and 7.1 of\r\n% <http:\/\/imageprocessingplace.com\/DIPUM-2E\/dipum2e_main_page.htm\r\n%  _Digital Image Processing Using MATLAB_>. \r\n%\r\n% <<https:\/\/blogs.mathworks.com\/images\/steve\/2011\/dipum-cover.png>>\r\n##### SOURCE END ##### 3eb34b21a4af46ae9328134b3f06b0f9\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2014\/image_types_08.jpg\" onError=\"this.style.display ='none';\" \/><\/div><!--introduction--><p><i>Today's post is part of an <a href=\"https:\/\/blogs.mathworks.com\/steve\/category\/dipum-tutorials\/\">ongoing (but long delayed) tutorial series on digital image processing using MATLAB<\/a>. I'm covering topics in roughly the order used in the book <a href=\"http:\/\/imageprocessingplace.com\/DIPUM-2E\/dipum2e_main_page.htm\">Digital Image Processing Using MATLAB<\/a>.<\/i>... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/steve\/2014\/07\/23\/image-types\/\">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":[334,76,36,30,52,306,130],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/1108"}],"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=1108"}],"version-history":[{"count":2,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/1108\/revisions"}],"predecessor-version":[{"id":1110,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/1108\/revisions\/1110"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media?parent=1108"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/categories?post=1108"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/tags?post=1108"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}