void PulsingColorAnimation::sinusSignal(double time) { double fac = std::sin(pulsation() * (time - delay())); /* QColor meanHSV = meanColor().toHsv(); QColor varHSV = varColor().toHsv(); int h = meanHSV.hue() + int(fac * varHSV.hue()); if (h>255) h=255; if (h<0) h=0; int s = meanHSV.saturation() + int(fac * varHSV.saturation()); if (s>255) s=255; if (s<0) s=0; int v = meanHSV.value() + int(fac * varHSV.value()); if (v>255) v=255; if (v<0) v=0; m_color.setHsv(h, s, v) ; */ int r = meanColor().red() + int(fac * varColor().red()); if (r>255) r-=255; if (r<0) r+=255; int g = meanColor().green() + int(fac * varColor().green()); if (g>255) g-=255; if (g<0) g+=255; int b = meanColor().blue() + int(fac * varColor().blue()); if (b>255) b-=255; if (b<0) b+=255; m_color.setRgb(r, g, b); }
void PulsingColorAnimation::saveToJSON(QJsonObject & obj) { obj["fromStart"] = fromStart(); obj["duration"] = duration(); QColor lowerColor(meanColor().red() - varColor().red(), meanColor().green() - varColor().green(), meanColor().blue() - varColor().blue()); obj["lowerColor"] = lowerColor.name(); QColor upperColor(meanColor().red() + varColor().red(), meanColor().green() + varColor().green(), meanColor().blue() + varColor().blue()); obj["upperColor"] = upperColor.name(); obj["frequency"] = pulsation() / (2 * M_PI); obj["delay"] = delay(); obj["pulseSignal"] = pulseSignalName(); obj["priority"] = priority(); }
QRgb pickScreenRGB(const QRect &rect) { QWidget *widget = QApplication::desktop(); #ifdef MACOSX // #Bugzilla 6514, possibly related to #QTBUG 23516 // Screen grabbing on a secondary screen seems to be broken on MAC // (see what happens using PixelTool), when no piece of the *primary* // screen is involved in the grab. As it seems to be very Qt-based, // the workaround is to trivially grab the smallest rect including the // requested one and a part of the primary screen. const QRect &screen0Geom = QApplication::desktop()->screenGeometry(0); int left = std::min(rect.left(), screen0Geom.right()); int top = std::min(rect.top(), screen0Geom.bottom()); int right = std::max(rect.right(), screen0Geom.left()); int bottom = std::max(rect.bottom(), screen0Geom.right()); QRect theRect(QPoint(left, top), QPoint(right, bottom)); #else const QRect &theRect = rect; #endif QImage img(QPixmap::grabWindow(widget->winId(), theRect.x(), theRect.y(), theRect.width(), theRect.height()) .toImage()); return meanColor( img, QRect(rect.left() - theRect.left(), rect.top() - theRect.top(), rect.width(), rect.height())); }
void OpenCVPicture::loadDataWithoutScalingRemoveMeanColor(int flags) { cv::Mat temp = cv::imread(filename, flags); if (temp.empty()) { std::cout << "Error : Image " << filename << " cannot be loaded..." << std::endl; exit(EXIT_FAILURE); } std::vector<float> meanColor(temp.channels()); for (int i = 0; i < temp.channels(); ++i) { for (int y = 0; y < temp.rows; y++) { for (int x = 0; x < temp.cols; x++) { int c = temp.ptr()[i + x * temp.channels() + y * temp.channels() * temp.cols]; meanColor[i] += c; } } } for (int i = 0; i < temp.channels(); ++i) meanColor[i] /= temp.rows * temp.cols; temp.convertTo(mat, CV_32FC(temp.channels())); float *matData = ((float *)(mat.data)); for (int i = 0; i < mat.channels(); ++i) for (int y = 0; y < temp.rows; y++) for (int x = 0; x < temp.cols; x++) // matData[i + x * mat.channels() + y * mat.channels() * mat.cols] -= // meanColor[i]; matData[i + x * mat.channels() + y * mat.channels() * mat.cols] = 128 + (matData[i + x * mat.channels() + y * mat.channels() * mat.cols] - meanColor[i]) / 2; backgroundColor = 128; xOffset = -mat.cols / 2; yOffset = -mat.rows / 2; }
QRgb pickRGB(QOpenGLWidget *widget, const QRect &rect) { widget->makeCurrent(); glReadBuffer(GL_FRONT); QImage img(rect.size(), QImage::Format_ARGB32); glReadPixels(rect.x(), widget->height() - rect.y(), rect.width(), rect.height(), GL_BGRA_EXT, GL_UNSIGNED_BYTE, img.bits()); widget->doneCurrent(); return meanColor(img, img.rect()); }
QRgb pickRGB(QWidget *widget, const QRect &rect) { QImage img(QPixmap::grabWidget(widget, rect.x(), rect.y(), rect.width(), rect.height()) .toImage()); return meanColor(img, img.rect()); }
PcaModel PcaModel::loadScmModel(path modelFilename, path landmarkVertexMappingFile, PcaModel::ModelType modelType) { logging::Logger logger = Loggers->getLogger("shapemodels"); PcaModel model; // Load the landmarks mappings /* std::ifstream ffpList; ffpList.open(landmarkVertexMappingFile.c_str(), std::ios::in); if (!ffpList.is_open()) { string errorMessage = "Error opening feature points file " + landmarkVertexMappingFile + "."; logger.error(errorMessage); throw std::runtime_error(errorMessage); } string line; while (ffpList.good()) { std::getline(ffpList, line); if (line == "" || line.substr(0, 2) == "//") { // empty line or starting with a '//' continue; } string currFfp; // Have a buffer string int currVertex = 0; std::stringstream ss(line); // Insert the string into a stream ss >> currFfp; ss >> currVertex; model.landmarkVertexMap.insert(make_pair(currFfp, currVertex)); currFfp.clear(); } ffpList.close(); */ // Identity mapping: // TODO DO PROPER SOLUTION /*for (int i = 0; i < 28635; ++i) { model.landmarkVertexMap.insert(make_pair(boost::lexical_cast<string>(i), i)); // maybe from 1 to ... }*/ // Load the model if (sizeof(unsigned int) != 4) { logger.warn("Warning: We're reading 4 Bytes from the file but sizeof(unsigned int) != 4. Check the code/behaviour."); } if (sizeof(double) != 8) { logger.warn("Warning: We're reading 8 Bytes from the file but sizeof(double) != 8. Check the code/behaviour."); } std::ifstream modelFile; modelFile.open(modelFilename.string(), std::ios::binary); if (!modelFile.is_open()) { logger.warn("Could not open model file: " + modelFilename.string()); exit(EXIT_FAILURE); } // Reading the shape model // Read (reference?) num triangles and vertices unsigned int numVertices = 0; unsigned int numTriangles = 0; modelFile.read(reinterpret_cast<char*>(&numVertices), 4); // 1 char = 1 byte. uint32=4bytes. float64=8bytes. modelFile.read(reinterpret_cast<char*>(&numTriangles), 4); // Read triangles vector<array<int, 3>> triangleList; triangleList.resize(numTriangles); unsigned int v0, v1, v2; for (unsigned int i=0; i < numTriangles; ++i) { v0 = v1 = v2 = 0; modelFile.read(reinterpret_cast<char*>(&v0), 4); // would be nice to pass a &vector and do it in one modelFile.read(reinterpret_cast<char*>(&v1), 4); // go, but didn't work. Maybe a cv::Mat would work? modelFile.read(reinterpret_cast<char*>(&v2), 4); triangleList[i][0] = v0; triangleList[i][1] = v1; triangleList[i][2] = v2; } // Read number of rows and columns of the shape projection matrix (pcaBasis) unsigned int numShapePcaCoeffs = 0; unsigned int numShapeDims = 0; // dimension of the shape vector (3*numVertices) modelFile.read(reinterpret_cast<char*>(&numShapePcaCoeffs), 4); modelFile.read(reinterpret_cast<char*>(&numShapeDims), 4); if (3*numVertices != numShapeDims) { logger.warn("Warning: Number of shape dimensions is not equal to three times the number of vertices. Something will probably go wrong during the loading."); } // Read shape projection matrix Mat pcaBasisShape(numShapeDims, numShapePcaCoeffs, CV_32FC1); // m x n (rows x cols) = numShapeDims x numShapePcaCoeffs logger.debug("Loading PCA basis matrix with " + lexical_cast<string>(pcaBasisShape.rows) + " rows and " + lexical_cast<string>(pcaBasisShape.cols) + "cols."); for (unsigned int col = 0; col < numShapePcaCoeffs; ++col) { for (unsigned int row = 0; row < numShapeDims; ++row) { double var = 0.0; modelFile.read(reinterpret_cast<char*>(&var), 8); pcaBasisShape.at<float>(row, col) = static_cast<float>(var); } } // Read mean shape vector unsigned int numMean = 0; // dimension of the mean (3*numVertices) modelFile.read(reinterpret_cast<char*>(&numMean), 4); if (numMean != numShapeDims) { logger.warn("Warning: Number of shape dimensions is not equal to the number of dimensions of the mean. Something will probably go wrong during the loading."); } Mat meanShape(numMean, 1, CV_32FC1); unsigned int counter = 0; double vd0, vd1, vd2; for (unsigned int i=0; i < numMean/3; ++i) { vd0 = vd1 = vd2 = 0.0; modelFile.read(reinterpret_cast<char*>(&vd0), 8); modelFile.read(reinterpret_cast<char*>(&vd1), 8); modelFile.read(reinterpret_cast<char*>(&vd2), 8); meanShape.at<float>(counter, 0) = static_cast<float>(vd0); ++counter; meanShape.at<float>(counter, 0) = static_cast<float>(vd1); ++counter; meanShape.at<float>(counter, 0) = static_cast<float>(vd2); ++counter; } // Read shape eigenvalues unsigned int numEigenValsShape = 0; modelFile.read(reinterpret_cast<char*>(&numEigenValsShape), 4); if (numEigenValsShape != numShapePcaCoeffs) { logger.warn("Warning: Number of coefficients in the PCA basis matrix is not equal to the number of eigenvalues. Something will probably go wrong during the loading."); } Mat eigenvaluesShape(numEigenValsShape, 1, CV_32FC1); for (unsigned int i=0; i < numEigenValsShape; ++i) { double var = 0.0; modelFile.read(reinterpret_cast<char*>(&var), 8); eigenvaluesShape.at<float>(i, 0) = static_cast<float>(var); } if (modelType == ModelType::SHAPE) { model.mean = meanShape; model.pcaBasis = pcaBasisShape; model.eigenvalues = eigenvaluesShape; model.triangleList = triangleList; modelFile.close(); return model; } // Reading the color model // Read number of rows and columns of projection matrix unsigned int numTexturePcaCoeffs = 0; unsigned int numTextureDims = 0; modelFile.read(reinterpret_cast<char*>(&numTexturePcaCoeffs), 4); modelFile.read(reinterpret_cast<char*>(&numTextureDims), 4); // Read color projection matrix Mat pcaBasisColor(numTextureDims, numTexturePcaCoeffs, CV_32FC1); logger.debug("Loading PCA basis matrix with " + lexical_cast<string>(pcaBasisShape.rows) + " rows and " + lexical_cast<string>(pcaBasisShape.cols) + "cols."); for (unsigned int col = 0; col < numTexturePcaCoeffs; ++col) { for (unsigned int row = 0; row < numTextureDims; ++row) { double var = 0.0; modelFile.read(reinterpret_cast<char*>(&var), 8); pcaBasisColor.at<float>(row, col) = static_cast<float>(var); } } // Read mean color vector unsigned int numMeanColor = 0; // dimension of the mean (3*numVertices) modelFile.read(reinterpret_cast<char*>(&numMeanColor), 4); Mat meanColor(numMeanColor, 1, CV_32FC1); counter = 0; for (unsigned int i=0; i < numMeanColor/3; ++i) { vd0 = vd1 = vd2 = 0.0; modelFile.read(reinterpret_cast<char*>(&vd0), 8); // order in hdf5: RGB. Order in OCV: BGR. But order in vertex.color: RGB modelFile.read(reinterpret_cast<char*>(&vd1), 8); modelFile.read(reinterpret_cast<char*>(&vd2), 8); meanColor.at<float>(counter, 0) = static_cast<float>(vd0); ++counter; meanColor.at<float>(counter, 0) = static_cast<float>(vd1); ++counter; meanColor.at<float>(counter, 0) = static_cast<float>(vd2); ++counter; } // Read color eigenvalues unsigned int numEigenValsColor = 0; modelFile.read(reinterpret_cast<char*>(&numEigenValsColor), 4); Mat eigenvaluesColor(numEigenValsColor, 1, CV_32FC1); for (unsigned int i=0; i < numEigenValsColor; ++i) { double var = 0.0; modelFile.read(reinterpret_cast<char*>(&var), 8); eigenvaluesColor.at<float>(i, 0) = static_cast<float>(var); } if (modelType == ModelType::COLOR) { model.mean = meanColor; model.pcaBasis = pcaBasisColor; model.eigenvalues = eigenvaluesColor; model.triangleList = triangleList; modelFile.close(); return model; } logger.error("Unknown ModelType, should never reach here."); //modelFile.close(); //return model; }