haraldhv
/
aigo
Archived
1
0
Fork 0

Started doing some actual guessing of what's happening on the board. Gui has sliders that adjust thresholds.

This commit is contained in:
Harald Hvaal 2007-02-23 22:28:29 +00:00
parent 7cb6c5f44f
commit f82e141315
10 changed files with 289 additions and 59 deletions

View File

@ -2,5 +2,6 @@
Settings::Settings( void ) Settings::Settings( void )
{ {
colorThreshold[0] = 100;
colorThreshold[1] = 1000;
} }

View File

@ -17,12 +17,20 @@ class Settings
public: public:
Settings( void ); Settings( void );
point getUL( void ); point getUL( void ) { return ul; }
point getUR( void ); point getUR( void ) { return ur; }
point getBL( void ); point getBL( void ) { return bl; }
point getBR( void ); point getBR( void ) { return br; }
int getWhiteTreshold( void ); void setUL( point p ) { ul = p; }
int getBlackTreshold( void ); 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: private:
//upper left, upper right etc... //upper left, upper right etc...

View File

@ -1,11 +1,39 @@
#include "state.h" #include "state.h"
#include <malloc.h> #include <stdio.h>
int State::size = 19; int State::size = 19;
State::State( void ) 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) State State::generateState (SDL_Surface *boardImg, Settings settings)
@ -13,22 +41,29 @@ State State::generateState (SDL_Surface *boardImg, Settings settings)
State state; State state;
int i, j, x, y, r, sum; int i, j, x, y, r, sum;
point p; point p;
r = 20; r = 10; //what kind of magic number is this?
for (i = 0; i < size; i++) { for (i = 0; i < size; i++) {
for (j = 0; j < size; j++) { for (j = 0; j < size; j++) { //for all points:
p = positionPoint(i, j, settings); 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 (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); 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; type t = BOARD;
if (sum < settings.getBlackTreshold()) t = BLACK; if (sum < settings.getBlackTreshold()) t = BLACK;
if (sum > settings.getWhiteTreshold()) t = WHITE; if (sum > settings.getWhiteTreshold()) t = WHITE;
PX_(state.board, i, j, size, size) = t; PX_(state.board, i, j, size, size) = t;
} }
//printf("\n");
} }
//printf("\n");
return state; return state;
} }

View File

@ -29,21 +29,30 @@ class State
* Standard constructor * Standard constructor
*/ */
State( void ); State( void );
virtual ~State( void );
void setSize(int size) { State::size = size; } void setSize(int size) { State::size = size; }
void printState( void );
static State generateState( SDL_Surface *board, Settings settings); static State generateState( SDL_Surface *board, Settings settings);
private:
/* /*
* Get the image point corresponding to board position (i, j) using * Get the image point corresponding to board position (i, j) using
* settings s. * settings s.
*/ */
static point positionPoint(int i, int j, 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; static int size;
type *board; // array describing the board situation
/**
* array describing the board situation
*/
type *board;
}; };

View File

@ -18,6 +18,7 @@ include(${QT_USE_FILE})
# the variable "qtproject_SRCS" contains all .cpp files of this project # the variable "qtproject_SRCS" contains all .cpp files of this project
set(aigoqt_SRCS set(aigoqt_SRCS
aigoqt.cpp aigoqt.cpp
clickablelabel.cpp
main.cpp main.cpp
) )

View File

@ -1,5 +1,6 @@
#include "aigoqt.h" #include "aigoqt.h"
#include <QtDebug> #include <QtDebug>
#include <state.h>
AigoQt::AigoQt(QWidget *parent) AigoQt::AigoQt(QWidget *parent)
: QMainWindow(parent) : QMainWindow(parent)
@ -9,7 +10,8 @@ AigoQt::AigoQt(QWidget *parent)
timer = new QTimer(this); timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(updateImage())); connect(timer, SIGNAL(timeout()), this, SLOT(updateImage()));
timer->start(200); timer->start(2000);
} }
AigoQt::~AigoQt( void ) AigoQt::~AigoQt( void )
@ -26,13 +28,73 @@ void AigoQt::updateImage( void )
void AigoQt::on_buttonSetCorners_clicked( 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(); State state = State::generateState(cam->getSurface(), settings);
timer->start(value); 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" #include "aigoqt.moc"

View File

@ -6,6 +6,8 @@
#include <quickcam.h> #include <quickcam.h>
#include <QTimer> #include <QTimer>
#include <settings.h>
class AigoQt : public QMainWindow, public Ui::AigoQt class AigoQt : public QMainWindow, public Ui::AigoQt
{ {
Q_OBJECT Q_OBJECT
@ -14,17 +16,25 @@ class AigoQt : public QMainWindow, public Ui::AigoQt
AigoQt(QWidget *parent = NULL); AigoQt(QWidget *parent = NULL);
virtual ~AigoQt( void ); virtual ~AigoQt( void );
private slots: private slots:
void on_buttonSetCorners_clicked( void ); 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(); void updateImage();
private: private:
Quickcam *cam; Quickcam *cam;
QTimer *timer; QTimer *timer;
Settings settings;
}; };
#endif /* ifndef _AIGOQT_H_ */ #endif /* ifndef _AIGOQT_H_ */

14
qt/clickablelabel.cpp Normal file
View File

@ -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"

21
qt/clickablelabel.h Normal file
View File

@ -0,0 +1,21 @@
#ifndef _CLICKABLELABEL_H_
#define _CLICKABLELABEL_H_
#include <QMouseEvent>
#include <QLabel>
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_ */

View File

@ -5,8 +5,8 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>849</width> <width>880</width>
<height>644</height> <height>684</height>
</rect> </rect>
</property> </property>
<property name="windowTitle" > <property name="windowTitle" >
@ -20,31 +20,6 @@
<property name="spacing" > <property name="spacing" >
<number>6</number> <number>6</number>
</property> </property>
<item row="1" column="1" >
<widget class="QLCDNumber" name="lcdNumber" />
</item>
<item row="1" column="0" >
<widget class="QSlider" name="slider" >
<property name="minimum" >
<number>10</number>
</property>
<property name="maximum" >
<number>400</number>
</property>
<property name="singleStep" >
<number>10</number>
</property>
<property name="pageStep" >
<number>50</number>
</property>
<property name="value" >
<number>200</number>
</property>
<property name="orientation" >
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
<item row="0" column="1" > <item row="0" column="1" >
<widget class="QGroupBox" name="groupBox_2" > <widget class="QGroupBox" name="groupBox_2" >
<property name="sizePolicy" > <property name="sizePolicy" >
@ -72,6 +47,20 @@
</property> </property>
</widget> </widget>
</item> </item>
<item>
<widget class="QPushButton" name="buttonGenerateState" >
<property name="text" >
<string>Generate state</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="buttonDrawPoints" >
<property name="text" >
<string>Draw points</string>
</property>
</widget>
</item>
<item> <item>
<spacer> <spacer>
<property name="orientation" > <property name="orientation" >
@ -88,6 +77,28 @@
</layout> </layout>
</widget> </widget>
</item> </item>
<item row="1" column="0" >
<widget class="QSlider" name="sliderWhite" >
<property name="minimum" >
<number>100</number>
</property>
<property name="maximum" >
<number>1000</number>
</property>
<property name="singleStep" >
<number>10</number>
</property>
<property name="pageStep" >
<number>50</number>
</property>
<property name="value" >
<number>200</number>
</property>
<property name="orientation" >
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
<item row="0" column="0" > <item row="0" column="0" >
<widget class="QGroupBox" name="groupBox" > <widget class="QGroupBox" name="groupBox" >
<property name="minimumSize" > <property name="minimumSize" >
@ -99,7 +110,7 @@
<property name="title" > <property name="title" >
<string>Preview</string> <string>Preview</string>
</property> </property>
<widget class="QLabel" name="image" > <widget class="ClickableLabel" name="image" >
<property name="geometry" > <property name="geometry" >
<rect> <rect>
<x>40</x> <x>40</x>
@ -126,6 +137,25 @@
</widget> </widget>
</widget> </widget>
</item> </item>
<item row="1" column="1" >
<widget class="QLCDNumber" name="lcdNumber" />
</item>
<item row="2" column="0" >
<widget class="QSlider" name="sliderBlack" >
<property name="minimum" >
<number>100</number>
</property>
<property name="maximum" >
<number>1000</number>
</property>
<property name="orientation" >
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
<item row="2" column="1" >
<widget class="QLCDNumber" name="lcdNumber_2" />
</item>
</layout> </layout>
</widget> </widget>
<widget class="QMenuBar" name="menubar" > <widget class="QMenuBar" name="menubar" >
@ -133,7 +163,7 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>849</width> <width>880</width>
<height>25</height> <height>25</height>
</rect> </rect>
</property> </property>
@ -152,10 +182,17 @@
</property> </property>
</action> </action>
</widget> </widget>
<customwidgets>
<customwidget>
<class>ClickableLabel</class>
<extends>QLabel</extends>
<header>clickablelabel.h</header>
</customwidget>
</customwidgets>
<resources/> <resources/>
<connections> <connections>
<connection> <connection>
<sender>slider</sender> <sender>sliderWhite</sender>
<signal>valueChanged(int)</signal> <signal>valueChanged(int)</signal>
<receiver>lcdNumber</receiver> <receiver>lcdNumber</receiver>
<slot>display(int)</slot> <slot>display(int)</slot>
@ -171,18 +208,50 @@
</hints> </hints>
</connection> </connection>
<connection> <connection>
<sender>slider</sender> <sender>sliderWhite</sender>
<signal>sliderMoved(int)</signal> <signal>sliderMoved(int)</signal>
<receiver>lcdNumber</receiver> <receiver>lcdNumber</receiver>
<slot>display(int)</slot> <slot>display(int)</slot>
<hints> <hints>
<hint type="sourcelabel" > <hint type="sourcelabel" >
<x>542</x> <x>542</x>
<y>621</y> <y>612</y>
</hint> </hint>
<hint type="destinationlabel" > <hint type="destinationlabel" >
<x>829</x> <x>834</x>
<y>628</y> <y>619</y>
</hint>
</hints>
</connection>
<connection>
<sender>sliderBlack</sender>
<signal>valueChanged(int)</signal>
<receiver>lcdNumber_2</receiver>
<slot>display(int)</slot>
<hints>
<hint type="sourcelabel" >
<x>701</x>
<y>638</y>
</hint>
<hint type="destinationlabel" >
<x>749</x>
<y>640</y>
</hint>
</hints>
</connection>
<connection>
<sender>sliderBlack</sender>
<signal>sliderMoved(int)</signal>
<receiver>lcdNumber_2</receiver>
<slot>display(int)</slot>
<hints>
<hint type="sourcelabel" >
<x>561</x>
<y>645</y>
</hint>
<hint type="destinationlabel" >
<x>825</x>
<y>640</y>
</hint> </hint>
</hints> </hints>
</connection> </connection>