From 8bfc0687d81a0c0b1c683cc1f304d67ac9ba7087 Mon Sep 17 00:00:00 2001 From: Andrey Tkachenko Date: Wed, 23 Jul 2014 20:50:12 +0400 Subject: [PATCH] Point Cloud --- CSV.pro | 7 +++++-- main.cpp | 2 +- svabstractkernel.h | 11 +++++------ svcurve.h | 7 +++++++ svdefs.h | 12 ++++++++++++ svimage.h | 2 +- svkernelv2.cpp | 10 ++++++---- svkernelv2.h | 7 ++----- svpoint.h | 2 +- svpointcloud.cpp | 10 ++++++++++ svpointcloud.h | 15 +++++++++++++++ svprocessor.cpp | 38 +++++++++++++++++++++++++++++--------- svprocessor.h | 24 ++++++++++++++++++++---- svworker.cpp | 15 ++++++++++++--- svworker.h | 9 ++++----- 15 files changed, 130 insertions(+), 41 deletions(-) create mode 100644 svdefs.h create mode 100644 svpointcloud.cpp create mode 100644 svpointcloud.h diff --git a/CSV.pro b/CSV.pro index 3a1af4c..73232b3 100644 --- a/CSV.pro +++ b/CSV.pro @@ -23,7 +23,8 @@ SOURCES += main.cpp \ svcurve.cpp \ svsimplepoint.cpp \ svfigure.cpp \ - svobject.cpp + svobject.cpp \ + svpointcloud.cpp HEADERS += \ svimage.h \ @@ -37,7 +38,9 @@ HEADERS += \ svcurve.h \ svsimplepoint.h \ svfigure.h \ - svobject.h + svobject.h \ + svpointcloud.h \ + svdefs.h RESOURCES += \ resource.qrc diff --git a/main.cpp b/main.cpp index 91c4b90..822e455 100644 --- a/main.cpp +++ b/main.cpp @@ -15,7 +15,7 @@ int main(int argc, char *argv[]) QQmlApplicationEngine engine; SvImageProvider imageProvider; - QImage imgLeft("../CSV/img/left1.png"); + QImage imgLeft("../CSV/img/right9.png"); QImage imgRight("../CSV/img/right1.png"); QImage imgStereo(imgLeft.width(), imgLeft.height(), QImage::Format_RGB32); diff --git a/svabstractkernel.h b/svabstractkernel.h index fa71161..93cdd7a 100644 --- a/svabstractkernel.h +++ b/svabstractkernel.h @@ -2,19 +2,18 @@ #define SVABSTRACTKERNEL_H #include "svimage.h" +#include "svpointcloud.h" class SvAbstractKernel { protected: - SvImage* m_left; - SvImage* m_right; - SvImage* m_result; + SvImage* m_image; + SvPointCloud* m_pointCloud; public: - void setLeftImage(SvImage* image) {m_left = image;} - void setRightImage(SvImage* image) {m_right = image;} - void setResultImage(SvImage* image) {m_result = image;} + void setImage(SvImage* image) {m_image = image;} + void setPointCloud(SvPointCloud* pointCloud) {m_pointCloud = pointCloud;} virtual void exec(int line) = 0; virtual ~SvAbstractKernel() {} }; diff --git a/svcurve.h b/svcurve.h index 674aa62..36846fa 100644 --- a/svcurve.h +++ b/svcurve.h @@ -1,7 +1,11 @@ #ifndef SVCURVE_H #define SVCURVE_H +#include + +#include "svdefs.h" #include "svpoint.h" +#include "svsimplepoint.h" class SvPoint; @@ -11,6 +15,9 @@ protected: SvPoint *m_ends; SvPoint *m_points; SvSimplePoint *m_keyPoints; + QColor m_leftColor; + QColor m_rightColor; + public: SvCurve(); diff --git a/svdefs.h b/svdefs.h new file mode 100644 index 0000000..ed4b8d0 --- /dev/null +++ b/svdefs.h @@ -0,0 +1,12 @@ +#ifndef SVDEFS_H +#define SVDEFS_H + +#include + +typedef unsigned int SvFrameId; + +#define sign(a) (a == 0 ? 0 : (a > 0 ? 1 : -1)) +#define max(a,b,c) (a > b ? (a > c ? a : c) : (b > c ? b : c)) +#define min(a,b,c) (a < b ? (a < c ? a : c) : (b < c ? b : c)) + +#endif // SVDEFS_H diff --git a/svimage.h b/svimage.h index 5986ff4..f029949 100644 --- a/svimage.h +++ b/svimage.h @@ -2,8 +2,8 @@ #define SVIMAGE_H #include -#include #include +#include class SvImage { diff --git a/svkernelv2.cpp b/svkernelv2.cpp index c3616e7..be2a98f 100644 --- a/svkernelv2.cpp +++ b/svkernelv2.cpp @@ -23,7 +23,7 @@ int SvKernelV2::rgbDiff(QRgb left, QRgb right) int minDiff= leftDiff < rightDiff ? rightDiff : leftDiff; - int diff = cLeft.value() - cRight.value(); + int diff = qGray(left) - qGray(right); int hsvDiff = minDiff > 70 ? abs(cLeft.hsvHue() - cRight.hsvHue()) : 0; if (hsvDiff > 180) { hsvDiff = 360 - hsvDiff; @@ -32,10 +32,10 @@ int SvKernelV2::rgbDiff(QRgb left, QRgb right) int dsign = sign(diff); dsign = dsign == 0 ? 1 : dsign; - return /*dsign * ((rightDiff - leftDiff)>>2) + */diff /*+ (hsvDiff > 5 ? dsign * 10 : 0)*/; + return dsign * (abs(rightDiff - leftDiff)>>2) + diff /*+ (hsvDiff > 5 ? dsign * 10 : 0)*/; } -void SvKernelV2::exec(int line) +void SvKernelV2::exec(SvImage *image, int line) { QRgb vtop, top, right, bottom, vbottom, _xy, xy; @@ -44,7 +44,7 @@ void SvKernelV2::exec(int line) int value; - for (int x = 0; x < m_result->getWidth(); x++) { + for (int x = 0; x < image->getWidth(); x++) { value = 0; vtop = m_left->getPixelRGB(x - 1, line - 3); @@ -65,6 +65,7 @@ void SvKernelV2::exec(int line) if (dX == _dX) { if (dX > dX_ && _dX > __dX || dX < dX_ && _dX < __dX) { + value += abs(dX); } } else { @@ -87,6 +88,7 @@ void SvKernelV2::exec(int line) } if (value > 0) { + m_pointCloud->addPoint(); m_result->putGrayPixel(x - 1, line - 1, value); } diff --git a/svkernelv2.h b/svkernelv2.h index 9bf76e8..8393ad8 100644 --- a/svkernelv2.h +++ b/svkernelv2.h @@ -1,18 +1,15 @@ #ifndef SVKERNELV2_H #define SVKERNELV2_H +#include "svdefs.h" #include "svabstractkernel.h" -#include -#define sign(a) (a == 0 ? 0 : (a > 0 ? 1 : -1)) -#define max(a,b,c) (a > b ? (a > c ? a : c) : (b > c ? b : c)) -#define min(a,b,c) (a < b ? (a < c ? a : c) : (b < c ? b : c)) class SvKernelV2: public SvAbstractKernel { public: SvKernelV2(); virtual ~SvKernelV2(); - void exec(int line) override; + void exec(SvImage *image, int line) override; int rgbDiff(QRgb left, QRgb right); }; diff --git a/svpoint.h b/svpoint.h index 0f1548a..d2678b0 100644 --- a/svpoint.h +++ b/svpoint.h @@ -2,7 +2,7 @@ #define SVPOINT_H #include "svsimplepoint.h" -#include "svcurve.h" +#include "svdefs.h" class SvCurve; diff --git a/svpointcloud.cpp b/svpointcloud.cpp new file mode 100644 index 0000000..449cea1 --- /dev/null +++ b/svpointcloud.cpp @@ -0,0 +1,10 @@ +#include "svpointcloud.h" + +SvPointCloud::SvPointCloud() +{ +} + +void SvPointCloud::addPoint(SvFrameId frame, SvPoint point) +{ + +} diff --git a/svpointcloud.h b/svpointcloud.h new file mode 100644 index 0000000..a3b3e64 --- /dev/null +++ b/svpointcloud.h @@ -0,0 +1,15 @@ +#ifndef SVPOINTCLOUD_H +#define SVPOINTCLOUD_H + +#include "svdefs.h" +#include "svpoint.h" + +class SvPointCloud +{ +public: + SvPointCloud(); + + void addPoint(SvFrameId frame, SvPoint point); +}; + +#endif // SVPOINTCLOUD_H diff --git a/svprocessor.cpp b/svprocessor.cpp index 9faecf8..6baaca5 100644 --- a/svprocessor.cpp +++ b/svprocessor.cpp @@ -6,7 +6,7 @@ SvProcessor::SvProcessor(QObject *parent): } -SvProcessor::SvProcessor(SvImage* left, SvImage* right, SvImage* result, int numberOfWorkers, int version) +SvProcessor::SvProcessor(SvPointCloud* pointCloud, int numberOfWorkers, int version) { unsigned int i; @@ -16,10 +16,7 @@ SvProcessor::SvProcessor(SvImage* left, SvImage* right, SvImage* result, int num m_kernel = new SvKernelV2(); } - m_kernel->setLeftImage(left); - m_kernel->setRightImage(right); - m_kernel->setResultImage(result); - + m_kernel->setPointCloud(pointCloud); m_numberOfWorkers = numberOfWorkers; m_workers = new SvWorker[m_numberOfWorkers]; m_threads = new QThread[m_numberOfWorkers]; @@ -32,17 +29,40 @@ SvProcessor::SvProcessor(SvImage* left, SvImage* right, SvImage* result, int num connect(&m_threads[i], &QThread::started, &m_workers[i], &SvWorker::start); connect(&m_workers[i], &SvWorker::finished, this, &SvProcessor::workerFinished); } - - for (i = 0; i < result->getHeight(); i++) { - m_workers[i % m_numberOfWorkers].addTask(i); - } } + SvProcessor::~SvProcessor() { delete m_kernel; delete[] m_workers; } +void SvProcessor::enqueueImage(SvFrameId frame, SvImage *image) +{ + SvProcessorTask *task; + + for (int i = 0; i < image->getHeight(); i++) { + task = new SvProcessorTask; + task->image = image; + task->line = i; + m_queue.enqueue(task); + } +} + +SvProcessorTask SvProcessor::nextTask() +{ + SvProcessorTask task; + SvProcessorTask *taskPtr; + + m_nextTaskMutex.lock(); + taskPtr = m_queue.dequeue(); + m_nextTaskMutex.unlock(); + task = *taskPtr; + delete taskPtr; + + return task; +} + void SvProcessor::workerFinished(int workerId) { qDebug() << "worker " << workerId << " finished"; diff --git a/svprocessor.h b/svprocessor.h index cd97560..e65eeda 100644 --- a/svprocessor.h +++ b/svprocessor.h @@ -3,15 +3,25 @@ #include -#include -#include -#include +#include +#include +#include +#include +#include #include "svimage.h" #include "svworker.h" #include "svabstractkernel.h" #include "svkernelv1.h" #include "svkernelv2.h" +#include "svpointcloud.h" + + +typedef struct { + SvImage *image; + int line; +} SvProcessorTask; + class SvProcessor: public QObject { @@ -24,12 +34,18 @@ protected: unsigned int m_numberOfWorkers; unsigned int m_workersFinished; unsigned int m_startTime; + QQueue m_queue; + QMutex m_nextTaskMutex; public: explicit SvProcessor(QObject *parent = 0); - SvProcessor(SvImage *left, SvImage *right, SvImage *result, int numberOfWorkers = 1, int version = 1); + SvProcessor(SvPointCloud *pointCloud, int numberOfWorkers = 1, int version = 1); ~SvProcessor(); void execute(); + void enqueueImage(SvFrameId frame, SvImage *image); + +protected: + SvProcessorTask nextTask(); protected slots: void workerFinished(int workerId); diff --git a/svworker.cpp b/svworker.cpp index 1e3f0b0..94dff88 100644 --- a/svworker.cpp +++ b/svworker.cpp @@ -6,8 +6,9 @@ SvWorker::SvWorker(QObject *parent) : } -SvWorker::SvWorker(SvAbstractKernel* kernel) +SvWorker::SvWorker(SvProcessorTask *processor, SvAbstractKernel *kernel) { + m_processor = processor; m_kernel = kernel; } @@ -18,8 +19,16 @@ void SvWorker::stop() void SvWorker::start() { - for (int i: m_tasks) { - m_kernel->exec(i); + SvProcessorTask task; + + while(true) { + try { + task = m_processor->nextTask(); + } catch (SvNoMoreTasks ex) { + break; + } + + m_kernel->exec(task.image, task.line); } emit finished(m_id); diff --git a/svworker.h b/svworker.h index 87a10d8..7c710c8 100644 --- a/svworker.h +++ b/svworker.h @@ -5,6 +5,7 @@ #include #include "svabstractkernel.h" +#include "svprocessor.h" #include "svimage.h" class SvWorker : public QObject @@ -13,15 +14,14 @@ class SvWorker : public QObject protected: int m_id; - SvAbstractKernel* m_kernel; - QList m_tasks; + SvAbstractKernel *m_kernel; + SvProcessor *m_processor; public: explicit SvWorker(QObject *parent = 0); - SvWorker(SvAbstractKernel* kernel); + SvWorker(SvProcessorTask *processor, SvAbstractKernel *kernel); void setId(int id) {m_id = id;} - void addTask(int line) {m_tasks << line;} void setKernel(SvAbstractKernel* kernel) {m_kernel = kernel;} signals: @@ -30,7 +30,6 @@ signals: public slots: void stop(); void start(); - }; #endif // SVWORKER_H