Ask Your Question

Revision history [back]

In the following code you have to call bhDisplayHSVHistogram for display HSI channel histogram.

IplImage* bhGet8Depth(const IplImage* srcImage,int zeroFlag)
{
    IplImage* resultImg = cvCreateImage(cvGetSize(srcImage),8,1);
    if (zeroFlag == 1)
        cvZero(resultImg);
    else if (zeroFlag == 2)
    {
        if (srcImage->depth == 8 )
        {
            if ( srcImage->nChannels == 3)
              cvCvtColor(srcImage,resultImg,CV_BGR2GRAY);
            else if( srcImage->nChannels == 1)
                cvCopyImage(srcImage,resultImg);

        }
        else if (srcImage->depth == IPL_DEPTH_32F && srcImage->nChannels ==1)
        {
            cvNormalize(srcImage,resultImg,0,255,CV_MINMAX);
        }
    }
    return  resultImg;
}

void bhGet1DHistogram(IplImage* srcImage ,CvHistogram** hist, int size, IplImage* mask)
{
    int sizes[] = {size};
    float ranges1[] = {0,float(size)};
    float* ranges[] = {ranges1};
    *hist = cvCreateHist(1,sizes,CV_HIST_ARRAY,ranges,1);
    if (mask != 0)
       cvCalcHist(&srcImage,*hist,0,mask);
    else cvCalcHist(&srcImage,*hist,0);


}

void bhShow1DHistogram( CvHistogram* hist,IplImage** histImage,int height)
{
    *histImage = cvCreateImage(cvSize(hist->mat.dim[0].size,height),8,1);
    cvZero(*histImage);
    float min_value, max_value;
    cvGetMinMaxHistValue(hist,&min_value,&max_value);
    float* pHist = hist->mat.data.fl;
    for (int j=0; j < hist->mat.dim[0].size; j++)
    {
        int value = cvRound( pHist[j] / max_value * height);
        cvLine(*histImage,cvPoint(j,height),cvPoint(j,height- value),cvScalarAll(255))   ;
    }

}

void bhDisplayHSVHistogram(IplImage* srcImage)
{
    IplImage* hsvImg = cvCreateImage(cvGetSize(srcImage),8,3);
    cvCvtColor(srcImage,hsvImg,CV_BGR2HSV);

    IplImage* hImg = bhGet8Depth(srcImage);
    IplImage* sImg = bhGet8Depth(srcImage);
    IplImage* vImg = bhGet8Depth(srcImage);

    cvSplit(hsvImg,hImg,sImg,vImg,0);

    CvHistogram *histH,*histS,*histI;
    bhGet1DHistogram(hImg,&histH,256,maskImage);
    bhGet1DHistogram(sImg,&histS,256,maskImage);
    bhGet1DHistogram(vImg,&histI,256,maskImage);

    IplImage *histImgH ,*histImgS,*histImgI;
    bhShow1DHistogram(histH,&histImgH,100);
    bhShow1DHistogram(histS,&histImgS,100);
    bhShow1DHistogram(histI,&histImgI,100);



    cvShowImage("Hue Histogram",histImgH);
    cvShowImage("Saturation Histogram",histImgS);
    cvShowImage("Intensity Histogram",histImgI);

    cvWaitKey(0);


    cvDestroyWindow("Hue Histogram");
    cvDestroyWindow("Saturation Histogram");
    cvDestroyWindow("Intensity Histogram");

    cvReleaseHist(&histH);
    cvReleaseHist(&histS);
    cvReleaseHist(&histI);

    cvReleaseImage(&histImgH);
    cvReleaseImage(&histImgS);
    cvReleaseImage(&histImgI);

    cvReleaseImage(&hImg);
    cvReleaseImage(&sImg);
    cvReleaseImage(&vImg);

    cvReleaseImage(&hsvImg);

}

In the following code you have to call bhDisplayHSVHistogram for display HSI channel channels histogram.

IplImage* bhGet8Depth(const IplImage* srcImage,int zeroFlag)
{
    IplImage* resultImg = cvCreateImage(cvGetSize(srcImage),8,1);
    if (zeroFlag == 1)
        cvZero(resultImg);
    else if (zeroFlag == 2)
    {
        if (srcImage->depth == 8 )
        {
            if ( srcImage->nChannels == 3)
              cvCvtColor(srcImage,resultImg,CV_BGR2GRAY);
            else if( srcImage->nChannels == 1)
                cvCopyImage(srcImage,resultImg);

        }
        else if (srcImage->depth == IPL_DEPTH_32F && srcImage->nChannels ==1)
        {
            cvNormalize(srcImage,resultImg,0,255,CV_MINMAX);
        }
    }
    return  resultImg;
}

void bhGet1DHistogram(IplImage* srcImage ,CvHistogram** hist, int size, IplImage* mask)
{
    int sizes[] = {size};
    float ranges1[] = {0,float(size)};
    float* ranges[] = {ranges1};
    *hist = cvCreateHist(1,sizes,CV_HIST_ARRAY,ranges,1);
    if (mask != 0)
       cvCalcHist(&srcImage,*hist,0,mask);
    else cvCalcHist(&srcImage,*hist,0);


}

void bhShow1DHistogram( CvHistogram* hist,IplImage** histImage,int height)
{
    *histImage = cvCreateImage(cvSize(hist->mat.dim[0].size,height),8,1);
    cvZero(*histImage);
    float min_value, max_value;
    cvGetMinMaxHistValue(hist,&min_value,&max_value);
    float* pHist = hist->mat.data.fl;
    for (int j=0; j < hist->mat.dim[0].size; j++)
    {
        int value = cvRound( pHist[j] / max_value * height);
        cvLine(*histImage,cvPoint(j,height),cvPoint(j,height- value),cvScalarAll(255))   ;
    }

}

void bhDisplayHSVHistogram(IplImage* srcImage)
{
    IplImage* hsvImg = cvCreateImage(cvGetSize(srcImage),8,3);
    cvCvtColor(srcImage,hsvImg,CV_BGR2HSV);

    IplImage* hImg = bhGet8Depth(srcImage);
    IplImage* sImg = bhGet8Depth(srcImage);
    IplImage* vImg = bhGet8Depth(srcImage);

    cvSplit(hsvImg,hImg,sImg,vImg,0);

    CvHistogram *histH,*histS,*histI;
    bhGet1DHistogram(hImg,&histH,256,maskImage);
    bhGet1DHistogram(sImg,&histS,256,maskImage);
    bhGet1DHistogram(vImg,&histI,256,maskImage);

    IplImage *histImgH ,*histImgS,*histImgI;
    bhShow1DHistogram(histH,&histImgH,100);
    bhShow1DHistogram(histS,&histImgS,100);
    bhShow1DHistogram(histI,&histImgI,100);



    cvShowImage("Hue Histogram",histImgH);
    cvShowImage("Saturation Histogram",histImgS);
    cvShowImage("Intensity Histogram",histImgI);

    cvWaitKey(0);


    cvDestroyWindow("Hue Histogram");
    cvDestroyWindow("Saturation Histogram");
    cvDestroyWindow("Intensity Histogram");

    cvReleaseHist(&histH);
    cvReleaseHist(&histS);
    cvReleaseHist(&histI);

    cvReleaseImage(&histImgH);
    cvReleaseImage(&histImgS);
    cvReleaseImage(&histImgI);

    cvReleaseImage(&hImg);
    cvReleaseImage(&sImg);
    cvReleaseImage(&vImg);

    cvReleaseImage(&hsvImg);

}

In the following code you have to call bhDisplayHSVHistogram for display HSI channels histogram.

IplImage* bhGet8Depth(const IplImage* srcImage,int zeroFlag)
{
    IplImage* resultImg = cvCreateImage(cvGetSize(srcImage),8,1);
    if (zeroFlag == 1)
        cvZero(resultImg);
    else if (zeroFlag == 2)
    {
        if (srcImage->depth == 8 )
        {
            if ( srcImage->nChannels == 3)
              cvCvtColor(srcImage,resultImg,CV_BGR2GRAY);
            else if( srcImage->nChannels == 1)
                cvCopyImage(srcImage,resultImg);

        }
        else if (srcImage->depth == IPL_DEPTH_32F && srcImage->nChannels ==1)
        {
            cvNormalize(srcImage,resultImg,0,255,CV_MINMAX);
        }
    }
    return  resultImg;
}

void bhGet1DHistogram(IplImage* srcImage ,CvHistogram** hist, int size, IplImage* mask)
{
    int sizes[] = {size};
    float ranges1[] = {0,float(size)};
    float* ranges[] = {ranges1};
    *hist = cvCreateHist(1,sizes,CV_HIST_ARRAY,ranges,1);
    if (mask != 0)
       cvCalcHist(&srcImage,*hist,0,mask);
    else cvCalcHist(&srcImage,*hist,0);


}

void bhShow1DHistogram( CvHistogram* hist,IplImage** histImage,int height)
{
    *histImage = cvCreateImage(cvSize(hist->mat.dim[0].size,height),8,1);
    cvZero(*histImage);
    float min_value, max_value;
    cvGetMinMaxHistValue(hist,&min_value,&max_value);
    float* pHist = hist->mat.data.fl;
    for (int j=0; j < hist->mat.dim[0].size; j++)
    {
        int value = cvRound( pHist[j] / max_value * height);
        cvLine(*histImage,cvPoint(j,height),cvPoint(j,height- value),cvScalarAll(255))   ;
    }

}

void bhDisplayHSVHistogram(IplImage* srcImage)
{
    IplImage* hsvImg = cvCreateImage(cvGetSize(srcImage),8,3);
    cvCvtColor(srcImage,hsvImg,CV_BGR2HSV);

    IplImage* hImg = bhGet8Depth(srcImage);
    IplImage* sImg = bhGet8Depth(srcImage);
    IplImage* vImg = bhGet8Depth(srcImage);

    cvSplit(hsvImg,hImg,sImg,vImg,0);

CvRect ROI = cvGetImageROI(srcImage);

maskImage = bhGet8Depth(srcImage,1);
cvRectangleR(maskImage,ROI,cvScalarAll(255),-1);

    CvHistogram *histH,*histS,*histI;
    bhGet1DHistogram(hImg,&histH,256,maskImage);
    bhGet1DHistogram(sImg,&histS,256,maskImage);
    bhGet1DHistogram(vImg,&histI,256,maskImage);

    cvReleaseImage(&maskImage);
    IplImage *histImgH ,*histImgS,*histImgI;
    bhShow1DHistogram(histH,&histImgH,100);
    bhShow1DHistogram(histS,&histImgS,100);
    bhShow1DHistogram(histI,&histImgI,100);



    cvShowImage("Hue Histogram",histImgH);
    cvShowImage("Saturation Histogram",histImgS);
    cvShowImage("Intensity Histogram",histImgI);

    cvWaitKey(0);


    cvDestroyWindow("Hue Histogram");
    cvDestroyWindow("Saturation Histogram");
    cvDestroyWindow("Intensity Histogram");

    cvReleaseHist(&histH);
    cvReleaseHist(&histS);
    cvReleaseHist(&histI);

    cvReleaseImage(&histImgH);
    cvReleaseImage(&histImgS);
    cvReleaseImage(&histImgI);

    cvReleaseImage(&hImg);
    cvReleaseImage(&sImg);
    cvReleaseImage(&vImg);

    cvReleaseImage(&hsvImg);

}