void GestureClassifierByHistogram::createReferenceHistogramsForClass3Gesture() { // create reference histograms ReferenceHistogramGeneratorForClass3Gesture refHistogramGenerator(local::class3GestureRefHistogramSigma); refHistogramGenerator.createHistograms(local::phaseHistBins, local::refHistogramNormalizationFactor); const std::vector<cv::MatND> &refHistograms = refHistogramGenerator.getHistograms(); refHistogramsForClass3Gesture_.assign(refHistograms.begin(), refHistograms.end()); #if 0 // FIXME [delete] >> // draw reference histograms for (std::vector<cv::MatND>::const_iterator it = refHistogramsForClass3Gesture_.begin(); it != refHistogramsForClass3Gesture_.end(); ++it) { #if 0 double maxVal = 0.0; cv::minMaxLoc(*it, NULL, &maxVal, NULL, NULL); #else const double maxVal = local::refHistogramNormalizationFactor * 0.05; #endif // draw 1-D histogram cv::Mat histImg(cv::Mat::zeros(local::phaseHistMaxHeight, local::phaseHistBins*local::phaseHistBinWidth, CV_8UC3)); HistogramUtil::drawHistogram1D(*it, local::phaseHistBins, maxVal, local::phaseHistBinWidth, local::phaseHistMaxHeight, histImg); cv::imshow(local::windowNameClass1Gesture2, histImg); cv::waitKey(0); } #endif }
void GestureClassifierByHistogram::drawMatchedIdPatternHistogram(const boost::circular_buffer<size_t> &matchedHistogramIndexes, const std::string &windowName) const { // calculate matched index histogram cv::MatND hist; #if defined(__GNUC__) { cv::Mat tmpmat(std::vector<unsigned char>(matchedHistogramIndexes.begin(), matchedHistogramIndexes.end())); cv::calcHist(&tmpmat, 1, local::indexHistChannels, cv::Mat(), hist, local::histDims, local::indexHistSize, local::indexHistRanges, true, false); } #else cv::calcHist(&cv::Mat(std::vector<unsigned char>(matchedHistogramIndexes.begin(), matchedHistogramIndexes.end())), 1, local::indexHistChannels, cv::Mat(), hist, local::histDims, local::indexHistSize, local::indexHistRanges, true, false); #endif // normalize histogram //HistogramUtil::normalizeHistogram(hist, params_.maxMatchedHistogramNum); // draw matched index histogram cv::Mat histImg(cv::Mat::zeros(local::indexHistMaxHeight, local::indexHistBins*local::indexHistBinWidth, CV_8UC3)); HistogramUtil::drawHistogram1D(hist, local::indexHistBins, params_.maxMatchedHistogramNum, local::indexHistBinWidth, local::indexHistMaxHeight, histImg); std::ostringstream sstream; sstream << "count: " << matchedHistogramIndexes.size(); cv::putText(histImg, sstream.str(), cv::Point(10, 15), cv::FONT_HERSHEY_COMPLEX, 0.5, CV_RGB(255, 0, 255), 1, 8, false); cv::imshow(windowName, histImg); }
void GestureClassifierByHistogram::createGestureIdPatternHistogramsForClass1Gesture() { // create gesture pattern histograms GestureIdPatternHistogramGeneratorForClass1Gesture gestureIdPatternHistogramGenerator(local::gesturePatternHistogramSigma); gestureIdPatternHistogramGenerator.createHistograms(local::gesturePatternHistogramBinNum, local::gesturePatternHistogramNormalizationFactor); const std::vector<cv::MatND> &gesturePatternHistograms = gestureIdPatternHistogramGenerator.getHistograms(); gestureIdPatternHistogramsForClass1Gesture_.assign(gesturePatternHistograms.begin(), gesturePatternHistograms.end()); #if 0 // FIXME [delete] >> // draw gesture pattern histograms for (std::vector<cv::MatND>::const_iterator it = gesturePatternHistograms.begin(); it != gesturePatternHistograms.end(); ++it) { #if 0 double maxVal = 0.0; cv::minMaxLoc(*it, NULL, &maxVal, NULL, NULL); #else const double maxVal = local::gesturePatternHistogramNormalizationFactor; #endif // draw 1-D histogram cv::Mat histImg(cv::Mat::zeros(local::indexHistMaxHeight, local::gesturePatternHistogramBinNum*local::indexHistBinWidth, CV_8UC3)); HistogramUtil::drawHistogram1D(*it, local::gesturePatternHistogramBinNum, maxVal, local::indexHistBinWidth, local::indexHistMaxHeight, histImg); cv::imshow(local::windowNameClass1Gesture2, histImg); cv::waitKey(0); } #endif }
//-------------------------------------------------------------------------------------------------- // cv::Mat Histogram1D::getHistogramImage( const cv::Mat& image ) { // Compute histogram first cv::MatND hist = getHistogram( image ); // Get min and max bin values double maxVal{ }; double minVal{ }; cv::minMaxLoc( hist, &minVal, &maxVal, 0, 0 ); // Image on which to display histogram cv::Mat histImg( histSize_[0], histSize_[0], CV_8U, cv::Scalar( 255 ) ); // set highest point at 90% of nbins int hpt = static_cast<int>( 0.9 * histSize_[0] ); // Draw vertical line for each bin for( int h = 0; h < histSize_[0]; ++h ) { float binVal = hist.at< float >( h ); int intensity = static_cast<int>( binVal * hpt / maxVal ); cv::line( histImg, cv::Point( h, histSize_[0] ), cv::Point( h, histSize_[0] - intensity ), cv::Scalar::all( 0 ) ); } return histImg; }
void GestureClassifierByHistogram::drawTemporalOrientationHistogram(const cv::MatND &temporalHist, const std::string &windowName) const { #if 1 double maxVal = 0.0; cv::minMaxLoc(temporalHist, NULL, &maxVal, NULL, NULL); #else const double maxVal = local::refHistogramNormalizationFactor * 0.05; #endif cv::Mat histImg(cv::Mat::zeros(temporalHist.rows*local::phaseVertScale, temporalHist.cols*local::phaseHorzScale, CV_8UC3)); HistogramUtil::drawHistogram2D(temporalHist, temporalHist.cols, temporalHist.rows, maxVal, local::phaseHorzScale, local::phaseVertScale, histImg); cv::imshow(windowName, histImg); }
void GestureClassifierByHistogram::drawAccumulatedPhaseHistogram(const cv::MatND &accumulatedHist, const std::string &windowName) const { #if 0 double maxVal = 0.0; cv::minMaxLoc(accumulatedHist, NULL, &maxVal, NULL, NULL); #else const double maxVal = local::refHistogramNormalizationFactor * 0.05; #endif cv::Mat histImg(cv::Mat::zeros(local::phaseHistMaxHeight, local::phaseHistBins*local::phaseHistBinWidth, CV_8UC3)); HistogramUtil::drawHistogram1D(accumulatedHist, local::phaseHistBins, maxVal, local::phaseHistBinWidth, local::phaseHistMaxHeight, histImg); cv::imshow(windowName, histImg); }
cv::Mat Histogram::getHistogramImage(const cv::Mat &image, const int &histSize) const { cv::MatND hist = getHistogramValues(image, histSize); //get HistogramValues and save in hist double max = 0; double min = 0; cv::minMaxLoc(hist, &min, &max); cv::Mat histImg(histSize, histSize, CV_8UC1, cv::Scalar(255)); for(int i = 0; i < histSize; i++) { float value = hist.at<float>(i); int intensity = static_cast<int>(value/max * 0.9*histSize); cv::line(histImg, cv::Point(i,histSize), cv::Point(i,histSize-intensity), cv::Scalar(0)); } return histImg; }
cv::Mat StrategyColorHistogram::getHistogramImage(const cv::Mat &image, cv::Scalar color) { cv::MatND hist = getHistogram(image); double maxVal = 0; double minVal = 0; cv::minMaxLoc(hist, &minVal, &maxVal, 0 , 0); cv::Mat histImg(m_histSize[0], m_histSize[0], CV_8UC3, cv::Scalar(0, 0, 0)); int hpt = static_cast<int>(0.9 * m_histSize[0]); for(int h = 0; h < m_histSize[0]; h++) { float binVal = hist.at<float>(h); int intensity = static_cast<int>(binVal * hpt / maxVal); cv::line(histImg, cv::Point(h, m_histSize[0]), cv::Point(h, m_histSize[0] - intensity), color); } return histImg; }
void Cognition::on_Histogram_clicked() { Mat src=imread(("img"+QString().setNum(imgNum-1)+".jpg").toStdString(),0); //cvtColor(src,hsv,CV_BGR2HSV); int gbins = 256; int histSize[] = {gbins}; // hue varies from 0 to 179, see cvtColor float granges[] = { 0, 255 }; const float* ranges[] = { granges }; MatND hist; // we compute the histogram from the 0-th and 1-st channels int channels[] = {0}; calcHist( &src, 1, channels, Mat(), // do not use mask hist, 1, histSize, ranges, true, // the histogram is uniform false ); double maxValue=0,minValue=0; minMaxLoc(hist,&minValue,&maxValue,0,0); cout<<maxValue<<" "<<minValue<<endl; Mat histImg(histSize[0],histSize[0],CV_8U,Scalar(255)); int hpt =static_cast<int>(0.9*histSize[0]); for(int h=0;h<histSize[0];h++) { float binVal =hist.at<float>(h); int intensity=static_cast<int>(binVal*hpt/maxValue); line(histImg,Point(h,histSize[0]),Point(h,histSize[0]-intensity),Scalar::all(0)); } namedWindow( "Source", 1 ); imshow( "Source", src ); namedWindow( "hist", 1 ); imshow("hist",histImg); waitKey(); }
void Image::showHistogramAsImage() { for( int n = 0; n < (int) _histograms.size(); n++ ) { double maxVal = 0; double minVal = 0; cv::minMaxLoc( _histograms.at( n ), &minVal, &maxVal ); cv::Mat histImg( 256, 256, CV_8U, cv::Scalar(255) ); int hpt = static_cast< int >( 0.9 * 256 ); for( int h = 0; h < 256; h++ ) { float binVal = _histograms.at( n ).at< float >( h ); int intensity = static_cast< int >( binVal * hpt/maxVal ); cv::line( histImg, cv::Point( h, 256 ), cv::Point( h, 256 - intensity ), cv::Scalar::all( 0 ) ); } std::string name = "Histogram channel " + Log::to_string( n ); cv::namedWindow( name, 0 ); cv::imshow( name, histImg ); } }