From 7d7e61fe28a31321ef9df4ea6169694afff7d8d3 Mon Sep 17 00:00:00 2001 From: haraldhv Date: Mon, 26 Feb 2007 10:45:05 +0000 Subject: [PATCH] Added image library and threshold function. Moved pixel access defines from state to image class. --- lib/CMakeLists.txt | 1 + lib/image.cpp | 19 +++++++++++++++++++ lib/image.h | 28 ++++++++++++++++++++++++++++ lib/state.h | 12 +----------- qt/aigoqt.cpp | 6 ++++-- 5 files changed, 53 insertions(+), 13 deletions(-) create mode 100644 lib/image.cpp create mode 100644 lib/image.h diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index 3795ec8..d16e137 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -5,6 +5,7 @@ set(AIGO_SRCS quickcam.cpp settings.cpp state.cpp + image.cpp ) #find SDL and its related libraries diff --git a/lib/image.cpp b/lib/image.cpp new file mode 100644 index 0000000..620794a --- /dev/null +++ b/lib/image.cpp @@ -0,0 +1,19 @@ +#include "image.h" + +namespace Image +{ + void threshold(SDL_Surface *image, int lowerBound, int upperBound) + { + int x, y; + for (int i = 0; i < image->w * image->h; ++i) + { + int gray = GRAY(((uint32_t*)image->pixels)[i]); + if (gray > lowerBound && gray < upperBound) + ((uint32_t*)image->pixels)[i] = 0xffffffff; + else + ((uint32_t*)image->pixels)[i] = 0xff000000; + //gray | (gray << 8) | (gray << 16); + + } + } +} diff --git a/lib/image.h b/lib/image.h new file mode 100644 index 0000000..ef8713c --- /dev/null +++ b/lib/image.h @@ -0,0 +1,28 @@ +#ifndef _IMAGE_H_ +#define _IMAGE_H_ + +#include + +// get pixel/something in array p of size w*h +#define PX_(p, x, y, w, h) ((p)[(x)+(y)*(w)]) +// get pixel in SDL_Surface i +#define PX(i,x,y) (PX_((uint32_t*)(i)->pixels, (x), (y), (i)->w, (i)->h)) +// get color components of pixel value. should probably be done in a nicer way +#define PXR(px) ((px) & 0xff) +#define PXG(px) (((px) & 0xff00) >> 8) +#define PXB(px) (((px) & 0xff0000) >> 16) + +//get the grayscale value of the pixel +#define GRAY(px) ((PXR(px) + PXG(px) + PXB(px)) / 3) + +// iterate over SDL_Surface s with variables x, y +#define iter_pixels(s,x,y) for (x = 0; x < s->w; x++) for (y = 0; y < s->h; y++) + +namespace Image +{ + + void threshold(SDL_Surface *image, int lowerBound, int upperBound); + +} + +#endif /* ifndef _IMAGE_H_ */ diff --git a/lib/state.h b/lib/state.h index 00a66d8..8c94fd9 100644 --- a/lib/state.h +++ b/lib/state.h @@ -4,17 +4,7 @@ #include "settings.h" #include #include - -// get pixel/something in array p of size w*h -#define PX_(p, x, y, w, h) ((p)[(x)+(y)*(w)]) -// get pixel in SDL_Surface i -#define PX(i,x,y) (PX_((uint32_t*)(i)->pixels, (x), (y), (i)->w, (i)->h)) -// get color components of pixel value. should probably be done in a nicer way -#define PXR(px) ((px) & 0xff) -#define PXG(px) (((px) & 0xff00) >> 8) -#define PXB(px) (((px) & 0xff0000) >> 16) -// iterate over SDL_Surface s with variables x, y -#define iter_pixels(s,x,y) for (x = 0; x < s->w; x++) for (y = 0; y < s->h; y++) +#include "image.h" /** diff --git a/qt/aigoqt.cpp b/qt/aigoqt.cpp index b3b51f6..922539b 100644 --- a/qt/aigoqt.cpp +++ b/qt/aigoqt.cpp @@ -1,6 +1,7 @@ #include "aigoqt.h" #include #include +#include "image.h" AigoQt::AigoQt(QWidget *parent) : QMainWindow(parent) @@ -9,8 +10,8 @@ AigoQt::AigoQt(QWidget *parent) cam = new Quickcam("/dev/video0"); timer = new QTimer(this); - connect(timer, SIGNAL(timeout()), this, SLOT(on_buttonDrawPoints_clicked())); - timer->start(200); + connect(timer, SIGNAL(timeout()), this, SLOT(updateImage())); + timer->start(10); } @@ -23,6 +24,7 @@ void AigoQt::updateImage( void ) { cam->update(); SDL_Surface *s = cam->getSurface(); + Image::threshold(s, sliderBlack->value(), sliderWhite->value()); image->setPixmap(QPixmap::fromImage(QImage((uchar*)s->pixels, s->w, s->h, QImage::Format_RGB32))); on_buttonGenerateState_clicked(); }