void FeatureVectorBuilder::createProjectionHistogram(cv::Mat& projection, cv::Mat& output, int outSize, int type) { if (type == 0) { cv::Mat histImage(outSize, projection.cols, CV_8UC1, cv::Scalar(0, 0, 0)); cv::normalize(projection, projection, 0, outSize, cv::NORM_MINMAX, -1, cv::Mat()); for (int i = 1; i < projection.cols; i++) { float val = projection.at<float>(0, i); cv::circle(histImage, cv::Point(i, val), 1, cv::Scalar(255, 255, 255), 1, 1, 0); } cv::flip(histImage, histImage, 0); histImage.copyTo(output); } if (type == 1) { cv::Mat histImage(projection.rows, outSize, CV_8UC1, cv::Scalar(0, 0, 0)); cv::normalize(projection, projection, 0, outSize, cv::NORM_MINMAX, -1, cv::Mat()); for (int i = 1; i < projection.rows; i++) { float val = projection.at<float>(i, 0); cv::circle(histImage, cv::Point(val, i), 1, cv::Scalar(255, 255, 255), 1, 1, 0); } histImage.copyTo(output); } }
void draw_histogram(cv::Mat &img, std::string image_name="../ImagesForStudents/out_hist.png"){ int histSize = 255; float range[] = {0,256}; const float* histRange = {range}; bool uniform = true; bool accumulate = false; cv::Mat hist = cv::Mat::zeros(512,400,CV_8UC1); calcHist( &img, 1, 0, cv::Mat(), hist, 1, &histSize, &histRange, uniform, accumulate ); int hist_w = 512; int hist_h = 400; int bin_w = cvRound( (double) hist_w/histSize ); cv::Mat histImage( hist_h, hist_w, CV_8UC1,cv::Scalar(255)); cv::normalize(hist, hist, 0, histImage.rows, cv::NORM_MINMAX, -1, cv::Mat() ); /// Draw for each channel for( int i = 1; i < histSize; i++ ) { line( histImage, cv::Point( bin_w*(i-1), hist_h - cvRound(hist.at<float>(i-1)) ) , cv::Point( bin_w*(i), hist_h - cvRound(hist.at<float>(i)) ), cv::Scalar( 0, 0, 0), 2, 8, 0 ); } cv::imwrite(image_name,histImage); }
void CImageAnalysis::Histogram() { Mat mat = imread( "F:\\4点定位剪切图.bmp",CV_LOAD_IMAGE_ANYDEPTH|CV_LOAD_IMAGE_ANYCOLOR ); Mat src,src1,src2; Mat hist; int histSize = 255; float range[] = { 0, 255 } ; const float* histRange = { range }; bool uniform = true; bool accumulate = false; calcHist( &src1, 1, 0, Mat(), hist, 1, &histSize, &histRange, uniform, accumulate ); int hist_w = 400; int hist_h = 400; int bin_w = cvRound( (double) hist_w/histSize ); Mat histImage( hist_w, hist_h, CV_8UC3, Scalar( 255,255,255) ); normalize(hist, hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() ); for( int i = 1; i < histSize; i++ ) { line( histImage, Point( bin_w*(i-1), hist_h - cvRound(hist.at<float>(i-1)) ) , Point( bin_w*(i), hist_h - cvRound(hist.at<float>(i)) ), Scalar( 0, 0, 255), 2, 8, 0 ); } namedWindow("calcHist Demo", CV_WINDOW_AUTOSIZE ); imshow("calcHist Demo", histImage ); }
void ReliabilityHistogram::displayHistogram( cv::Mat histogram) { int hist_w = 512; int hist_h = 400; int bin_w = cvRound( (double) hist_w/numBins ); cv::Mat histNorm, histImage( hist_h, hist_w, CV_8UC3, cv::Scalar( 0,0,0) ); if(histNorm.data == NULL) cv::normalize(histogram, histNorm, 0, histImage.rows, cv::NORM_MINMAX, -1, cv::Mat() ); for( int i = 1; i < this->numBins; i++ ) { cv::line( histImage, cv::Point( bin_w*(i-1), hist_h - cvRound(histNorm.at<float>(i-1)) ) , cv::Point( bin_w*(i), hist_h - cvRound(histNorm.at<float>(i)) ), cv::Scalar( 0, 255, 0), 2, 8, 0 ); } #ifdef __OPENCV3__ cv::namedWindow("Reliability Histogram", cv::WINDOW_AUTOSIZE ); #else cv::namedWindow("Reliability Histogram", CV_WINDOW_AUTOSIZE ); #endif cv::imshow("Reliability Histogram", histImage ); }
Mat Histogram::drawHist(MatND _histMat) { if(_histMat.channels()>1) cvError(1,__FUNCTION__,"Only for 1D histograms",__FILE__,__LINE__); int w = 400; int h = 400; Mat histImage( h, w, CV_8UC3, Scalar( 0,0,0) ); Mat i2; cv::normalize(_histMat,i2,0,255,CV_MINMAX); for( int i = 0; i < _histSize[0]; i++ ) { int bin_w = cvRound( (double) w/_histSize[0]); rectangle( histImage, Point( i*bin_w, h ), Point( (i+1)*bin_w, h - cvRound( i2.at<float>(i)*h/255.0 ) ), Scalar( 0, 0, 255 ), -1 ); /*line( histImage, Point( bin_w*(i-1), hist_h - cvRound(1*i2.at<float>(i-1)) ) , Point( bin_w*(i), hist_h - cvRound(1*i2.at<float>(i)) ), Scalar( 255, 0, 0), 2, 8, 0 ); /*line( histImage, Point( bin_w*(i-1), hist_h - cvRound(g_hist.at<float>(i-1)) ) , Point( bin_w*(i), hist_h - cvRound(g_hist.at<float>(i)) ), Scalar( 0, 255, 0), 2, 8, 0 ); line( histImage, Point( bin_w*(i-1), hist_h - cvRound(r_hist.at<float>(i-1)) ) , Point( bin_w*(i), hist_h - cvRound(r_hist.at<float>(i)) ), Scalar( 0, 0, 255), 2, 8, 0 );*/ } return histImage; }
//直方图函数 void histGram(Mat hist) { //1直方图属性 int histSize = 255; float range[] = { 0, 255 }; const float* histRange = { range }; //2计算直方图 bool uniform = true, accumulate = false; Mat g_hist; calcHist(&hist, 1, 0, Mat(), g_hist, 1, &histSize, &histRange, uniform, accumulate); //--------------------了解Hist的内部结构???????????????????????? /*namedWindow("g_hist",CV_WINDOW_NORMAL); imshow("g_hist", g_hist);*/ //3创建画布 int hist_w = 400; int hist_h = 400; int bin_w = cvRound((double)hist_w / histSize); Mat histImage(hist_w, hist_h, CV_8UC3, Scalar::all(0)); //4归一化直方图到定义的窗口 normalize(g_hist, g_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat()); //5在直方图上画布上画出直方图 for (int i = 1; i < histSize; i++) { line(histImage, Point(bin_w*(i - 1), hist_h - cvRound(g_hist.at<float>(i - 1))), Point(bin_w*(i), hist_h - cvRound(g_hist.at<float>(i))), Scalar(0, 0, 250), 2, 8, 0); } //6显示直方图 namedWindow("calcHist"); imshow("calcHist", histImage); }
cv::Mat histogram(cv::Mat image,cv::Mat mask) { int rangeMax=256; int histSize = rangeMax; float range[] = { 0, rangeMax } ; const float* histRange = { range }; bool uniform = true; bool accumulate = false; cv::Mat hist; cv::calcHist( &image, 1, 0, mask, hist, 1, &histSize, &histRange, uniform, accumulate ); //Draw the histogram int hist_w = 512; int hist_h = 400; int bin_w = cvRound( (double) hist_w/histSize ); cv::Mat histImage( hist_h, hist_w, CV_8UC3, cv::Scalar( 0,0,0) ); normalize(hist, hist, 0, histImage.rows, cv::NORM_MINMAX, -1, cv::Mat() ); for( int i = 1; i < histSize; i++ ) { cv::line( histImage, cv::Point( bin_w*(i-1), hist_h - cvRound(hist.at<float>(i-1)) ) , cv::Point( bin_w*(i), hist_h - cvRound(hist.at<float>(i)) ), cv::Scalar( 255, 0, 255), 2, 8, 0 ); } return histImage; }
void PlayFieldDetectionModule::showHistogram( cv::Mat histogram, char windowName[]) { int hist_w = 512; int hist_h = 400; int bin_w = cvRound( (double) hist_w/numBins ); cv::Mat histNorm, histImage( hist_h, hist_w, CV_8UC3, cv::Scalar( 0,0,0) ); if(histNorm.data == NULL) cv::normalize(histogram, histNorm, 0, histImage.rows, cv::NORM_MINMAX, -1, cv::Mat() ); for( int i = 1; i < histogram.rows; i++ ) { cv::line( histImage, cv::Point( bin_w*(i-1), hist_h - cvRound(histNorm.at<float>(i-1)) ) , cv::Point( bin_w*(i), hist_h - cvRound(histNorm.at<float>(i)) ), cv::Scalar( 0, 255, 0), 2, 8, 0 ); } #ifdef __OPENCV3__ cv::namedWindow(windowName, cv::WINDOW_AUTOSIZE ); #else cv::namedWindow(windowName, CV_WINDOW_AUTOSIZE ); #endif cv::imshow(windowName, histImage ); }
/** * Returns a 255 by 100 QImage containing a Histogram for the given channel. * The background is transparent (Alpha 0, RGB=255) */ QImage Histogram::getImage(Channel channel = LChannel, QBrush pen = Qt::gray) { // Create blank QImage and fill it with transparent background: QImage histImage(255, 100, QImage::Format_ARGB32); histImage.fill(0); QPainter painter(&histImage); painter.setBrush(Qt::transparent); painter.setPen(Qt::transparent); painter.drawRect(0,0,255,100); // Calculate the aspect ratio using the maximal value of the color histograms int maximum = (channel == LChannel ? maximumValue(LChannel) : maximumValue(RGB)); float ratio = 100.0/float(maximum); // Preparing the painter: painter.setBrush(pen); painter.setPen(pen.color()); int h; // Draw histogram QHash<int, int>* hist = get(channel); QHash<int, int>::const_iterator cit = hist->begin(); while (cit != hist->end()) { h = 100 - floor(ratio*cit.value()); painter.drawLine(cit.key(), h, cit.key(), 100); ++cit; } return histImage; }
void showHist(Mat src) { Mat hsv_src,dst; if( !src.data ) cout<<"The image data is null!"<<endl; /// 分割成3个单通道图像 ( R, G 和 B ) vector<Mat> hsv_planes; cvtColor(src,hsv_src,CV_BGR2HSV); split( hsv_src, hsv_planes ); /// 设定bin数目 int histSize = 255; /// 设定取值范围 ( R,G,B) ) float range[] = { 0, 255 } ; const float* histRange = { range }; bool uniform = true; bool accumulate = false; Mat h_hist, s_hist, v_hist; /// 计算直方图: calcHist( &hsv_planes[0], 1, 0, Mat(), h_hist, 1, &histSize, &histRange, uniform, accumulate ); calcHist( &hsv_planes[1], 1, 0, Mat(), s_hist, 1, &histSize, &histRange, uniform, accumulate ); calcHist( &hsv_planes[2], 1, 0, Mat(), v_hist, 1, &histSize, &histRange, uniform, accumulate ); // 创建直方图画布 int hist_w = 400; int hist_h = 400; int bin_w = cvRound( (double) hist_w/histSize ); Mat histImage( hist_w, hist_h, CV_8UC3, Scalar( 0,0,0) ); /// 将直方图归一化到范围 [ 0, histImage.rows ] normalize(h_hist, h_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() ); normalize(s_hist, s_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() ); normalize(v_hist, v_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() ); /// 在直方图画布上画出直方图 for( int i = 1; i < histSize; i++ ) { line( histImage, Point( bin_w*(i-1), hist_h - cvRound(h_hist.at<float>(i-1)) ) , Point( bin_w*(i), hist_h - cvRound(h_hist.at<float>(i)) ), Scalar( 0, 0, 255), 2, 8, 0 ); line( histImage, Point( bin_w*(i-1), hist_h - cvRound(s_hist.at<float>(i-1)) ) , Point( bin_w*(i), hist_h - cvRound(s_hist.at<float>(i)) ), Scalar( 0, 255, 0), 2, 8, 0 ); line( histImage, Point( bin_w*(i-1), hist_h - cvRound(v_hist.at<float>(i-1)) ) , Point( bin_w*(i), hist_h - cvRound(v_hist.at<float>(i)) ), Scalar( 255, 0, 0), 2, 8, 0 ); } /// 显示直方图 namedWindow("calcHist Demo", CV_WINDOW_AUTOSIZE ); imshow("calcHist Demo", histImage ); waitKey(5); }
/** ############################################################################### * @Draw histogram * Takes in triple vector of values e.g. std::vector<Mat> bgrPixels(3); */ int visionUtils::drawHist(std::vector<Mat> pixelPlanes, int histBlurPixels)//( int, char** argv ) { /// Establish the number of bins int histSize = 256; /// Set the ranges ( for B,G,R) ) float range[] = { 0, 256 } ; const float* histRange = { range }; bool uniform = true; bool accumulate = false; Mat b_hist, g_hist, r_hist; /// Compute the histograms: calcHist( &pixelPlanes[0], 1, 0, Mat(), b_hist, 1, &histSize, &histRange, uniform, accumulate ); calcHist( &pixelPlanes[1], 1, 0, Mat(), g_hist, 1, &histSize, &histRange, uniform, accumulate ); calcHist( &pixelPlanes[2], 1, 0, Mat(), r_hist, 1, &histSize, &histRange, uniform, accumulate ); // Draw the histograms for B, G and R or H S V int hist_w = 512; int hist_h = 400; int bin_w = cvRound( (double) hist_w/histSize ); Mat histImage( hist_h, hist_w, CV_8UC3, Scalar( 0,0,0) ); // Smooth hists if needed if (histBlurPixels>0) { GaussianBlur(b_hist, b_hist, Size(1,histBlurPixels), 1, 1); GaussianBlur(g_hist, g_hist, Size(1,histBlurPixels), 1, 1); GaussianBlur(r_hist, r_hist, Size(1,histBlurPixels), 1, 1); } /// Normalize the result to [ 0, histImage.rows ] cv::normalize(b_hist, b_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() ); cv::normalize(g_hist, g_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() ); cv::normalize(r_hist, r_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() ); /// Draw for each channel for( int i = 1; i < histSize; i++ ) { line( histImage, Point( bin_w*(i-1), hist_h - cvRound(b_hist.at<float>(i-1)) ) , Point( bin_w*(i), hist_h - cvRound(b_hist.at<float>(i)) ), Scalar( 255, 0, 0), 2, 8, 0 ); line( histImage, Point( bin_w*(i-1), hist_h - cvRound(g_hist.at<float>(i-1)) ) , Point( bin_w*(i), hist_h - cvRound(g_hist.at<float>(i)) ), Scalar( 0, 255, 0), 2, 8, 0 ); line( histImage, Point( bin_w*(i-1), hist_h - cvRound(r_hist.at<float>(i-1)) ) , Point( bin_w*(i), hist_h - cvRound(r_hist.at<float>(i)) ), Scalar( 0, 0, 255), 2, 8, 0 ); } /// Display namedWindow("calcHist Demo", WINDOW_AUTOSIZE ); imshow("calcHist Demo", histImage ); waitKey(1); return 0; }
void RGBLineChartThread::run() { Mat frame; Mat b_hist,g_hist,r_hist; std::vector<Mat> bgr_planes; int histSize = 256; float range[] = {0,256}; const float* histRange = {range}; bool uniform = true; bool accumulate = false; int hist_w = 512; int hist_h = 400; int bin_w = cvRound((double)hist_w / histSize); Mat histImage(hist_h , hist_w , CV_8UC3 , Scalar(0,0,0)); Mat MergedImage; if( !cap.isOpened() ) return; while(true) { cap >> frame; split(frame , bgr_planes); histImage = Mat::zeros(hist_h , hist_w , CV_8UC3); cv::calcHist(&bgr_planes[0],1,0,Mat(),b_hist,1,&histSize,&histRange,uniform,accumulate); cv::calcHist(&bgr_planes[1],1,0,Mat(),g_hist,1,&histSize,&histRange,uniform,accumulate); cv::calcHist(&bgr_planes[2],1,0,Mat(),r_hist,1,&histSize,&histRange,uniform,accumulate); cv::normalize(b_hist,b_hist,0,histImage.rows,NORM_MINMAX,-1,Mat()); cv::normalize(g_hist,g_hist,0,histImage.rows,NORM_MINMAX,-1,Mat()); cv::normalize(r_hist,r_hist,0,histImage.rows,NORM_MINMAX,-1,Mat()); for(int i=1; i<histSize; i++) { cv::line(histImage,Point(bin_w*(i-1) , hist_h - cvRound(b_hist.at<float>(i - 1)) ),Point(bin_w*i , hist_h - cvRound(b_hist.at<float>(i)) ),Scalar(255,0,0),2,8,0); cv::line(histImage,Point(bin_w*(i-1) , hist_h - cvRound(g_hist.at<float>(i - 1)) ),Point(bin_w*i , hist_h - cvRound(g_hist.at<float>(i))),Scalar(0,255,0),2,8,0); cv::line(histImage,Point(bin_w*(i-1) , hist_h - cvRound(r_hist.at<float>(i - 1)) ),Point(bin_w*i , hist_h - cvRound(r_hist.at<float>(i))),Scalar(0,0,255),2,8,0); } merge(bgr_planes,frame); resize(frame,frame,cv::Size(histImage.cols,histImage.rows)); int totalCols = histImage.cols + frame.cols; MergedImage.create(histImage.rows, totalCols,histImage.type()); Mat subImage = MergedImage.colRange(0,histImage.cols); histImage.copyTo(subImage); subImage = MergedImage.colRange(histImage.cols , totalCols); frame.copyTo(subImage); emit NewLineChart(&MergedImage); bgr_planes.clear(); waitKey(30); } return; }
int main( int argc, char* argv[] ) { if( argc != 2 ) { std::cerr << "Usage: " << argv[0] << " <InputImage>" << std::endl; return EXIT_FAILURE; } cv::Mat image = cv::imread( argv[1],1); if(!image.data) { return EXIT_FAILURE; } std::vector<cv::Mat> bgr_planes; cv::split( image, bgr_planes ); int histSize = 256; float range[] = { 0, 256 } ; const float* histRange = { range }; bool uniform = true; bool accumulate = false; cv::Mat b_hist, g_hist, r_hist; cv::calcHist( &bgr_planes[0], 1, 0, cv::Mat(), b_hist, 1, &histSize, &histRange, uniform, accumulate ); cv::calcHist( &bgr_planes[1], 1, 0, cv::Mat(), g_hist, 1, &histSize, &histRange, uniform, accumulate ); cv::calcHist( &bgr_planes[2], 1, 0, cv::Mat(), r_hist, 1, &histSize, &histRange, uniform, accumulate ); //Draw the histograms int hist_w = 512; int hist_h = 400; int bin_w = cvRound( (double) hist_w/histSize ); cv::Mat histImage( hist_h, hist_w, CV_8UC3, cv::Scalar( 0,0,0) ); normalize(b_hist, b_hist, 0, histImage.rows, cv::NORM_MINMAX, -1, cv::Mat() ); normalize(g_hist, g_hist, 0, histImage.rows, cv::NORM_MINMAX, -1, cv::Mat() ); normalize(r_hist, r_hist, 0, histImage.rows, cv::NORM_MINMAX, -1, cv::Mat() ); for( int i = 1; i < histSize; i++ ) { cv::line( histImage, cv::Point( bin_w*(i-1), hist_h - cvRound(b_hist.at<float>(i-1)) ) , cv::Point( bin_w*(i), hist_h - cvRound(b_hist.at<float>(i)) ), cv::Scalar( 255, 0, 0), 2, 8, 0 ); cv::line( histImage, cv::Point( bin_w*(i-1), hist_h - cvRound(g_hist.at<float>(i-1)) ) , cv::Point( bin_w*(i), hist_h - cvRound(g_hist.at<float>(i)) ), cv::Scalar( 0, 255, 0), 2, 8, 0 ); cv::line( histImage, cv::Point( bin_w*(i-1), hist_h - cvRound(r_hist.at<float>(i-1)) ) , cv::Point( bin_w*(i), hist_h - cvRound(r_hist.at<float>(i)) ), cv::Scalar( 0, 0, 255), 2, 8, 0 ); } //Display histogram cv::namedWindow( argv[1], CV_WINDOW_AUTOSIZE ); cv::imshow( argv[1], histImage ); cv::waitKey(0); return EXIT_SUCCESS; }
void caclBGRHist() { Mat src = imread("C:\\Users\\Administrator\\Desktop\\lena.jpg"); //Load image Mat dst; if( !src.data ) exit(0); // Separate the image in 3 places ( B, G and R ) vector<Mat> bgr_planes; split( src, bgr_planes ); int histSize = 256; // Establish the number of bins float range[] = { 0, 256 }; /// Set the ranges ( for B,G,R) ) const float* histRange = { range }; bool uniform = true; bool accumulate = false; Mat b_hist, g_hist, r_hist; // Compute the histograms: calcHist( &bgr_planes[0], 1, 0, Mat(), b_hist, 1, &histSize, &histRange, uniform, accumulate ); calcHist( &bgr_planes[1], 1, 0, Mat(), g_hist, 1, &histSize, &histRange, uniform, accumulate ); calcHist( &bgr_planes[2], 1, 0, Mat(), r_hist, 1, &histSize, &histRange, uniform, accumulate ); // Draw the histograms for B, G and R int hist_w = 600; int hist_h = 400; int bin_w = cvRound( (double) hist_w/histSize ); Mat histImage( hist_h, hist_w, CV_8UC3, Scalar( 0,0,0) ); // Normalize the result to [ 0, histImage.rows ] normalize(b_hist, b_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() ); normalize(g_hist, g_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() ); normalize(r_hist, r_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() ); // Draw for each channel for( int i = 1; i < histSize; i++ ){ line( histImage, Point( bin_w*(i-1), hist_h - cvRound(b_hist.at<float>(i-1)) ) , Point( bin_w*(i), hist_h - cvRound(b_hist.at<float>(i)) ), Scalar( 255, 0, 0), 2, 8, 0 ); line( histImage, Point( bin_w*(i-1), hist_h - cvRound(g_hist.at<float>(i-1)) ) , Point( bin_w*(i), hist_h - cvRound(g_hist.at<float>(i)) ), Scalar( 0, 255, 0), 2, 8, 0 ); line( histImage, Point( bin_w*(i-1), hist_h - cvRound(r_hist.at<float>(i-1)) ) , Point( bin_w*(i), hist_h - cvRound(r_hist.at<float>(i)) ), Scalar( 0, 0, 255), 2, 8, 0 ); } namedWindow("calcHist", CV_WINDOW_AUTOSIZE ); imshow("calcHist", histImage ); waitKey(0); }
cv::Mat channelHistogram(cv::Mat image,cv::Mat mask,int minRange, int maxRange, int maxRange2) { std::vector<cv::Mat> channels; cv::split( image, channels ); int histSize = maxRange-minRange; float range[] = { minRange, maxRange } ; const float* histRange = { range }; bool uniform = true; bool accumulate = false; cv::Mat b_hist, g_hist, r_hist; if (maxRange2!=0) { int histSize2 = maxRange2-minRange; float range2[] = { minRange, maxRange2 } ; const float* histRange2 = { range2 }; cv::calcHist( &channels[0], 1, 0, mask, b_hist, 1, &histSize2, &histRange2, uniform, accumulate ); } else { cv::calcHist( &channels[0], 1, 0, mask, b_hist, 1, &histSize, &histRange, uniform, accumulate ); } cv::calcHist( &channels[1], 1, 0, mask, g_hist, 1, &histSize, &histRange, uniform, accumulate ); cv::calcHist( &channels[2], 1, 0, mask, r_hist, 1, &histSize, &histRange, uniform, accumulate ); //Draw the histograms int hist_w = 512; int hist_h = 400; int bin_w = cvRound( (double) hist_w/histSize ); cv::Mat histImage( hist_h, hist_w, CV_8UC3, cv::Scalar( 0,0,0) ); normalize(b_hist, b_hist, 0, histImage.rows, cv::NORM_MINMAX, -1, cv::Mat() ); normalize(g_hist, g_hist, 0, histImage.rows, cv::NORM_MINMAX, -1, cv::Mat() ); normalize(r_hist, r_hist, 0, histImage.rows, cv::NORM_MINMAX, -1, cv::Mat() ); for( int i = 1; i < histSize; i++ ) { if(i<(maxRange2-minRange)) { cv::line( histImage, cv::Point( bin_w*(i-1), hist_h - cvRound(b_hist.at<float>(i-1)) ) , cv::Point( bin_w*(i), hist_h - cvRound(b_hist.at<float>(i)) ), cv::Scalar( 255, 0, 0), 2, 8, 0 ); } cv::line( histImage, cv::Point( bin_w*(i-1), hist_h - cvRound(g_hist.at<float>(i-1)) ) , cv::Point( bin_w*(i), hist_h - cvRound(g_hist.at<float>(i)) ), cv::Scalar( 0, 255, 0), 2, 8, 0 ); cv::line( histImage, cv::Point( bin_w*(i-1), hist_h - cvRound(r_hist.at<float>(i-1)) ) , cv::Point( bin_w*(i), hist_h - cvRound(r_hist.at<float>(i)) ), cv::Scalar( 0, 0, 255), 2, 8, 0 ); } return histImage; }
void FeatureVectorBuilder::createSingleHistogramImage(cv::Mat& histogram, cv::Mat& output, int histSize, int histW, int histH) { int bin_w = cvRound((double) histW / histSize); cv::Mat histImage(histH, histW, CV_8UC3, cv::Scalar(0, 0, 0)); /// Normalize the result to [ 0, histImage.rows ] cv::normalize(histogram, histogram, 0, histImage.rows, cv::NORM_MINMAX, -1, cv::Mat()); /// Draw for each channel for (int i = 1; i < histSize; i++) { cv::line(histImage, cv::Point(bin_w * (i - 1), histH - cvRound(histogram.at<float>(i - 1))), cv::Point(bin_w * (i), histH - cvRound(histogram.at<float>(i))), cv::Scalar(255, 0, 0), 2, 8, 0); } histImage.copyTo(output); }
void histDisplay(int histogram[], const char* name) { int hist[256]; for (int i = 0; i < 256; i++) { hist[i] = histogram[i]; } // draw the histograms int hist_w = 512; int hist_h = 400; int bin_w = cvRound((double)hist_w / 256); cv::Mat histImage(hist_h, hist_w, CV_8UC1, cv::Scalar(255, 255, 255)); // find the maximum intensity element from histogram int max = hist[0]; for (int i = 1; i < 256; i++){ if (max < hist[i]){ max = hist[i]; } } // normalize the histogram between 0 and histImage.rows for (int i = 0; i < 256; i++){ hist[i] = ((double)hist[i] / max)*histImage.rows; } // draw the intensity line for histogram for (int i = 0; i < 256; i++) { line(histImage, cv::Point(bin_w*(i), hist_h), cv::Point(bin_w*(i), hist_h - hist[i]), cv::Scalar(0, 0, 0), 1, 8, 0); } // display histogram cv::namedWindow(name, CV_WINDOW_AUTOSIZE); imshow(name, histImage); }
cv::Mat FSVision::histogram(cv::Mat &img) { /// Separate the image in 3 places ( B, G and R ) /// Establish the number of bins int histSize = 256; /// Set the ranges ( for B,G,R) ) float range[] = { 0, 256 } ; const float* histRange = { range }; bool uniform = true; bool accumulate = false; cv::Mat hist; /// Compute the histograms: //cv::calcHist() cv::calcHist(&img, 1, 0, cv::Mat(), hist, 1, &histSize, &histRange, uniform, accumulate ); // Draw the histograms for B, G and R int hist_w = 512; int hist_h = 400; int bin_w = cvRound( (double) hist_w/histSize ); cv::Mat histImage( hist_h, hist_w, CV_8UC3, cv::Scalar(0,0,0) ); /// Normalize the result to [ 0, histImage.rows ] cv::normalize(hist, hist, 0, histImage.rows, cv::NORM_MINMAX, -1, cv::Mat() ); /// Draw for each channel for( int i = 1; i < histSize; i++ ) { cv::line( histImage, cv::Point( bin_w*(i-1), hist_h - cvRound(hist.at<float>(i-1)) ) , cv::Point( bin_w*(i), hist_h - cvRound(hist.at<float>(i)) ), cv::Scalar( 255, 0, 0), 2, 8, 0 ); } return histImage; }
void BlobDetection::showHist(cv::Mat& img, string name) const { #ifdef DEBUG /// Establish the number of bins int histSize = 256; /// Set the ranges ( for B,G,R) ) float range[] = { 0, 256 } ; const float* histRange = { range }; bool uniform = true; bool accumulate = false; Mat hist; /// Compute the histograms: calcHist( &img, 1, 0, Mat(), hist, 1, &histSize, &histRange, uniform, accumulate ); // Draw the histograms for B, G and R int hist_w = 512; int hist_h = 400; int bin_w = cvRound( (double) hist_w / histSize ); Mat histImage( hist_h, hist_w, CV_8UC1, Scalar( 0) ); /// Normalize the result to [ 0, histImage.rows ] normalize(hist, hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() ); /// Draw hist for( int i = 1; i < histSize; i++ ) line( histImage, Point( bin_w * (i-1), hist_h - cvRound(hist.at<float>(i-1)) ), Point( bin_w*(i), hist_h - cvRound(hist.at<float>(i)) ), Scalar( 255), 2, 8, 0 ); int upperBound = 42; n.getParam("upperBound", upperBound); cv::line(histImage, cv::Point( bin_w * upperBound, 0), cv::Point( bin_w * upperBound, hist_h - 1), Scalar(127), 2, 8, 0 ); /// Display namedWindow(name, CV_WINDOW_AUTOSIZE ); imshow(name, histImage ); #endif }
int main( int argc, char** argv ) { cv::Mat src, dst; /// Load image src = cv::imread("fontana.jpeg", 1); if( !src.data ) return -1; /// Separate the image in 3 places (B,G,R) one for each channel std::vector<cv::Mat> bgr_planes; split( src, bgr_planes ); /// Establish the number of bins int histSize = 256; /// Set the ranges ( for B,G,R) between 0 and 255 float range[] = { 0, 256 } ; const float* histRange = { range }; bool uniform = true; bool accumulate = false; /// Creation of 3 Mat objects to save each histogramm cv::Mat b_hist, g_hist, r_hist; /// Compute the histograms: cv::calcHist( &bgr_planes[0], 1, 0, cv::Mat(), b_hist, 1, &histSize, &histRange, uniform, accumulate ); cv::calcHist( &bgr_planes[1], 1, 0, cv::Mat(), g_hist, 1, &histSize, &histRange, uniform, accumulate ); cv::calcHist( &bgr_planes[2], 1, 0, cv::Mat(), r_hist, 1, &histSize, &histRange, uniform, accumulate ); /// Draw the histograms for B, G and R int hist_w = 512; int hist_h = 400; int bin_w = cvRound( (double) hist_w/histSize ); cv::Mat histImage( hist_h, hist_w, CV_8UC3, cv::Scalar( 0,0,0) ); /// Normalize the result to [ 0, histImage.rows ] cv::normalize(b_hist, b_hist, 0, histImage.rows, cv::NORM_MINMAX, -1, cv::Mat() ); cv::normalize(g_hist, g_hist, 0, histImage.rows, cv::NORM_MINMAX, -1, cv::Mat() ); cv::normalize(r_hist, r_hist, 0, histImage.rows, cv::NORM_MINMAX, -1, cv::Mat() ); /// Draw for each channel for( int i = 1; i < histSize; i++ ) { cv::line( histImage, cv::Point( bin_w*(i-1), hist_h - cvRound(b_hist.at<float>(i-1)) ) , cv::Point( bin_w*(i), hist_h - cvRound(b_hist.at<float>(i)) ), cv::Scalar( 255, 0, 0), 2, 8, 0 ); cv::line( histImage, cv::Point( bin_w*(i-1), hist_h - cvRound(g_hist.at<float>(i-1)) ) , cv::Point( bin_w*(i), hist_h - cvRound(g_hist.at<float>(i)) ), cv::Scalar( 0, 255, 0), 2, 8, 0 ); cv::line( histImage, cv::Point( bin_w*(i-1), hist_h - cvRound(r_hist.at<float>(i-1)) ) , cv::Point( bin_w*(i), hist_h - cvRound(r_hist.at<float>(i)) ), cv::Scalar( 0, 0, 255), 2, 8, 0 ); } /// Display cv::imshow("calcHist Demo", histImage ); cv::waitKey(0); return 0; }
CvPoint2D32f getPupilCenter(Mat &eye_box){ //find x and y gradients Mat gradientX = computeGradient(eye_box); Mat gradientY = computeGradient(eye_box.t()).t(); //normalize and threshold the gradient Mat mags = matrixMagnitude(gradientX, gradientY); //create a blurred and inverted image for weighting Mat weight; bitwise_not(eye_box, weight); blur(weight, weight, Size(2,2)); //weight the magnitudes, convert to 8-bit for thresholding weight.convertTo(weight, CV_32F); mags = mags.mul(weight); normalize(mags, mags, 0, 1, NORM_MINMAX, CV_32F); mags.convertTo(mags, CV_8UC1, 255); //threshold using Otsu's method threshold(mags, mags, 0, 255, THRESH_BINARY | THRESH_OTSU); //convert to CV_32S and filter gradients mags.convertTo(mags, CV_32S); gradientY = gradientY.mul(mags); gradientX = gradientX.mul(mags); //resize arrays to same size resize(gradientX, gradientX, Size(EYE_FRAME_SIZE, EYE_FRAME_SIZE), 0, 0, INTER_NEAREST); resize(gradientY, gradientY, Size(EYE_FRAME_SIZE, EYE_FRAME_SIZE), 0, 0, INTER_NEAREST); resize(weight, weight, Size(EYE_FRAME_SIZE, EYE_FRAME_SIZE), 0, 0, INTER_NEAREST); //imshow("gradY", gradientY * 255); //imshow("weight", weight / 255); //run the algorithm: // for each possible gradient location // Note: these loops are reversed from the way the paper does them // it evaluates every possible center for each gradient location instead of // every possible gradient location for every center. Mat out = Mat::zeros(weight.rows,weight.cols, CV_32F); float max_val = 0; //for all pixels in the image for (int y = 0; y < EYE_FRAME_SIZE; ++y) { const int *grad_x = gradientX.ptr<int>(y), *grad_y = gradientY.ptr<int>(y); for (int x = 0; x < EYE_FRAME_SIZE; ++x) { int gX = grad_x[x], gY = grad_y[x]; if (gX == 0 && gY == 0) { continue; } //for all possible centers for (int cy = 0; cy < EYE_FRAME_SIZE; ++cy) { float *Or = out.ptr<float>(cy); const float *Wr = weight.ptr<float>(cy); for (int cx = 0; cx < EYE_FRAME_SIZE; ++cx) { //ignore center of box if (x == cx && y == cy) { continue; } //create a vector from the possible center to the gradient origin int dx = x - cx; int dy = y - cy; //compute dot product using lookup table float dotProduct; if(dx > 0 && dy > 0){ dotProduct = dpX[dx+EYE_FRAME_SIZE*dy]*gX + dpY[dx+EYE_FRAME_SIZE*dy]*gY; }else if(dx > 0){ dotProduct = dpX[dx-EYE_FRAME_SIZE*dy]*gX - dpY[dx-EYE_FRAME_SIZE*dy]*gY; }else if(dy > 0){ dotProduct = -dpX[-dx+EYE_FRAME_SIZE*dy]*gX - dpY[-dx+EYE_FRAME_SIZE*dy]*gY; }else{ dotProduct = -dpX[-dx-EYE_FRAME_SIZE*dy]*gX - dpY[-dx-EYE_FRAME_SIZE*dy]*gY; } //ignore negative dot products as they point away from eye if(dotProduct <= 0.0){ continue; } //square and multiply by the weight Or[cx] += dotProduct * dotProduct * Wr[cx]; //compare with max if(Or[cx] > max_val){ max_val = Or[cx]; } } } } } //resize for debugging resize(out, out, Size(500,500), 0, 0, INTER_NEAREST); out = 255 * out / max_val; //imshow("calc", out / 255); //histogram setup Mat hist; int histSize = 256; float range[] = { 0, 256 } ; const float* histRange = { range }; //calculate the histogram calcHist(&out,1, 0, Mat(), hist, 1, &histSize, &histRange, true, //uniform true //accumulate ); //get cutoff for top 10 pixels float top_end_sum = 0; int top_end = 0.92 * 255; for (int i = 255; i > 0; i--) { top_end_sum += hist.at<float>(i); if(top_end_sum > 3000){ top_end = i; break; } } //draw image for debugging Mat histImage(400, 512, CV_8UC3, Scalar(0,0,0)); int bin_w = cvRound( (double) 512/histSize ); normalize(hist, hist, 0, histImage.rows, NORM_MINMAX, -1, Mat()); /// Draw for each channel for( int i = 1; i < histSize; i++) { line(histImage, Point(bin_w*(i), 400 - cvRound(hist.at<float>(i))), Point(bin_w*(i), 400), Scalar(i, i, i), 2, 8, 0); } //imshow("hist", histImage); //threshold to get just the pupil //printf("top_end: %d\n", top_end); threshold(out, out, top_end, 255, THRESH_TOZERO); //calc center of mass float sum = 0; float sum_x = 0; float sum_y = 0; for (int y = 0; y < out.rows; ++y) { float* row = out.ptr<float>(y); for (int x = 0; x < out.cols; ++x) { float val = row[x]*row[x]; if(val > 0){ sum += val; sum_x += val*x; sum_y += val*y; } } } Size eye_box_size = eye_box.size(); Size out_size = out.size(); //cout << "Size1: "+to_string(eye_box_size.width)+","+to_string(eye_box_size.height)+"\n"; //cout << "Size2: "+to_string(out_size.width)+","+to_string(out_size.height)+"\n"; float x_scale = (float) eye_box_size.width / out_size.width; float y_scale = (float) eye_box_size.height / out_size.height; CvPoint2D32f max = cvPoint2D32f(x_scale*sum_x/sum, y_scale*sum_y/sum); //circle(out, max, 3, 0); //imshow("thresh", out / 255); return max; }
int main(int argc, char* argv[]) { char* filename = argc == 2 ? argv[1] : "test.avi"; VideoCapture capture(filename); if (!capture.isOpened()) return -1; namedWindow("Video", CV_WINDOW_AUTOSIZE); //namedWindow("Hist", CV_WINDOW_AUTOSIZE); vector<int> vec; int number = 0; int width = capture.get(CV_CAP_PROP_FRAME_WIDTH); int height = capture.get(CV_CAP_PROP_FRAME_HEIGHT); int wait = 1000 / capture.get(CV_CAP_PROP_FPS); int temp = width * PERCENT_EDGE; long int amount_pixels = (width * height) - (2 * temp * height); cv::Mat mask(height, width, CV_8U, 0.0); // создание маски, дл¤ подсчета гистограммы без учЄта краЄв rectangle(mask, cv::Point(temp,0), cv::Point(width - temp, height), 1, CV_FILLED); // ѕараметры гистограммы int histSize = 256; float range[] = { 0, 256 }; const float* histRange = { range }; bool uniform = true; bool accumulate = false; int hist_w = 512; int hist_h = 400; int bin_w = cvRound((double)hist_w / histSize); while (capture.isOpened()) { Mat src; if (!capture.read(src)) break; imshow("Video", src); // Ѕудем считать гистогрмму только дл¤ канала R std::vector<Mat> bgr_planes; split(src, bgr_planes); Mat r_hist; calcHist(&bgr_planes[2], 1, 0, mask, r_hist, 1, &histSize, &histRange, uniform, accumulate); Mat histImage(hist_h, hist_w, CV_8UC3, Scalar(0, 0, 0)); HistEvidenceAnaliz(&r_hist, histSize, amount_pixels, &vec, number); // Normalize the result to [ 0, histImage.rows ] //normalize(r_hist, r_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat()); // Draw Hist /*for (int i = 1; i < histSize; i++) { line(histImage, Point(bin_w*(i - 1), hist_h - cvRound(r_hist.at<float>(i - 1))), Point(bin_w*(i), hist_h - cvRound(r_hist.at<float>(i))), Scalar(0, 0, 255), 2, 8, 0); }*/ //imshow("Hist", histImage); if (waitKey(wait) >= 0) break; } vector<pair<int, int>> linking_frames; int counter = 0; int start = 0; for (vector<int>::iterator it = vec.begin(); it != vec.end(); it++) { // рассчЄт мест сшивки, чтобы избежать попадани¤ случайных кадров if ((it + 1) != vec.end()) { if (*(it + 1) - *it <= THRESHOLD_GAP_FRAME) { if (counter == 0) start = *it; counter++; } else { if (counter < THRESHOLD_FRAME_SEQUENCE) counter = 0; else { if ((*it) - start > MIN_LENGTH_SEQUENCE_FRAME) linking_frames.push_back(pair<int, int>(start, *it)); counter = 0; } } } else if (counter >= THRESHOLD_FRAME_SEQUENCE) { if ((*it) - start > MIN_LENGTH_SEQUENCE_FRAME) linking_frames.push_back(pair<int, int>(start, *it)); } } cout << "Linking frames:" << endl; for (vector<pair<int, int>>::iterator it = linking_frames.begin(); it != linking_frames.end(); it++) cout << it->first << " - " << it->second << endl; getchar(); return 0; }
void CImageAnalysis::OnBnClickedHough() { CMainFrame * pwnd = (CMainFrame *)AfxGetMainWnd(); GetDlgItem(IDC_Show); Mat mat = imread( "F:\\4点定位剪切图.bmp",CV_LOAD_IMAGE_ANYDEPTH|CV_LOAD_IMAGE_ANYCOLOR ); Mat src,src1,src2; mat.copyTo(src); medianBlur(src,src1,5);//中值滤波 int threshold1=100; Mat hist; int histSize = 255; float range[] = { 0, 255 } ; const float* histRange = { range }; bool uniform = true; bool accumulate = false; calcHist( &src1, 1, 0, Mat(), hist, 1, &histSize, &histRange, uniform, accumulate ); int hist_w = 400; int hist_h = 400; int bin_w = cvRound( (double) 3*hist_w/histSize ); Mat histImage( hist_w, hist_h, CV_8UC3, Scalar( 255,255,255) ); normalize(hist, hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() ); for( int i = 1; i < 255; i++ ) { line( histImage, Point( bin_w*(i-1), hist_h - cvRound(hist.at<float>(i-1)) ) , Point( bin_w*(i), hist_h - cvRound(hist.at<float>(i)) ), Scalar( 0, 0, 0), 2, 8, 0 ); /*line( histImage, Point( bin_w*(i), hist_h - cvRound(hist.at<float>(i)) ) , Point( bin_w*(i), hist_h ), Scalar( 0, 0, 0), 2, 8, 0 );*/ } namedWindow("calcHist Demo", CV_WINDOW_AUTOSIZE ); imshow("calcHist Demo", histImage ); imwrite("hist.bmp",histImage); //Histogram(); //m_Show.ShowImage(hist,0); //threshold(src1,src1,threshold1,255,THRESH_BINARY);//二值图的生成; //vector<vector<Point> > contours; //vector<Vec3f> circles; //findContours(src2,contours,CV_RETR_LIST,CV_CHAIN_APPROX_NONE); // Apply the Hough Transform to find the circles /*HoughCircles( src,circles, CV_HOUGH_GRADIENT, 1, 1, 200, 10, 2,1000); for( size_t i = 0; i < circles.size(); i++ ) { Point center(cvRound(circles[i][0]), cvRound(circles[i][1])); int radius = cvRound(circles[i][2]); // circle center circle( src, center, 3, Scalar(0,255,0), -1, 8, 0 ); // circle outline circle( src, center, radius, Scalar(0,0,255), 3, 8, 0 ); }*/ /*pwnd->m_imageprocess.ImageRotate(src1,src2,1.5); Mat src3=Mat::zeros(src2.rows,src2.cols,src2.type());//src2.cols+10 Mat dst=Mat::zeros(src2.rows,src2.cols,src2.type()); int src2_half_rows=src2.rows/2; int src1_half_rows=src1.rows/2; int i,j,k; uchar *p,*q; MatIterator_<uchar> begin1,begin2; begin1=src1.begin<uchar>(); begin2=src2.begin<uchar>(); for(i=src2_half_rows-src1_half_rows;i!=src2_half_rows+src1_half_rows;++i) { p=src3.ptr<uchar>(i); for(j=0;j!=src1.cols;++j) { p[j]=*begin1; ++begin1; } } int dx=825,dy=5;//dst中只装载src1 小于(828,0)则向原点移动 for (int i = 0; i < dst.rows; i++) { p = dst.ptr<uchar>(i); for (int j = 0; j < dst.cols; j++) { //平移后坐标映射到原图像 int x = j - dx; int y = i - dy; //保证映射后的坐标在原图像范围内 if (x >= 0 && y >= 0 && x <src3. cols && y < src3.rows) p[j] = src3.ptr<uchar>(y)[x]; } } for(i=0;i!=src2.rows;++i) { q=dst.ptr<uchar>(i); for(j=0;j!=src2.cols;++j)//src2.cols+10 { k=q[j]/2+*begin2/2; q[j]=k; ++begin2; } } //m_Show.ShowImage(src2,0); //m_Show.ShowImage(dst,0); //imwrite("F:\\result1.bmp",dst); threshold(dst,dst,threshold1,255,THRESH_BINARY);//二值图的生成; vector<vector<Point> > contours; findContours(dst,contours,CV_RETR_LIST,CV_CHAIN_APPROX_NONE); //system("pause"); /// 绘出轮廓 Mat gray ; cvtColor(dst,gray , CV_GRAY2BGR); for( int i = 0; i<contours.size(); i++ ) { drawContours( gray, contours, i, Scalar(255,0,0), 2, 8, contours[0], 0, Point() ); } m_Show.ShowImage(hist,0);*/ }
void showHistogram(Mat im,string str) { im.convertTo(im, CV_8U); cout << "th1=" <<TH1<< endl; string savePath; /// 设定bin数目 int histSize = 256-TH1; /// 设定取值范围 ( R,G,B) ) float range[] = { TH1, 255 }; const float* histRange = { range }; bool uniform = true; bool accumulate = true; Mat r_hist; /// 计算直方图: calcHist(&im, 1, 0, Mat(), r_hist, 1, &histSize, &histRange, uniform, true); savePath = "./output/histogram_" + str + ".xls"; outXls(savePath,r_hist,"float" ); Mat accumulate_hist = Mat::zeros(r_hist.rows, 1, CV_32F); // 创建直方图画布 int hist_w = 400; int hist_h = 400; int bin_w = cvRound((double)hist_w / histSize); Mat histImage(hist_w, hist_h, CV_8UC3, Scalar(0, 0, 0)); //将负的直方图扩展进来 //accumulate histogram for (int m = 0; m < r_hist.rows;m++) { accumulate_hist.at<float>(m, 0) = sum(r_hist,m); } outXls("./output/accuxls.xls", accumulate_hist, "float"); /// 将直方图归一化到范围 [ 0, histImage.rows ],[0,400] normalize(r_hist, r_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat()); //normalize(accumulate_hist, accumulate_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat()); /// 在直方图画布上画出直方图 for (int i = 1; i < histSize; i++) { /*Point up(bin_w*(i - 1), hist_h - cvRound(r_hist.at<float>(i - 1))); Point bottom(bin_w*(i - 1), hist_h); line(histImage, bottom, up, Scalar(0, 0, 255), 2, 8, 0);*/ /*line(histImage, Point(bin_w*(i - 1), hist_h - cvRound(accumulate_hist.at<float>(i - 1))), Point(bin_w*(i), hist_h - cvRound(accumulate_hist.at<float>(i))), Scalar(0, 0, 255), 2, 8, 0);*/ line(histImage, Point(bin_w*(i - 1), hist_h - cvRound(r_hist.at<float>(i - 1))), Point(bin_w*(i), hist_h - cvRound(r_hist.at<float>(i))), Scalar(0, 0, 255), 2, 8, 0); } /// 显示直方图 imshow(str, histImage); }
//----------------------------------------------------------------------------------------------------- void FrameAnalyzerHistogram::analyze(Mat &input, Mat &output) { lastFrame=currentFrame; currentFrame=input; frameList.append(input); std::cout << frameList.length() << std::endl; /// ---------------------------------------------------------------------- vector<Mat> bgr_planes; /// Separate the image in 3 places ( B, G and R ) split(input, bgr_planes); // Установка количества бинов: int histSize = 256; // Установка граничных значений ( for B,G,R) ) float range[] = { 0, 256 }; const float* histRange = { range }; bool uniform = true; bool accumulate = false; Mat b_hist, g_hist, r_hist; // Вычислить гистограммы: calcHist(&bgr_planes[0], 1, 0, Mat(), b_hist, 1, &histSize, &histRange, uniform, accumulate); calcHist(&bgr_planes[1], 1, 0, Mat(), g_hist, 1, &histSize, &histRange, uniform, accumulate); calcHist(&bgr_planes[2], 1, 0, Mat(), r_hist, 1, &histSize, &histRange, uniform, accumulate); // Отрисовать гистограммы для B, G и R int hist_w = 640; int hist_h = 480; int bin_w = cvRound((double) hist_w / histSize); Mat histImage(hist_h, hist_w, CV_8UC3, Scalar(0, 0, 0)); // Нормализация результата к [ 0, histImage.rows ] normalize(b_hist, b_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat()); normalize(g_hist, g_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat()); normalize(r_hist, r_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat()); // Отрисовать для каждого канала for (int i = 1; i < histSize; i++) { line(histImage, Point(bin_w * (i - 1), hist_h - cvRound(b_hist.at<float>(i - 1))), Point(bin_w * (i), hist_h - cvRound(b_hist.at<float>(i))), Scalar(255, 0, 0), 2, 8, 0); line(histImage, Point(bin_w * (i - 1), hist_h - cvRound(g_hist.at<float>(i - 1))), Point(bin_w * (i), hist_h - cvRound(g_hist.at<float>(i))), Scalar(0, 255, 0), 2, 8, 0); line(histImage, Point(bin_w * (i - 1), hist_h - cvRound(r_hist.at<float>(i - 1))), Point(bin_w * (i), hist_h - cvRound(r_hist.at<float>(i))), Scalar(0, 0, 255), 2, 8, 0); } output = histImage; /* /// Display namedWindow("calcHist Demo", CV_WINDOW_AUTOSIZE); imshow("calcHist Demo", histImage); */ }