# SPDX-License-Identifier: BSD-2-Clause # # Copyright (C) 2019, Raspberry Pi Ltd # # camera tuning tool Macbeth chart locator from ctt_ransac import * from ctt_tools import * import warnings """ NOTE: some custom functions have been used here to make the code more readable. These are defined in tools.py if they are needed for reference. """ """ Some inconsistencies between packages cause runtime warnings when running the clustering algorithm. This catches these warnings so they don't flood the output to the console """ def fxn(): warnings.warn("runtime", RuntimeWarning) """ Define the success message """ success_msg = 'Macbeth chart located successfully' def find_macbeth(Cam, img, mac_config=(0, 0)): small_chart, show = mac_config print('Locating macbeth chart') Cam.log += '\nLocating macbeth chart' """ catch the warnings """ warnings.simplefilter("ignore") fxn() """ Reference macbeth chart is created that will be correlated with the located macbeth chart guess to produce a confidence value for the match. """ ref = cv2.imread(Cam.path + 'ctt_ref.pgm', flags=cv2.IMREAD_GRAYSCALE) ref_w = 120 ref_h = 80 rc1 = (0, 0) rc2 = (0, ref_h) rc3 = (ref_w, ref_h) rc4 = (ref_w, 0) ref_corns = np.array((rc1, rc2, rc3, rc4), np.float32) ref_data = (ref, ref_w, ref_h, ref_corns) """ locate macbeth chart """ cor, mac, coords, msg = get_macbeth_chart(img, ref_data) # Keep a list that will include this and any brightened up versions of # the image for reuse. all_images = [img] """ following bits of code tries to fix common problems with simple techniques. If now or at any point the best correlation is of above 0.75, then nothing more is tried as this is a high enough confidence to ensure reliable macbeth square centre placement. """ """ brighten image 2x """ if cor < 0.75: a = 2 img_br = cv2.convertScaleAbs(img, alpha=a, beta=0) all_images.append(img_br) cor_b, mac_b, coords_b, msg_b = get_macbeth_chart(img_br, ref_data) if cor_b > cor: cor, mac, coords, msg = cor_b, mac_b, coords_b, msg_b """ brighten image 4x """ if cor < 0.75: a = 4 img_br = cv2.convertScaleAbs(img, alpha=a, beta=0) all_images.append(img_br) cor_b, mac_b, coords_b, msg_b = get_macbeth_chart(img_br, ref_data) if cor_b > cor: cor, mac, coords, msg = cor_b, mac_b, coords_b, msg_b """ In case macbeth chart is too small, take a selection of the image and attempt to locate macbeth chart within that. The scale increment is root 2 """ """ These variables will be used to transform the found coordinates at smaller scales back into the original. If ii is still -1 after this section that means it was not successful """ ii = -1 w_best = 0 h_best = 0 d_best = 100 """ d_best records the scale of the best match. Macbeth charts are only looked for at one scale increment smaller than the current best match in order to avoid unecessarily searching for macbeth charts at small scales. If a macbeth chart ha already been found then set d_best to 0 """ if cor != 0: d_best = 0 """ scale 3/2 (approx root2) """ if cor < 0.75: imgs = [] """ get size of image """ shape = list(img.shape[:2]) w, h = shape """ set dimensions of the subselection and the step along each axis between selections """ w_sel = int(2*w/3) h_sel = int(2*h/3) w_inc = int(w/6) h_inc = int(h/6) """ for each subselection, look for a macbeth chart loop over this and any brightened up images that we made to increase the likelihood of success """ for img_br in all_images: for i in range(3): for j in range(3): w_s, h_s = i*w_inc, j*h_inc img_sel = img_br[w_s:w_s+w_sel, h_s:h_s+h_sel] cor_ij, mac_ij, coords_ij, msg_ij = get_macbeth_chart(img_sel, ref_data) """ if the correlation is better than the best then record the scale and current subselection at which macbeth chart was found. Also record the coordinates, macbeth chart and message. """ if cor_ij > cor: cor = cor_ij mac, coords, msg = mac_ij, coords_ij, msg_ij ii, jj = i, j w_best, h_best = w_inc, h_inc d_best = 1 """ /* SPDX-License-Identifier: LGPL-2.1-or-later */ /* * Copyright (C) 2021-2022, Ideas On Board * * blc.h - RkISP1 Black Level Correction control */ #pragma once #include "algorithm.h" namespace libcamera { namespace ipa::rkisp1::algorithms { class BlackLevelCorrection : public Algorithm { public: BlackLevelCorrection(); ~BlackLevelCorrection() = default; int init(IPAContext &context, const YamlObject &tuningData) override; void prepare(IPAContext &context, const uint32_t frame, IPAFrameContext &frameContext, rkisp1_params_cfg *params) override; private: bool tuningParameters_; int16_t blackLevelRed_; int16_t blackLevelGreenR_; int16_t blackLevelGreenB_; int16_t blackLevelBlue_; }; } /* namespace ipa::rkisp1::algorithms */ } /* namespace libcamera */