您现在的位置:首页 > 教案格式 > 正文

第七章 学习OpenCV(6)

2019-07-04 21:05 网络整理 教案网

运行结果如下图:

肤色图像

EMD距离

利用手机的手的图像,设计一个直方图,可以判断给定的图像是在哪种光线条件下被捕捉到的。然后,建立亮度值采样特征程序中三幅图像已经过处理,依次比前一幅亮度增加25,具体代码如下:

#include <cv.h>
#include <highgui.h>  
#include <stdlib.h>  
#include <stdio.h> 
#include <math.h>
using namespace std;
int main(int argc, char* argv[])
{
    //源图像 HSV格式图像
    IplImage* src1, *src2, *src3, *src4, *src5, *src6, *Imask, *hsv1, *hsv2, *hsv3, *hsv4,*hsv5, *hsv6;     
    //src1 src2 亮度相同  src3 src4亮度相同 src5 src6亮度相同  每级依次增加20
    //模板 室内
    if (!(src1 = cvLoadImage("D:\\Template\\OpenCV\\Template54_value_Compare\\Debug\\handdd.jpg")))
        return -1;
    //测试 室内
    if (!(src2 = cvLoadImage("D:\\Template\\OpenCV\\Template54_value_Compare\\Debug\\handd.jpg")))
        return -2;
    //模板 室外阴影
    if (!(src3 = cvLoadImage("D:\\Template\\OpenCV\\Template54_value_Compare\\Debug\\handdd_out.jpg")))
        return -3;
    //测试 室外阴影
    if (!(src4 = cvLoadImage("D:\\Template\\OpenCV\\Template54_value_Compare\\Debug\\handd_out.jpg")))
        return -4;
    //模板 室外光照
    if (!(src5 = cvLoadImage("D:\\Template\\OpenCV\\Template54_value_Compare\\Debug\\handdd_out_sun.jpg")))
        return -5;
    if (!(src6 = cvLoadImage("D:\\Template\\OpenCV\\Template54_value_Compare\\Debug\\handd_out_sun.jpg")))
        return -6;
    //Mask为手掌掩码 过滤掉其他背景 只分析手掌颜色直方图 可略
    if (!(Imask = cvLoadImage("D:\\Template\\OpenCV\\Template54_value_Compare\\Debug\\Imask.jpg", 
        CV_LOAD_IMAGE_GRAYSCALE)))
        return -7;
    hsv1 = cvCreateImage(cvGetSize(src1), src1->depth, src1->nChannels);
    hsv2 = cvCreateImage(cvGetSize(src2), src2->depth, src2->nChannels);
    hsv3 = cvCreateImage(cvGetSize(src3), src3->depth, src3->nChannels);
    hsv4 = cvCreateImage(cvGetSize(src4), src4->depth, src4->nChannels);
    hsv5 = cvCreateImage(cvGetSize(src5), src5->depth, src5->nChannels);
    hsv6 = cvCreateImage(cvGetSize(src6), src6->depth, src6->nChannels);
    cvCvtColor(src1, hsv1, CV_BGR2HSV);     //源图像->HSV格式图像
    cvCvtColor(src2, hsv2, CV_BGR2HSV);     //源图像->HSV格式图像
    cvCvtColor(src3, hsv3, CV_BGR2HSV);     //源图像->HSV格式图像
    cvCvtColor(src4, hsv4, CV_BGR2HSV);     //源图像->HSV格式图像
    cvCvtColor(src5, hsv5, CV_BGR2HSV);     //源图像->HSV格式图像
    cvCvtColor(src6, hsv6, CV_BGR2HSV);     //源图像->HSV格式图像
    //色调(hue) 饱和度(saturation) 明度(value)
    IplImage *h_plane_1 = cvCreateImage(cvSize(hsv1->width, hsv1->height), IPL_DEPTH_8U, 1);
    IplImage *s_plane_1 = cvCreateImage(cvSize(hsv1->width, hsv1->height), IPL_DEPTH_8U, 1);
    IplImage *v_plane_1 = cvCreateImage(cvSize(hsv1->width, hsv1->height), IPL_DEPTH_8U, 1);
    IplImage *h_plane_2 = cvCreateImage(cvSize(hsv2->width, hsv2->height), IPL_DEPTH_8U, 1);
    IplImage *s_plane_2 = cvCreateImage(cvSize(hsv2->width, hsv2->height), IPL_DEPTH_8U, 1);
    IplImage *v_plane_2 = cvCreateImage(cvSize(hsv2->width, hsv2->height), IPL_DEPTH_8U, 1);
    IplImage *h_plane_3 = cvCreateImage(cvSize(hsv3->width, hsv3->height), IPL_DEPTH_8U, 1);
    IplImage *s_plane_3 = cvCreateImage(cvSize(hsv3->width, hsv3->height), IPL_DEPTH_8U, 1);
    IplImage *v_plane_3 = cvCreateImage(cvSize(hsv3->width, hsv3->height), IPL_DEPTH_8U, 1);
    IplImage *h_plane_4 = cvCreateImage(cvSize(hsv4->width, hsv4->height), IPL_DEPTH_8U, 1);
    IplImage *s_plane_4 = cvCreateImage(cvSize(hsv4->width, hsv4->height), IPL_DEPTH_8U, 1);
    IplImage *v_plane_4 = cvCreateImage(cvSize(hsv4->width, hsv4->height), IPL_DEPTH_8U, 1);
    IplImage *h_plane_5 = cvCreateImage(cvSize(hsv5->width, hsv5->height), IPL_DEPTH_8U, 1);
    IplImage *s_plane_5 = cvCreateImage(cvSize(hsv5->width, hsv5->height), IPL_DEPTH_8U, 1);
    IplImage *v_plane_5 = cvCreateImage(cvSize(hsv5->width, hsv5->height), IPL_DEPTH_8U, 1);
    IplImage *h_plane_6 = cvCreateImage(cvSize(hsv6->width, hsv6->height), IPL_DEPTH_8U, 1);
    IplImage *s_plane_6 = cvCreateImage(cvSize(hsv6->width, hsv6->height), IPL_DEPTH_8U, 1);
    IplImage *v_plane_6 = cvCreateImage(cvSize(hsv6->width, hsv6->height), IPL_DEPTH_8U, 1);
    IplImage *planes1[] = { v_plane_1 };                        //色相饱和度数组
    IplImage *planes2[] = { v_plane_2 };                        //色相饱和度数组
    IplImage *planes3[] = { v_plane_3 };                        //色相饱和度数组
    IplImage *planes4[] = { v_plane_4 };                        //色相饱和度数组
    IplImage *planes5[] = { v_plane_5 };                        //色相饱和度数组
    IplImage *planes6[] = { v_plane_6 };                        //色相饱和度数组
    cvCvtPixToPlane(hsv1, h_plane_1, s_plane_1, v_plane_1, NULL);       //图像分割
    cvCvtPixToPlane(hsv2, h_plane_2, s_plane_2, v_plane_2, NULL);       //图像分割
    cvCvtPixToPlane(hsv3, h_plane_3, s_plane_3, v_plane_3, NULL);       //图像分割
    cvCvtPixToPlane(hsv4, h_plane_4, s_plane_4, v_plane_4, NULL);       //图像分割
    cvCvtPixToPlane(hsv5, h_plane_5, s_plane_5, v_plane_5, NULL);       //图像分割
    cvCvtPixToPlane(hsv6, h_plane_6, s_plane_6, v_plane_6, NULL);       //图像分割
    //cvSplit(hsv, h_plane, s_plane, v_plane, NULL);
    //建立直方图
    CvHistogram *hist[6];
    int v_bins = 32;
    int hist_size[] = { v_bins };               //对应维数包含bins个数的数组
    float v_ranges[] = { 0, 255 };              //V通道划分范围
    float* ranges[] = { v_ranges }; //划分范围数对, ****均匀bin,range只要最大最小边界
    //创建直方图 (维数,对应维数bins个数,密集矩阵方式存储,划分范围数对,均匀直方图)
    hist[0] = cvCreateHist(1, hist_size, CV_HIST_ARRAY, ranges, 1);
    hist[1] = cvCreateHist(1, hist_size, CV_HIST_ARRAY, ranges, 1);
    hist[2] = cvCreateHist(1, hist_size, CV_HIST_ARRAY, ranges, 1);
    hist[3] = cvCreateHist(1, hist_size, CV_HIST_ARRAY, ranges, 1);
    hist[4] = cvCreateHist(1, hist_size, CV_HIST_ARRAY, ranges, 1);
    hist[5] = cvCreateHist(1, hist_size, CV_HIST_ARRAY, ranges, 1);
    //创建直方图 (维数,对应维数bins个数,密集矩阵方式存储,划分范围数对,均匀直方图)
    cvCalcHist(planes1, hist[0], 0, Imask); //计算直方图(图像,直方图结构,不累加,mask仅采集手掌)
    cvCalcHist(planes2, hist[1], 0, 0);     //计算直方图(图像,直方图结构,不累加,掩码)
    cvCalcHist(planes3, hist[2], 0, Imask);     //计算直方图(图像,直方图结构,不累加,掩码)
    cvCalcHist(planes4, hist[3], 0, 0);     //计算直方图(图像,直方图结构,不累加,掩码)
    cvCalcHist(planes5, hist[4], 0, Imask);     //计算直方图(图像,直方图结构,不累加,掩码)
    cvCalcHist(planes6, hist[5], 0, 0);     //计算直方图(图像,直方图结构,不累加,掩码)
    //直方图归一化
    cvNormalizeHist(hist[0], 1.0);
    cvNormalizeHist(hist[1], 1.0);
    cvNormalizeHist(hist[2], 1.0);
    cvNormalizeHist(hist[3], 1.0);
    cvNormalizeHist(hist[4], 1.0);
    cvNormalizeHist(hist[5], 1.0);
    for (int i = 1; i < 4; i++)
    {
        double min = 0,max=0;
        CvPoint point;
        CvMat* mat = cvCreateMat(1, 3, CV_64FC1);
        double value1 = cvCompareHist(hist[0], hist[2 * i - 1], CV_COMP_BHATTACHARYYA); //B距离方式比较
        double value2 = cvCompareHist(hist[2], hist[2 * i - 1], CV_COMP_BHATTACHARYYA); //B距离方式比较
        double value3 = cvCompareHist(hist[4], hist[2 * i - 1], CV_COMP_BHATTACHARYYA); //B距离方式比较
        printf("第 %d 幅与亮度模板比较值:\n",i);
        printf(" 室内:%lf, 室外:%lf, 室外阳光:%lf\n", value1, value2, value3);
        cvSet1D(mat, 0, cvScalar(value1));
        cvSet1D(mat, 1, cvScalar(value2));
        cvSet1D(mat, 2, cvScalar(value3));
        cvMinMaxLoc(mat, &min, &max, &point, NULL, NULL);
        switch (point.x)
        {
            case 0:
                printf("第 %d 幅与亮度模板比较值最小值:%lf\n判定在室内环境下拍摄.\n", i, min);
                break;
            case 1:
                printf("第 %d 幅与亮度模板比较值最小值:%lf\n判定在室外环境下拍摄.\n", i, min);
                break;
            case 2:
                printf("第 %d 幅与亮度模板比较值最小值:%lf\n判定在室外阳光下拍摄.\n", i, min);
                break;
        }
        cvReleaseMat(&mat);
        cout << endl;
    }
    cvNamedWindow("Room_model", 1);
    cvNamedWindow("Out_model", 1);
    cvNamedWindow("Out_Sun_model", 1);
    cvNamedWindow("IMASK", 1);
    cvNamedWindow("Room_test", 1);
    cvNamedWindow("Out_test", 1);
    cvNamedWindow("Out_Sun_test", 1);
    cvShowImage("Room_model", src1);
    cvShowImage("Out_model", src3);
    cvShowImage("Out_Sun_model", src5);
    cvShowImage("IMASK", Imask);
    cvShowImage("Room_test", src2);
    cvShowImage("Out_test", src4);
    cvShowImage("Out_Sun_test", src6);
    cvWaitKey(0);
    //system("pause");
    cvReleaseHist(&hist[0]);
    cvReleaseHist(&hist[1]);
    cvReleaseHist(&hist[2]);
    cvReleaseHist(&hist[3]);
    cvReleaseHist(&hist[4]);
    cvReleaseHist(&hist[5]);
    cvReleaseImage(&src1);
    cvReleaseImage(&src2);
    cvReleaseImage(&src3);
    cvReleaseImage(&src4);
    cvReleaseImage(&src5);
    cvReleaseImage(&src6);
    cvReleaseImage(&Imask);
    cvReleaseImage(&hsv1);
    cvReleaseImage(&hsv2);
    cvReleaseImage(&hsv3);
    cvReleaseImage(&hsv4);
    cvReleaseImage(&hsv5);
    cvReleaseImage(&hsv6);
    cvReleaseImage(&h_plane_1);
    cvReleaseImage(&s_plane_1);
    cvReleaseImage(&v_plane_1);
    cvReleaseImage(&h_plane_2);
    cvReleaseImage(&s_plane_2);
    cvReleaseImage(&v_plane_2);
    cvReleaseImage(&h_plane_3);
    cvReleaseImage(&s_plane_3);
    cvReleaseImage(&v_plane_3);
    cvReleaseImage(&h_plane_4);
    cvReleaseImage(&s_plane_4);
    cvReleaseImage(&v_plane_4);
    cvReleaseImage(&h_plane_5);
    cvReleaseImage(&s_plane_5);
    cvReleaseImage(&v_plane_5);
    cvReleaseImage(&h_plane_6);
    cvReleaseImage(&s_plane_6);
    cvReleaseImage(&v_plane_6);
    cvDestroyAllWindows();
}