Skip to content

Commit 7fc162e

Browse files
committed
Add function to find cropping of a game that doesn't use the full screen.
1 parent ac3195e commit 7fc162e

4 files changed

Lines changed: 67 additions & 1 deletion

File tree

SerialPrograms/Source/CommonTools/Images/ImageTools.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "CommonFramework/ImageTypes/ImageRGB32.h"
99
#include "CommonFramework/ImageTools/ImageBoxes.h"
1010
#include "CommonFramework/ImageTools/FloatPixel.h"
11+
#include "CommonTools/Images/SolidColorTest.h"
1112
#include "ImageTools.h"
1213

1314
namespace PokemonAutomation{
@@ -47,6 +48,50 @@ ImageRGB32 image_diff_greyscale(const ImageViewRGB32& x, const ImageViewRGB32& y
4748

4849

4950

51+
ImagePixelBox find_contents_pixel_box(const ImageViewRGB32& image){
52+
const double MAX_SUM = 10;
53+
const double MAX_STDDEV = 10;
54+
55+
size_t top = 0;
56+
for (; top < image.height(); top++){
57+
ImageViewRGB32 line = image.sub_image(0, top, image.width(), 1);
58+
if (!is_black(line, MAX_SUM, MAX_STDDEV)){
59+
break;
60+
}
61+
}
62+
63+
size_t bot = image.height();
64+
for (; bot > top; bot--){
65+
ImageViewRGB32 line = image.sub_image(0, bot - 1, image.width(), 1);
66+
if (!is_black(line, MAX_SUM, MAX_STDDEV)){
67+
break;
68+
}
69+
}
70+
71+
size_t left = 0;
72+
for (; left < image.width(); left++){
73+
ImageViewRGB32 line = image.sub_image(left, 0, 1, image.height());
74+
if (!is_black(line, MAX_SUM, MAX_STDDEV)){
75+
break;
76+
}
77+
}
78+
79+
size_t right = image.width();
80+
for (; right > left; right--){
81+
ImageViewRGB32 line = image.sub_image(right - 1, 0, 1, image.height());
82+
if (!is_black(line, MAX_SUM, MAX_STDDEV)){
83+
break;
84+
}
85+
}
86+
87+
return ImagePixelBox(left, top, right, bot);
88+
}
89+
ImageFloatBox find_contents_float_box(const ImageViewRGB32& image){
90+
return pixelbox_to_floatbox(
91+
image.width(), image.height(),
92+
find_contents_pixel_box(image)
93+
);
94+
}
5095

5196

5297

SerialPrograms/Source/CommonTools/Images/ImageTools.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,18 @@ namespace PokemonAutomation{
1212

1313
class ImageViewRGB32;
1414
class ImageRGB32;
15+
struct ImagePixelBox;
16+
struct ImageFloatBox;
1517

1618

1719
// Deprecated
1820
ImageRGB32 image_diff_greyscale(const ImageViewRGB32& x, const ImageViewRGB32& y);
1921

2022

23+
ImagePixelBox find_contents_pixel_box(const ImageViewRGB32& image);
24+
ImageFloatBox find_contents_float_box(const ImageViewRGB32& image);
25+
26+
2127

2228

2329

SerialPrograms/Source/NintendoSwitch/DevPrograms/TestProgramSwitch.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@
172172
#include "Common/Cpp/StreamConnections/MockDevice.h"
173173
#include "ML/Inference/ML_PaddleOCRPipeline.h"
174174
#include "CommonTools/OCR/OCR_RawPaddleOCR.h"
175+
#include "CommonTools/Images/ImageTools.h"
175176

176177

177178

@@ -493,6 +494,8 @@ class MockConnection : public StreamConnection{
493494

494495

495496

497+
498+
496499
void TestProgram::program(MultiSwitchProgramEnvironment& env, CancellableScope& scope){
497500
using namespace Kernels;
498501
using namespace Kernels::Waterfill;
@@ -514,13 +517,20 @@ void TestProgram::program(MultiSwitchProgramEnvironment& env, CancellableScope&
514517
// JoyconContext context(scope, console.controller<JoyconController>());
515518
VideoOverlaySet overlays(overlay);
516519

520+
auto snapshot = feed.snapshot();
517521

522+
ImageFloatBox box = find_contents_float_box(snapshot);
523+
cout << box.x << ", " << box.y << ", " << box.width << ", " << box.height << endl;
524+
525+
overlays.add(COLOR_RED, box);
518526

527+
528+
#if 0
519529
PokemonLA::EventDialogDetector detector(logger, overlay, true);
520530

521531
auto snapshot = feed.snapshot();
522532
detector.process_frame(snapshot, current_time());
523-
533+
#endif
524534

525535
#if 0
526536
UpdateMenuWatcher update_menu(console, COLOR_PURPLE);

SerialPrograms/Source/NintendoSwitch/DevPrograms/TestProgramSwitch.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ class TestProgram : public MultiSwitchProgramInstance, public ButtonListener{
6767
std::atomic<uint64_t>& m_resets;
6868
};
6969

70+
virtual void start_program_border_check(
71+
VideoStream& stream, size_t console_index,
72+
FeedbackType feedback_type
73+
) override{}
74+
7075
private:
7176
ButtonCell BUTTON0;
7277
ButtonOption BUTTON1;

0 commit comments

Comments
 (0)