{"cells":[{"metadata":{},"cell_type":"markdown","source":"# Faster Approach - RGB to HSV & CMYK\n\nThose who are working in image processing domain with limited processing power must have faced issue of color coversion time consuming. Believe me, I have been working on such issues for more than a year now. And after checking in-built functions of skimage, I came up with my own functions with help of numba. \n\nFor demonstration purpose I have taken a large image of 4000 x 3000 pixel from siim-isic-melanoma-classification competition.\n\nDo comment if this is helpful. \n\nI know these snippets couldn't be the most optimized one. So if you have better/improved function, do share with me. \n\n**Below are function I created for RGB to HSV/CMYK or both**\n\nI hope those with limited resource will be benefited from these code snippets.\n\nSame script is copied on Github https://gist.github.com/vrajs5/3f540ef3bc84bc31ca49c11ba6b0d6ae","execution_count":null},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nfrom skimage import io\nimport os\nfrom numba import njit\nfrom skimage import color\n\nRGB_SCALE = 255\nCMYK_SCALE = 100\n\n@njit\ndef rgb_to_hsv_cmyk(img):\n    dx, dy, dz = img.shape\n    convData = np.zeros((dx, dy, 7))\n    \n    for i in range(dx):\n        for j in range(dy):\n            r, g, b = img[i,j]/RGB_SCALE\n            \n            mx = max(r, g, b)\n            mn = min(r, g, b)\n            df = mx-mn\n            if mx == mn:\n                h = 0\n            elif mx == r:\n                h = (60 * ((g-b)/df) + 360) % 360\n            elif mx == g:\n                h = (60 * ((b-r)/df) + 120) % 360\n            elif mx == b:\n                h = (60 * ((r-g)/df) + 240) % 360\n            if mx == 0:\n                s = 0\n            else:\n                s = (df/mx)*100\n            v = mx*100\n            convData[i,j,:3] = h, s, v\n            \n            \n            if (r == 0) and (g == 0) and (b == 0):\n                convData[i,j,3:] = 0, 0, 0, CMYK_SCALE\n            else:\n                c, m, y = 1 - r, 1 - g, 1 - b\n                \n                # extract out k [0, 1]\n                min_cmy = min(c, m, y)\n                divisor = (1 - min_cmy)\n                c = (c - min_cmy) / divisor\n                m = (m - min_cmy) / divisor\n                y = (y - min_cmy) / divisor\n                k = min_cmy\n\n                # rescale to the range [0,CMYK_SCALE]\n                convData[i,j,3:] = c * CMYK_SCALE, m * CMYK_SCALE, y * CMYK_SCALE, k * CMYK_SCALE\n                \n    return convData\n\n@njit\ndef rgb_to_cmyk(img):\n    dx, dy, dz = img.shape\n    cmykData = np.zeros((dx, dy, 4))\n    \n    for i in range(dx):\n        for j in range(dy):\n            if img[i,j].any() ==  False:\n                cmykData[i,j] = 0, 0, 0, CMYK_SCALE\n            else:\n                c, m, y = 1 - img[i,j] / RGB_SCALE\n                \n                # extract out k [0, 1]\n                min_cmy = min(c, m, y)\n                divosor = (1 - min_cmy)\n                c = (c - min_cmy) / divosor\n                m = (m - min_cmy) / divosor\n                y = (y - min_cmy) / divosor\n                k = min_cmy\n\n                # rescale to the range [0,CMYK_SCALE]\n                cmykData[i,j] = c * CMYK_SCALE, m * CMYK_SCALE, y * CMYK_SCALE, k * CMYK_SCALE\n\n    return cmykData\n\n\n@njit\ndef rgb_to_hsv(img):\n    dx, dy, dz = img.shape\n    hsvData = np.zeros((dx, dy, dz))\n    \n    for i in range(dx):\n        for j in range(dy):\n            r, g, b = img[i,j]/RGB_SCALE\n            mx = max(r, g, b)\n            mn = min(r, g, b)\n            df = mx-mn\n            if mx == mn:\n                h = 0\n            elif mx == r:\n                h = (60 * ((g-b)/df) + 360) % 360\n            elif mx == g:\n                h = (60 * ((b-r)/df) + 120) % 360\n            elif mx == b:\n                h = (60 * ((r-g)/df) + 240) % 360\n            if mx == 0:\n                s = 0\n            else:\n                s = (df/mx)*100\n            v = mx*100\n            hsvData[i,j] = h, s, v\n    return hsvData\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Taking image of size 4000 x 6000 pixel size.","execution_count":null},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"masterInpath = '/kaggle/input/siim-isic-melanoma-classification/'\nimgName = 'ISIC_7685852.jpg'\nimg = io.imread(masterInpath + 'jpeg/train/' + imgName)\nprint(img.shape)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**skimage color conversion function performance.**","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"%timeit color.rgb2hsv(img)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**Performance of custom function**\n\nOnly difference between two function is custom one will convert hsv to its scale of 360, 100, 100","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"%timeit rgb_to_hsv(img)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Performance of custom cmyk function","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"%timeit rgb_to_cmyk(img)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Performance of combine hsv & cmyk function.","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"%timeit rgb_to_hsv_cmyk(img)","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":4}