151 lines
3.9 KiB
C++
151 lines
3.9 KiB
C++
#include "imagepipeline.h"
|
|
#include <uvosunwrap/unwrap.h>
|
|
#include <uvosunwrap/normalize.h>
|
|
#include <uvosunwrap/curve.h>
|
|
#include <QtConcurrent/QtConcurrentRun>
|
|
#include <QFuture>
|
|
#include <QDebug>
|
|
|
|
ImagePipeline::ImagePipeline(Cameras* cameras, QObject *parent): QObject(parent), cameras_(cameras)
|
|
{
|
|
connect(cameras_, &Cameras::newImages, this, &ImagePipeline::apply);
|
|
}
|
|
|
|
cv::Mat ImagePipeline::process(const Profile profile, std::vector<Camera::Image> images)
|
|
{
|
|
qDebug()<<__FUNCTION__<<"got"<<images.size()<<"images";
|
|
std::vector<RemapedImage> remapedImages;
|
|
remapedImages.reserve(images.size());
|
|
|
|
try
|
|
{
|
|
if(profile.nodistort && images.size() > 0)
|
|
{
|
|
for(auto& image : images)
|
|
{
|
|
if(profile.cameras[0].id == image.cameraId)
|
|
{
|
|
RemapedImage img;
|
|
img.origin.x = 0;
|
|
img.origin.y = 0;
|
|
image.mat.copyTo(img.image);
|
|
if(profile.cameras[0].darkmap.data)
|
|
img.image = img.image - profile.cameras[0].darkmap;
|
|
remapedImages.push_back(img);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
for(Camera::Image& image : images)
|
|
{
|
|
qDebug()<<__FUNCTION__<<"image cam id"<<image.cameraId;
|
|
bool matched = false;
|
|
for(auto& camera : profile.cameras)
|
|
{
|
|
if(camera.id == image.cameraId)
|
|
{
|
|
if(camera.darkmap.data)
|
|
{
|
|
cv::Mat subtracted;
|
|
image.mat.copyTo(subtracted);
|
|
qDebug()<<"Camera"<<camera.id<<"has darkmap"<<image.mat.type()<<camera.darkmap.type();
|
|
subtracted = image.mat - camera.darkmap;
|
|
image.mat = subtracted;
|
|
}
|
|
RemapedImage remaped = applyRemap(image.mat, camera.remapMap);
|
|
qDebug()<<"Camera"<<camera.id<<"image remaped to"<<remaped.image.data<<remaped.image.rows<<remaped.image.cols
|
|
<<"at"<<remaped.origin.x<<'x'<<remaped.origin.y;
|
|
remapedImages.push_back(applyRemap(image.mat, camera.remapMap));
|
|
matched = true;
|
|
break;
|
|
}
|
|
}
|
|
if(!matched)
|
|
qWarning()<<"non matching image recived";
|
|
}
|
|
}
|
|
}
|
|
catch(cv::Exception ex)
|
|
{
|
|
qDebug()<<ex.err.c_str();
|
|
qDebug()<<"Image pipe failure";
|
|
return cv::Mat();
|
|
}
|
|
|
|
if(remapedImages.size() > 0)
|
|
{
|
|
cv::Mat output = simpleStich(remapedImages);
|
|
output.convertTo(output, CV_32FC1, 1.0/255.0, 0);
|
|
|
|
if(profile.lightmap.data)
|
|
normalize(output, profile.lightmap);
|
|
|
|
if(profile.calcurve.data)
|
|
applyCurve(output, profile.calcurve);
|
|
return output;
|
|
}
|
|
else
|
|
{
|
|
qDebug()<<"Image pipe failure insufficant matched images";
|
|
return cv::Mat();
|
|
}
|
|
}
|
|
|
|
void ImagePipeline::apply(std::vector<Camera::Image> images)
|
|
{
|
|
if(!invalid_)
|
|
{
|
|
futureImageWatchers_.push_back(new QFutureWatcher<cv::Mat>());
|
|
connect(futureImageWatchers_.back(), &QFutureWatcher<cv::Mat>::finished, this, &ImagePipeline::imageFinished);
|
|
|
|
statusMsg("Processing");
|
|
QFuture<cv::Mat> futureImage = QtConcurrent::run(&ImagePipeline::process, profile_, images);
|
|
futureImageWatchers_.back()->setFuture(futureImage);
|
|
}
|
|
}
|
|
|
|
void ImagePipeline::setProfile(const Profile& profile)
|
|
{
|
|
profile_ = profile;
|
|
cameras_->setExposureTime(profile_.exposureTime);
|
|
cameras_->setLighting(profile_.lighting);
|
|
qDebug()<<"setting profile "<<profile_.getName();
|
|
for(CameraSetup& setup : profile_.cameras)
|
|
{
|
|
qDebug()<<setup.id;
|
|
//TODO: dehardcode this
|
|
setup.remapMap.outputCellSize=200;
|
|
}
|
|
invalid_ = false;
|
|
if(!profile_.camerasSufficant(cameras_->getCameras()))
|
|
{
|
|
invalid_ = true;
|
|
sigInvalidProfile("A camera required for this profile is not available");
|
|
}
|
|
statusMsg("set profile " + profile_.getName());
|
|
}
|
|
|
|
void ImagePipeline::imageFinished()
|
|
{
|
|
for(size_t i = 0; i < futureImageWatchers_.size(); ++i)
|
|
{
|
|
if(futureImageWatchers_[i]->isFinished())
|
|
{
|
|
cv::Mat result = futureImageWatchers_[i]->result();
|
|
if(result.data)
|
|
{
|
|
sigResult(Camera::Image(result, 0));
|
|
delete futureImageWatchers_[i];
|
|
futureImageWatchers_.erase(futureImageWatchers_.begin()+i);
|
|
i--;
|
|
statusMsg("Finished");
|
|
}
|
|
else
|
|
{
|
|
sigInvalidProfile("Image pipe failure");
|
|
}
|
|
}
|
|
}
|
|
}
|