Skip to content

Instantly share code, notes, and snippets.

@hecomi
Created April 2, 2014 16:10
Show Gist options
  • Save hecomi/9937283 to your computer and use it in GitHub Desktop.
Save hecomi/9937283 to your computer and use it in GitHub Desktop.
#include "ovrvision.h"
#include <opencv2/opencv.hpp>
#include <QImage>
#include <QPainter>
/* --------------------------------------------------------------------------------
* OVRVisionItem
* -------------------------------------------------------------------------------- */
std::shared_ptr<OVR::Ovrvision> OVRVision::instance;
const std::shared_ptr<OVR::Ovrvision>& OVRVision::getInstance()
{
if (!instance) {
instance = std::shared_ptr<OVR::Ovrvision>(new OVR::Ovrvision(), [](OVR::Ovrvision* ptr) {
ptr->Close();
});
instance->Open(0, OVR::OV_CAMVGA_FULL);
}
return instance;
}
bool OVRVision::isReady()
{
return getInstance();
}
void OVRVision::getLeftImage(unsigned char* dest)
{
getInstance()->GetCamImage(dest, OVR::OV_CAMEYE_LEFT);
}
void OVRVision::getRightImage(unsigned char* dest)
{
getInstance()->GetCamImage(dest, OVR::OV_CAMEYE_RIGHT);
}
/* --------------------------------------------------------------------------------
* OVRVisionItem
* -------------------------------------------------------------------------------- */
namespace {
int OVR_CAM_WIDTH = 640;
int OVR_CAM_HEIGHT = 480;
}
OVRVisionItem::OVRVisionItem(QQuickItem *parent) :
QQuickPaintedItem(parent), isCanny_(false), camera_(Left),
cannyThreshold1_(50.0), cannyThreshold2_(200.0)
{
}
void OVRVisionItem::paint(QPainter *painter)
{
if (thread_.joinable()) {
thread_.join();
}
thread_ = std::thread([this] {
cv::Mat ovrImg(OVR_CAM_HEIGHT, OVR_CAM_WIDTH, CV_8UC3);
if (OVRVision::isReady()) {
switch (camera_) {
case Left: OVRVision::getLeftImage(ovrImg.data); break;
case Right: OVRVision::getRightImage(ovrImg.data); break;
}
if (isCanny_) {
cv::Mat cannied, blurred;
cv::Canny(ovrImg, cannied, cannyThreshold1_, cannyThreshold2_);
cv::bitwise_not(cannied, cannied);
cv::cvtColor(cannied, cannied, CV_GRAY2BGR);
cv::GaussianBlur(cannied, cannied, cv::Size(), 0.5);
uchar lut[256];
for (int i = 0; i < 256; ++i) {
lut[i] = static_cast<int>(pow(i / 255.0, 100) * 255);
}
cv::Mat lutMat(1, 256, CV_8UC1, lut);
cv::LUT(cannied, lutMat, cannied);
cv::medianBlur(ovrImg, blurred, 21);
cv::bitwise_and(cannied, blurred, ovrImg);
cv::cvtColor(ovrImg, ovrImg, CV_BGR2RGBA);
/*
cv::Mat grayImg, thresholdImg, adaptiveThresholdImg, andImg;
cv::cvtColor(ovrImg, grayImg, CV_RGB2GRAY);
cv::GaussianBlur(grayImg, grayImg, cv::Size(), 5);
cv::threshold(grayImg, thresholdImg, 90, 255, CV_THRESH_BINARY);
cv::adaptiveThreshold(grayImg, adaptiveThresholdImg, 255, CV_ADAPTIVE_THRESH_MEAN_C, CV_THRESH_BINARY, 11, 10);
cv::bitwise_and(thresholdImg, adaptiveThresholdImg, andImg);
cv::cvtColor(andImg, andImg, CV_GRAY2BGR);
cv::medianBlur(ovrImg, ovrImg, 15);
cv::bitwise_and(andImg, ovrImg, ovrImg);
cv::cvtColor(ovrImg, ovrImg, CV_BGR2RGBA);
*/
} else {
cv::cvtColor(ovrImg, ovrImg, CV_BGR2RGBA);
}
}
img_ = ovrImg;
});
if (img_.empty()) return;
cv::Mat ovrScaledImg(height(), width(), img_.type());
cv::resize(img_, ovrScaledImg, ovrScaledImg.size(), cv::INTER_CUBIC);
QImage qmlImg(ovrScaledImg.data, ovrScaledImg.cols, ovrScaledImg.rows, QImage::Format_ARGB32);
// qmlImg = qmlImg.scaled(width(), height()); // <-- slow
painter->drawImage(width()/2 - qmlImg.size().width()/2,
height()/2 - qmlImg.size().height()/2,
qmlImg);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment