2012年7月14日 星期六

OpenCV Read from CAMERA

#include "opencv\highgui.h"
#include "opencv\cv.h"
using namespace std;
//using namespace cv;
char* windowTitle = "test";
cv::Mat frame;
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);
 }
}

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;
    cv::Mat grayimage; //highgui.h, libopencv_highgui242.dll, libopencv_core242.dll
    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);
        if(cv::waitKey(30) >= 0) break;
    }
    /*
    char* filename = "test.jpg";
    cv::Mat image = cv::imread(filename,1);
    cv::imshow(windowTitle, image);
    */
    return 0;
}

沒有留言:

張貼留言