Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't detect a board with light gray squares #44

Open
zoharl3 opened this issue Jan 9, 2024 · 2 comments
Open

Can't detect a board with light gray squares #44

zoharl3 opened this issue Jan 9, 2024 · 2 comments

Comments

@zoharl3
Copy link

zoharl3 commented Jan 9, 2024

light_board

@zoharl3
Copy link
Author

zoharl3 commented Jan 9, 2024

Quantizing to three colors and changing the middle one resolves some of these boards. Otherwise, some of the white queens are detected as black.

Image source: "Chess Tactics Book for Beginners 200 Puzzles Learn How to Play and Checkmate in One and Two Moves"

light_board

@zoharl3
Copy link
Author

zoharl3 commented Jan 9, 2024

A solution that seems to work so far:

  1. Quantize 3 colors and detect black square color as the one that isn't white or black (max and min color norm). Find a pixel with such color in the image.
  2. Quantize 8 colors. Get the color of the pixel above and change it to red.

Matlab:

% process image

function chess_util( fin, fout )
    % read
    a = imread( fin );
    assert( size( a, 3 ) == 3 );

    % quantize colors
    [ia, cm] = rgb2ind( a, 3, 'nodither' ); % 3, 4

    %imshow( ia, cm )

    % impixelinfo()
    % colormapeditor

    % change color
    if 1
        % visualize color map
        if 0
            rgbplot(cm)
            hold on
            colormap(cm)
            colorbar('Ticks',[])
            hold off
        end

        % find dark square color in color map
        % eliminate black and white
        ii = 1:size( cm, 1 ); % candidates

        dd = sum( cm.^2, 2 );
        [~, imin] = min( dd(ii) ); % black
        ii(imin) = [];

        [~, imin] = min( -dd(ii) ); % white
        ii(imin) = [];

        [~, imin] = min( dd(ii) ); % darkest
        icl = ii(imin);
        %cl = cm( icl, :);
        [row, col] = find( ia == icl - 1 ); % find a pixel with that value

        % load more colors
        [ia, cm] = rgb2ind( a, 8, 'nodither' );
        icl = ia( row(1), col(1) ); % get color from pixel location
        cm(icl + 1, :) = [1, 0, 0]; % change
    end

    % write
    imwrite( ia, cm, fout );
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant