|
5 | 5 | */ |
6 | 6 |
|
7 | 7 | #include <opencv2/imgproc.hpp> |
| 8 | +#include "CommonFramework/Logging/Logger.h" |
8 | 9 | #include "CommonTools/Images/ImageFilter.h" |
9 | 10 | #include "CommonTools/ImageMatch/ImageCropper.h" |
10 | 11 | #include "PokemonSV/Resources/PokemonSV_PokemonSprites.h" |
11 | 12 | #include "PokemonSV_TeraSilhouetteReader.h" |
12 | 13 |
|
| 14 | +//#include <iostream> |
| 15 | +//using std::cout; |
| 16 | +//using std::endl; |
| 17 | + |
13 | 18 | namespace PokemonAutomation{ |
14 | 19 | namespace NintendoSwitch{ |
15 | 20 | namespace PokemonSV{ |
@@ -46,32 +51,62 @@ ImageMatch::ImageMatchResult TeraSilhouetteReader::read(const ImageViewRGB32& sc |
46 | 51 | static constexpr double MAX_ALPHA = 110; |
47 | 52 | static constexpr double ALPHA_SPREAD = 20; |
48 | 53 |
|
49 | | - // Get a loose crop of the silhouette icon |
50 | | - ImageViewRGB32 cropped_image = extract_box_reference(screen, m_box); |
51 | | - //cropped_image.save("cropped_image.png"); |
| 54 | + const std::vector<uint32_t> BRIGHTNESS_THRESHOLDS{ |
| 55 | + 200, |
| 56 | + 150, |
| 57 | + 100, |
| 58 | + 125, |
| 59 | + 175, |
| 60 | + 225, |
| 61 | + }; |
| 62 | + |
| 63 | + for (uint32_t threshold : BRIGHTNESS_THRESHOLDS){ |
| 64 | +// cout << "check0" << endl; |
| 65 | + // Get a loose crop of the silhouette icon |
| 66 | + ImageViewRGB32 cropped_image = extract_box_reference(screen, m_box); |
| 67 | +// cropped_image.save("tera_cropped_image.png"); |
| 68 | + |
| 69 | +// cout << "check1" << endl; |
| 70 | + ImageRGB32 preprocessed_image(cropped_image.width(), cropped_image.height()); |
| 71 | + cv::medianBlur(cropped_image.to_opencv_Mat(), preprocessed_image.to_opencv_Mat(), 5); |
| 72 | +// preprocessed_image.save("tera_blurred_image.png"); |
| 73 | + |
| 74 | + // Get a tight crop |
| 75 | +// cout << "check2" << endl; |
| 76 | + const ImagePixelBox tight_box = ImageMatch::enclosing_rectangle_with_pixel_filter( |
| 77 | + preprocessed_image, |
| 78 | + // The filter is a lambda function that returns true on black silhouette pixels. |
| 79 | + [=](Color pixel){ |
| 80 | +// return (uint32_t)pixel.red() + pixel.green() + pixel.blue() < 200; |
| 81 | + return (uint32_t)pixel.red() + pixel.green() + pixel.blue() < threshold; |
| 82 | + } |
| 83 | + ); |
| 84 | + |
| 85 | + if (tight_box.area() == 0){ |
| 86 | + global_logger_tagged().log("TeraSilhouetteReader::read(): Cropped image is empty.", COLOR_RED); |
| 87 | + return ImageMatch::ImageMatchResult(); |
| 88 | + } |
52 | 89 |
|
53 | | - ImageRGB32 preprocessed_image(cropped_image.width(), cropped_image.height()); |
54 | | - cv::medianBlur(cropped_image.to_opencv_Mat(), preprocessed_image.to_opencv_Mat(), 5); |
55 | | - //preprocessed_image.save("preprocessed_image.png"); |
| 90 | +// cout << "check3" << endl; |
| 91 | + ImageRGB32 processed_image = extract_box_reference(preprocessed_image, tight_box).copy(); |
| 92 | +// processed_image.save("tera_processed_image.png"); |
56 | 93 |
|
57 | | - // Get a tight crop |
58 | | - const ImagePixelBox tight_box = ImageMatch::enclosing_rectangle_with_pixel_filter( |
59 | | - preprocessed_image, |
60 | | - // The filter is a lambda function that returns true on black silhouette pixels. |
61 | | - [](Color pixel){ |
62 | | - return (uint32_t)pixel.red() + pixel.green() + pixel.blue() < 200; |
63 | | - } |
64 | | - ); |
65 | | - ImageRGB32 processed_image = extract_box_reference(preprocessed_image, tight_box).copy(); |
66 | | - // processed_image.save("processed_image.png"); |
| 94 | +// cout << "check4" << endl; |
| 95 | + ImageRGB32 filtered_image = to_blackwhite_rgb32_range(processed_image, 0xff000000, 0xff5f5f5f, true); |
| 96 | +// filtered_image.save("tera_filtered_image.png"); |
67 | 97 |
|
68 | | - ImageRGB32 filtered_image = to_blackwhite_rgb32_range(processed_image, 0xff000000, 0xff5f5f5f, true); |
69 | | - // filtered_image.save("filtered_image.png"); |
| 98 | +// cout << "check5" << endl; |
| 99 | + ImageMatch::ImageMatchResult slugs = TERA_RAID_SILHOUETTE_MATCHER().match(filtered_image, ALPHA_SPREAD); |
| 100 | + slugs.clear_beyond_alpha(MAX_ALPHA); |
70 | 101 |
|
71 | | - ImageMatch::ImageMatchResult slugs = TERA_RAID_SILHOUETTE_MATCHER().match(filtered_image, ALPHA_SPREAD); |
72 | | - slugs.clear_beyond_alpha(MAX_ALPHA); |
| 102 | +// slugs.log(global_logger_tagged(), MAX_ALPHA); |
| 103 | + |
| 104 | + if (slugs.results.size() == 1){ |
| 105 | + return slugs; |
| 106 | + } |
| 107 | + } |
73 | 108 |
|
74 | | - return slugs; |
| 109 | + return ImageMatch::ImageMatchResult(); |
75 | 110 | } |
76 | 111 |
|
77 | 112 |
|
|
0 commit comments