Skip to content

Commit 9a6c636

Browse files
author
Gin
committed
clean up code
1 parent 6e7a1c4 commit 9a6c636

File tree

6 files changed

+14
-136
lines changed

6 files changed

+14
-136
lines changed

SerialPrograms/Source/CommonTools/ImageMatch/WaterfillTemplateMatcher.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ class WaterfillTemplateMatcher{
5252
// before computing RMSD.
5353
// The part of the image template where alpha is 0 is not used to compare with the corresponding part in the input image.
5454
// In case the image is invalid, return a large value.
55-
// It also calls the virtual function `check_image()` on the image.
56-
// If the function returns false, then return a large value.
55+
// It also calls the virtual function `check_image(input_resolution, image)` on the image, where `input_resolution` is
56+
// the full screen resolution where the image is from. If the function returns false, then return a large value.
5757
double rmsd(Resolution input_resolution, const ImageViewRGB32& image) const;
5858

5959
// Compute RMSD of the object on the image against the template.
@@ -86,6 +86,12 @@ class WaterfillTemplateMatcher{
8686
const ImageRGB32& image_template() const { return m_matcher->image_template(); }
8787

8888
protected:
89+
// This function is called inside each rmsd...() function before the actual RMSD computation.
90+
// Derived classes can override this function to offer additional checks before computing RMSD.
91+
// rmsd...() functions will proceed to RMSD computation if this function returns true. If false
92+
// returns a large value.
93+
// input_resolution: resolution of the full screen where the input image is from. This is useful for checks on
94+
// input image pixel count or minimum size.
8995
virtual bool check_image(Resolution input_resolution, const ImageViewRGB32& image) const{ return true; };
9096
bool check_aspect_ratio(size_t candidate_width, size_t candidate_height) const;
9197
bool check_area_ratio(double candidate_area_ratio) const;

SerialPrograms/Source/CommonTools/Images/WaterfillUtilities.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ std::pair<PackedBinaryMatrix, size_t> remove_center_pixels(
4646
// checking aspect ratio thresholds, area thresholds and RMSD threshold.
4747
// If a template match is found, call the function `check_matched_object()` on it.
4848
//
49+
// input_resolution: full screen resolution. If WaterfillTemplateMatcher implements a `check_image()` member function
4950
// image: the image.
5051
// matcher: the template matcher holding the template. It is also responsible for checking aspect ratio thresholds.
5152
// filters: each filter is parameterized by min and max color thresholds for detected pixels. For each filter, the function checks

SerialPrograms/Source/PokemonLZA/Inference/PokemonLZA_DialogDetector.cpp

Lines changed: 5 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -133,12 +133,12 @@ const static ImageFloatBox DIALOG_ARROW_BOX{0.727, 0.868, 0.037, 0.086};
133133

134134
// detect the white arrow in blue dialog box and transparent battle dialog box
135135
bool detect_white_arrow(const ImageViewRGB32& screen, PokemonAutomation::ImageFloatBox& found_box){
136-
double screen_rel_size = (screen.height() / 1080.0);
137-
double screen_rel_size_2 = screen_rel_size * screen_rel_size;
136+
const double screen_rel_size = (screen.height() / 1080.0);
137+
const double screen_rel_size_2 = screen_rel_size * screen_rel_size;
138138

139-
double min_area_1080p = 150.0;
140-
double rmsd_threshold = 120.0;
141-
size_t min_area = size_t(screen_rel_size_2 * min_area_1080p);
139+
const double min_area_1080p = 150.0;
140+
const double rmsd_threshold = 120.0;
141+
const size_t min_area = size_t(screen_rel_size_2 * min_area_1080p);
142142

143143
const std::vector<std::pair<uint32_t, uint32_t>> FILTERS = {
144144
{0xffc0c0c0, 0xffffffff},
@@ -194,107 +194,6 @@ bool detect_blue_arrow(const ImageViewRGB32& screen, PokemonAutomation::ImageFlo
194194

195195

196196

197-
198-
NormalDialogDetector::NormalDialogDetector(Logger& logger, VideoOverlay& overlay, bool stop_on_detected)
199-
: VisualInferenceCallback("NormalDialogDetector")
200-
, m_stop_on_detected(stop_on_detected)
201-
, m_detected(false)
202-
, m_title_green_line_box(0.224, 0.727, 0.016, 0.056)
203-
{}
204-
void NormalDialogDetector::make_overlays(VideoOverlaySet& items) const{
205-
items.add(COLOR_RED, m_title_green_line_box);
206-
items.add(COLOR_RED, DIALOG_ARROW_BOX);
207-
}
208-
bool NormalDialogDetector::process_frame(const ImageViewRGB32& frame, WallClock timestamp){
209-
const double screen_rel_size = (frame.height() / 1080.0);
210-
const double screen_rel_size_2 = screen_rel_size * screen_rel_size;
211-
212-
bool found_green_title_line = false;
213-
bool found_black_arrow = false;
214-
215-
// Example green pixels from screenshots:
216-
// 194,230,70
217-
// 212,235,127
218-
// 176,212,62
219-
const std::vector<std::pair<uint32_t, uint32_t>> green_line_filters = {
220-
{combine_rgb(160,200,55), combine_rgb(220, 240, 130)}
221-
};
222-
223-
const double min_green_line_size_1080P = 100.0;
224-
const double green_line_rmsd_threshold = 50.0;
225-
const size_t min_green_line_size = size_t(screen_rel_size_2 * min_green_line_size_1080P);
226-
match_template_by_waterfill(
227-
frame.size(),
228-
extract_box_reference(frame, m_title_green_line_box),
229-
DialogTitleGreenLineMatcher::instance(),
230-
green_line_filters,
231-
{min_green_line_size, SIZE_MAX},
232-
green_line_rmsd_threshold,
233-
[&](Kernels::Waterfill::WaterfillObject& object) -> bool {
234-
found_green_title_line = true;
235-
return true;
236-
}
237-
);
238-
239-
// Example green pixels from screenshots:
240-
// [37,34,6] [20,15,55]
241-
// [39,42,58] [60,56,74] [50,49,63]
242-
const std::vector<std::pair<uint32_t, uint32_t>> black_arrow_filters = {
243-
{combine_rgb(0,0,0), combine_rgb(100, 100, 100)}
244-
};
245-
246-
const double min_black_arrow_size_1080P = 150.0;
247-
const double black_arrow_rmsd_threshold = 120.0;
248-
const size_t min_black_arrow_size = size_t(screen_rel_size_2 * min_black_arrow_size_1080P);
249-
match_template_by_waterfill(
250-
frame.size(),
251-
extract_box_reference(frame, DIALOG_ARROW_BOX),
252-
DialogBlackArrowMatcher::instance(),
253-
black_arrow_filters,
254-
{min_black_arrow_size, SIZE_MAX},
255-
black_arrow_rmsd_threshold,
256-
[&](Kernels::Waterfill::WaterfillObject& object) -> bool {
257-
found_black_arrow = true;
258-
return true;
259-
}
260-
);
261-
262-
bool is_dialog_box = found_green_title_line & found_black_arrow;
263-
264-
if (is_dialog_box){
265-
m_detected.store(is_dialog_box);
266-
}
267-
268-
return is_dialog_box & m_stop_on_detected;
269-
270-
271-
// size_t hits = 0;
272-
273-
// const ImageStats title_top = image_stats(extract_box_reference(frame, m_title_top));
274-
// const ImageStats title_bottom = image_stats(extract_box_reference(frame, m_title_bottom));
275-
// const ImageStats title_left = image_stats(extract_box_reference(frame, m_title_left));
276-
// const ImageStats title_right = image_stats(extract_box_reference(frame, m_title_right));
277-
278-
// ImageStats top_white = image_stats(extract_box_reference(frame, m_top_white));
279-
// hits += is_white(top_white, 480, 30) ? 1 : 0;
280-
281-
// ImageStats bottom_white = image_stats(extract_box_reference(frame, m_bottom_white));
282-
// hits += is_white(bottom_white, 480, 30) ? 1 : 0;
283-
284-
// ImageStats left_white = image_stats(extract_box_reference(frame, m_left_white));
285-
// hits += is_white(left_white, 480, 30) ? 1 : 0;
286-
287-
// ImageStats right_white = image_stats(extract_box_reference(frame, m_right_white));
288-
// hits += is_white(right_white, 480, 30) ? 1 : 0;
289-
290-
// bool detected = hits == 5;
291-
// m_detected.store(detected, std::memory_order_release);
292-
293-
// return detected && m_stop_on_detected;
294-
}
295-
296-
297-
298197
FlatWhiteDialogDetector::FlatWhiteDialogDetector(Color color, VideoOverlay* overlay)
299198
: m_color(color)
300199
, m_overlay(overlay)

SerialPrograms/Source/PokemonLZA/Inference/PokemonLZA_DialogDetector.h

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,31 +20,6 @@ namespace PokemonAutomation{
2020
namespace NintendoSwitch{
2121
namespace PokemonLZA{
2222

23-
// Detect normal dialogue that is used in cases like when you talk to npcs in most situations.
24-
//
25-
// Don't use this as there are lot of different dialogs. Use one of the newer
26-
// ones below. This was done using official screenshots before launch before
27-
// we knew there were so many different dialogs.
28-
//
29-
class NormalDialogDetector : public VisualInferenceCallback{
30-
public:
31-
NormalDialogDetector(Logger& logger, VideoOverlay& overlay, bool stop_on_detected);
32-
33-
bool detected() const{
34-
return m_detected.load(std::memory_order_acquire);
35-
}
36-
37-
virtual void make_overlays(VideoOverlaySet& items) const override;
38-
virtual bool process_frame(const ImageViewRGB32& frame, WallClock timestamp) override;
39-
40-
private:
41-
bool m_stop_on_detected;
42-
std::atomic<bool> m_detected;
43-
ImageFloatBox m_title_green_line_box;
44-
};
45-
46-
47-
4823

4924
// Common white dialog box
5025
class FlatWhiteDialogDetector : public StaticScreenDetector{

SerialPrograms/Source/Tests/PokemonLZA_Tests.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818

1919
class ImageViewRGB32;
2020

21-
22-
int test_pokemonZLA_NormalDialogBoxDetector(const ImageViewRGB32& image, bool target);
2321

2422
int test_pokemonLZA_FlatWhiteDialogDetector(const ImageViewRGB32& image, bool target);
2523

SerialPrograms/Source/Tests/TestMap.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,6 @@ const std::map<std::string, TestFunction> TEST_MAP = {
288288
{"PokemonSV_MapFlyMenuDetector", std::bind(image_bool_detector_helper, test_pokemonSV_MapFlyMenuDetector, _1)},
289289
{"PokemonSV_SandwichPlateDetector", std::bind(image_words_detector_helper, test_pokemonSV_SandwichPlateDetector, _1)},
290290
{"PokemonSV_RecentlyBattledDetector", std::bind(image_bool_detector_helper, test_pokemonSV_RecentlyBattledDetector, _1)},
291-
{"PokemonLZA_NormalDialogBoxDetector", std::bind(image_bool_detector_helper, test_pokemonZLA_NormalDialogBoxDetector, _1)},
292291
{"PokemonLZA_FlatWhiteDialogDetector", std::bind(image_bool_detector_helper, test_pokemonLZA_FlatWhiteDialogDetector, _1)},
293292
{"PokemonLZA_BlueDialogDetector", std::bind(image_bool_detector_helper, test_pokemonLZA_BlueDialogDetector, _1)},
294293
{"PokemonLZA_TransparentBattleDialogDetector", std::bind(image_bool_detector_helper, test_pokemonLZA_TransparentBattleDialogDetector, _1)},

0 commit comments

Comments
 (0)