{"id":1040,"date":"2019-10-05T14:52:31","date_gmt":"2019-10-05T14:52:31","guid":{"rendered":"https:\/\/muthu.co\/?p=1040"},"modified":"2021-05-24T02:52:53","modified_gmt":"2021-05-24T02:52:53","slug":"reduce-the-number-of-colors-of-an-image-using-uniform-quantization","status":"publish","type":"post","link":"http:\/\/write.muthu.co\/reduce-the-number-of-colors-of-an-image-using-uniform-quantization\/","title":{"rendered":"Reduce the number of colors of an image using Uniform Quantization"},"content":{"rendered":"\n

Reducing the number of colors in an image is also called Color quantization. It’s commonly used for generating GIF images which currently supports only 256 colors. The general idea is, group similar colors in an image into regions, replace them with the color which closely resembles or represents the region. This color is also called the representative color.<\/p>\n\n\n\n

Lets understand the simplest color reduction algorithm called the Uniform Quantization.<\/p>\n\n\n\n

Algorithm<\/span><\/h2>\n\n\n\n

A single color pixel represented by an RGB tuple can take values between 0-255 each. The steps involved in uniform quantization are:<\/p>\n\n\n\n

  1. Find 3 color regions by dividing the color spaces R,G,B into equal parts.<\/li>
  2. For each pixel, assign its R, G and B components to the corresponding color region.<\/li>
  3. Find the color that represents the region by taking the average of all colors in it.<\/li>
  4. Go through the pixels in the image and replace the color with the representative color of the region.<\/li><\/ol>\n\n\n\n

    For example, the color space red is divided into regions R(0-31), R(32-63), R(64-95), R(96-127), R(128-159), R(160-191), R(192-223), R(224-255). If a color C is (3,34,189), it will go into region R(0-31), G(32-63) and B(160-191). Now if the average of all the Red colors falling in region R(0-31) is 16, Green G(32-63) is 48 and Blue B(160-191) is 178, then the color C = (3,34,189) will become C = (16, 48, 178) after quantization.<\/p>\n\n\n\n

    Implementation<\/h2>\n\n\n\n
    import matplotlib.pyplot as plt\nimport numpy as np\nfrom skimage.io import imread, imsave\n\ncar = imread('resources\/auto.jpg')\n\ndef get_region_index(color_value):\n    # colors divided into 8 regions for each color space\n    eight_regions = [[0,31], [32,63], [64,95], [96,127], [128,159], [160,191], [192,223], [224,255]]\n    for index, region_value in enumerate(eight_regions):\n        if color_value >= region_value[0] and  color_value <= region_value[1]:\n            return index\n        \nr_region_mappings = [[],[],[],[],[],[],[],[]]\ng_region_mappings = [[],[],[],[],[],[],[],[]]\nb_region_mappings = [[],[],[],[],[],[],[],[]]    \n\n# loop through all pixels and the put the colors into the respective color regions\nfor rows in car:\n    for pixel in rows:\n        red = pixel[0]\n        green = pixel[1]\n        blue = pixel[2]\n        #find the index where the color is supposed to go and add it\n        r_region_mappings[get_region_index(red)].append(red)\n        g_region_mappings[get_region_index(green)].append(green)\n        b_region_mappings[get_region_index(blue)].append(blue)\n        \n# find the color that represents each region\nr_representative_color_per_region = [0,0,0,0,0,0,0,0]\ng_representative_color_per_region = [0,0,0,0,0,0,0,0]\nb_representative_color_per_region = [0,0,0,0,0,0,0,0]\n\n# find the average of all colors in the regions to find the representative color\nfor index in range(8):\n    r_representative_color_per_region[index] = np.mean(r_region_mappings[index]).astype(int)\n    g_representative_color_per_region[index] = np.mean(g_region_mappings[index]).astype(int)\n    b_representative_color_per_region[index] = np.mean(b_region_mappings[index]).astype(int)\n\n# now replace all colors in the image with their uniform quantized representative colors\nnew_car = np.copy(car)\nfor rindex, rows in enumerate(car):\n    for cindex, pixel in enumerate(rows):\n        red = pixel[0]\n        green = pixel[1]\n        blue = pixel[2]\n        new_car[rindex, cindex][0] = r_representative_color_per_region[get_region_index(red)]\n        new_car[rindex, cindex][1] = g_representative_color_per_region[get_region_index(green)]\n        new_car[rindex, cindex][2] = b_representative_color_per_region[get_region_index(blue)]\n\n\nimsave('uniform_quantized.jpg', new_car)<\/code><\/pre>\n\n\n\n

    Jupyter Notebook<\/h3>\n\n\n\n

    You can understand the entire process from my notebook<\/a> attached below.<\/p>\n\n\n\n