2012年7月16日 星期一

opencv-calcHist

Histogram.h
#ifndef HISTOGRAM_H_
#define HISTOGRAM_H_

#include "opencv/cv.h"

class Histogram
{
private:
    int histSize[1];
    float hrangee[2];
    const float* ranges[1];
    int channels[1];
protected:
    cv::Mat getHistogram(const cv::Mat& image);
public:
     Histogram();
     cv::Mat getHistogramImage(const cv::Mat& image, int channel);
};
#endif /*HISTOGRAM_H_*/
Histogram.cpp
#include "Histogram.h"

Histogram::Histogram()
{
    histSize[0] = 256;
    hrangee[0] = 0.0;
    hrangee[1] = 255.0;
    ranges[0] = hrangee;
    channels[0] = 0;
}

cv::Mat Histogram::getHistogram(const cv::Mat& image)
{
    cv::MatND hist;
    cv::calcHist(&image, 1, channels, cv::Mat(), hist, 1, histSize, ranges);
    return hist;
}

cv::Mat Histogram::getHistogramImage(const cv::Mat& image, int channel)
{
    std::vector planes;
    cv::split(image,planes);
    cv::Scalar color;
    if(planes.size() == 1){
        channel = 0;
        color = cv::Scalar(0,0,0);
    }else{
        color = cv::Scalar(channel==0?255:0, channel==1?255:0, channel==2?255:0);
    }
     cv::MatND hist = getHistogram(planes[channel]);
    double maxVal = 0;
    double minVal = 0;
    cv::minMaxLoc(hist, &minVal, &maxVal, 0, 0); //尋找一個矩陣中最大(maxVal)和最小值(minVal),並得到它們的位置
    cv::Mat histImg(histSize[0], histSize[0], CV_8UC3, cv::Scalar(255,255,255));
    int hpt = static_cast(0.9*histSize[0]);
    for(int h=0; h(h);
        float binVal2 = hist.at(h+1);
        int intensity = static_cast(binVal*hpt/maxVal);
        int intensity2 = static_cast(binVal2*hpt/maxVal);
        cv::line(histImg, cv::Point(h,histSize[0]-intensity), cv::Point(h,histSize[0]-intensity2), color);
    }
    return histImg;
}

main.cpp
//#include 
#include "opencv\highgui.h"
#include "opencv\cv.h"
#include "Histogram.h"
using namespace std;
//using namespace cv;
char* windowTitle = "test";
cv::Mat frame;
cv::Mat grayimage; //highgui.h, libopencv_highgui242.dll, libopencv_core242.dll
CvPoint VertexLT,VertexRD;//長方形的左上點和右下點
cv::Scalar color = CV_RGB(0,255,0);
int thickness = 2;
int shift = 0;
void onMouse(int event,int x,int y,int flags,void* param);
void onMouse(int event,int x,int y,int flag,void* param)
    {
    printf("( %d, %d) ",x,y);
    printf("The Event is : %d ",event);
    printf("The flags is : %d ",flag);
    printf("The param is : %d\n",param);
    if(event==CV_EVENT_LBUTTONDOWN||event==CV_EVENT_RBUTTONDOWN)
    {
        VertexLT=cv::Point(x,y);//得到左上角座標
    }
    if(event==CV_EVENT_LBUTTONUP||event==CV_EVENT_RBUTTONUP)
    {
        VertexRD=cv::Point(x,y); //得到右下角座標
    }
    if(flag==CV_EVENT_FLAG_LBUTTON||flag==CV_EVENT_FLAG_RBUTTON){//拖曳滑鼠
       VertexRD = cv::Point(x,y);
	   // cvReleaseImage(&Image);//如果沒有的話,記憶體會暴漲
	   // Image = cvCloneImage(Imagex);//cvCopy(Imagex, Image, 0);
	    cv::rectangle(frame,VertexLT,VertexRD,color,thickness,CV_AA,shift);
	    cv::imshow(windowTitle,frame);
        Histogram h;
        cv::namedWindow("His");
        cv::imshow("His",h.getHistogramImage(grayimage,0));
	}
}

int main()
{
    cv::namedWindow(windowTitle,1);  //highgui.h, libopencv_highgui242.dll, libopencv_core242.dll
    cv::setMouseCallback(windowTitle,onMouse,NULL);//設定滑鼠callback函式
    cv::VideoCapture cap(0); // open the default camera //highgui.h, libopencv_highgui242.dll, libopencv_core242.dll
    if(!cap.isOpened()) return -1;
    for(;;)
    {
        cap >> frame;
        cv::cvtColor(frame, grayimage, CV_BGR2GRAY);  //cv.h, libopencv_imgproc242.dll
       // cv:: GaussianBlur(grayimage, grayimage, cv::Size(7,7), 1.5, 1.5); //cv.h, libopencv_imgproc242.dll
       // cv::Canny(grayimage, grayimage, 0, 30, 3); //cv.h, libopencv_imgproc242.dll
       // grayimage = frame;
        cv::rectangle(grayimage,VertexLT,VertexRD,color,thickness,CV_AA,shift);
        cv::imshow(windowTitle, grayimage);
        int key = cv::waitKey(10);
        if(key==27) break;
        switch(key){
            case 'a':
            break;
        }
       // break;
    }
    /*
    char* filename = "test.jpg";
    cv::Mat image = cv::imread(filename,1);
    cv::imshow(windowTitle, image);
    */
    return 0;
}

沒有留言:

張貼留言