OpenGL PointCloudViewer added

This commit is contained in:
Andrey Tkachenko 2014-07-25 11:32:00 +04:00
parent 8bfc0687d8
commit a7c102324c
22 changed files with 313 additions and 258 deletions

15
CSV.pro
View File

@ -16,23 +16,21 @@ SOURCES += main.cpp \
svimage.cpp \
svworker.cpp \
svprocessor.cpp \
svkernelv1.cpp \
svkernelv2.cpp \
svimageprovider.cpp \
svpoint.cpp \
svcurve.cpp \
svsimplepoint.cpp \
svfigure.cpp \
svobject.cpp \
svpointcloud.cpp
svpointcloud.cpp \
svkernel.cpp \
svpointcloudviewer.cpp \
svpointcloudrenderer.cpp
HEADERS += \
svimage.h \
svworker.h \
svprocessor.h \
svabstractkernel.h \
svkernelv1.h \
svkernelv2.h \
svimageprovider.h \
svpoint.h \
svcurve.h \
@ -40,7 +38,10 @@ HEADERS += \
svfigure.h \
svobject.h \
svpointcloud.h \
svdefs.h
svdefs.h \
svkernel.h \
svpointcloudviewer.h \
svpointcloudrenderer.h
RESOURCES += \
resource.qrc

View File

@ -1,6 +1,7 @@
import QtQuick 2.2
import QtQuick.Window 2.1
import QtQuick.Controls 1.1
import SvPCV 1.0
ApplicationWindow {
id: mainWindow
@ -147,29 +148,11 @@ ApplicationWindow {
anchors.fill: parent
Tab {
id: imageView
title: "Image View"
title: "PointCloud View"
Item {
Flickable {
anchors.fill: parent
SvPointCloudViewer {
anchors.fill: parent
contentWidth: image.width
contentHeight: image.height
interactive: true
anchors.margins: 2
clip: true
Item {
Image {
id: image
source: ""
smooth: false
}
Connections {
target: processor
onFinished: {
image.source = "image://images/result";
}
}
}
}
}
}

View File

@ -7,33 +7,38 @@
#include "svimage.h"
#include "svprocessor.h"
#include "svimageprovider.h"
#include "svpointcloudviewer.h"
int main(int argc, char *argv[])
{
QGuiApplication a(argc, argv);
qmlRegisterType<SvPointCloudViewer>("SvPCV", 1, 0, "SvPointCloudViewer");
QQmlApplicationEngine engine;
SvImageProvider imageProvider;
QImage imgLeft("../CSV/img/right9.png");
QImage imgRight("../CSV/img/right1.png");
QImage imgStereo(imgLeft.width(), imgLeft.height(), QImage::Format_RGB32);
SvImage left(imgLeft);
SvImage right(imgRight);
SvImage stereo(imgStereo);
SvProcessor proc(&left, &right, &stereo, 4, 2);
SvPointCloud pointCloud(imgLeft.width(), imgLeft.height());
SvProcessor proc(4);
proc.enqueueImage(&pointCloud, &left);
imageProvider.addImage("left", &left);
imageProvider.addImage("right", &right);
imageProvider.addImage("result", &stereo);
engine.addImageProvider("images", &imageProvider);
engine.load(QUrl(QStringLiteral("qrc:///Main.qml")));
engine.rootContext()->setContextProperty("processor", &proc);
engine.load(QUrl(QStringLiteral("qrc:///Main.qml")));
proc.start();
return a.exec();
}

View File

@ -1,21 +0,0 @@
#ifndef SVABSTRACTKERNEL_H
#define SVABSTRACTKERNEL_H
#include "svimage.h"
#include "svpointcloud.h"
class SvAbstractKernel
{
protected:
SvImage* m_image;
SvPointCloud* m_pointCloud;
public:
void setImage(SvImage* image) {m_image = image;}
void setPointCloud(SvPointCloud* pointCloud) {m_pointCloud = pointCloud;}
virtual void exec(int line) = 0;
virtual ~SvAbstractKernel() {}
};
#endif // SVABSTRACTKERNEL_H

View File

@ -4,6 +4,18 @@
#include <QDebug>
typedef unsigned int SvFrameId;
typedef unsigned int uint;
#include "svpointcloud.h"
#include "svimage.h"
class SvPointCloud;
typedef struct {
SvPointCloud *pointCloud;
SvImage *image;
int line;
} SvProcessorTask;
#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))

View File

@ -7,7 +7,7 @@
class SvFigure
{
protected:
SvPoint ends[2];
SvPoint *ends;
public:

View File

@ -1,16 +1,16 @@
#include "svkernelv2.h"
#include "svkernel.h"
SvKernelV2::SvKernelV2()
SvKernel::SvKernel()
{
}
SvKernelV2::~SvKernelV2()
SvKernel::~SvKernel()
{
}
int SvKernelV2::rgbDiff(QRgb left, QRgb right)
int SvKernel::rgbDiff(QRgb left, QRgb right)
{
QColor cLeft(left);
QColor cRight(right);
@ -35,7 +35,7 @@ int SvKernelV2::rgbDiff(QRgb left, QRgb right)
return dsign * (abs(rightDiff - leftDiff)>>2) + diff /*+ (hsvDiff > 5 ? dsign * 10 : 0)*/;
}
void SvKernelV2::exec(SvImage *image, int line)
void SvKernel::exec(SvPointCloud *pcl, SvImage *image, int line)
{
QRgb vtop, top, right, bottom, vbottom, _xy, xy;
@ -47,12 +47,12 @@ void SvKernelV2::exec(SvImage *image, int line)
for (int x = 0; x < image->getWidth(); x++) {
value = 0;
vtop = m_left->getPixelRGB(x - 1, line - 3);
top = m_left->getPixelRGB(x - 1, line - 2);
right = m_left->getPixelRGB(x, line - 1);
bottom = m_left->getPixelRGB(x - 1, line);
vbottom = m_left->getPixelRGB(x - 1, line + 1);
xy = m_left->getPixelRGB(x - 1, line - 1);
vtop = image->getPixelRGB(x - 1, line - 3);
top = image->getPixelRGB(x - 1, line - 2);
right = image->getPixelRGB(x, line - 1);
bottom = image->getPixelRGB(x - 1, line);
vbottom = image->getPixelRGB(x - 1, line + 1);
xy = image->getPixelRGB(x - 1, line - 1);
dX_ = rgbDiff(_xy, right);
dY_ = rgbDiff(vtop, top);
@ -88,8 +88,8 @@ void SvKernelV2::exec(SvImage *image, int line)
}
if (value > 0) {
m_pointCloud->addPoint();
m_result->putGrayPixel(x - 1, line - 1, value);
//m_result->putGrayPixel(x - 1, line - 1, value);
}
__dX = _dX;

17
svkernel.h Normal file
View File

@ -0,0 +1,17 @@
#ifndef SVKERNELV2_H
#define SVKERNELV2_H
#include "svdefs.h"
#include "svimage.h"
#include "svpointcloud.h"
class SvKernel
{
public:
SvKernel();
virtual ~SvKernel();
void exec(SvPointCloud *pcl, SvImage *image, int line);
int rgbDiff(QRgb left, QRgb right);
};
#endif // SVKERNELV2_H

View File

@ -1,105 +0,0 @@
#include "svkernelv1.h"
SvKernelV1::SvKernelV1()
{
}
SvKernelV1::~SvKernelV1()
{
}
int SvKernelV1::getPixelColor(int cursor)
{
if (cursor == 0) {
return 0;
}
return ((10 * m_left->getWidth()) / (2.0 * 0.9 * cursor ));
}
int SvKernelV1::diff(int lx, int ly, int rx, int ry)
{
int Red, Green, Blue, color;
int Lvalue = m_left->getPixelValue(lx, ly);
int Rvalue = m_right->getPixelValue(rx, ry);
int dLvalue = m_left->getPixelValue(rx - 1, ry) - Lvalue;
int dRvalue = m_right->getPixelValue(rx - 1, ry) - Rvalue;
int dLsign = dLvalue >= 0 ? 1 : -1;
int dRsign = dRvalue >= 0 ? 1 : -1;
Red = abs(m_left->getPixel(lx, ly, 0) - m_right->getPixel(rx, ry, 0));
Green = abs(m_left->getPixel(lx, ly, 1) - m_right->getPixel(rx, ry, 1));
Blue = abs(m_left->getPixel(lx, ly, 2) - m_right->getPixel(rx, ry, 2));
color = abs(Green - Red) + abs(Blue - Green);
return ((Green + Red + Blue) +
color << 2) + (dLsign == dRsign ? 0 : 100);
}
int SvKernelV1::match(int x, int y, int j)
{
int error = 0;
int ms = 5;
int c = diff(x, y, x + j, y),
l = 0, r = 0, t = 0, b = 0;
error = c;
for (int i = 1; i <= ms; i++) {
l += diff(x - i, y, x + j - i, y);
r += diff(x + i, y, x + j + i, y);
t += diff(x, y - i, x + j, y - i);
b += diff(x, y + i, x + j, y + i);
}
error += l > r ? r : l;
error += t > b ? b : t;
//error += l + r + b + t;
return error;
}
void SvKernelV1::exec(int line)
{
unsigned int x;
int cursor, closest, tmp;
int minErrorValue, tmpSmoothed, matched;
int dist;
cursor = 0;
for (x = 0; x < m_result->getWidth(); x+=1) {
closest = -1; minErrorValue = -1;matched=0;
minErrorValue=-1;
for (int i = 1; i < m_windowSize; i++) {
dist = (abs(cursor - i));
tmp = match(x, line, i);
tmpSmoothed = tmp;
if (tmpSmoothed < minErrorValue || minErrorValue == -1) {
minErrorValue = tmpSmoothed;
closest = i;
matched = 1;
} else if (tmpSmoothed == minErrorValue) {
if (abs(cursor - i) < abs(cursor - closest)) {
closest = i;
matched++;
}
}
}
if (matched == 1) {
cursor = closest;
}
m_result->putGrayPixel(x/* + cursor*/, line, getPixelColor(cursor));
}
}

View File

@ -1,23 +0,0 @@
#ifndef SVKERNELV1_H
#define SVKERNELV1_H
#include "svabstractkernel.h"
class SvKernelV1: public SvAbstractKernel
{
protected:
int m_windowSize = 160;
public:
void exec(int line) override;
SvKernelV1();
virtual ~SvKernelV1();
protected:
int getPixelColor(int cursor);
int diff(int lx, int ly, int rx, int ry);
int match(int x, int y, int i);
};
#endif // SVKERNELV1_H

View File

@ -1,16 +0,0 @@
#ifndef SVKERNELV2_H
#define SVKERNELV2_H
#include "svdefs.h"
#include "svabstractkernel.h"
class SvKernelV2: public SvAbstractKernel
{
public:
SvKernelV2();
virtual ~SvKernelV2();
void exec(SvImage *image, int line) override;
int rgbDiff(QRgb left, QRgb right);
};
#endif // SVKERNELV2_H

View File

@ -3,6 +3,9 @@
#include "svfigure.h"
class SvFigure;
class SvObject
{
protected:

View File

@ -1,10 +1,27 @@
#include "svpointcloud.h"
SvPointCloud::SvPointCloud()
SvPointCloud::SvPointCloud(uint width, uint height)
{
m_pointFiledHeight = height;
m_pointFiledWidth = width;
m_pointField = new SvPoint* [height];
for (uint y = 0; y < height; y++) {
m_pointField[y] = new SvPoint[width];
}
}
SvPointCloud::~SvPointCloud()
{
for (uint y = 0; y < m_pointFiledHeight; y++) {
delete[] m_pointField[y];
}
delete[] m_pointField;
}
void SvPointCloud::addPoint(SvFrameId frame, SvPoint point)
{
m_pointField[point.y()][point.x()] = point;
}

View File

@ -3,13 +3,30 @@
#include "svdefs.h"
#include "svpoint.h"
#include "svcurve.h"
#include "svfigure.h"
#include "svobject.h"
class SvCurve;
class SvFigure;
class SvObject;
class SvPointCloud
{
protected:
uint m_pointFiledHeight;
uint m_pointFiledWidth;
SvPoint** m_pointField;
QList<SvCurve*> m_curves;
QList<SvFigure*> m_figures;
QList<SvObject*> m_objects;
public:
SvPointCloud();
SvPointCloud(uint width, uint height);
~SvPointCloud();
void addPoint(SvFrameId frame, SvPoint point);
};
#endif // SVPOINTCLOUD_H

61
svpointcloudrenderer.cpp Normal file
View File

@ -0,0 +1,61 @@
#include "svpointcloudrenderer.h"
SvPointCloudRenderer::SvPointCloudRenderer(QObject *parent) :
QObject(parent)
{
}
void SvPointCloudRenderer::paint()
{
if (!m_program) {
m_program = new QOpenGLShaderProgram();
m_program->addShaderFromSourceCode(QOpenGLShader::Vertex,
"attribute highp vec4 vertices;"
"varying highp vec2 coords;"
"void main() {"
" gl_Position = vertices;"
" coords = vertices.xy;"
"}");
m_program->addShaderFromSourceCode(QOpenGLShader::Fragment,
"uniform lowp float t;"
"varying highp vec2 coords;"
"void main() {"
" lowp float i = 1. - (pow(abs(coords.x), 4.) + pow(abs(coords.y), 4.));"
" i = smoothstep(t - 0.8, t + 0.8, i);"
" i = floor(i * 20.) / 20.;"
" gl_FragColor = vec4(coords * .5 + .5, i, i);"
"}");
m_program->bindAttributeLocation("vertices", 0);
m_program->link();
}
m_program->bind();
m_program->enableAttributeArray(0);
float values[] = {
-1, -1,
1, -1,
-1, 1,
1, 1
};
m_program->setAttributeArray(0, GL_FLOAT, values, 2);
m_program->setUniformValue("t", (float) m_t);
glViewport(0, 0, m_viewportSize.width(), m_viewportSize.height());
glDisable(GL_DEPTH_TEST);
glClearColor(0, 0, 0, 1);
glClear(GL_COLOR_BUFFER_BIT);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
m_program->disableAttributeArray(0);
m_program->release();
}

24
svpointcloudrenderer.h Normal file
View File

@ -0,0 +1,24 @@
#ifndef SVPOINTCLOUDRENDERER_H
#define SVPOINTCLOUDRENDERER_H
#include <QObject>
#include <QtGui/QOpenGLShaderProgram>
#include <QtGui/QOpenGLContext>
class SvPointCloudRenderer : public QObject
{
Q_OBJECT
public:
explicit SvPointCloudRenderer(QObject *parent = 0);
void setViewportSize(const QSize &size) { m_viewportSize = size; }
public slots:
void paint();
private:
QSize m_viewportSize;
qreal m_t;
QOpenGLShaderProgram *m_program;
};
#endif // SVPOINTCLOUDRENDERER_H

37
svpointcloudviewer.cpp Normal file
View File

@ -0,0 +1,37 @@
#include "svpointcloudviewer.h"
SvPointCloudViewer::SvPointCloudViewer(QQuickItem *parent) :
QQuickItem(parent)
{
}
void SvPointCloudViewer::handleWindowChanged(QQuickWindow *win)
{
if (win) {
connect(win, &QQuickWindow::beforeSynchronizing,
this, &SvPointCloudViewer::sync, Qt::DirectConnection);
connect(win, &QQuickWindow::sceneGraphInvalidated,
this, &SvPointCloudViewer::cleanup, Qt::DirectConnection);
}
}
void SvPointCloudViewer::sync()
{
if (!m_pointCloudRenderer) {
m_pointCloudRenderer = new SvPointCloudRenderer();
connect(window(), &QQuickWindow::beforeRendering,
m_pointCloudRenderer, &SvPointCloudRenderer::paint, Qt::DirectConnection);
}
m_pointCloudRenderer->setViewportSize(window()->size() * window()->devicePixelRatio());
}
void SvPointCloudViewer::cleanup()
{
if (m_pointCloudRenderer) {
delete m_pointCloudRenderer;
m_pointCloudRenderer = 0;
}
}

44
svpointcloudviewer.h Normal file
View File

@ -0,0 +1,44 @@
#ifndef SVPOINTCLOUDVIEWER_H
#define SVPOINTCLOUDVIEWER_H
#include <QQuickItem>
#include <QQuickWindow>
#include "svpointcloud.h"
#include "svpointcloudrenderer.h"
class SvPointCloudViewer : public QQuickItem
{
Q_OBJECT
// Q_PROPERTY(qreal cameraX READ cameraX WRITE setCameraX NOTIFY cameraXChanged)
// Q_PROPERTY(qreal cameraY READ cameraY WRITE setCameraY NOTIFY cameraYChanged)
// Q_PROPERTY(qreal cameraZ READ cameraZ WRITE setCameraZ NOTIFY cameraZChanged)
protected:
SvPointCloud *pointCloud;
SvPointCloudRenderer *m_pointCloudRenderer;
qreal m_cameraX;
qreal m_cameraY;
qreal m_cameraZ;
public:
explicit SvPointCloudViewer(QQuickItem *parent = 0);
signals:
void cameraXChanged();
void cameraYChanged();
void cameraZChanged();
private slots:
void handleWindowChanged(QQuickWindow *win);
public slots:
void sync();
void cleanup();
//void resetCamera();
//void moveCamera(qreal X, qreal Y, qreal Z);
// void rotateCamera(qreal X, qreal Y, qreal Z);
};
#endif // SVPOINTCLOUDVIEWER_H

View File

@ -6,17 +6,12 @@ SvProcessor::SvProcessor(QObject *parent):
}
SvProcessor::SvProcessor(SvPointCloud* pointCloud, int numberOfWorkers, int version)
SvProcessor::SvProcessor(int numberOfWorkers)
{
unsigned int i;
uint i;
if (version == 1) {
m_kernel = new SvKernelV1();
} else if (version == 2) {
m_kernel = new SvKernelV2();
}
m_kernel = new SvKernel();
m_kernel->setPointCloud(pointCloud);
m_numberOfWorkers = numberOfWorkers;
m_workers = new SvWorker[m_numberOfWorkers];
m_threads = new QThread[m_numberOfWorkers];
@ -37,26 +32,30 @@ SvProcessor::~SvProcessor()
delete[] m_workers;
}
void SvProcessor::enqueueImage(SvFrameId frame, SvImage *image)
void SvProcessor::enqueueImage(SvPointCloud *pointCloud, SvImage *image)
{
SvProcessorTask *task;
for (int i = 0; i < image->getHeight(); i++) {
for (uint i = 0; i < image->getHeight(); i++) {
task = new SvProcessorTask;
task->image = image;
task->line = i;
m_queue.enqueue(task);
task->pointCloud = pointCloud;
m_taskQueue.enqueue(task);
}
}
SvProcessorTask SvProcessor::nextTask()
{
SvProcessorTask task;
SvProcessorTask *taskPtr;
SvProcessorTask task, *taskPtr;
if (!m_taskQueue.size()) {
throw new SvNoMoreTasks();
}
m_nextTaskMutex.lock();
taskPtr = m_queue.dequeue();
taskPtr = m_taskQueue.dequeue();
m_nextTaskMutex.unlock();
task = *taskPtr;
delete taskPtr;
@ -75,13 +74,13 @@ void SvProcessor::workerFinished(int workerId)
void SvProcessor::start()
{
unsigned int i;
uint i;
m_startTime = time(NULL);
m_workersFinished = 0;
for (i = 0; i < m_numberOfWorkers; i++) {
m_threads[i].start(QThread::HighestPriority);
m_threads[i].start(QThread::HighPriority);
}
}

View File

@ -8,43 +8,41 @@
#include <QDebug>
#include <QQueue>
#include <QMutex>
#include <QException>
#include "svimage.h"
#include "svworker.h"
#include "svabstractkernel.h"
#include "svkernelv1.h"
#include "svkernelv2.h"
#include "svkernel.h"
#include "svpointcloud.h"
typedef struct {
SvImage *image;
int line;
} SvProcessorTask;
class SvWorker;
class SvNoMoreTasks: public QException
{
};
class SvProcessor: public QObject
{
Q_OBJECT
protected:
QThread* m_threads;
SvWorker* m_workers;
SvAbstractKernel* m_kernel;
unsigned int m_numberOfWorkers;
unsigned int m_workersFinished;
unsigned int m_startTime;
QQueue<SvProcessorTask*> m_queue;
QMutex m_nextTaskMutex;
QThread* m_threads;
SvWorker* m_workers;
SvKernel* m_kernel;
uint m_numberOfWorkers;
uint m_workersFinished;
uint m_startTime;
QQueue<SvProcessorTask*> m_taskQueue;
QMutex m_nextTaskMutex;
public:
explicit SvProcessor(QObject *parent = 0);
SvProcessor(SvPointCloud *pointCloud, int numberOfWorkers = 1, int version = 1);
SvProcessor(int numberOfWorkers = 1);
~SvProcessor();
void execute();
void enqueueImage(SvFrameId frame, SvImage *image);
protected:
void enqueueImage(SvPointCloud *pointCloud, SvImage *image);
SvProcessorTask nextTask();
protected slots:

View File

@ -6,7 +6,7 @@ SvWorker::SvWorker(QObject *parent) :
}
SvWorker::SvWorker(SvProcessorTask *processor, SvAbstractKernel *kernel)
SvWorker::SvWorker(SvProcessor *processor, SvKernel *kernel)
{
m_processor = processor;
m_kernel = kernel;
@ -28,7 +28,7 @@ void SvWorker::start()
break;
}
m_kernel->exec(task.image, task.line);
m_kernel->exec(task.pointCloud, task.image, task.line);
}
emit finished(m_id);

View File

@ -4,25 +4,27 @@
#include <QObject>
#include <QList>
#include "svabstractkernel.h"
#include "svkernel.h"
#include "svprocessor.h"
#include "svimage.h"
class SvProcessor;
class SvWorker : public QObject
{
Q_OBJECT
protected:
int m_id;
SvAbstractKernel *m_kernel;
SvProcessor *m_processor;
int m_id;
SvKernel *m_kernel;
SvProcessor *m_processor;
public:
explicit SvWorker(QObject *parent = 0);
SvWorker(SvProcessorTask *processor, SvAbstractKernel *kernel);
SvWorker(SvProcessor *processor, SvKernel *kernel);
void setId(int id) {m_id = id;}
void setKernel(SvAbstractKernel* kernel) {m_kernel = kernel;}
void setKernel(SvKernel* kernel) {m_kernel = kernel;}
signals:
void finished(int id);