From 554ea8f54a699950a9226e1d0808cc41ca4859f0 Mon Sep 17 00:00:00 2001 From: Dean Lee Date: Tue, 15 Dec 2020 07:33:40 +0800 Subject: [PATCH] refactor imgproc/utils (#2766) * refactor imgproc/utils * const * space * return value --- selfdrive/camerad/cameras/camera_qcom.cc | 4 +-- selfdrive/camerad/imgproc/utils.cc | 45 ++++++++++-------------- selfdrive/camerad/imgproc/utils.h | 11 +++--- 3 files changed, 25 insertions(+), 35 deletions(-) diff --git a/selfdrive/camerad/cameras/camera_qcom.cc b/selfdrive/camerad/cameras/camera_qcom.cc index a781d1dbe9..a439058217 100644 --- a/selfdrive/camerad/cameras/camera_qcom.cc +++ b/selfdrive/camerad/cameras/camera_qcom.cc @@ -2106,12 +2106,12 @@ void camera_process_frame(MultiCameraState *s, CameraState *c, int cnt) { CL_CHECK(clEnqueueReadBuffer(b->q, s->rgb_conv_result_cl, true, 0, b->rgb_width / NUM_SEGMENTS_X * b->rgb_height / NUM_SEGMENTS_Y * sizeof(int16_t), s->conv_result.get(), 0, 0, 0)); - get_lapmap_one(s->conv_result.get(), &s->lapres[roi_id], b->rgb_width / NUM_SEGMENTS_X, b->rgb_height / NUM_SEGMENTS_Y); + s->lapres[roi_id] = get_lapmap_one(s->conv_result.get(), b->rgb_width / NUM_SEGMENTS_X, b->rgb_height / NUM_SEGMENTS_Y); // setup self recover const float lens_true_pos = s->rear.lens_true_pos; std::atomic& self_recover = s->rear.self_recover; - if (is_blur(&s->lapres[0]) && + if (is_blur(&s->lapres[0], std::size(s->lapres)) && (lens_true_pos < (s->rear.device == DEVICE_LP3 ? LP3_AF_DAC_DOWN : OP3T_AF_DAC_DOWN) + 1 || lens_true_pos > (s->rear.device == DEVICE_LP3 ? LP3_AF_DAC_UP : OP3T_AF_DAC_UP) - 1) && self_recover < 2) { diff --git a/selfdrive/camerad/imgproc/utils.cc b/selfdrive/camerad/imgproc/utils.cc index 0b36d68637..4aeffac530 100644 --- a/selfdrive/camerad/imgproc/utils.cc +++ b/selfdrive/camerad/imgproc/utils.cc @@ -1,43 +1,36 @@ #include "utils.h" #include #include - +#include // calculate score based on laplacians in one area -void get_lapmap_one(int16_t *lap, uint16_t *res, int x_pitch, int y_pitch) { - int size = x_pitch * y_pitch; +uint16_t get_lapmap_one(const int16_t *lap, int x_pitch, int y_pitch) { + const int size = x_pitch * y_pitch; // avg and max of roi - float fsum = 0; - int16_t mean, max; - max = 0; - - for (int i = 0; i < size; i++) { - int x_offset = i % x_pitch; - int y_offset = i / x_pitch; - fsum += lap[x_offset + y_offset*x_pitch]; - max = std::max(lap[x_offset + y_offset*x_pitch], max); + int16_t max = 0; + int sum = 0; + for (int i = 0; i < size; ++i) { + const int16_t v = lap[i % x_pitch + (i / x_pitch) * x_pitch]; + sum += v; + if (v > max) max = v; } - mean = fsum / size; + const int16_t mean = sum / size; // var of roi - float fvar = 0; - for (int i = 0; i < size; i++) { - int x_offset = i % x_pitch; - int y_offset = i / x_pitch; - fvar += (float)((lap[x_offset + y_offset*x_pitch] - mean) * (lap[x_offset + y_offset*x_pitch] - mean)); + int var = 0; + for (int i = 0; i < size; ++i) { + var += std::pow(lap[i % x_pitch + (i / x_pitch) * x_pitch] - mean, 2); } - fvar = fvar / size; - - *res = std::min(5 * fvar + max, (float)65535); + const float fvar = (float)var / size; + return std::min(5 * fvar + max, (float)65535); } -bool is_blur(uint16_t *lapmap) { - int n_roi = (ROI_X_MAX - ROI_X_MIN + 1) * (ROI_Y_MAX - ROI_Y_MIN + 1); +bool is_blur(const uint16_t *lapmap, const size_t size) { float bad_sum = 0; - for (int i = 0; i < n_roi; i++) { - if (*(lapmap + i) < LM_THRESH) { - bad_sum += 1/(float)n_roi; + for (int i = 0; i < size; i++) { + if (lapmap[i] < LM_THRESH) { + bad_sum += 1 / (float)size; } } return (bad_sum > LM_PREC_THRESH); diff --git a/selfdrive/camerad/imgproc/utils.h b/selfdrive/camerad/imgproc/utils.h index 203ac57a66..4928f55a68 100644 --- a/selfdrive/camerad/imgproc/utils.h +++ b/selfdrive/camerad/imgproc/utils.h @@ -1,8 +1,7 @@ -#ifndef IMGPROC_UTILS -#define IMGPROC_UTILS +#pragma once #include - +#include #define NUM_SEGMENTS_X 8 #define NUM_SEGMENTS_Y 6 @@ -24,7 +23,5 @@ const int16_t lapl_conv_krnl[9] = {0, 1, 0, 1, -4, 1, 0, 1, 0}; -void get_lapmap_one(int16_t *lap, uint16_t *res, int x_pitch, int y_pitch); -bool is_blur(uint16_t *lapmap); - -#endif +uint16_t get_lapmap_one(const int16_t *lap, int x_pitch, int y_pitch); +bool is_blur(const uint16_t *lapmap, const size_t size);