{
  "id": 154876,
  "title": "Color constancy as a pre-processing step",
  "url": "/competitions/siim-isic-melanoma-classification/discussion/154876",
  "author_name": "",
  "post_date": "2020-05-30T07:09:32.474019500Z",
  "votes": 18,
  "comment_count": 8,
  "views": 0,
  "content": "<p>Color constancy is a well-known pre-processing method that was used in many previous studies for skin lesion analysis (including the top-ranked methods for the ISIC 2017, ISIC 2018 and ISIC 2019 challenges for skin lesion classification). It mainly deals with the color illumination variations that may appear in different skin lesion images. </p>\n\n<p>Here you will find a MATLAB implementation of color constancy algorithm: \n<a href=\"https://ch.mathworks.com/matlabcentral/fileexchange/52633-color-constancy-toolbox\">https://ch.mathworks.com/matlabcentral/fileexchange/52633-color-constancy-toolbox</a></p>\n\n<p>You may find it useful in your development. \n<img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-user-content/o/inbox%2F1559551%2F6861e777b1e9c4b76dae7d33be3d0924%2F1.jpg?generation=1590822530645604&amp;alt=media\" alt=\"\"></p>",
  "messages": [
    {
      "id": "867328",
      "postDate": "05/30/2020 07:09:32",
      "content": "<p>Color constancy is a well-known pre-processing method that was used in many previous studies for skin lesion analysis (including the top-ranked methods for the ISIC 2017, ISIC 2018 and ISIC 2019 challenges for skin lesion classification). It mainly deals with the color illumination variations that may appear in different skin lesion images. </p>\n\n<p>Here you will find a MATLAB implementation of color constancy algorithm: \n<a href=\"https://ch.mathworks.com/matlabcentral/fileexchange/52633-color-constancy-toolbox\">https://ch.mathworks.com/matlabcentral/fileexchange/52633-color-constancy-toolbox</a></p>\n\n<p>You may find it useful in your development. \n<img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-user-content/o/inbox%2F1559551%2F6861e777b1e9c4b76dae7d33be3d0924%2F1.jpg?generation=1590822530645604&amp;alt=media\" alt=\"\"></p>",
      "rawMarkdown": "Color constancy is a well-known pre-processing method that was used in many previous studies for skin lesion analysis (including the top-ranked methods for the ISIC 2017, ISIC 2018 and ISIC 2019 challenges for skin lesion classification). It mainly deals with the color illumination variations that may appear in different skin lesion images. \n\nHere you will find a MATLAB implementation of color constancy algorithm: \nhttps://ch.mathworks.com/matlabcentral/fileexchange/52633-color-constancy-toolbox\n\nYou may find it useful in your development. \n![](https://www.googleapis.com/download/storage/v1/b/kaggle-user-content/o/inbox%2F1559551%2F6861e777b1e9c4b76dae7d33be3d0924%2F1.jpg?generation=1590822530645604&amp;alt=media)",
      "votes": null
    },
    {
      "id": "867412",
      "postDate": "05/30/2020 09:02:48",
      "content": "<p>See <strong>python</strong> function taken from <a href=\"https://github.com/abhishekrana/isic2018-skin-lesion-classifier-tensorflow/blob/master/utils/utils_image.py\">isic2018-skin-lesion-classifier-tensorflow</a></p>\n\n<p>```\ndef color_constancy(img, power=6, gamma=None):\n    img = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)\n    img_dtype = img.dtype</p>\n\n<pre><code>if gamma is not None:\n    img = img.astype('uint8')\n    look_up_table = np.ones((256,1), dtype='uint8') * 0\n    for i in range(256):\n        look_up_table[i][0] = 255*pow(i/255, 1/gamma)\n    img = cv2.LUT(img, look_up_table)\n\nimg = img.astype('float32')\nimg_power = np.power(img, power)\nrgb_vec = np.power(np.mean(img_power, (0,1)), 1/power)\nrgb_norm = np.sqrt(np.sum(np.power(rgb_vec, 2.0)))\nrgb_vec = rgb_vec/rgb_norm\nrgb_vec = 1/(rgb_vec*np.sqrt(3))\nimg = np.multiply(img, rgb_vec)\n\nimg = cv2.cvtColor(np.array(img), cv2.COLOR_BGR2RGB)\nreturn img.astype(img_dtype)\n</code></pre>\n\n<p>```</p>",
      "rawMarkdown": "See **python** function taken from [isic2018-skin-lesion-classifier-tensorflow](https://github.com/abhishekrana/isic2018-skin-lesion-classifier-tensorflow/blob/master/utils/utils_image.py)\n\n```\ndef color_constancy(img, power=6, gamma=None):\n    img = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)\n    img_dtype = img.dtype\n\n    if gamma is not None:\n        img = img.astype('uint8')\n        look_up_table = np.ones((256,1), dtype='uint8') * 0\n        for i in range(256):\n            look_up_table[i][0] = 255*pow(i/255, 1/gamma)\n        img = cv2.LUT(img, look_up_table)\n\n    img = img.astype('float32')\n    img_power = np.power(img, power)\n    rgb_vec = np.power(np.mean(img_power, (0,1)), 1/power)\n    rgb_norm = np.sqrt(np.sum(np.power(rgb_vec, 2.0)))\n    rgb_vec = rgb_vec/rgb_norm\n    rgb_vec = 1/(rgb_vec*np.sqrt(3))\n    img = np.multiply(img, rgb_vec)\n\n    img = cv2.cvtColor(np.array(img), cv2.COLOR_BGR2RGB)\n    return img.astype(img_dtype)\n\n```",
      "votes": null
    },
    {
      "id": "924010",
      "postDate": "07/11/2020 08:06:49",
      "content": "<p>I try this code ,why that's not work!!!</p>",
      "rawMarkdown": "I try this code ,why that's not work!!!",
      "votes": null
    },
    {
      "id": "924067",
      "postDate": "07/11/2020 08:37:03",
      "content": "<p><a href=\"/jinxiaoqiang\">@jinxiaoqiang</a>  The above code is already used successfully:\n- See <a href=\"https://www.kaggle.com/apacheco/shades-of-gray-color-constancy\">public kernel</a>\n- Discussion <a href=\"https://www.kaggle.com/c/siim-isic-melanoma-classification/discussion/161719\">post</a> where left-hand images are original and right-hand images after processing</p>",
      "rawMarkdown": "jinxiaoqiang  The above code is already used successfully:\n- See [public kernel](https://www.kaggle.com/apacheco/shades-of-gray-color-constancy)\n- Discussion [post](https://www.kaggle.com/c/siim-isic-melanoma-classification/discussion/161719) where left-hand images are original and right-hand images after processing",
      "votes": null
    },
    {
      "id": "924126",
      "postDate": "07/11/2020 09:12:13",
      "content": "<p>See my images,are you think that's ok ?</p>",
      "rawMarkdown": "See my images,are you think that's ok ?",
      "votes": null
    },
    {
      "id": "924168",
      "postDate": "07/11/2020 09:42:49",
      "content": "<p><a href=\"/jinxiaoqiang\">@jinxiaoqiang</a> No, your images (taken from external dataset) are looking different from mine (see below). For example, see ISIC_0000002.jpeg which looks very different in your attachment.\nPerhaps you have modified the above code in some way?</p>\n\n<p>Before and After applying Color Constancy code:\n<img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-user-content/o/inbox%2F656212%2Ff53d3ef94b7aad22390710ae8f298289%2Fafter_applying_color_constant.png?generation=1594460523272866&amp;alt=media\" alt=\"\"></p>",
      "rawMarkdown": "jinxiaoqiang No, your images (taken from external dataset) are looking different from mine (see below). For example, see ISIC_0000002.jpeg which looks very different in your attachment.\nPerhaps you have modified the above code in some way?\n\nBefore and After applying Color Constancy code:\n![](https://www.googleapis.com/download/storage/v1/b/kaggle-user-content/o/inbox%2F656212%2Ff53d3ef94b7aad22390710ae8f298289%2Fafter_applying_color_constant.png?generation=1594460523272866&amp;alt=media)",
      "votes": null
    },
    {
      "id": "968216",
      "postDate": "08/12/2020 19:21:02",
      "content": "<p>Has anyone made an albumentation out of this?  I tried but I was getting some errors and moved on from it.</p>",
      "rawMarkdown": "Has anyone made an albumentation out of this?  I tried but I was getting some errors and moved on from it.",
      "votes": null
    },
    {
      "id": "968531",
      "postDate": "08/13/2020 04:58:02",
      "content": "<p><a href=\"https://www.kaggle.com/sirishks\" target=\"_blank\">@sirishks</a> posted a function reference from <a href=\"https://github.com/abhishekrana/isic2018-skin-lesion-classifier-tensorflow/blob/master/utils/utils_image.py\" target=\"_blank\">here</a>. Below is what I used.</p>\n<pre><code>import albumentations as A\nclass ColorConstancy(A.ImageOnlyTransform):\n    def apply(self, img, power=6, gamma=None, **params):\n         # see Sirish function\n</code></pre>",
      "rawMarkdown": "sirishks posted a function reference from [here](https://github.com/abhishekrana/isic2018-skin-lesion-classifier-tensorflow/blob/master/utils/utils_image.py). Below is what I used.\n\n```\nimport albumentations as A\nclass ColorConstancy(A.ImageOnlyTransform):\n    def apply(self, img, power=6, gamma=None, **params):\n         # see Sirish function\n\n```",
      "votes": null
    },
    {
      "id": "969521",
      "postDate": "08/13/2020 18:38:23",
      "content": "<p>Does anyone use the gamma option of this?  If so, and if you are using it as a transform, you may wish to move the LUT into the init?() function, so that it doesn't have to be created for every single image.  I haven't seen notebooks leveraging the gamma though so was curious if anyone has found that to be useful.</p>",
      "rawMarkdown": "Does anyone use the gamma option of this?  If so, and if you are using it as a transform, you may wish to move the LUT into the init?() function, so that it doesn't have to be created for every single image.  I haven't seen notebooks leveraging the gamma though so was curious if anyone has found that to be useful.",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 968216,
      "author_name": "brianfeeny",
      "author_url": "",
      "post_date": "08/12/2020 19:21:02",
      "content": "<p>Has anyone made an albumentation out of this?  I tried but I was getting some errors and moved on from it.</p>",
      "votes": null,
      "replies": [
        {
          "id": 968531,
          "author_name": "waylongo",
          "author_url": "",
          "post_date": "08/13/2020 04:58:02",
          "content": "<p><a href=\"https://www.kaggle.com/sirishks\" target=\"_blank\">@sirishks</a> posted a function reference from <a href=\"https://github.com/abhishekrana/isic2018-skin-lesion-classifier-tensorflow/blob/master/utils/utils_image.py\" target=\"_blank\">here</a>. Below is what I used.</p>\n<pre><code>import albumentations as A\nclass ColorConstancy(A.ImageOnlyTransform):\n    def apply(self, img, power=6, gamma=None, **params):\n         # see Sirish function\n</code></pre>",
          "votes": null,
          "replies": []
        }
      ]
    },
    {
      "id": 969521,
      "author_name": "brianfeeny",
      "author_url": "",
      "post_date": "08/13/2020 18:38:23",
      "content": "<p>Does anyone use the gamma option of this?  If so, and if you are using it as a transform, you may wish to move the LUT into the init?() function, so that it doesn't have to be created for every single image.  I haven't seen notebooks leveraging the gamma though so was curious if anyone has found that to be useful.</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 867412,
      "author_name": "sirishks",
      "author_url": "",
      "post_date": "05/30/2020 09:02:48",
      "content": "<p>See <strong>python</strong> function taken from <a href=\"https://github.com/abhishekrana/isic2018-skin-lesion-classifier-tensorflow/blob/master/utils/utils_image.py\">isic2018-skin-lesion-classifier-tensorflow</a></p>\n\n<p>```\ndef color_constancy(img, power=6, gamma=None):\n    img = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)\n    img_dtype = img.dtype</p>\n\n<pre><code>if gamma is not None:\n    img = img.astype('uint8')\n    look_up_table = np.ones((256,1), dtype='uint8') * 0\n    for i in range(256):\n        look_up_table[i][0] = 255*pow(i/255, 1/gamma)\n    img = cv2.LUT(img, look_up_table)\n\nimg = img.astype('float32')\nimg_power = np.power(img, power)\nrgb_vec = np.power(np.mean(img_power, (0,1)), 1/power)\nrgb_norm = np.sqrt(np.sum(np.power(rgb_vec, 2.0)))\nrgb_vec = rgb_vec/rgb_norm\nrgb_vec = 1/(rgb_vec*np.sqrt(3))\nimg = np.multiply(img, rgb_vec)\n\nimg = cv2.cvtColor(np.array(img), cv2.COLOR_BGR2RGB)\nreturn img.astype(img_dtype)\n</code></pre>\n\n<p>```</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 924010,
      "author_name": "jinxiaoqiang",
      "author_url": "",
      "post_date": "07/11/2020 08:06:49",
      "content": "<p>I try this code ,why that's not work!!!</p>",
      "votes": null,
      "replies": [
        {
          "id": 924067,
          "author_name": "sirishks",
          "author_url": "",
          "post_date": "07/11/2020 08:37:03",
          "content": "<p><a href=\"/jinxiaoqiang\">@jinxiaoqiang</a>  The above code is already used successfully:\n- See <a href=\"https://www.kaggle.com/apacheco/shades-of-gray-color-constancy\">public kernel</a>\n- Discussion <a href=\"https://www.kaggle.com/c/siim-isic-melanoma-classification/discussion/161719\">post</a> where left-hand images are original and right-hand images after processing</p>",
          "votes": null,
          "replies": []
        }
      ]
    },
    {
      "id": 924126,
      "author_name": "jinxiaoqiang",
      "author_url": "",
      "post_date": "07/11/2020 09:12:13",
      "content": "<p>See my images,are you think that's ok ?</p>",
      "votes": null,
      "replies": [
        {
          "id": 924168,
          "author_name": "sirishks",
          "author_url": "",
          "post_date": "07/11/2020 09:42:49",
          "content": "<p><a href=\"/jinxiaoqiang\">@jinxiaoqiang</a> No, your images (taken from external dataset) are looking different from mine (see below). For example, see ISIC_0000002.jpeg which looks very different in your attachment.\nPerhaps you have modified the above code in some way?</p>\n\n<p>Before and After applying Color Constancy code:\n<img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-user-content/o/inbox%2F656212%2Ff53d3ef94b7aad22390710ae8f298289%2Fafter_applying_color_constant.png?generation=1594460523272866&amp;alt=media\" alt=\"\"></p>",
          "votes": null,
          "replies": []
        }
      ]
    }
  ],
  "raw_markdown_by_id": {
    "867328": "Color constancy is a well-known pre-processing method that was used in many previous studies for skin lesion analysis (including the top-ranked methods for the ISIC 2017, ISIC 2018 and ISIC 2019 challenges for skin lesion classification). It mainly deals with the color illumination variations that may appear in different skin lesion images. \n\nHere you will find a MATLAB implementation of color constancy algorithm: \nhttps://ch.mathworks.com/matlabcentral/fileexchange/52633-color-constancy-toolbox\n\nYou may find it useful in your development. \n![](https://www.googleapis.com/download/storage/v1/b/kaggle-user-content/o/inbox%2F1559551%2F6861e777b1e9c4b76dae7d33be3d0924%2F1.jpg?generation=1590822530645604&amp;alt=media)",
    "867412": "See **python** function taken from [isic2018-skin-lesion-classifier-tensorflow](https://github.com/abhishekrana/isic2018-skin-lesion-classifier-tensorflow/blob/master/utils/utils_image.py)\n\n```\ndef color_constancy(img, power=6, gamma=None):\n    img = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)\n    img_dtype = img.dtype\n\n    if gamma is not None:\n        img = img.astype('uint8')\n        look_up_table = np.ones((256,1), dtype='uint8') * 0\n        for i in range(256):\n            look_up_table[i][0] = 255*pow(i/255, 1/gamma)\n        img = cv2.LUT(img, look_up_table)\n\n    img = img.astype('float32')\n    img_power = np.power(img, power)\n    rgb_vec = np.power(np.mean(img_power, (0,1)), 1/power)\n    rgb_norm = np.sqrt(np.sum(np.power(rgb_vec, 2.0)))\n    rgb_vec = rgb_vec/rgb_norm\n    rgb_vec = 1/(rgb_vec*np.sqrt(3))\n    img = np.multiply(img, rgb_vec)\n\n    img = cv2.cvtColor(np.array(img), cv2.COLOR_BGR2RGB)\n    return img.astype(img_dtype)\n\n```",
    "924010": "I try this code ,why that's not work!!!",
    "924067": "jinxiaoqiang  The above code is already used successfully:\n- See [public kernel](https://www.kaggle.com/apacheco/shades-of-gray-color-constancy)\n- Discussion [post](https://www.kaggle.com/c/siim-isic-melanoma-classification/discussion/161719) where left-hand images are original and right-hand images after processing",
    "924126": "See my images,are you think that's ok ?",
    "924168": "jinxiaoqiang No, your images (taken from external dataset) are looking different from mine (see below). For example, see ISIC_0000002.jpeg which looks very different in your attachment.\nPerhaps you have modified the above code in some way?\n\nBefore and After applying Color Constancy code:\n![](https://www.googleapis.com/download/storage/v1/b/kaggle-user-content/o/inbox%2F656212%2Ff53d3ef94b7aad22390710ae8f298289%2Fafter_applying_color_constant.png?generation=1594460523272866&amp;alt=media)",
    "968216": "Has anyone made an albumentation out of this?  I tried but I was getting some errors and moved on from it.",
    "968531": "sirishks posted a function reference from [here](https://github.com/abhishekrana/isic2018-skin-lesion-classifier-tensorflow/blob/master/utils/utils_image.py). Below is what I used.\n\n```\nimport albumentations as A\nclass ColorConstancy(A.ImageOnlyTransform):\n    def apply(self, img, power=6, gamma=None, **params):\n         # see Sirish function\n\n```",
    "969521": "Does anyone use the gamma option of this?  If so, and if you are using it as a transform, you may wish to move the LUT into the init?() function, so that it doesn't have to be created for every single image.  I haven't seen notebooks leveraging the gamma though so was curious if anyone has found that to be useful."
  },
  "source": "meta"
}