diff --git a/Common/Cpp/Color.cpp b/Common/Cpp/Color.cpp new file mode 100644 index 000000000..59a30b063 --- /dev/null +++ b/Common/Cpp/Color.cpp @@ -0,0 +1,27 @@ +/* Color + * + * From: https://github.com/PokemonAutomation/Arduino-Source + * + * A very lightweight color class to avoid pulling in . + * + */ + +#include "Color.h" +#include +#include + +namespace PokemonAutomation{ + +std::string Color::to_string() const{ + std::ostringstream os; + os << "[0x" << std::internal << std::setfill('0'); + os << std::hex << std::uppercase << std::setw(8) << m_argb << std::dec; + os << " A=" << std::setw(2) << int(alpha()); + os << " R=" << std::setw(2) << int(red()); + os << " G=" << std::setw(2) << int(green()); + os << " B=" << std::setw(2) << int(blue()) << "]"; + return os.str(); +} + +} + diff --git a/Common/Cpp/Color.h b/Common/Cpp/Color.h index 307de7000..0fbcae539 100644 --- a/Common/Cpp/Color.h +++ b/Common/Cpp/Color.h @@ -10,6 +10,7 @@ #define PokemonAutomation_Color_H #include +#include namespace PokemonAutomation{ @@ -45,6 +46,9 @@ class Color{ uint8_t green () const { return (uint8_t)(m_argb >> 8); } uint8_t blue () const { return (uint8_t)(m_argb >> 0); } + // Example: "[0xFFFDBD00 A=255 R=253 G=189 B=00]" + std::string to_string() const; + private: uint32_t m_argb; }; diff --git a/SerialPrograms/CMakeLists.txt b/SerialPrograms/CMakeLists.txt index 9c8615bc8..493e8ddf3 100644 --- a/SerialPrograms/CMakeLists.txt +++ b/SerialPrograms/CMakeLists.txt @@ -64,6 +64,7 @@ file(GLOB MAIN_SOURCES ../Common/Cpp/AbstractLogger.h ../Common/Cpp/CancellableScope.cpp ../Common/Cpp/CancellableScope.h + ../Common/Cpp/Color.cpp ../Common/Cpp/Color.h ../Common/Cpp/Concurrency/AsyncDispatcher.cpp ../Common/Cpp/Concurrency/AsyncDispatcher.h diff --git a/SerialPrograms/Scripts/add_new_file.py b/SerialPrograms/Scripts/add_new_file.py index 8ebde0c05..2e48a5bf6 100755 --- a/SerialPrograms/Scripts/add_new_file.py +++ b/SerialPrograms/Scripts/add_new_file.py @@ -13,6 +13,7 @@ Example usage: python3 add_new_file.py Source/PokemonSV/Inference/Map/PokemonSV_MapFlyMenuDetectorDetector.h +python3 add_new_file.py ../Common/Cpp/Color.cpp """ import sys diff --git a/SerialPrograms/SerialPrograms.pro b/SerialPrograms/SerialPrograms.pro index 8ce1dd36f..4c5dad49c 100644 --- a/SerialPrograms/SerialPrograms.pro +++ b/SerialPrograms/SerialPrograms.pro @@ -81,6 +81,7 @@ SOURCES += \ ../ClientSource/Libraries/MessageConverter.cpp \ ../Common/CRC32.cpp \ ../Common/Cpp/CancellableScope.cpp \ + ../Common/Cpp/Color.cpp \ ../Common/Cpp/Concurrency/AsyncDispatcher.cpp \ ../Common/Cpp/Concurrency/FireForgetDispatcher.cpp \ ../Common/Cpp/Concurrency/ParallelTaskRunner.cpp \ diff --git a/SerialPrograms/Source/CommonFramework/GlobalSettingsPanel.cpp b/SerialPrograms/Source/CommonFramework/GlobalSettingsPanel.cpp index 2fb555aed..bc07c1fd8 100644 --- a/SerialPrograms/Source/CommonFramework/GlobalSettingsPanel.cpp +++ b/SerialPrograms/Source/CommonFramework/GlobalSettingsPanel.cpp @@ -83,6 +83,7 @@ void PreloadSettings::load(const JsonValue& json){ if (debug_obj){ debug_obj->read_boolean(DEBUG.COLOR_CHECK, "COLOR_CHECK"); debug_obj->read_boolean(DEBUG.IMAGE_TEMPLATE_MATCHING, "IMAGE_TEMPLATE_MATCHING"); + debug_obj->read_boolean(DEBUG.IMAGE_DICTIONARY_MATCHING, "IMAGE_DICTIONARY_MATCHING"); } } @@ -366,6 +367,7 @@ JsonValue GlobalSettings::to_json() const{ const auto& debug_settings = PreloadSettings::instance().DEBUG; debug_obj["COLOR_CHECK"] = debug_settings.COLOR_CHECK; debug_obj["IMAGE_TEMPLATE_MATCHING"] = debug_settings.IMAGE_TEMPLATE_MATCHING; + debug_obj["IMAGE_DICTIONARY_MATCHING"] = debug_settings.IMAGE_DICTIONARY_MATCHING; obj["DEBUG"] = std::move(debug_obj); return obj; diff --git a/SerialPrograms/Source/CommonFramework/GlobalSettingsPanel.h b/SerialPrograms/Source/CommonFramework/GlobalSettingsPanel.h index 641cd5cd8..0beeec6e0 100644 --- a/SerialPrograms/Source/CommonFramework/GlobalSettingsPanel.h +++ b/SerialPrograms/Source/CommonFramework/GlobalSettingsPanel.h @@ -47,6 +47,7 @@ class ResolutionOption : public GroupOption{ struct DebugSettings{ bool COLOR_CHECK = false; bool IMAGE_TEMPLATE_MATCHING = false; + bool IMAGE_DICTIONARY_MATCHING = false; }; diff --git a/SerialPrograms/Source/CommonFramework/ImageMatch/CroppedImageDictionaryMatcher.cpp b/SerialPrograms/Source/CommonFramework/ImageMatch/CroppedImageDictionaryMatcher.cpp index c42db4241..d13ce78b4 100644 --- a/SerialPrograms/Source/CommonFramework/ImageMatch/CroppedImageDictionaryMatcher.cpp +++ b/SerialPrograms/Source/CommonFramework/ImageMatch/CroppedImageDictionaryMatcher.cpp @@ -7,11 +7,13 @@ #include #include "Common/Cpp/Exceptions.h" #include "CommonFramework/ImageTypes/ImageViewRGB32.h" +#include "CommonFramework/GlobalSettingsPanel.h" +#include "CommonFramework/Tools/DebugDumper.h" #include "ImageCropper.h" //#include "ImageDiff.h" #include "CroppedImageDictionaryMatcher.h" -//#include +#include //using std::cout; //using std::endl; @@ -60,9 +62,17 @@ ImageMatchResult CroppedImageDictionaryMatcher::match( return results; } + if (PreloadSettings::debug().IMAGE_DICTIONARY_MATCHING){ + std::cout << "CroppedImageDictionaryMatcher: match input image: " << std::endl; + dump_debug_image(global_logger_command_line(), "CommonFramework/CroppedImageDictionaryMatcher", "match_input", image); + } + Color background; ImageRGB32 processed = process_image(image, background); -// cout << (uint32_t)background << endl; + if (PreloadSettings::debug().IMAGE_DICTIONARY_MATCHING){ + std::cout << "CroppedImageDictionaryMatcher: process input image with background " << background.to_string() << std::endl; + dump_debug_image(global_logger_command_line(), "CommonFramework/CroppedImageDictionaryMatcher", "match_input_processed", image); + } for (const auto& item : m_database){ #if 0 @@ -75,6 +85,16 @@ ImageMatchResult CroppedImageDictionaryMatcher::match( results.clear_beyond_spread(alpha_spread); } + if (PreloadSettings::debug().IMAGE_DICTIONARY_MATCHING){ + std::cout << "CroppedImageDictionaryMatcher: results: " << std::endl; + size_t count = 0; + for(const auto& result : results.results){ + std::cout << "alpha=" << result.first << ", " << result.second << std::endl; + const auto& image_template = m_database.find(result.second)->second.image_template(); + dump_debug_image(global_logger_command_line(), "CommonFramework/CroppedImageDictionaryMatcher", "match_result_" + std::to_string(count) + "_" + result.second, image_template); + ++count; + } + } return results; } diff --git a/SerialPrograms/Source/CommonFramework/Tools/DebugDumper.cpp b/SerialPrograms/Source/CommonFramework/Tools/DebugDumper.cpp index 0367017e8..c7ede4be4 100644 --- a/SerialPrograms/Source/CommonFramework/Tools/DebugDumper.cpp +++ b/SerialPrograms/Source/CommonFramework/Tools/DebugDumper.cpp @@ -31,7 +31,7 @@ std::string dump_debug_image( ){ create_debug_folder(path); std::string full_path = debug_dump_folder_name; - full_path += "/" + path + "/" + now_to_filestring() + "-" + label + ".jpg"; + full_path += "/" + path + "/" + now_to_filestring() + "-" + label + ".png"; logger.log("Saving debug image to: " + full_path, COLOR_YELLOW); image.save(full_path); return full_path; diff --git a/SerialPrograms/Source/CommonFramework/Tools/DebugDumper.h b/SerialPrograms/Source/CommonFramework/Tools/DebugDumper.h index eb1b411d4..804c870eb 100644 --- a/SerialPrograms/Source/CommonFramework/Tools/DebugDumper.h +++ b/SerialPrograms/Source/CommonFramework/Tools/DebugDumper.h @@ -14,7 +14,7 @@ namespace PokemonAutomation{ class ImageViewRGB32; class Logger; -// Dump debug image to ./DebugDumps/`path`/-`label`.jpg +// Dump debug image to ./DebugDumps/`path`/-`label`.png // Return image path. std::string dump_debug_image( Logger& logger,