{"id":842,"date":"2018-09-01T02:45:26","date_gmt":"2018-09-01T02:45:26","guid":{"rendered":"http:\/\/muthu.co\/?p=842"},"modified":"2021-05-24T03:04:14","modified_gmt":"2021-05-24T03:04:14","slug":"converting-color-images-to-grayscale-using-numpy-and-some-mathematics","status":"publish","type":"post","link":"http:\/\/write.muthu.co\/converting-color-images-to-grayscale-using-numpy-and-some-mathematics\/","title":{"rendered":"Converting Color Images to Grayscale using numpy and some Mathematics"},"content":{"rendered":"\n

An extremely magnified image at the end is just blocks of colors called pixels, where each pixel is formed by the combination of Red, Blue and Green, our primary colors. RGB color space or RGB color system<\/a>, constructs all the colors from the combination of the intensities of Red, Green and Blue colors.<\/p>\n\n\n\n

\"\"<\/a><\/figure><\/div>\n\n\n\n

The red, green and blue use 8 bits each, which have integer values from 0 to 255. This makes 256*256*256=16777216 possible colors. Below is a chart of basic colors.<\/p>\n\n\n\n

Color<\/th>HTML \/ CSS Name<\/th>Hex Code\n

 <\/p>\n

#RRGGBB<\/p>\n<\/th>

Decimal Code\n

 <\/p>\n

(R,G,B)<\/p>\n<\/th><\/tr><\/thead>

 <\/td>Black<\/td>#000000<\/td>(0,0,0)<\/td><\/tr>
 <\/td>White<\/td>#FFFFFF<\/td>(255,255,255)<\/td><\/tr>
 <\/td>Red<\/td>#FF0000<\/td>(255,0,0)<\/td><\/tr>
 <\/td>Lime<\/td>#00FF00<\/td>(0,255,0)<\/td><\/tr>
 <\/td>Blue<\/td>#0000FF<\/td>(0,0,255)<\/td><\/tr>
 <\/td>Yellow<\/td>#FFFF00<\/td>(255,255,0)<\/td><\/tr>
 <\/td>Cyan \/ Aqua<\/td>#00FFFF<\/td>(0,255,255)<\/td><\/tr>
 <\/td>Magenta \/ Fuchsia<\/td>#FF00FF<\/td>(255,0,255)<\/td><\/tr>
 <\/td>Silver<\/td>#C0C0C0<\/td>(192,192,192)<\/td><\/tr>
 <\/td>Gray<\/td>#808080<\/td>(128,128,128)<\/td><\/tr>
 <\/td>Maroon<\/td>#800000<\/td>(128,0,0)<\/td><\/tr>
 <\/td>Olive<\/td>#808000<\/td>(128,128,0)<\/td><\/tr>
 <\/td>Green<\/td>#008000<\/td>(0,128,0)<\/td><\/tr>
 <\/td>Purple<\/td>#800080<\/td>(128,0,128)<\/td><\/tr>
 <\/td>Teal<\/td>#008080<\/td>(0,128,128)<\/td><\/tr>
 <\/td>Navy<\/td>#000080<\/td>(0,0,128)<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n

If you look at Gray and Silver, you would see that all the components Red, Green and Blue are equal. So the idea behind converting any image to grayscale would be to make Red, Green and Blue value equal in each pixel. Some of the ways you can do this are given below:<\/p>\n\n\n\n

Method 1: The lightness method<\/h3>\n\n\n\n

This method averages the most prominent and least prominent Colors: (max(R, G, B) + min(R, G, B)) \/ 2<\/p>\n\n\n\n

gray_light[:] = np.max(gray_light,axis=-1,keepdims=1)\/2 + np.min(gray_light,axis=-1,keepdims=1)\/2<\/code><\/pre>\n\n\n\n

output image:<\/p>\n\n\n\n

\"\"<\/a><\/figure><\/div>\n\n\n\n

Method 2: The average method<\/h3>\n\n\n\n

This method simply averages the values: (R + G + B) \/ 3<\/p>\n\n\n\n

gray_avg[:] = np.sum(gray_avg,axis=-1,keepdims=1)\/3<\/code><\/pre>\n\n\n\n

output image:<\/p>\n\n\n\n

\"\"<\/a><\/figure><\/div>\n\n\n\n

Method 3: The luminosity method<\/h3>\n\n\n\n

This is a more sophisticated version of the average method. It also averages the values, but it forms a weighted average to account for human perception. We’re more sensitive to green than other colors, so green is weighted most heavily. The formula for luminosity is 0.21 R + 0.72 G + 0.07 B<\/p>\n\n\n\n

luminosity_constant = [0.21,0.72,0.07]\ngray_lumin = np.dot(orig_img[...,:3], luminosity_constant).astype(np.uint8)<\/code><\/pre>\n\n\n\n

output image:<\/p>\n\n\n\n

\"\"<\/a><\/figure><\/div>\n\n\n\n

You can see the entire source code here: Various ways of converting an image to grayscale<\/a><\/p>\n\n\n\n

Method 4: Simply use some API \ud83d\ude09<\/h3>\n\n\n\n

You can use the standard skimage method to perform the same grayscale conversion.<\/p>\n\n\n\n

from skimage.color import rgb2gray\nimg_gray = rgb2gray(orig_img)<\/code><\/pre>\n\n\n\n

The algorithm used within rgb2gray<\/em> is the luminosity method.<\/p>\n\n\n\n

References:
https:\/\/docs.gimp.org\/2.6\/en\/gimp-tool-desaturate.html<\/a>
http:\/\/poynton.ca\/PDFs\/ColorFAQ.pdf<\/a>
http:\/\/scikit-image.org\/docs\/dev\/api\/skimage.color.html#skimage.color.rgb2gray<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"

An extremely magnified image at the end is just blocks of colors called pixels, where each pixel is formed by the combination of Red, Blue and Green, our primary colors. RGB color space or RGB color system, constructs all the colors from the combination of the intensities of Red, Green and Blue colors. The red, green and […]<\/p>\n","protected":false},"author":1,"featured_media":843,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[38,6],"tags":[47,54],"_links":{"self":[{"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/posts\/842"}],"collection":[{"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/comments?post=842"}],"version-history":[{"count":2,"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/posts\/842\/revisions"}],"predecessor-version":[{"id":1886,"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/posts\/842\/revisions\/1886"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/media\/843"}],"wp:attachment":[{"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/media?parent=842"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/categories?post=842"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/tags?post=842"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}