From f82e141315903e6c9027e0cbcddbd0bca75afdf2 Mon Sep 17 00:00:00 2001 From: haraldhv Date: Fri, 23 Feb 2007 22:28:29 +0000 Subject: [PATCH] Started doing some actual guessing of what's happening on the board. Gui has sliders that adjust thresholds. --- lib/settings.cpp | 3 +- lib/settings.h | 20 ++++-- lib/state.cpp | 49 ++++++++++++--- lib/state.h | 15 ++++- qt/CMakeLists.txt | 1 + qt/aigoqt.cpp | 72 ++++++++++++++++++++-- qt/aigoqt.h | 16 ++++- qt/clickablelabel.cpp | 14 +++++ qt/clickablelabel.h | 21 +++++++ qt/main.ui | 137 +++++++++++++++++++++++++++++++----------- 10 files changed, 289 insertions(+), 59 deletions(-) create mode 100644 qt/clickablelabel.cpp create mode 100644 qt/clickablelabel.h diff --git a/lib/settings.cpp b/lib/settings.cpp index 7a461b1..10b8874 100644 --- a/lib/settings.cpp +++ b/lib/settings.cpp @@ -2,5 +2,6 @@ Settings::Settings( void ) { - + colorThreshold[0] = 100; + colorThreshold[1] = 1000; } diff --git a/lib/settings.h b/lib/settings.h index 660f5e0..e97b170 100644 --- a/lib/settings.h +++ b/lib/settings.h @@ -17,12 +17,20 @@ class Settings public: Settings( void ); - point getUL( void ); - point getUR( void ); - point getBL( void ); - point getBR( void ); - int getWhiteTreshold( void ); - int getBlackTreshold( void ); + point getUL( void ) { return ul; } + point getUR( void ) { return ur; } + point getBL( void ) { return bl; } + point getBR( void ) { return br; } + void setUL( point p ) { ul = p; } + void setUR( point p ) { ur = p; } + void setBL( point p ) { bl = p; } + void setBR( point p ) { br = p; } + + int getWhiteTreshold( void ) { return colorThreshold[WHITE]; } + int getBlackTreshold( void ) { return colorThreshold[BLACK]; } + + void setWhiteThreshold( int t) { colorThreshold[WHITE] = t; } + void setBlackThreshold( int t) { colorThreshold[BLACK] = t; } private: //upper left, upper right etc... diff --git a/lib/state.cpp b/lib/state.cpp index 775421b..0d7c53a 100644 --- a/lib/state.cpp +++ b/lib/state.cpp @@ -1,11 +1,39 @@ #include "state.h" -#include +#include int State::size = 19; State::State( void ) { - board = (type*)malloc(sizeof(type) * size * size); + board = new type[size * size]; +} + +State::~State( void ) +{ + delete[] board; +} + +void State::printState( void ) +{ + int i,j; + for (i = 0; i < size; i++) { + for (j = 0; j < size; j++) { //for all points: + switch (board[i*size + j]) + { + case BLACK: + printf("B "); + break; + case WHITE: + printf("W "); + break; + case BOARD: + printf("- "); + break; + } + } + printf("\n"); + } + printf("\n"); } State State::generateState (SDL_Surface *boardImg, Settings settings) @@ -13,22 +41,29 @@ State State::generateState (SDL_Surface *boardImg, Settings settings) State state; int i, j, x, y, r, sum; point p; - r = 20; + r = 10; //what kind of magic number is this? for (i = 0; i < size; i++) { - for (j = 0; j < size; j++) { - p = positionPoint(i, j, settings); + for (j = 0; j < size; j++) { //for all points: + p = positionPoint(i, j, settings); //retrieve the estimated point + sum = 0; + + //TODO: this loop should be placed into its own function! for (y = p.y-r; y < p.y+r; y++) { - for (x = p.x-r; x < p.x+r; x++) { + for (x = p.x-r; x < p.x+r; x++) { //for -20 to 20 around the point uint32_t px = PX(boardImg, x, y); - sum += PXR(px) + PXG(px) + PXB(px); + sum += (1024 -PXR(px) - PXG(px) + PXB(px)); //sum up the colors } } + sum/=400; + //printf("%d\t", sum); type t = BOARD; if (sum < settings.getBlackTreshold()) t = BLACK; if (sum > settings.getWhiteTreshold()) t = WHITE; PX_(state.board, i, j, size, size) = t; } + //printf("\n"); } + //printf("\n"); return state; } diff --git a/lib/state.h b/lib/state.h index 2c5971b..00a66d8 100644 --- a/lib/state.h +++ b/lib/state.h @@ -29,21 +29,30 @@ class State * Standard constructor */ State( void ); + virtual ~State( void ); void setSize(int size) { State::size = size; } + void printState( void ); static State generateState( SDL_Surface *board, Settings settings); - - private: /* * Get the image point corresponding to board position (i, j) using * settings s. */ static point positionPoint(int i, int j, Settings s); + private: + + /** + * The size of the board, e.g. 19x19, 13x13 etc. + */ static int size; - type *board; // array describing the board situation + + /** + * array describing the board situation + */ + type *board; }; diff --git a/qt/CMakeLists.txt b/qt/CMakeLists.txt index 277bc81..47879df 100644 --- a/qt/CMakeLists.txt +++ b/qt/CMakeLists.txt @@ -18,6 +18,7 @@ include(${QT_USE_FILE}) # the variable "qtproject_SRCS" contains all .cpp files of this project set(aigoqt_SRCS aigoqt.cpp + clickablelabel.cpp main.cpp ) diff --git a/qt/aigoqt.cpp b/qt/aigoqt.cpp index e55f1b2..f5b511e 100644 --- a/qt/aigoqt.cpp +++ b/qt/aigoqt.cpp @@ -1,5 +1,6 @@ #include "aigoqt.h" #include +#include AigoQt::AigoQt(QWidget *parent) : QMainWindow(parent) @@ -9,7 +10,8 @@ AigoQt::AigoQt(QWidget *parent) timer = new QTimer(this); connect(timer, SIGNAL(timeout()), this, SLOT(updateImage())); - timer->start(200); + timer->start(2000); + } AigoQt::~AigoQt( void ) @@ -26,13 +28,73 @@ void AigoQt::updateImage( void ) void AigoQt::on_buttonSetCorners_clicked( void ) { - + disconnect(image, 0, 0, 0); + connect(image, SIGNAL(clicked(int, int)), this, SLOT(handleCornerClicked(int, int))); + statusBar()->showMessage("Please click the upper left corner of the board."); } -void AigoQt::on_slider_valueChanged( int value) +void AigoQt::on_buttonGenerateState_clicked( void ) { - timer->stop(); - timer->start(value); + State state = State::generateState(cam->getSurface(), settings); + state.printState(); +} + +void AigoQt::on_sliderBlack_valueChanged( int value) +{ + settings.setBlackThreshold(value); + on_buttonGenerateState_clicked(); +} + +void AigoQt::on_sliderWhite_valueChanged( int value) +{ + settings.setWhiteThreshold(value); + on_buttonGenerateState_clicked(); +} + +void AigoQt::on_buttonDrawPoints_clicked( void ) +{ + for (int i = 0; i < 19; i++) + { + for (int j = 0; j < 19; j++) + { + point p = State::positionPoint(i, j, settings); + ((uint8_t*)cam->getSurface()->pixels)[(p.y*640 + p.x) * 4] = 0xff000000; + } + } + SDL_Surface *s = cam->getSurface(); + image->setPixmap(QPixmap::fromImage(QImage((uchar*)s->pixels, s->w, s->h, QImage::Format_RGB32))); +} + +void AigoQt::handleCornerClicked( int x, int y) +{ + point p; + p.x = x; + p.y = y; + + QString pointString = QString::fromLatin1("(%1, %2)").arg(x).arg(y); + + static int corner = -1; + ++corner %= 4; + switch (corner) + { + case 0: + settings.setUL(p); + statusBar()->showMessage("Upper left set to " + pointString + ". Click upper right."); + break; + case 1: + settings.setUR(p); + statusBar()->showMessage("Upper right set to " + pointString + ". Click bottom left."); + break; + case 2: + settings.setBL(p); + statusBar()->showMessage("Bottom left set to " + pointString + ". Click bottom right."); + break; + case 3: + settings.setBR(p); + statusBar()->showMessage("Bottom right set to " + pointString + ". All done."); + disconnect(image, NULL, this, NULL); + break; + } } #include "aigoqt.moc" diff --git a/qt/aigoqt.h b/qt/aigoqt.h index 9e5c716..fad6d49 100644 --- a/qt/aigoqt.h +++ b/qt/aigoqt.h @@ -6,6 +6,8 @@ #include #include +#include + class AigoQt : public QMainWindow, public Ui::AigoQt { Q_OBJECT @@ -14,17 +16,25 @@ class AigoQt : public QMainWindow, public Ui::AigoQt AigoQt(QWidget *parent = NULL); virtual ~AigoQt( void ); - private slots: void on_buttonSetCorners_clicked( void ); - void on_slider_valueChanged(int value); + void on_buttonGenerateState_clicked( void ); + void on_sliderWhite_valueChanged(int value); + void on_sliderBlack_valueChanged(int value); + + void on_buttonDrawPoints_clicked( void ); + + + void handleCornerClicked(int x, int y); void updateImage(); private: - Quickcam *cam; QTimer *timer; + Settings settings; + + }; #endif /* ifndef _AIGOQT_H_ */ diff --git a/qt/clickablelabel.cpp b/qt/clickablelabel.cpp new file mode 100644 index 0000000..093ff59 --- /dev/null +++ b/qt/clickablelabel.cpp @@ -0,0 +1,14 @@ +#include "clickablelabel.h" + +ClickableLabel::ClickableLabel(QWidget *parent) + : QLabel(parent) +{ + //do nothing +} + +void ClickableLabel::mouseReleaseEvent(QMouseEvent *event) +{ + emit clicked(event->x(), event->y()); +} + +#include "clickablelabel.moc" diff --git a/qt/clickablelabel.h b/qt/clickablelabel.h new file mode 100644 index 0000000..90fce2a --- /dev/null +++ b/qt/clickablelabel.h @@ -0,0 +1,21 @@ +#ifndef _CLICKABLELABEL_H_ +#define _CLICKABLELABEL_H_ + +#include +#include + +class ClickableLabel : public QLabel +{ + Q_OBJECT + + public: + ClickableLabel(QWidget *parent = NULL); + virtual void mouseReleaseEvent(QMouseEvent *event); + + signals: + void clicked( int x, int y ); + + +}; + +#endif /* ifndef _CLICKABLELABEL_H_ */ diff --git a/qt/main.ui b/qt/main.ui index 87bb962..e60db28 100644 --- a/qt/main.ui +++ b/qt/main.ui @@ -5,8 +5,8 @@ 0 0 - 849 - 644 + 880 + 684 @@ -20,31 +20,6 @@ 6 - - - - - - - 10 - - - 400 - - - 10 - - - 50 - - - 200 - - - Qt::Horizontal - - - @@ -72,6 +47,20 @@ + + + + Generate state + + + + + + + Draw points + + + @@ -88,6 +77,28 @@ + + + + 100 + + + 1000 + + + 10 + + + 50 + + + 200 + + + Qt::Horizontal + + + @@ -99,7 +110,7 @@ Preview - + 40 @@ -126,6 +137,25 @@ + + + + + + + 100 + + + 1000 + + + Qt::Horizontal + + + + + + @@ -133,7 +163,7 @@ 0 0 - 849 + 880 25 @@ -152,10 +182,17 @@ + + + ClickableLabel + QLabel +
clickablelabel.h
+
+
- slider + sliderWhite valueChanged(int) lcdNumber display(int) @@ -171,18 +208,50 @@ - slider + sliderWhite sliderMoved(int) lcdNumber display(int) 542 - 621 + 612 - 829 - 628 + 834 + 619 + + + + + sliderBlack + valueChanged(int) + lcdNumber_2 + display(int) + + + 701 + 638 + + + 749 + 640 + + + + + sliderBlack + sliderMoved(int) + lcdNumber_2 + display(int) + + + 561 + 645 + + + 825 + 640