uart
Dependencies: MAX32620FTHR USBDevice
img_utils.c@0:c62425b2f286, 18 months ago (annotated)
- Committer:
- rsjawale24
- Date:
- Sun Oct 16 15:35:01 2022 +0000
- Revision:
- 0:c62425b2f286
ok
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rsjawale24 | 0:c62425b2f286 | 1 | /******************************************************************************* |
rsjawale24 | 0:c62425b2f286 | 2 | * Copyright (C) Maxim Integrated Products, Inc., All rights Reserved. |
rsjawale24 | 0:c62425b2f286 | 3 | * |
rsjawale24 | 0:c62425b2f286 | 4 | * This software is protected by copyright laws of the United States and |
rsjawale24 | 0:c62425b2f286 | 5 | * of foreign countries. This material may also be protected by patent laws |
rsjawale24 | 0:c62425b2f286 | 6 | * and technology transfer regulations of the United States and of foreign |
rsjawale24 | 0:c62425b2f286 | 7 | * countries. This software is furnished under a license agreement and/or a |
rsjawale24 | 0:c62425b2f286 | 8 | * nondisclosure agreement and may only be used or reproduced in accordance |
rsjawale24 | 0:c62425b2f286 | 9 | * with the terms of those agreements. Dissemination of this information to |
rsjawale24 | 0:c62425b2f286 | 10 | * any party or parties not specified in the license agreement and/or |
rsjawale24 | 0:c62425b2f286 | 11 | * nondisclosure agreement is expressly prohibited. |
rsjawale24 | 0:c62425b2f286 | 12 | * |
rsjawale24 | 0:c62425b2f286 | 13 | * The above copyright notice and this permission notice shall be included |
rsjawale24 | 0:c62425b2f286 | 14 | * in all copies or substantial portions of the Software. |
rsjawale24 | 0:c62425b2f286 | 15 | * |
rsjawale24 | 0:c62425b2f286 | 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
rsjawale24 | 0:c62425b2f286 | 17 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
rsjawale24 | 0:c62425b2f286 | 18 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
rsjawale24 | 0:c62425b2f286 | 19 | * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES |
rsjawale24 | 0:c62425b2f286 | 20 | * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
rsjawale24 | 0:c62425b2f286 | 21 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
rsjawale24 | 0:c62425b2f286 | 22 | * OTHER DEALINGS IN THE SOFTWARE. |
rsjawale24 | 0:c62425b2f286 | 23 | * |
rsjawale24 | 0:c62425b2f286 | 24 | * Except as contained in this notice, the name of Maxim Integrated |
rsjawale24 | 0:c62425b2f286 | 25 | * Products, Inc. shall not be used except as stated in the Maxim Integrated |
rsjawale24 | 0:c62425b2f286 | 26 | * Products, Inc. Branding Policy. |
rsjawale24 | 0:c62425b2f286 | 27 | * |
rsjawale24 | 0:c62425b2f286 | 28 | * The mere transfer of this software does not imply any licenses |
rsjawale24 | 0:c62425b2f286 | 29 | * of trade secrets, proprietary technology, copyrights, patents, |
rsjawale24 | 0:c62425b2f286 | 30 | * trademarks, maskwork rights, or any other form of intellectual |
rsjawale24 | 0:c62425b2f286 | 31 | * property whatsoever. Maxim Integrated Products, Inc. retains all |
rsjawale24 | 0:c62425b2f286 | 32 | * ownership rights. |
rsjawale24 | 0:c62425b2f286 | 33 | ******************************************************************************* |
rsjawale24 | 0:c62425b2f286 | 34 | */ |
rsjawale24 | 0:c62425b2f286 | 35 | |
rsjawale24 | 0:c62425b2f286 | 36 | #include "img_utils.h" |
rsjawale24 | 0:c62425b2f286 | 37 | |
rsjawale24 | 0:c62425b2f286 | 38 | int getMaxPixelValue(const int pixels[], const unsigned int num_pixels) |
rsjawale24 | 0:c62425b2f286 | 39 | { |
rsjawale24 | 0:c62425b2f286 | 40 | int maxpixel=-99999; |
rsjawale24 | 0:c62425b2f286 | 41 | for (unsigned int i = 0; i < num_pixels; i++) { |
rsjawale24 | 0:c62425b2f286 | 42 | if (maxpixel < pixels[i]) { |
rsjawale24 | 0:c62425b2f286 | 43 | maxpixel = pixels[i]; |
rsjawale24 | 0:c62425b2f286 | 44 | } |
rsjawale24 | 0:c62425b2f286 | 45 | } |
rsjawale24 | 0:c62425b2f286 | 46 | return maxpixel; |
rsjawale24 | 0:c62425b2f286 | 47 | } |
rsjawale24 | 0:c62425b2f286 | 48 | |
rsjawale24 | 0:c62425b2f286 | 49 | int getMinPixelValue(const int pixels[], const unsigned int num_pixels) |
rsjawale24 | 0:c62425b2f286 | 50 | { |
rsjawale24 | 0:c62425b2f286 | 51 | int minpixel=99999; |
rsjawale24 | 0:c62425b2f286 | 52 | for (unsigned int i = 0; i < num_pixels; i++) { |
rsjawale24 | 0:c62425b2f286 | 53 | if (minpixel > pixels[i]) { |
rsjawale24 | 0:c62425b2f286 | 54 | minpixel = pixels[i]; |
rsjawale24 | 0:c62425b2f286 | 55 | } |
rsjawale24 | 0:c62425b2f286 | 56 | } |
rsjawale24 | 0:c62425b2f286 | 57 | return minpixel; |
rsjawale24 | 0:c62425b2f286 | 58 | } |
rsjawale24 | 0:c62425b2f286 | 59 | |
rsjawale24 | 0:c62425b2f286 | 60 | // Zero out pixels below threshold value. Returns the number of pixels above the threshold |
rsjawale24 | 0:c62425b2f286 | 61 | unsigned int zeroPixelsBelowThreshold(int pixels[], const unsigned int num_pixels, const int threshold) |
rsjawale24 | 0:c62425b2f286 | 62 | { |
rsjawale24 | 0:c62425b2f286 | 63 | int pixelsAboveThresholdCount = num_pixels; |
rsjawale24 | 0:c62425b2f286 | 64 | for (unsigned int i = 0; i < num_pixels; i++) { |
rsjawale24 | 0:c62425b2f286 | 65 | if (pixels[i] < threshold) { |
rsjawale24 | 0:c62425b2f286 | 66 | pixels[i] = 0; |
rsjawale24 | 0:c62425b2f286 | 67 | pixelsAboveThresholdCount--; |
rsjawale24 | 0:c62425b2f286 | 68 | } |
rsjawale24 | 0:c62425b2f286 | 69 | } |
rsjawale24 | 0:c62425b2f286 | 70 | return pixelsAboveThresholdCount; |
rsjawale24 | 0:c62425b2f286 | 71 | } |
rsjawale24 | 0:c62425b2f286 | 72 | |
rsjawale24 | 0:c62425b2f286 | 73 | // Caller must keep static filtpixels array |
rsjawale24 | 0:c62425b2f286 | 74 | void filterLowPassPixels(int pixels[], float filtpixels[], const unsigned int num_pixels, const float alpha) |
rsjawale24 | 0:c62425b2f286 | 75 | { |
rsjawale24 | 0:c62425b2f286 | 76 | for (unsigned int i=0; i< num_pixels; i++) { |
rsjawale24 | 0:c62425b2f286 | 77 | filtpixels[i] = (1.0 - alpha) * filtpixels[i] + alpha * pixels[i]; |
rsjawale24 | 0:c62425b2f286 | 78 | } |
rsjawale24 | 0:c62425b2f286 | 79 | for (unsigned int i=0; i< num_pixels; i++) { |
rsjawale24 | 0:c62425b2f286 | 80 | pixels[i] = (int)filtpixels[i]; |
rsjawale24 | 0:c62425b2f286 | 81 | } |
rsjawale24 | 0:c62425b2f286 | 82 | } |
rsjawale24 | 0:c62425b2f286 | 83 | |
rsjawale24 | 0:c62425b2f286 | 84 | // Implement background subtraction by subtracting long exponential smoothing average from a shorter one |
rsjawale24 | 0:c62425b2f286 | 85 | // (or simply the current pixel if alpha_short_avg is set to 1.0) |
rsjawale24 | 0:c62425b2f286 | 86 | // alpha_long_avg should be smaller than alpha_short_avg |
rsjawale24 | 0:c62425b2f286 | 87 | // Caller must keep static shart_avg_pixels[] and long_avg_pixels[] |
rsjawale24 | 0:c62425b2f286 | 88 | // The bigger alpha long is, the more aggressive the high pass filter. |
rsjawale24 | 0:c62425b2f286 | 89 | void subtractBackground(int pixels[], float short_avg_pixels[], float long_avg_pixels[], const unsigned int num_pixels, const float alpha_short_avg, const float alpha_long_avg) |
rsjawale24 | 0:c62425b2f286 | 90 | { |
rsjawale24 | 0:c62425b2f286 | 91 | for (unsigned int i=0; i< num_pixels; i++) { |
rsjawale24 | 0:c62425b2f286 | 92 | long_avg_pixels[i] = (1.0f - alpha_long_avg) * long_avg_pixels[i] + alpha_long_avg * pixels[i]; |
rsjawale24 | 0:c62425b2f286 | 93 | } |
rsjawale24 | 0:c62425b2f286 | 94 | for (unsigned int i=0; i< num_pixels; i++) { |
rsjawale24 | 0:c62425b2f286 | 95 | short_avg_pixels[i] = (1.0f - alpha_short_avg) * short_avg_pixels[i] + alpha_short_avg * pixels[i]; |
rsjawale24 | 0:c62425b2f286 | 96 | } |
rsjawale24 | 0:c62425b2f286 | 97 | for (unsigned int i=0; i< num_pixels; i++) { |
rsjawale24 | 0:c62425b2f286 | 98 | pixels[i] = short_avg_pixels[i] - (int)long_avg_pixels[i]; |
rsjawale24 | 0:c62425b2f286 | 99 | } |
rsjawale24 | 0:c62425b2f286 | 100 | } |
rsjawale24 | 0:c62425b2f286 | 101 | |
rsjawale24 | 0:c62425b2f286 | 102 | void calcCenterOfMass(const int pixels[], const unsigned int xres, const unsigned int yres, float *cmx, float *cmy, int *totalmass) |
rsjawale24 | 0:c62425b2f286 | 103 | { |
rsjawale24 | 0:c62425b2f286 | 104 | int cmx_numer=0, cmy_numer=0; |
rsjawale24 | 0:c62425b2f286 | 105 | for (unsigned int i = 0; i < xres*yres; i++) { |
rsjawale24 | 0:c62425b2f286 | 106 | cmx_numer += (i%xres)*pixels[i]; |
rsjawale24 | 0:c62425b2f286 | 107 | cmy_numer += (i/xres)*pixels[i]; |
rsjawale24 | 0:c62425b2f286 | 108 | *totalmass += pixels[i]; |
rsjawale24 | 0:c62425b2f286 | 109 | } |
rsjawale24 | 0:c62425b2f286 | 110 | if (*totalmass == 0) { |
rsjawale24 | 0:c62425b2f286 | 111 | *totalmass = 1; // avoid NaN |
rsjawale24 | 0:c62425b2f286 | 112 | } |
rsjawale24 | 0:c62425b2f286 | 113 | *cmx = (float)cmx_numer/(float)(*totalmass); |
rsjawale24 | 0:c62425b2f286 | 114 | *cmy = (float)cmy_numer/(float)(*totalmass); |
rsjawale24 | 0:c62425b2f286 | 115 | } |
rsjawale24 | 0:c62425b2f286 | 116 | |
rsjawale24 | 0:c62425b2f286 | 117 | void interpn(const int pixels[], int interp_pixels[], const int w, const int h, const int interpolation_factor) |
rsjawale24 | 0:c62425b2f286 | 118 | { |
rsjawale24 | 0:c62425b2f286 | 119 | int w2 = (w - 1) * interpolation_factor + 1; |
rsjawale24 | 0:c62425b2f286 | 120 | int h2 = (h - 1) * interpolation_factor + 1; |
rsjawale24 | 0:c62425b2f286 | 121 | int A, B, C, x, y; |
rsjawale24 | 0:c62425b2f286 | 122 | float x_ratio = 1.0f / (float)interpolation_factor; |
rsjawale24 | 0:c62425b2f286 | 123 | float y_ratio = 1.0f / (float)interpolation_factor; |
rsjawale24 | 0:c62425b2f286 | 124 | |
rsjawale24 | 0:c62425b2f286 | 125 | // First stretch in x-direction, index through each pixel of destination array. Skip rows in destination array |
rsjawale24 | 0:c62425b2f286 | 126 | for (int i = 0; i < h; i++) { |
rsjawale24 | 0:c62425b2f286 | 127 | for (int j = 0; j < w2; j++) { |
rsjawale24 | 0:c62425b2f286 | 128 | x = (int)(x_ratio * j); // x index of original frame |
rsjawale24 | 0:c62425b2f286 | 129 | int index = i * w + x; // pixel index of original frame |
rsjawale24 | 0:c62425b2f286 | 130 | if (x == w - 1) // last pixel on right edge of original frame |
rsjawale24 | 0:c62425b2f286 | 131 | interp_pixels[i * w2 * interpolation_factor + j] = pixels[index]; // skip rows in dest array |
rsjawale24 | 0:c62425b2f286 | 132 | else { |
rsjawale24 | 0:c62425b2f286 | 133 | A = pixels[index]; |
rsjawale24 | 0:c62425b2f286 | 134 | B = pixels[index + 1]; |
rsjawale24 | 0:c62425b2f286 | 135 | float x_diff = (x_ratio * j) - x; // For 2x interpolation, will be 0, 1/2, 0, 1/2... |
rsjawale24 | 0:c62425b2f286 | 136 | interp_pixels[i * w2 * interpolation_factor + j] = (int)(A + (B - A) * x_diff); // skip rows in dest array |
rsjawale24 | 0:c62425b2f286 | 137 | } |
rsjawale24 | 0:c62425b2f286 | 138 | } |
rsjawale24 | 0:c62425b2f286 | 139 | } |
rsjawale24 | 0:c62425b2f286 | 140 | // Then stretch in y-direction, index through each pixel of destination array |
rsjawale24 | 0:c62425b2f286 | 141 | for (int i = 0; i < h2; i++) { |
rsjawale24 | 0:c62425b2f286 | 142 | for (int j = 0; j < w2; j++) { |
rsjawale24 | 0:c62425b2f286 | 143 | y = (int)(y_ratio * i); // y index of original frame |
rsjawale24 | 0:c62425b2f286 | 144 | int index = y * w2 * interpolation_factor + j; // pixel index of frame |
rsjawale24 | 0:c62425b2f286 | 145 | if (y == h - 1) // pixel on bottom of original frame |
rsjawale24 | 0:c62425b2f286 | 146 | interp_pixels[i * w2 + j] = interp_pixels[index]; |
rsjawale24 | 0:c62425b2f286 | 147 | else { |
rsjawale24 | 0:c62425b2f286 | 148 | A = interp_pixels[index]; |
rsjawale24 | 0:c62425b2f286 | 149 | C = interp_pixels[index + w2 * interpolation_factor]; |
rsjawale24 | 0:c62425b2f286 | 150 | float y_diff = (y_ratio * i) - y; |
rsjawale24 | 0:c62425b2f286 | 151 | interp_pixels[i * w2 + j] = (int)(A + (C - A) * y_diff); |
rsjawale24 | 0:c62425b2f286 | 152 | } |
rsjawale24 | 0:c62425b2f286 | 153 | } |
rsjawale24 | 0:c62425b2f286 | 154 | } |
rsjawale24 | 0:c62425b2f286 | 155 | } |