{"id":18802,"url":"https://patchwork.libcamera.org/api/1.1/patches/18802/?format=json","web_url":"https://patchwork.libcamera.org/patch/18802/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/1.1/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20230706013926.218131-4-ben.benson@raspberrypi.com>","date":"2023-07-06T01:39:26","name":"[libcamera-devel,3/3] utils: raspberrypi: ctt: Code tidying","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"35d9dd716c4d1ccc4f6e994f5600a681be382d9f","submitter":{"id":171,"url":"https://patchwork.libcamera.org/api/1.1/people/171/?format=json","name":"Ben Benson","email":"ben.benson@raspberrypi.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/18802/mbox/","series":[{"id":3963,"url":"https://patchwork.libcamera.org/api/1.1/series/3963/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=3963","date":"2023-07-06T01:39:23","name":"Raspberry Pi Color Matrix and Tuning Tool Improvements","version":1,"mbox":"https://patchwork.libcamera.org/series/3963/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/18802/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/18802/checks/","tags":{},"headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id C4146C32AA\n\tfor <parsemail@patchwork.libcamera.org>;\n\tFri,  7 Jul 2023 13:41:26 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 21CF1628C9;\n\tFri,  7 Jul 2023 15:41:25 +0200 (CEST)","from mail-lf1-x132.google.com (mail-lf1-x132.google.com\n\t[IPv6:2a00:1450:4864:20::132])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 06524628BD\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri,  7 Jul 2023 14:07:47 +0200 (CEST)","by mail-lf1-x132.google.com with SMTP id\n\t2adb3069b0e04-4fb73ba3b5dso2903183e87.1\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 07 Jul 2023 05:07:46 -0700 (PDT)","from localhost.localdomain ([93.93.133.154])\n\tby smtp.gmail.com with ESMTPSA id\n\tz13-20020a7bc7cd000000b003fc0062f0f8sm1155915wmk.9.2023.07.07.05.07.45\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tFri, 07 Jul 2023 05:07:45 -0700 (PDT)"],"DKIM-Signature":["v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1688737285;\n\tbh=14/yBbWi1pMx5U4NcC4p6D5TYZpaAe33Pejpo/N0vwo=;\n\th=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe:\n\tList-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc:\n\tFrom;\n\tb=OUVVeN33jbXLZIgq2NGq4ztC0uf6nUuHIYdFtFo9Kg1p/HQNrrul5Sruq3ai/XF40\n\tsuamOXAOm4a7HExQprupSlgvCF4jeePh7YcWl7XjldXRFaBIiipIwrk/11hq910nLX\n\t/F7AsXZgU5u1aGXyklTd/eAh4J+DBo+tFdkKBwlYmn/tnXVzw0GyJCg5lYIVTaEhRg\n\tYCO0949XkcWq1lL5BrA4zSaqPyYi1YJQERorsY+MvTtx/gC/bLDx9J/Zz5nlARVMUW\n\tduZ6N1ctoTVWnV+RrMzZtdKZi2ceJSdpArw1dF41TIOIYgqYdT5qoZW6VSJeEh7RAC\n\tAoE92/IqjJoLA==","v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=raspberrypi.com; s=google; t=1688731666; x=1691323666;\n\th=content-transfer-encoding:mime-version:references:in-reply-to\n\t:message-id:date:subject:cc:to:from:from:to:cc:subject:date\n\t:message-id:reply-to;\n\tbh=DYyHsn0OMeUjMVdng7IjTkP1xYPflT8FFtPeTKBCgwk=;\n\tb=MjyAknFJPrComEIoEf/b0LugyKHerE1xmOrAlkr4f55M87Zdsv+6buxySuOUZu5UUd\n\ttDh/3CsrpwV/wEWrXMhRqcGg+NDZoHX1QIKtmQ9bQTswdjIG2Z0SJTa+OssXWksrjRHN\n\tPSyJrGcC6WhF3B9zXrKQe2I6cv+Q4816TIUfmck299GldJUb/ZzzJ+z0Zu+LO5Ex1P+Q\n\tiHVVQNYzae1m3gXV8N2R2ukMduN1JZI0+NQobGgbYzwQAMKCy/gugZpf7dv3XWgAy0U2\n\tj7TWGt5PaUuR/r3kHEZdD438rFnr6EeejlX1/pzSg7WKKWa2bhqGyoExM281sWYFkWmN\n\t8ESA=="],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (2048-bit key; \n\tunprotected) header.d=raspberrypi.com\n\theader.i=@raspberrypi.com\n\theader.b=\"MjyAknFJ\"; dkim-atps=neutral","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20221208; t=1688731666; x=1691323666;\n\th=content-transfer-encoding:mime-version:references:in-reply-to\n\t:message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc\n\t:subject:date:message-id:reply-to;\n\tbh=DYyHsn0OMeUjMVdng7IjTkP1xYPflT8FFtPeTKBCgwk=;\n\tb=LWbzitKzNETMbLxifxoJeAD/ieu8dip2lJUfbwOV3t5EZjEe1/1fIL09eqkSlpxCwS\n\tvbQgoxZtUp99sJX0s/ohonNV+eO1KKjTbBeOccL4Iq/TgMCVxwohrpUisF2v5+daZhIr\n\tRMUipVqg31KC5bOt65hfiLypB8YzHH3tc54eKMNqoU6RpbNJJ0vXpJhsLpKGXotM1We8\n\tDD4CO0NXqmr9Q6eDQaUpzkuLhMqdqOrjC7TMlw9ISW5s+DKs+pWAMEWPoUKmnKxWeVSG\n\tdDBo9hNQupyPYHW/TuERldgPCpMv1dtXnessDeyuDwfm18Qsa32y/yAkN6Aql5e47E1A\n\tl1cQ==","X-Gm-Message-State":"ABy/qLYordUo9/8kj17pVM5GSiFpzT+63nE/uwf1v8BI5WC8h7wGZH3Y\n\teYwZNoS08rHpQnANZRpCFzJdW+yWlqo7coWgjSRvhg==","X-Google-Smtp-Source":"APBJJlEB6+VmnvLmF/D4mhaS3u6j7o0BBMtD/S51UWLRWjrAeQHXzaN0jX/7PSGaEC/mcAhQe5n/ng==","X-Received":"by 2002:a05:6512:b8b:b0:4fb:8ff3:1f72 with SMTP id\n\tb11-20020a0565120b8b00b004fb8ff31f72mr5109669lfv.1.1688731665815; \n\tFri, 07 Jul 2023 05:07:45 -0700 (PDT)","To":"libcamera-devel@lists.libcamera.org","Date":"Thu,  6 Jul 2023 02:39:26 +0100","Message-Id":"<20230706013926.218131-4-ben.benson@raspberrypi.com>","X-Mailer":"git-send-email 2.34.1","In-Reply-To":"<20230706013926.218131-1-ben.benson@raspberrypi.com>","References":"<20230706013926.218131-1-ben.benson@raspberrypi.com>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","X-Mailman-Approved-At":"Fri, 07 Jul 2023 15:41:21 +0200","Subject":"[libcamera-devel] [PATCH 3/3] utils: raspberrypi: ctt: Code tidying","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","From":"Ben Benson via libcamera-devel <libcamera-devel@lists.libcamera.org>","Reply-To":"Ben Benson <ben.benson@raspberrypi.com>","Cc":"Ben Benson <ben.benson@raspberrypi.com>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"Altered the way that some lines are laid out, made functions\nmore attractive to look at, and tidied up messy areas.\n\nSigned-off-by Ben Benson <ben.benson@raspberrypi.com>\n---\n utils/raspberrypi/ctt/ctt_ccm.py | 61 +++++++++++++++-----------------\n 1 file changed, 29 insertions(+), 32 deletions(-)","diff":"diff --git a/utils/raspberrypi/ctt/ctt_ccm.py b/utils/raspberrypi/ctt/ctt_ccm.py\nindex bd44b4d8..85ca6827 100644\n--- a/utils/raspberrypi/ctt/ctt_ccm.py\n+++ b/utils/raspberrypi/ctt/ctt_ccm.py\n@@ -47,11 +47,8 @@ def degamma(x):\n \n \n def gamma(x):\n-    # return (x *  * (1 / 2.4)  *  1.055 - 0.055)\n-    e = []\n-    for i in range(len(x)):\n-        e.append(((x[i] / 255) ** (1 / 2.4) * 1.055 - 0.055) * 255)\n-    return e\n+    # Take 3 long array of color values and gamma them\n+    return [((colour / 255) ** (1 / 2.4) * 1.055 - 0.055) * 255 for colour in x]\n \n \n \"\"\"\n@@ -96,10 +93,8 @@ def ccm(Cam, cal_cr_list, cal_cb_list):\n     \"\"\"\n     m_srgb = degamma(m_rgb)  # now in 16 bit color.\n \n-    m_lab = []\n-    for col in m_srgb:\n-        m_lab.append(colors.RGB_to_LAB(col / 256))\n-    # This produces matrix of LAB values for ideal color chart)\n+    m_lab = [colors.RGB_to_LAB(color / 256) for color in m_srgb]\n+    # This produces array of LAB values for ideal color chart)\n \n     \"\"\"\n     reorder reference values to match how patches are ordered\n@@ -168,7 +163,7 @@ def ccm(Cam, cal_cr_list, cal_cb_list):\n         sumde = 0\n         ccm = do_ccm(r, g, b, m_srgb)\n         # This is the initial guess that our optimisation code works with.\n-\n+        original_ccm = ccm\n         r1 = ccm[0]\n         r2 = ccm[1]\n         g1 = ccm[3]\n@@ -199,12 +194,13 @@ def ccm(Cam, cal_cr_list, cal_cb_list):\n         [r1, r2, g1, g2, b1, b2] = result.x\n         # The new, optimised color correction matrix values\n         optimised_ccm = [r1, r2, (1 - r1 - r2), g1, g2, (1 - g1 - g2), b1, b2, (1 - b1 - b2)]\n+\n         # This is the optimised Color Matrix (preserving greys by summing rows up to 1)\n         Cam.log += str(optimised_ccm)\n         Cam.log += \"\\n Old Color Correction Matrix Below \\n\"\n         Cam.log += str(ccm)\n \n-        formatted_ccm = np.array(ccm).reshape((3, 3))\n+        formatted_ccm = np.array(original_ccm).reshape((3, 3))\n \n         '''\n         below is a whole load of code that then applies the latest color\n@@ -213,22 +209,23 @@ def ccm(Cam, cal_cr_list, cal_cb_list):\n         '''\n         optimised_ccm_rgb = []  # Original Color Corrected Matrix RGB / LAB\n         optimised_ccm_lab = []\n-        for w in range(24):\n-            RGB = np.array([r[w], g[w], b[w]])\n-            ccm_applied_rgb = np.dot(formatted_ccm, (RGB / 256))\n-            optimised_ccm_rgb.append(gamma(ccm_applied_rgb))\n-            optimised_ccm_lab.append(colors.RGB_to_LAB(ccm_applied_rgb))\n \n         formatted_optimised_ccm = np.array(ccm).reshape((3, 3))\n         after_gamma_rgb = []\n         after_gamma_lab = []\n-        for w in range(24):\n-            RGB = np.array([r[w], g[w], b[w]])\n+\n+        for red, green, blue in zip(r, g, b):\n+            RGB = np.array([red, green, blue])\n+            ccm_applied_rgb = np.dot(formatted_ccm, (RGB / 256))\n+            optimised_ccm_rgb.append(gamma(ccm_applied_rgb))\n+            optimised_ccm_lab.append(colors.RGB_to_LAB(ccm_applied_rgb))\n+\n             optimised_ccm_applied_rgb = np.dot(formatted_optimised_ccm, RGB / 256)\n             after_gamma_rgb.append(gamma(optimised_ccm_applied_rgb))\n             after_gamma_lab.append(colors.RGB_to_LAB(optimised_ccm_applied_rgb))\n+\n         '''\n-        Gamma After RGB / LAB\n+        Gamma After RGB / LAB - not used in calculations, only used for visualisation\n         We now want to spit out some data that shows\n         how the optimisation has improved the color matricies\n         '''\n@@ -303,8 +300,8 @@ def guess(x0, r, g, b, m_lab):       # provides a method of numerical feedback f\n def transform_and_evaluate(ccm, r, g, b, m_lab):  # Transforms colors to LAB and applies the correction matrix\n     # create list of matrix changed colors\n     realrgb = []\n-    for i in range(len(r)):\n-        RGB = np.array([r[i], g[i], b[i]])\n+    for red, green, blue in zip(r, g, b):\n+        RGB = np.array([red, green, blue])\n         rgb_post_ccm = np.dot(ccm, RGB)  # This is RGB values after the color correction matrix has been applied\n         realrgb.append(colors.RGB_to_LAB(rgb_post_ccm))\n     # now compare that with m_lab and return numeric result, averaged for each patch\n@@ -315,12 +312,12 @@ def sumde(listA, listB):\n     global typenum, test_patches\n     sumde = 0\n     maxde = 0\n-    patchde = []\n-    for i in range(len(listA)):\n-        if maxde < (deltae(listA[i], listB[i])):\n-            maxde = deltae(listA[i], listB[i])\n-        patchde.append(deltae(listA[i], listB[i]))\n-        sumde += deltae(listA[i], listB[i])\n+    patchde = []  # Create array of the delta E values for each patch. useful for optimisation of certain patches\n+    for listA_item, listB_item in zip(listA, listB):\n+        if maxde < (deltae(listA_item, listB_item)):\n+            maxde = deltae(listA_item, listB_item)\n+        patchde.append(deltae(listA_item, listB_item))\n+        sumde += deltae(listA_item, listB_item)\n     '''\n     The different options specified at the start allow for\n     the maximum to be returned, average or specific patches\n@@ -330,9 +327,8 @@ def sumde(listA, listB):\n     if typenum == 1:\n         return maxde\n     if typenum == 2:\n-        output = 0\n-        for y in range(len(test_patches)):\n-            output += patchde[test_patches[y]]   # grabs the specific patches (no need for averaging here)\n+        output = sum([patchde[test_patch] for test_patch in test_patches])\n+        # Selects only certain patches and returns the output for them\n         return output\n \n \n@@ -341,8 +337,9 @@ calculates the ccm for an individual image.\n ccms are calculate in rgb space, and are fit by hand. Although it is a 3x3\n matrix, each row must add up to 1 in order to conserve greyness, simplifying\n calculation.\n-Should you want to fit them in another space (e.g. LAB) we wish you the best of\n-luck and send us the code when you are done! :-)\n+The initial CCM is calculated in RGB, and then optimised in LAB color space\n+This simplifies the initial calculation but then gets us the accuracy of\n+using LAB color space.\n \"\"\"\n \n \n","prefixes":["libcamera-devel","3/3"]}