{"id":1237,"date":"2012-03-20T15:52:10","date_gmt":"2012-03-20T18:52:10","guid":{"rendered":"http:\/\/wordpress.matbra.com\/?p=1237"},"modified":"2012-03-20T15:52:10","modified_gmt":"2012-03-20T18:52:10","slug":"conversao-de-imagens-para-grayscalebinariargb","status":"publish","type":"post","link":"https:\/\/wordpress.matbra.com\/en\/2012\/03\/20\/conversao-de-imagens-para-grayscalebinariargb\/","title":{"rendered":"Image convertion to GrayScale\/Binary\/R\/G\/B"},"content":{"rendered":"<p>Hello,<\/p>\n<p>This is a small exercice of Multimedia class (INE5431). We need to implement: <\/p>\n<blockquote><p>\n1) Transform the image to GrayScale<br \/>\n2) Generate one image to each component color (R\/G\/B)<br \/>\n3) Transform the GrayScale image to binary\n<\/p><\/blockquote>\n<p>To transform RGB to GrayScale we need to use this expression: <\/p>\n<blockquote><p>Y = 0.3R + 0.59G + 0.11B;<\/p><\/blockquote>\n<p>And them, to convert the GrayScale to Binary <\/p>\n<blockquote><p>if (Y > 127) {b = 1; } else { b = 0; }<\/p><\/blockquote>\n<p>If we want just one color component, we need to get all image pixels and leave just the component that we want, or just copy the component to a new image.<\/p>\n<p>In this project, we access an image and we have some functions to do what we want, convertion to GrayScale, Binary, and Component RGB. From the source image, we create a buffer image of the same size to all images and we create a for to go over all pixels and make the transformation and put it to the new buffer image. <\/p>\n<p><!--more--><\/p>\n<p>GrayScale method, receive a Bufferedmage and convert it to GrayScale.<br \/>\n[code lang=&#8221;java&#8221;]<br \/>\npublic static BufferedImage criaImagemCinza(BufferedImage imgJPEG) {<br \/>\n\t\t\/\/ Create a new buffer to BYTE_GRAY<br \/>\n\t\tBufferedImage img = new BufferedImage(imgJPEG.getWidth(), imgJPEG.getHeight(), BufferedImage.TYPE_BYTE_GRAY);<br \/>\n\t\tWritableRaster raster = img.getRaster();<br \/>\n\t\tWritableRaster rasterJPEG = imgJPEG.getRaster();<br \/>\n\t\t\/\/ Foreach pixel we transofrm it to Gray Scale and put it on the same image<br \/>\n\t\tfor(int h=0;h<256;h++)\n\t\t\tfor(int w=0;w<256;w++) {\n\t\t\t\tint[] p = new int[4];\n\t\t\t\trasterJPEG.getPixel(w, h, p);\n\t\t\t\tp[0] = (int) (0.3 * p[0]); \n\t\t\t\tp[1] = (int) (0.59 * p[1]);\n\t\t\t\tp[2] = (int) (0.11 * p[2]);\n\t\t\t\tint y = p[0] + p[1] + p[2];\n\t\t\t\traster.setSample(w,h,0,y);\n\t\t\t}\n\t\treturn img;\n\t}\n[\/code]\n\nBinary Image method, receive  a Bufferedmage and convert it to Binary.\n[code lang=\"java\"]\n\tpublic static BufferedImage criaImagemBinaria(BufferedImage imgJPEG) {\n\t\t\/\/ Create a new Binary Buffer\n\t\tBufferedImage img = new BufferedImage(imgJPEG.getWidth(), imgJPEG.getHeight(), BufferedImage.TYPE_BYTE_BINARY);\n\t\tWritableRaster raster = img.getRaster();\n\t\tWritableRaster rasterPB = criaImagemCinza(imgJPEG).getRaster();\n\t\t\/\/ Foreach pixel  check if the new one must be white or black \n\t\tfor(int h=0;h<256;h++)\n\t\t\tfor(int w=0;w<256;w++) {\n\t\t\t\tint[] p = new int[1];\n\t\t\t\trasterPB.getPixel(w, h, p);\n\t\t\t\tif(p[0] > 127) {<br \/>\n\t\t\t\t\traster.setSample(w, h, 0, 1);<br \/>\n\t\t\t\t} else {<br \/>\n\t\t\t\t\traster.setSample(w, h, 0, 0);<br \/>\n\t\t\t\t}<br \/>\n\t\t\t}<br \/>\n\t\treturn img;<br \/>\n\t}<br \/>\n[\/code]<\/p>\n<p>RGB Image method, receive a Bufferedmage and a Type (0 &#8211; red, 1 &#8211; green, 2 &#8211; blue) and convert it to RGB Component that is used as parameter.<br \/>\n[code lang=&#8221;java&#8221;]<br \/>\npublic static BufferedImage criaImagemRGB(BufferedImage imgJPEG, int tipo) {<br \/>\n\t\t\/\/ Create a new RGB Buffer<br \/>\n\t\tBufferedImage img = new BufferedImage(imgJPEG.getWidth(), imgJPEG.getHeight(), BufferedImage.TYPE_INT_RGB);<\/p>\n<p>\t\tWritableRaster rasterT = img.getRaster();<br \/>\n\t\tWritableRaster raster = imgJPEG.getRaster();<br \/>\n\t\t\/\/ Foreach pixel just use the component that is passed as parameter.<br \/>\n\t\tfor(int h=0;h<256;h++)\n\t\t\tfor(int w=0;w<256;w++) {\n\t\t\t\tint[] p = new int[4];\n\t\t\t\traster.getPixel(w,h,p);\n\t\t\t\trasterT.setSample(w,h,tipo-1,p[tipo-1]);\n\t\t\t} \n\t\treturn img;\n\t}\n[\/code]\n\n<a href='http:\/\/wordpress.matbra.com\/wp-content\/uploads\/SM-GrayScale-Binary-RGB.zip'>Manipulate images to GrayScale, Binary and RGB Components<\/a><\/p>\n<p>Matheus<\/p>\n<p><\/p>","protected":false},"excerpt":{"rendered":"<p>Hello, This is a small exercice of Multimedia class (INE5431). We need to implement: 1) Transform the image to GrayScale 2) Generate one image to<\/p>\n<div class=\"more-link-wrapper\"><a class=\"more-link\" href=\"https:\/\/wordpress.matbra.com\/en\/2012\/03\/20\/conversao-de-imagens-para-grayscalebinariargb\/\">Continue reading<span class=\"screen-reader-text\">Image convertion to GrayScale\/Binary\/R\/G\/B<\/span><\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[47],"tags":[226,225,223,222,221,321,13,224,220],"class_list":["post-1237","post","type-post","status-publish","format-standard","hentry","category-java","tag-binary-java","tag-grayscale-java","tag-imagem-binaria","tag-imagem-grayscale","tag-imagens","tag-java","tag-programacao","tag-rgb","tag-sistemas-multimida","entry"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/psjGE-jX","_links":{"self":[{"href":"https:\/\/wordpress.matbra.com\/en\/wp-json\/wp\/v2\/posts\/1237","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wordpress.matbra.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wordpress.matbra.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wordpress.matbra.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wordpress.matbra.com\/en\/wp-json\/wp\/v2\/comments?post=1237"}],"version-history":[{"count":1,"href":"https:\/\/wordpress.matbra.com\/en\/wp-json\/wp\/v2\/posts\/1237\/revisions"}],"predecessor-version":[{"id":1239,"href":"https:\/\/wordpress.matbra.com\/en\/wp-json\/wp\/v2\/posts\/1237\/revisions\/1239"}],"wp:attachment":[{"href":"https:\/\/wordpress.matbra.com\/en\/wp-json\/wp\/v2\/media?parent=1237"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wordpress.matbra.com\/en\/wp-json\/wp\/v2\/categories?post=1237"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wordpress.matbra.com\/en\/wp-json\/wp\/v2\/tags?post=1237"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}