From patchwork Thu Jul 6 01:39:26 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ben Benson X-Patchwork-Id: 18802 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id C4146C32AA for ; Fri, 7 Jul 2023 13:41:26 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 21CF1628C9; Fri, 7 Jul 2023 15:41:25 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1688737285; bh=14/yBbWi1pMx5U4NcC4p6D5TYZpaAe33Pejpo/N0vwo=; h=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=OUVVeN33jbXLZIgq2NGq4ztC0uf6nUuHIYdFtFo9Kg1p/HQNrrul5Sruq3ai/XF40 suamOXAOm4a7HExQprupSlgvCF4jeePh7YcWl7XjldXRFaBIiipIwrk/11hq910nLX /F7AsXZgU5u1aGXyklTd/eAh4J+DBo+tFdkKBwlYmn/tnXVzw0GyJCg5lYIVTaEhRg YCO0949XkcWq1lL5BrA4zSaqPyYi1YJQERorsY+MvTtx/gC/bLDx9J/Zz5nlARVMUW duZ6N1ctoTVWnV+RrMzZtdKZi2ceJSdpArw1dF41TIOIYgqYdT5qoZW6VSJeEh7RAC AoE92/IqjJoLA== Received: from mail-lf1-x132.google.com (mail-lf1-x132.google.com [IPv6:2a00:1450:4864:20::132]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 06524628BD for ; Fri, 7 Jul 2023 14:07:47 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (2048-bit key; unprotected) header.d=raspberrypi.com header.i=@raspberrypi.com header.b="MjyAknFJ"; dkim-atps=neutral Received: by mail-lf1-x132.google.com with SMTP id 2adb3069b0e04-4fb73ba3b5dso2903183e87.1 for ; Fri, 07 Jul 2023 05:07:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=raspberrypi.com; s=google; t=1688731666; x=1691323666; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=DYyHsn0OMeUjMVdng7IjTkP1xYPflT8FFtPeTKBCgwk=; b=MjyAknFJPrComEIoEf/b0LugyKHerE1xmOrAlkr4f55M87Zdsv+6buxySuOUZu5UUd tDh/3CsrpwV/wEWrXMhRqcGg+NDZoHX1QIKtmQ9bQTswdjIG2Z0SJTa+OssXWksrjRHN PSyJrGcC6WhF3B9zXrKQe2I6cv+Q4816TIUfmck299GldJUb/ZzzJ+z0Zu+LO5Ex1P+Q iHVVQNYzae1m3gXV8N2R2ukMduN1JZI0+NQobGgbYzwQAMKCy/gugZpf7dv3XWgAy0U2 j7TWGt5PaUuR/r3kHEZdD438rFnr6EeejlX1/pzSg7WKKWa2bhqGyoExM281sWYFkWmN 8ESA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688731666; x=1691323666; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=DYyHsn0OMeUjMVdng7IjTkP1xYPflT8FFtPeTKBCgwk=; b=LWbzitKzNETMbLxifxoJeAD/ieu8dip2lJUfbwOV3t5EZjEe1/1fIL09eqkSlpxCwS vbQgoxZtUp99sJX0s/ohonNV+eO1KKjTbBeOccL4Iq/TgMCVxwohrpUisF2v5+daZhIr RMUipVqg31KC5bOt65hfiLypB8YzHH3tc54eKMNqoU6RpbNJJ0vXpJhsLpKGXotM1We8 DD4CO0NXqmr9Q6eDQaUpzkuLhMqdqOrjC7TMlw9ISW5s+DKs+pWAMEWPoUKmnKxWeVSG dDBo9hNQupyPYHW/TuERldgPCpMv1dtXnessDeyuDwfm18Qsa32y/yAkN6Aql5e47E1A l1cQ== X-Gm-Message-State: ABy/qLYordUo9/8kj17pVM5GSiFpzT+63nE/uwf1v8BI5WC8h7wGZH3Y eYwZNoS08rHpQnANZRpCFzJdW+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 b11-20020a0565120b8b00b004fb8ff31f72mr5109669lfv.1.1688731665815; Fri, 07 Jul 2023 05:07:45 -0700 (PDT) Received: from localhost.localdomain ([93.93.133.154]) by smtp.gmail.com with ESMTPSA id z13-20020a7bc7cd000000b003fc0062f0f8sm1155915wmk.9.2023.07.07.05.07.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 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 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: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Ben Benson via libcamera-devel From: Ben Benson Reply-To: Ben Benson Cc: Ben Benson Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Altered the way that some lines are laid out, made functions more attractive to look at, and tidied up messy areas. Signed-off-by Ben Benson --- utils/raspberrypi/ctt/ctt_ccm.py | 61 +++++++++++++++----------------- 1 file changed, 29 insertions(+), 32 deletions(-) diff --git a/utils/raspberrypi/ctt/ctt_ccm.py b/utils/raspberrypi/ctt/ctt_ccm.py index bd44b4d8..85ca6827 100644 --- a/utils/raspberrypi/ctt/ctt_ccm.py +++ b/utils/raspberrypi/ctt/ctt_ccm.py @@ -47,11 +47,8 @@ def degamma(x): def gamma(x): - # return (x * * (1 / 2.4) * 1.055 - 0.055) - e = [] - for i in range(len(x)): - e.append(((x[i] / 255) ** (1 / 2.4) * 1.055 - 0.055) * 255) - return e + # Take 3 long array of color values and gamma them + return [((colour / 255) ** (1 / 2.4) * 1.055 - 0.055) * 255 for colour in x] """ @@ -96,10 +93,8 @@ def ccm(Cam, cal_cr_list, cal_cb_list): """ m_srgb = degamma(m_rgb) # now in 16 bit color. - m_lab = [] - for col in m_srgb: - m_lab.append(colors.RGB_to_LAB(col / 256)) - # This produces matrix of LAB values for ideal color chart) + m_lab = [colors.RGB_to_LAB(color / 256) for color in m_srgb] + # This produces array of LAB values for ideal color chart) """ reorder reference values to match how patches are ordered @@ -168,7 +163,7 @@ def ccm(Cam, cal_cr_list, cal_cb_list): sumde = 0 ccm = do_ccm(r, g, b, m_srgb) # This is the initial guess that our optimisation code works with. - + original_ccm = ccm r1 = ccm[0] r2 = ccm[1] g1 = ccm[3] @@ -199,12 +194,13 @@ def ccm(Cam, cal_cr_list, cal_cb_list): [r1, r2, g1, g2, b1, b2] = result.x # The new, optimised color correction matrix values optimised_ccm = [r1, r2, (1 - r1 - r2), g1, g2, (1 - g1 - g2), b1, b2, (1 - b1 - b2)] + # This is the optimised Color Matrix (preserving greys by summing rows up to 1) Cam.log += str(optimised_ccm) Cam.log += "\n Old Color Correction Matrix Below \n" Cam.log += str(ccm) - formatted_ccm = np.array(ccm).reshape((3, 3)) + formatted_ccm = np.array(original_ccm).reshape((3, 3)) ''' below is a whole load of code that then applies the latest color @@ -213,22 +209,23 @@ def ccm(Cam, cal_cr_list, cal_cb_list): ''' optimised_ccm_rgb = [] # Original Color Corrected Matrix RGB / LAB optimised_ccm_lab = [] - for w in range(24): - RGB = np.array([r[w], g[w], b[w]]) - ccm_applied_rgb = np.dot(formatted_ccm, (RGB / 256)) - optimised_ccm_rgb.append(gamma(ccm_applied_rgb)) - optimised_ccm_lab.append(colors.RGB_to_LAB(ccm_applied_rgb)) formatted_optimised_ccm = np.array(ccm).reshape((3, 3)) after_gamma_rgb = [] after_gamma_lab = [] - for w in range(24): - RGB = np.array([r[w], g[w], b[w]]) + + for red, green, blue in zip(r, g, b): + RGB = np.array([red, green, blue]) + ccm_applied_rgb = np.dot(formatted_ccm, (RGB / 256)) + optimised_ccm_rgb.append(gamma(ccm_applied_rgb)) + optimised_ccm_lab.append(colors.RGB_to_LAB(ccm_applied_rgb)) + optimised_ccm_applied_rgb = np.dot(formatted_optimised_ccm, RGB / 256) after_gamma_rgb.append(gamma(optimised_ccm_applied_rgb)) after_gamma_lab.append(colors.RGB_to_LAB(optimised_ccm_applied_rgb)) + ''' - Gamma After RGB / LAB + Gamma After RGB / LAB - not used in calculations, only used for visualisation We now want to spit out some data that shows how the optimisation has improved the color matricies ''' @@ -303,8 +300,8 @@ def guess(x0, r, g, b, m_lab): # provides a method of numerical feedback f def transform_and_evaluate(ccm, r, g, b, m_lab): # Transforms colors to LAB and applies the correction matrix # create list of matrix changed colors realrgb = [] - for i in range(len(r)): - RGB = np.array([r[i], g[i], b[i]]) + for red, green, blue in zip(r, g, b): + RGB = np.array([red, green, blue]) rgb_post_ccm = np.dot(ccm, RGB) # This is RGB values after the color correction matrix has been applied realrgb.append(colors.RGB_to_LAB(rgb_post_ccm)) # now compare that with m_lab and return numeric result, averaged for each patch @@ -315,12 +312,12 @@ def sumde(listA, listB): global typenum, test_patches sumde = 0 maxde = 0 - patchde = [] - for i in range(len(listA)): - if maxde < (deltae(listA[i], listB[i])): - maxde = deltae(listA[i], listB[i]) - patchde.append(deltae(listA[i], listB[i])) - sumde += deltae(listA[i], listB[i]) + patchde = [] # Create array of the delta E values for each patch. useful for optimisation of certain patches + for listA_item, listB_item in zip(listA, listB): + if maxde < (deltae(listA_item, listB_item)): + maxde = deltae(listA_item, listB_item) + patchde.append(deltae(listA_item, listB_item)) + sumde += deltae(listA_item, listB_item) ''' The different options specified at the start allow for the maximum to be returned, average or specific patches @@ -330,9 +327,8 @@ def sumde(listA, listB): if typenum == 1: return maxde if typenum == 2: - output = 0 - for y in range(len(test_patches)): - output += patchde[test_patches[y]] # grabs the specific patches (no need for averaging here) + output = sum([patchde[test_patch] for test_patch in test_patches]) + # Selects only certain patches and returns the output for them return output @@ -341,8 +337,9 @@ calculates the ccm for an individual image. ccms are calculate in rgb space, and are fit by hand. Although it is a 3x3 matrix, each row must add up to 1 in order to conserve greyness, simplifying calculation. -Should you want to fit them in another space (e.g. LAB) we wish you the best of -luck and send us the code when you are done! :-) +The initial CCM is calculated in RGB, and then optimised in LAB color space +This simplifies the initial calculation but then gets us the accuracy of +using LAB color space. """