opencv中计算Back Projection的函数为calcBackProject,mixChannels是用来从输入中拷贝某通道到输出中特定的通道。
还是以例子说明
(1)例如灰度图像如下
Image=
(2)该灰度图的直方图为(bin指定的区间为[0,3),[4,7),[8,11),[12,16))
Histogram=
(3)反向投影图
Back_Projection=
例如位置(0,0)上的像素值为0,对应的bin为[0,3),所以反向直方图在该位置上的值这个bin的值4。
代码如下:
老版本:
#include <cv.h>
#include <highgui.h>
#include"stdio.h"
int main()
{
IplImage*src= cvLoadImage("hand.jpg", 1);
IplImage*templ=cvLoadImage("part.jpg",1);
cvNamedWindow( "Source" );
cvShowImage( "Source", src );
IplImage* h_plane2 = cvCreateImage( cvGetSize(src), 8, 1 );
IplImage* s_plane2 = cvCreateImage( cvGetSize(src), 8, 1 );
IplImage* v_plane2 = cvCreateImage( cvGetSize(src), 8, 1);
IplImage* planes2[] = { h_plane2, s_plane2,v_plane2 };
IplImage* hsv2 = cvCreateImage( cvGetSize(src), 8, 3 );
cvCvtColor( src, hsv2, CV_BGR2HSV );
cvSplit( hsv2, h_plane2, s_plane2, v_plane2, 0 );
printf("h%d",h_plane2->widthStep);
printf("s%d",h_plane2->widthStep);
printf("v%d",h_plane2->widthStep);
IplImage* h_plane = cvCreateImage( cvGetSize(templ), 8, 1 );
IplImage* s_plane = cvCreateImage( cvGetSize(templ), 8, 1 );
IplImage* v_plane = cvCreateImage( cvGetSize(templ), 8, 1);
IplImage* planes[] = { h_plane, s_plane,v_plane };
IplImage* hsv = cvCreateImage( cvGetSize(templ), 8, 3 );
cvCvtColor( templ, hsv, CV_BGR2HSV );
cvSplit( hsv, h_plane, s_plane, v_plane, 0 );
printf("h%d\n",h_plane->widthStep);
printf("s%d\n",s_plane->widthStep);
printf("v%d\n",v_plane->widthStep);
int h_bins = 30, s_bins = 32,v_bins=32;
int hist_size[] = {h_bins, s_bins,v_bins};
float h_ranges[] = { 0, 180 };
float s_ranges[]={0,255};
float v_ranges[]={0,255};
float* ranges[] = { h_ranges, s_ranges,v_ranges};
CvHistogram* hist;
hist = cvCreateHist( 3, hist_size, CV_HIST_ARRAY, ranges, 1 );
cvCalcHist( planes, hist, 0, 0 );
//1.double a=1.f;
//2.cvNormalizeHist(hist,a);
//templ's hist is just calculate
IplImage*back_project=cvCreateImage(cvGetSize(src),8,1);//!!归一,把8改成32,就弹出对话框,说planes的steps不是一致的!
cvZero(back_project); //但是我去掉归一,改成8就可以显示了。为什么浮点型不行呢???
//NOW we begin calculate back project
cvCalcBackProject(planes2,back_project,hist);
cvNamedWindow( "back_project" );
cvShowImage( "back_project", back_project );
cvWaitKey(0);
return 0;
}
新版本
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
using namespace cv;
using namespace std;
/// Global Variables
Mat src; Mat hsv; Mat hue;
vector<Mat>h_s_v;
int bins = 25;
/// Function Headers
void Hist_and_Backproj(int, void* );
/** @function main */
int main( int argc, char** argv )
{
/// Read the image
src = imread("200.jpg", 1 );
/// Transform it to HSV
cvtColor( src, hsv, CV_BGR2HSV );
/// Use only the Hue value
hue.create( hsv.size(), hsv.depth() );
//int ch[] = { 0, 0 };
//mixChannels( &hsv, 1, &hue, 1, ch, 1 );
/// Create Trackbar to enter the number of bins
split(hsv,h_s_v);
char* window_image = "Source image";
namedWindow( window_image, CV_WINDOW_AUTOSIZE );
createTrackbar("* Hue bins: ", window_image, &bins, 180, Hist_and_Backproj );
Hist_and_Backproj(0, 0);
/// Show the image
imshow( window_image, src );
/// Wait until user exits the program
waitKey(0);
return 0;
}
/**
* @function Hist_and_Backproj
* @brief Callback to Trackbar
*/
void Hist_and_Backproj(int, void* )
{
MatND hist;
int histSize = MAX( bins, 2 );
float hue_range[] = { 0, 180 };
float s_range[] = { 0, 255 };
const float* ranges = { hue_range,s };
/// Get the Histogram and normalize it
calcHist( &hue, 1, 0, Mat(), hist, 1, &histSize, &ranges, true, false );
normalize( hist, hist, 0, 255, NORM_MINMAX, -1, Mat() );
/// Get Backprojection
MatND backproj;
calcBackProject( &hue, 1, 0, hist, backproj, &ranges, 1, true );
/// Draw the backproj
imshow( "BackProj", backproj );
/// Draw the histogram
int w = 400; int h = 400;
int bin_w = cvRound( (double) w / histSize );
Mat histImg = Mat::zeros( w, h, CV_8UC3 );
for( int i = 0; i < bins; i ++ )
{
rectangle( histImg, Point( i*bin_w, h ), Point( (i+1)*bin_w, h - cvRound( hist.at<float>(i)*h/255.0 ) ), Scalar(0,0,255),1,8,0);
}
imshow( "Histogram", histImg );
}