From patchwork Fri Jul 7 03:17:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ben Benson X-Patchwork-Id: 18818 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 0F7CBC3243 for ; Thu, 13 Jul 2023 10:35:25 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id A5098628BE; Thu, 13 Jul 2023 12:35:24 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1689244524; bh=jw+82qi0/UuzPpsk/6k6piAp+aRX3cyESCpbl+L9cMk=; 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=IBvbIxf8T+2Av3q3r9W62GfHUMHkfhXSdsIn0ovOrK1UV8v/vCEt+WgarWeGI98aQ GjHRC3d2i0i+32tAAFeDAJrrG8Tvhqx56JKdyK1qlyk+0VK1unc2uzt5h/rOIZzOV/ ogp0eIlAulRtHNdlw/Hu8V5lBPPpKUwnsG7/N+pPo9CH4mTwDO2aIoG1/uFbW0ypFW uvuvPN0xtO+ayo1YfMTYf8EYIcbSgnIx52vOsAKoDR9HJ9lNyv5n7mTxSzktxO06Gz lAtcnZIUEzHcnV8GC5dcXa5/jlrWd2tK0kdD37eY6ozOrR8cQpfO7YEU0pFhIYZUIn 0s05o96Xz497w== Received: from mail-wm1-x332.google.com (mail-wm1-x332.google.com [IPv6:2a00:1450:4864:20::332]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 00EBB628BC for ; Thu, 13 Jul 2023 12:35:20 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (2048-bit key; unprotected) header.d=raspberrypi.com header.i=@raspberrypi.com header.b="sNkbTw9c"; dkim-atps=neutral Received: by mail-wm1-x332.google.com with SMTP id 5b1f17b1804b1-3fbc59de009so4449075e9.3 for ; Thu, 13 Jul 2023 03:35:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=raspberrypi.com; s=google; t=1689244519; x=1691836519; 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=uDUF0jTZCrHu9jkvf3VStWYgCNFIJCcJisHmCTREtxY=; b=sNkbTw9cHlceE5z9cUiWHPNsARgYKxSjg8EaexuOE5ZCMOb9iX0a5SDAxQPvVQ7y6t hzMSxs8rkcSLoBpcpZMh05l5GOfPNeChSTvkbAk+SiAPchyeD+3hZyM9K+F5jJKVmfqi XtUJUnBLpF4ENHXPHEShIdKuVHtVcV4PVrnWXMbHzTu+g9ZW1g1WaRHPCtuyD9s6v4Wd HIm+tE9U8qmXWEqZmbxeUV+VwRQp3cN4UItRrevdjqxjIjt9hEdlkuQ9l3sLTNwC9q9P F2ydRu8ZvSF5sH5px7AWu+XYhJm1d8Wgv2J+uC5u/dTaFRDsNnEpgvhH+VW9ZdvEvtmX SEXg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689244519; x=1691836519; 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=uDUF0jTZCrHu9jkvf3VStWYgCNFIJCcJisHmCTREtxY=; b=fB4/EVjZpkm/8mDoLqIYBESEz5ywR4x3N025Za0miPBBNHcO5sOvqNwlAF7ZAmhGyo QDaT12nls6yxOPZ1y3glbyIqlxyIduQMTvEib8yAwwW+8ftKwYBfhZocMxEHm+Ht21wk C2+pspcBWeZwMH2NEuv4rkYYmm6SjQwyb8NsHOC9VPv2qAP0d+7K9l3eztfeSCzTJsSb 8y4HWaXeZ34mn7ZlTGeNKt82cKp4FeE092WqHWjxgObzIhxySPrd7qRjyK0OHBFP17xu w96wlFWbL/I/5tY859g6EB97ftbyBWILPdbcGWO6A0e8U3vpJ249XOXnXk9KCVWQqsBa k5LQ== X-Gm-Message-State: ABy/qLYQkWS7S7OCJj6d71mPqjDpDnKXm0Xdt1A72n2gADWpDRigpmU3 djWSJE+kU371CyZ0SrriJxYquHkIjGIhDgojwZEOVTmo X-Google-Smtp-Source: APBJJlF/7g/yRz47991wE4qKUA33UGYdJcYKj8Dg4ROpcAkbhP3lVAn3Q89KU7RirjvMWiIgVWTxOA== X-Received: by 2002:a5d:4bc7:0:b0:313:f862:6e3e with SMTP id l7-20020a5d4bc7000000b00313f8626e3emr1049626wrt.40.1689244519325; Thu, 13 Jul 2023 03:35:19 -0700 (PDT) Received: from localhost.localdomain ([93.93.133.154]) by smtp.gmail.com with ESMTPSA id r8-20020a056000014800b0030fa3567541sm7551143wrx.48.2023.07.13.03.35.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Jul 2023 03:35:19 -0700 (PDT) To: libcamera-devel@lists.libcamera.org Date: Fri, 7 Jul 2023 04:17:02 +0100 Message-Id: <20230707031702.603261-4-ben.benson@raspberrypi.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230707031702.603261-1-ben.benson@raspberrypi.com> References: <20230707031702.603261-1-ben.benson@raspberrypi.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 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, 27 insertions(+), 34 deletions(-) diff --git a/utils/raspberrypi/ctt/ctt_ccm.py b/utils/raspberrypi/ctt/ctt_ccm.py index 49159535..a09bfd09 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) + # Produce array of LAB values for ideal color chart + m_lab = [colors.RGB_to_LAB(color / 256) for color in m_srgb] """ 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] @@ -188,7 +183,7 @@ def ccm(Cam, cal_cr_list, cal_cb_list): We use our old CCM as the initial guess for the program to find the optimised matrix ''' - result = minimize(guess, x0, args=(r, g, b, m_lab), tol=0.0000000001) + result = minimize(guess, x0, args=(r, g, b, m_lab), tol=0.01) ''' This produces a color matrix which has the lowest delta E possible, based off the input data. Note it is impossible for this to reach @@ -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,21 @@ 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)) + formatted_optimised_ccm = np.array(optimised_ccm).reshape((3, 3)) after_gamma_rgb = [] after_gamma_lab = [] - for w in range(24): - RGB = np.array([r[w], g[w], b[w]]) - optimised_ccm_applied_rgb = np.dot(formatted_optimised_ccm, RGB / 256) + + for RGB in zip(r, g, b): + ccm_applied_rgb = np.dot(formatted_ccm, (np.array(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, np.array(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 matrices ''' @@ -303,9 +298,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]]) - rgb_post_ccm = np.dot(ccm, RGB) # This is RGB values after the color correction matrix has been applied + for RGB in zip(r, g, b): + rgb_post_ccm = np.dot(ccm, np.array(RGB) / 256) # 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 return (sumde(realrgb, m_lab) / 24) # returns an average result of delta E @@ -315,12 +309,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 +324,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