Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/* Image Histogram
*
* From: https://github.com/PokemonAutomation/
*
*/

#include <algorithm>
#include "CommonFramework/ImageTypes/ImageViewRGB32.h"

#include "ImageHistogram.h"

namespace PokemonAutomation{



void BrightestChannelHistogram::add(uint32_t pixel){
const uint32_t red = (pixel >> 16) & 0xff;
const uint32_t green = (pixel >> 8) & 0xff;
const uint32_t blue = pixel & 0xff;
m_bins[std::max({red, green, blue})]++;
m_count++;
}
uint8_t BrightestChannelHistogram::percentile(double fraction) const{
if (m_count == 0){
return 0;
}
// The position of the pixel to return among the sorted pixels, counted from 0.
fraction = std::clamp(fraction, 0.0, 1.0);
const uint64_t rank = std::min((uint64_t)((double)m_count * fraction), m_count - 1);
uint64_t seen = 0;
for (size_t level = 0; level < m_bins.size(); level++){
seen += m_bins[level];
if (seen > rank){
return (uint8_t)level;
}
}
return 255;
}

static void add_if_opaque(BrightestChannelHistogram& histogram, uint32_t pixel){
if ((pixel >> 24) >= 128){
histogram.add(pixel);
}
}
BrightestChannelHistogram image_brightest_channel_histogram(const ImageViewRGB32& image){
BrightestChannelHistogram histogram;
const size_t width = image.width();
const size_t height = image.height();
for (size_t y = 0; y < height; y++){
for (size_t x = 0; x < width; x++){
add_if_opaque(histogram, image.pixel(x, y));
}
}
return histogram;
}
BrightestChannelHistogram image_border_brightest_channel_histogram(const ImageViewRGB32& image){
BrightestChannelHistogram histogram;
const size_t width = image.width();
const size_t height = image.height();
if (width == 0 || height == 0){
return histogram;
}
// Top and bottom rows, then the left and right columns without the corners.
for (size_t x = 0; x < width; x++){
add_if_opaque(histogram, image.pixel(x, 0));
if (height > 1){
add_if_opaque(histogram, image.pixel(x, height - 1));
}
}
for (size_t y = 1; y + 1 < height; y++){
add_if_opaque(histogram, image.pixel(0, y));
if (width > 1){
add_if_opaque(histogram, image.pixel(width - 1, y));
}
}
return histogram;
}



}
51 changes: 51 additions & 0 deletions SerialPrograms/Source/CommonFramework/ImageTools/ImageHistogram.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/* Image Histogram
*
* From: https://github.com/PokemonAutomation/
*
*/

#ifndef PokemonAutomation_CommonFramework_ImageHistogram_H
#define PokemonAutomation_CommonFramework_ImageHistogram_H

#include <array>
#include <cstdint>

namespace PokemonAutomation{
class ImageViewRGB32;


// A histogram for the distribution of the brightness on an image.
// Brightness is defined here as max(R, G, B) of a pixel, which in HSV is called "value".
// This histogram is useful as a feature of an image or to compute a cutoff threshold
// for binarization of the image.
class BrightestChannelHistogram{
public:
// Add a pixel (0xAARRGGBB) into the histogram. Its alpha is ignored.
void add(uint32_t pixel);

// How many pixels were added
uint64_t count() const{ return m_count; }

// Return the percentile. 0 if no pixel were added.
// Given a `fraction` (0.0 to 1.0), it sorts the counted pixels from darkest to brightnest
// and return the brightness level so that the pixels having brightness smaller or equal
// to that takes up this fraction of the total pixel count.
// e.g. 0.5 gives the median and 1.0 gives the max brightness recorded.
uint8_t percentile(double fraction) const;

private:
std::array<uint64_t, 256> m_bins{};
uint64_t m_count = 0;
};

// Pixels with alpha < 128 are ignored.
BrightestChannelHistogram image_brightest_channel_histogram(const ImageViewRGB32& image);

// Same as `image_brightest_channel_histogram()`, but only on the one-pixel-wide
// border of the image, with each border pixel counted once.
BrightestChannelHistogram image_border_brightest_channel_histogram(const ImageViewRGB32& image);



}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <array>
#include <map>
#include <memory>
#include <optional>
#include <string>
#include <opencv2/imgproc.hpp>
#include "Common/Cpp/Color.h" // needed for COLOR_RED, COLOR_ORANGE
Expand All @@ -17,6 +18,7 @@
#include "Kernels/Waterfill/Kernels_Waterfill_Session.h"
#include "CommonFramework/GlobalAutoPaths.h"
#include "CommonFramework/ImageTools/ImageBoxes.h"
#include "CommonFramework/ImageTools/ImageHistogram.h"
#include "CommonFramework/ImageTypes/ImageRGB32.h"
#include "CommonFramework/ImageTypes/ImageViewRGB32.h"
#include "CommonFramework/ImageTypes/ImageRGB32_OpenCV.h"
Expand Down Expand Up @@ -154,6 +156,31 @@ static ImagePixelBox tighten_to_glyph(const ImageViewRGB32& region, const ImageP
);
}

// Return the brightness that separates the digits from the background in `image`.
// Return std::nullopt if `image` has no digits.
//
// - Background brightness is defined as the median brightness of the 1-pixel wide
// `image` border. It is relatively bright.
// - Digits brightness is defined as the 5th percentile brightness of all pixels.
// It is relatively dark.
// The cutoff is 70% of the way from the digits to the background.
// If the digits are less than a fifth darker than the background, the region has no
// digits.
static std::optional<uint32_t> digit_cutoff(Logger& logger, const ImageViewRGB32& image){
const uint32_t background = image_border_brightest_channel_histogram(image).percentile(0.5);
const uint32_t digit = image_brightest_channel_histogram(image).percentile(0.05);
const std::string levels =
"DigitReader: background brightness " + std::to_string(background) +
", digit brightness " + std::to_string(digit);
if (digit + background / 5 > background){
// logger.log(levels + ": too little contrast for digits.", COLOR_RED);
return std::nullopt;
}
const uint32_t cutoff = digit + (background - digit) * 7 / 10;
// logger.log(levels + ", cutoff " + std::to_string(cutoff));
return cutoff;
}

// ---------------------------------------------------------------------------
// Main function
// ---------------------------------------------------------------------------
Expand All @@ -163,7 +190,6 @@ int read_digits_waterfill_template(
DigitTemplateType template_type,
double rmsd_threshold,
const std::string& dump_prefix,
uint8_t binarize_high,
bool save_debug_images
){
using namespace Kernels::Waterfill;
Expand All @@ -190,16 +216,15 @@ int read_digits_waterfill_template(

// ------------------------------------------------------------------
// Step 2: Binarise the blurred image.
// Pixels where ALL channels <= binarize_high become 1 (foreground).
// Default 0xBE (190) works for yellow stat boxes.
// Use 0x7F (127) for the lilac level box to prevent the blurred
// lilac background (B~208, drops to ~156 near shadows) from being
// captured and merging digit blobs.
// Pixels where ALL channels <= cutoff become 1 (foreground).
// ------------------------------------------------------------------
uint32_t bh = binarize_high;
uint32_t binarize_color = 0xff000000u | (bh << 16) | (bh << 8) | bh;
const std::optional<uint32_t> cutoff = digit_cutoff(logger, blurred_img);
if (!cutoff){
return -1;
}
const uint32_t cutoff_color = 0xff000000u | (*cutoff << 16) | (*cutoff << 8) | *cutoff;
PackedBinaryMatrix matrix =
compress_rgb32_to_binary_range(blurred_img, 0xff000000u, binarize_color);
compress_rgb32_to_binary_range(blurred_img, 0xff000000u, cutoff_color);

// ------------------------------------------------------------------
// Step 3: Waterfill - find connected dark blobs (individual digits).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,25 @@ enum class DigitTemplateType{
DialogBox, // White dialog box: PokemonFRLG/DialogDigits/
};

// Read a string of decimal digits from `stat_region`.
// Read a number written in the FRLG pixel font, e.g. a stat, a level or a trainer ID.
// It finds each digit's shape, then compares the digit against 10 template images.
//
// template_type Which template set to use (StatBox or LevelBox).
// dump_prefix Prefix used when saving debug crop PNGs to DebugDumps/.
// template_type The template set, chosen by the type of the UI the number is on (see
// `DigitTemplateType`).
// rmsd_threshold The highest RMSD that still counts as a match. 175 by default;
// `StatsReader::read_level()` uses 230, because the lilac
// background raises the RMSD.
// dump_prefix File name prefix of the digit crops saved to DebugDumps/.
// save_debug_images Save each digit crop to DebugDumps/.
//
// Returns the parsed integer, or -1 on failure.
// Returns the number, or -1 if `stat_region` is empty or no digit can be found.
int read_digits_waterfill_template(
Logger& logger,
const ImageViewRGB32& stat_region,
DigitTemplateType template_type = DigitTemplateType::StatBox,
double rmsd_threshold = 175.0,
const std::string& dump_prefix = "digit",
uint8_t binarize_high = 0xBE, // 0xBE=190 for yellow stat boxes;
bool save_debug_images = false
// use 0x7F=127 for lilac level box
);


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,12 @@ bool StatsReader::read_level(
);

// The level uses white text with dark shadow on a lilac background.
// The digit reader's binarizer captures dark pixels (<=190 on all channels)
// but NOT the white text (all channels 255 -> excluded). This leaves the
// shadow outline fragmented into many small disconnected blobs.
// Preprocess: convert bright-white text pixels to black so the binarizer
// merges text + shadow into one solid connected blob per digit.
// The digit reader's binarizer captures pixels darker than the background
// but NOT the white text. This leaves the shadow outline fragmented into
// many small disconnected blobs.
// To solve this, we run a preprocess: convert bright-white text pixels to
// black so the binarizer merges text + shadow into one solid connected blob
// per digit.
ImageRGB32 preprocessed = level_box.scale_to(level_box.width(), level_box.height());
for (size_t r = 0; r < level_box.height(); r++){
for (size_t c = 0; c < level_box.width(); c++){
Expand All @@ -202,7 +203,7 @@ bool StatsReader::read_level(
// give higher RMSD than yellow stat-box crops due to background colour.
stats.level = read_digits_waterfill_template(
logger, preprocessed, DigitTemplateType::LevelBox, 230,
"levelDigit", 0x7F);
"levelDigit");
// log if it's an obviously bad read
if (!stats.level.has_value() || stats.level.value_or(-1) < 2 || stats.level.value_or(-1) > 100){
logger.log("Level OCR result out of range", COLOR_RED);
Expand Down
2 changes: 2 additions & 0 deletions SerialPrograms/cmake/SourceFiles.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,8 @@ file(GLOB LIBRARY_SOURCES
Source/CommonFramework/ImageTools/ImageBoxes.h
Source/CommonFramework/ImageTools/ImageDiff.cpp
Source/CommonFramework/ImageTools/ImageDiff.h
Source/CommonFramework/ImageTools/ImageHistogram.cpp
Source/CommonFramework/ImageTools/ImageHistogram.h
Source/CommonFramework/ImageTools/ImageStats.cpp
Source/CommonFramework/ImageTools/ImageStats.h
Source/CommonFramework/ImageTools/ImageStats_Tests.cpp
Expand Down
Loading