LookupStitcher.cpp
Go to the documentation of this file.
00001 
00002 #include "omnicamera/LookupStitcher.h"
00003 
00004 #include <stdio.h>
00005 #include <opencv2/core/core.hpp>
00006 #include <opencv2/highgui/highgui.hpp>
00007 #include <libpng/png.h>
00008 
00009 using cv::Mat;
00010 using cv::MatIterator_;
00011 using cv::Scalar;
00012 using cv::Size;
00013 
00014 namespace omnicamera {
00015 
00016 LookupStitcher::LookupStitcher() {
00017 }
00018 LookupStitcher::~LookupStitcher() {
00019 }
00020 
00021 void LookupStitcher::clearLookupTables() {
00022   lutCount = 0;
00023   lutX.clear();
00024   lutY.clear();
00025   lutA.clear();
00026 }
00027 
00028 void LookupStitcher::addLookupTables(Mat xMat, Mat yMat, Mat aMat) {
00029   lutCount++;
00030   lutX.push_back(xMat);
00031   lutY.push_back(yMat);
00032   lutA.push_back(aMat);
00033 }
00034 
00035 void LookupStitcher::addLookupTables(const std::string xPath, const std::string yPath, const std::string aPath) {
00036   Mat xMat = cv::imread(xPath, -1);
00037   Mat yMat = cv::imread(yPath, -1);
00038   Mat aMat = cv::imread(aPath, -1);
00039   // Mat xMat = cv::imread(xPath, 0); seems not to work properly for 16bit images.
00040   addLookupTables(xMat, yMat, aMat);
00041 }
00042 
00043 void LookupStitcher::stitchImage(const cv::Mat &src, cv::Mat &image) {
00044   // Try using at method which may not be the fastest...
00045   int rows = lutX[0].rows;
00046   int cols = lutX[0].cols;
00047   int channels = src.channels();
00048   // Assuming image is uint8 0-255 and alpha uint8 0-255, uint16 may be used for accumulators.
00049   Mat temp(Size(cols, rows), CV_16UC3);
00050   temp.setTo(Scalar(0, 0, 0));
00051   for (int i = 0; i < getLutCount(); i++) {
00052     for (int r = 0; r < rows; r++) {
00053       for (int c = 0; c < cols; c++) {
00054         ushort a = (ushort) lutA[i].at<uchar>(r, c);
00055         if (a == 0) {
00056           continue;
00057         }
00058         ushort x = lutX[i].at<ushort>(r, c);
00059         ushort y = lutY[i].at<ushort>(r, c);
00060         for (int ch = 0; ch < channels; ch++) {
00061           ushort addend = a * (ushort) src.at<cv::Vec3b>(y, x)[ch];
00062           temp.at<cv::Vec3w>(r, c)[ch] = temp.at<cv::Vec3w>(r, c)[ch] + addend;
00063         }
00064       }
00065     }
00066   }
00067   temp.convertTo(image, CV_8UC3, 255.0/65535.0);
00068 }
00069 
00070 bool LookupStitcher::fixBounds(const int width, const int height) {
00071   bool anyFix = false;
00072   if (getLutCount() < 1) {
00073     return anyFix;
00074   }
00075   int rows = lutX[0].rows;
00076   int cols = lutY[0].cols;
00077   for (int i = 0; i < getLutCount(); i++) {
00078     for (int r = 0; r < rows; r++) {
00079       for (int c = 0; c < cols; c++) {
00080         if (lutX[i].at<ushort>(r, c) < 0 || lutY[i].at<ushort>(r, c) < 0
00081             || lutX[i].at<ushort>(r, c) > width - 1 || lutY[i].at<ushort>(r, c) > height - 1) {
00082           anyFix = true;
00083           lutA[i].at<uchar>(r, c) = 0;
00084           lutX[i].at<ushort>(r, c) = 0;
00085           lutY[i].at<ushort>(r, c) = 0;
00086         }
00087       }
00088     }
00089   }
00090   return anyFix;
00091 }
00092 
00093 
00094 int LookupStitcher::getLutCount() const {
00095   return lutCount;
00096 }
00097 
00098 vector<Mat> LookupStitcher::getLutX() {
00099   return lutX;
00100 }
00101 vector<Mat> LookupStitcher::getLutY() {
00102   return lutY;
00103 }
00104 vector<Mat> LookupStitcher::getLutA() {
00105   return lutA;
00106 }
00107 
00108 } // namespace omnicamera
 All Classes Namespaces Files Functions Variables Typedefs Defines


omnicamera
Author(s): Tomas Petricek / petrito1@cmp.felk.cvut.cz
autogenerated on Tue Dec 10 2013 14:26:53