|
1 | | -/* Image Diff |
2 | | - * |
3 | | - * From: https://github.com/PokemonAutomation/ |
4 | | - * |
5 | | - */ |
6 | | - |
7 | | -#include <cmath> |
8 | | -#include "Common/Compiler.h" |
9 | | -#include "Common/Cpp/Exceptions.h" |
10 | | -#include "Kernels/ImageStats/Kernels_ImagePixelSumSqr.h" |
11 | | -#include "Kernels/ImageStats/Kernels_ImagePixelSumSqrDev.h" |
12 | | -#include "Kernels/ImageScaleBrightness/Kernels_ImageScaleBrightness.h" |
13 | | -#include "CommonFramework/ImageTypes/ImageViewRGB32.h" |
14 | | -#include "CommonFramework/ImageTypes/ImageRGB32.h" |
15 | | -#include "ImageDiff.h" |
16 | | - |
17 | | -#include <iostream> |
18 | | -using std::cout; |
19 | | -using std::endl; |
20 | | - |
21 | | -namespace PokemonAutomation{ |
22 | | -namespace ImageMatch{ |
23 | | - |
24 | | - |
25 | | -FloatPixel pixel_average(const ImageViewRGB32& image, const ImageViewRGB32& alpha_mask){ |
26 | | - if (!image){ |
27 | | - throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "Invalid Dimensions"); |
28 | | - } |
29 | | - if (image.width() != alpha_mask.width() || image.height() != alpha_mask.height()){ |
30 | | - throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "Mismatching Dimensions"); |
31 | | - } |
32 | | - Kernels::PixelSums sums; |
33 | | - Kernels::pixel_sum_sqr( |
34 | | - sums, image.width(), image.height(), |
35 | | - image.data(), image.bytes_per_row(), |
36 | | - alpha_mask.data(), alpha_mask.bytes_per_row() |
37 | | - ); |
38 | | - |
39 | | - FloatPixel sum((double)sums.sumR, (double)sums.sumG, (double)sums.sumB); |
40 | | - sum /= (double)sums.count; |
41 | | - return sum; |
42 | | -} |
43 | | - |
44 | | - |
45 | | - |
46 | | -void scale_brightness(ImageRGB32& image, const FloatPixel& multiplier){ |
47 | | - Kernels::scale_brightness( |
48 | | - image.width(), image.height(), |
49 | | - image.data(), image.bytes_per_row(), |
50 | | - (float)multiplier.r, (float)multiplier.g, (float)multiplier.b |
51 | | - ); |
52 | | -} |
53 | | - |
54 | | - |
55 | | - |
56 | | -double pixel_RMSD(const ImageViewRGB32& reference, const ImageViewRGB32& image){ |
57 | | - if (!image){ |
58 | | -// throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "Invalid Dimensions"); |
59 | | - return 765; // Max possible deviation. |
60 | | - } |
61 | | - if (reference.width() != image.width() || reference.height() != image.height()){ |
62 | | - throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "Mismatching Dimensions"); |
63 | | - } |
64 | | - uint64_t count = 0; |
65 | | - uint64_t sumsqrs = 0; |
66 | | - Kernels::sum_sqr_deviation( |
67 | | - count, sumsqrs, |
68 | | - reference.width(), reference.height(), |
69 | | - reference.data(), reference.bytes_per_row(), |
70 | | - image.data(), image.bytes_per_row() |
71 | | - ); |
72 | | - return std::sqrt((double)sumsqrs / (double)count); |
73 | | -} |
74 | | -double pixel_RMSD(const ImageViewRGB32& reference, const ImageViewRGB32& image, Color background){ |
75 | | - if (!image){ |
76 | | -// throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "Invalid Dimensions"); |
77 | | - return 765; // Max possible deviation. |
78 | | - } |
79 | | - if (reference.width() != image.width() || reference.height() != image.height()){ |
80 | | - throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "Mismatching Dimensions"); |
81 | | - } |
82 | | - uint64_t count = 0; |
83 | | - uint64_t sumsqrs = 0; |
84 | | - Kernels::sum_sqr_deviation( |
85 | | - count, sumsqrs, |
86 | | - reference.width(), reference.height(), |
87 | | - reference.data(), reference.bytes_per_row(), |
88 | | - image.data(), image.bytes_per_row(), |
89 | | - (uint32_t)background |
90 | | - ); |
91 | | - return std::sqrt((double)sumsqrs / (double)count); |
92 | | -} |
93 | | -double pixel_RMSD_masked(const ImageViewRGB32& reference, const ImageViewRGB32& image){ |
94 | | - if (!image){ |
95 | | -// throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "Invalid Dimensions"); |
96 | | - return 765; // Max possible deviation. |
97 | | - } |
98 | | - if (reference.width() != image.width() || reference.height() != image.height()){ |
99 | | - throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "Mismatching Dimensions"); |
100 | | - } |
101 | | - uint64_t count = 0; |
102 | | - uint64_t sumsqrs = 0; |
103 | | - Kernels::sum_sqr_deviation_masked( |
104 | | - count, sumsqrs, |
105 | | - reference.width(), reference.height(), |
106 | | - reference.data(), reference.bytes_per_row(), |
107 | | - image.data(), image.bytes_per_row() |
108 | | - ); |
109 | | - return std::sqrt((double)sumsqrs / (double)count); |
110 | | -} |
111 | | - |
112 | | - |
113 | | - |
114 | | -} |
115 | | -} |
| 1 | +/* Image Diff |
| 2 | + * |
| 3 | + * From: https://github.com/PokemonAutomation/ |
| 4 | + * |
| 5 | + */ |
| 6 | + |
| 7 | +#include <cmath> |
| 8 | +#include "Common/Compiler.h" |
| 9 | +#include "Common/Cpp/Exceptions.h" |
| 10 | +#include "Kernels/ImageStats/Kernels_ImagePixelSumSqr.h" |
| 11 | +#include "Kernels/ImageStats/Kernels_ImagePixelSumSqrDev.h" |
| 12 | +#include "Kernels/ImageScaleBrightness/Kernels_ImageScaleBrightness.h" |
| 13 | +#include "CommonFramework/ImageTypes/ImageViewRGB32.h" |
| 14 | +#include "CommonFramework/ImageTypes/ImageRGB32.h" |
| 15 | +#include "ImageDiff.h" |
| 16 | + |
| 17 | +#include <iostream> |
| 18 | +using std::cout; |
| 19 | +using std::endl; |
| 20 | + |
| 21 | +namespace PokemonAutomation{ |
| 22 | +namespace ImageMatch{ |
| 23 | + |
| 24 | + |
| 25 | +FloatPixel pixel_average(const ImageViewRGB32& image, const ImageViewRGB32& alpha_mask){ |
| 26 | + if (!image){ |
| 27 | + throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "Invalid Dimensions"); |
| 28 | + } |
| 29 | + if (image.width() != alpha_mask.width() || image.height() != alpha_mask.height()){ |
| 30 | + throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "Mismatching Dimensions"); |
| 31 | + } |
| 32 | + Kernels::PixelSums sums; |
| 33 | + Kernels::pixel_sum_sqr( |
| 34 | + sums, image.width(), image.height(), |
| 35 | + image.data(), image.bytes_per_row(), |
| 36 | + alpha_mask.data(), alpha_mask.bytes_per_row() |
| 37 | + ); |
| 38 | + |
| 39 | + FloatPixel sum((double)sums.sumR, (double)sums.sumG, (double)sums.sumB); |
| 40 | + sum /= (double)sums.count; |
| 41 | + return sum; |
| 42 | +} |
| 43 | + |
| 44 | + |
| 45 | + |
| 46 | +void scale_brightness(ImageRGB32& image, const FloatPixel& multiplier){ |
| 47 | + Kernels::scale_brightness( |
| 48 | + image.width(), image.height(), |
| 49 | + image.data(), image.bytes_per_row(), |
| 50 | + (float)multiplier.r, (float)multiplier.g, (float)multiplier.b |
| 51 | + ); |
| 52 | +} |
| 53 | + |
| 54 | + |
| 55 | + |
| 56 | +double pixel_RMSD(const ImageViewRGB32& reference, const ImageViewRGB32& image){ |
| 57 | + if (!image){ |
| 58 | +// throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "Invalid Dimensions"); |
| 59 | + return 765; // Max possible deviation. |
| 60 | + } |
| 61 | + if (reference.width() != image.width() || reference.height() != image.height()){ |
| 62 | + throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "Mismatching Dimensions"); |
| 63 | + } |
| 64 | + uint64_t count = 0; |
| 65 | + uint64_t sumsqrs = 0; |
| 66 | + Kernels::sum_sqr_deviation( |
| 67 | + count, sumsqrs, |
| 68 | + reference.width(), reference.height(), |
| 69 | + reference.data(), reference.bytes_per_row(), |
| 70 | + image.data(), image.bytes_per_row() |
| 71 | + ); |
| 72 | + return std::sqrt((double)sumsqrs / (double)count); |
| 73 | +} |
| 74 | +double pixel_RMSD(const ImageViewRGB32& reference, const ImageViewRGB32& image, Color background){ |
| 75 | + if (!image){ |
| 76 | +// throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "Invalid Dimensions"); |
| 77 | + return 765; // Max possible deviation. |
| 78 | + } |
| 79 | + if (reference.width() != image.width() || reference.height() != image.height()){ |
| 80 | + throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "Mismatching Dimensions"); |
| 81 | + } |
| 82 | + uint64_t count = 0; |
| 83 | + uint64_t sumsqrs = 0; |
| 84 | + Kernels::sum_sqr_deviation( |
| 85 | + count, sumsqrs, |
| 86 | + reference.width(), reference.height(), |
| 87 | + reference.data(), reference.bytes_per_row(), |
| 88 | + image.data(), image.bytes_per_row(), |
| 89 | + (uint32_t)background |
| 90 | + ); |
| 91 | + return std::sqrt((double)sumsqrs / (double)count); |
| 92 | +} |
| 93 | +double pixel_RMSD_masked(const ImageViewRGB32& reference, const ImageViewRGB32& image){ |
| 94 | + if (!image){ |
| 95 | +// throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "Invalid Dimensions"); |
| 96 | + return 765; // Max possible deviation. |
| 97 | + } |
| 98 | + if (reference.width() != image.width() || reference.height() != image.height()){ |
| 99 | + throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "Mismatching Dimensions"); |
| 100 | + } |
| 101 | + uint64_t count = 0; |
| 102 | + uint64_t sumsqrs = 0; |
| 103 | + Kernels::sum_sqr_deviation_masked( |
| 104 | + count, sumsqrs, |
| 105 | + reference.width(), reference.height(), |
| 106 | + reference.data(), reference.bytes_per_row(), |
| 107 | + image.data(), image.bytes_per_row() |
| 108 | + ); |
| 109 | + return std::sqrt((double)sumsqrs / (double)count); |
| 110 | +} |
| 111 | + |
| 112 | + |
| 113 | + |
| 114 | +} |
| 115 | +} |
0 commit comments