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 )
{
colorThreshold[0] = 100;
colorThreshold[1] = 1000;
}

View File

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

View File

@ -1,11 +1,39 @@
#include "state.h"
#include <malloc.h>
#include <stdio.h>
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;
}

View File

@ -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;
};

View File

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

View File

@ -1,5 +1,6 @@
#include "aigoqt.h"
#include <QtDebug>
#include <state.h>
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"

View File

@ -6,6 +6,8 @@
#include <quickcam.h>
#include <QTimer>
#include <settings.h>
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_ */

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>
<x>0</x>
<y>0</y>
<width>849</width>
<height>644</height>
<width>880</width>
<height>684</height>
</rect>
</property>
<property name="windowTitle" >
@ -20,31 +20,6 @@
<property name="spacing" >
<number>6</number>
</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" >
<widget class="QGroupBox" name="groupBox_2" >
<property name="sizePolicy" >
@ -72,6 +47,20 @@
</property>
</widget>
</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>
<spacer>
<property name="orientation" >
@ -88,6 +77,28 @@
</layout>
</widget>
</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" >
<widget class="QGroupBox" name="groupBox" >
<property name="minimumSize" >
@ -99,7 +110,7 @@
<property name="title" >
<string>Preview</string>
</property>
<widget class="QLabel" name="image" >
<widget class="ClickableLabel" name="image" >
<property name="geometry" >
<rect>
<x>40</x>
@ -126,6 +137,25 @@
</widget>
</widget>
</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>
</widget>
<widget class="QMenuBar" name="menubar" >
@ -133,7 +163,7 @@
<rect>
<x>0</x>
<y>0</y>
<width>849</width>
<width>880</width>
<height>25</height>
</rect>
</property>
@ -152,10 +182,17 @@
</property>
</action>
</widget>
<customwidgets>
<customwidget>
<class>ClickableLabel</class>
<extends>QLabel</extends>
<header>clickablelabel.h</header>
</customwidget>
</customwidgets>
<resources/>
<connections>
<connection>
<sender>slider</sender>
<sender>sliderWhite</sender>
<signal>valueChanged(int)</signal>
<receiver>lcdNumber</receiver>
<slot>display(int)</slot>
@ -171,18 +208,50 @@
</hints>
</connection>
<connection>
<sender>slider</sender>
<sender>sliderWhite</sender>
<signal>sliderMoved(int)</signal>
<receiver>lcdNumber</receiver>
<slot>display(int)</slot>
<hints>
<hint type="sourcelabel" >
<x>542</x>
<y>621</y>
<y>612</y>
</hint>
<hint type="destinationlabel" >
<x>829</x>
<y>628</y>
<x>834</x>
<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>
</hints>
</connection>