在学习 《学习opencv》的cvCalcBackProject时自己写的例子
- #include <iostream>
- #include <cv.h>
- #include <highgui.h>
- #include <cxcore.h>
- #include <opencv2/opencv.hpp>
- using namespace std;
- int main()
- {
- IplImage *src = cvLoadImage("f:\\images\\hand00.jpg");
- IplImage *hsv_src = cvCloneImage(src);
- cvCvtColor(src,hsv_src,CV_BGR2HSV);
- IplImage *h_plane_src = cvCreateImage(cvGetSize(src),8,1);
- cvSplit(hsv_src,h_plane_src,NULL,NULL,NULL);
- cvSave("h.xml",h_plane_src);
- int dims = 1;
- int size[] = {180};
- float ranges_h[] ={0,181};
- float *ranges[] = {ranges_h};
- CvHistogram *hist_src = cvCreateHist(1,size,CV_HIST_ARRAY,ranges);
- cvCalcHist(&h_plane_src,hist_src);
- IplImage *dst = cvLoadImage("f:\\images\\hand1.jpg");
- IplImage *hsv_dst = cvCreateImage(cvGetSize(dst),8,3);
- cvCvtColor(dst,hsv_dst,CV_BGR2HSV);
- IplImage *h_plane_dst = cvCreateImage(cvGetSize(dst),8,1);
- cvSplit(hsv_dst,h_plane_dst,NULL,NULL,NULL);
- IplImage *dst_probability = cvCreateImage(cvGetSize(h_plane_dst),IPL_DEPTH_8U,1);
- cvZero(dst_probability);
- cvCalcBackProject(&h_plane_dst,dst_probability,hist_src);
- cout<<cvSum(dst_probability).val[0]<<endl;
- cvShowImage("result",dst_probability);
- cvThreshold(dst_probability,dst_probability,250,255,CV_THRESH_BINARY);
- cvShowImage("dst1",dst_probability);
- IplConvKernel *kernel = cvCreateStructuringElementEx(31,31,15,15,CV_SHAPE_RECT);
- cvMorphologyEx(dst_probability,dst_probability,NULL,kernel,CV_MOP_OPEN);
- cvShowImage("dst2",dst_probability);
- cvWaitKey();
- cvReleaseImage(&src);
- cvReleaseImage(&hsv_src);
- cvReleaseImage(&h_plane_src);
- cvReleaseImage(&dst);
- cvReleaseImage(&hsv_dst);
- cvReleaseImage(&h_plane_dst);
- cvReleaseHist(&hist_src);
- cvDestroyAllWindows();
- }
其中hand00.jpg图片为:
第二次读入的图片可为:
输出的结果: