06 cvSetMouseCallback 滑鼠動作

目的:

cvSetMouseCallback 滑鼠動作

函數介紹:

cvInRangeS 找出特定的顏色,找到的話,就顯示為白色,不然為黑色。

參數值:

cvSetMouseCallback("image", mymouse);
void mymouse(int event, int x, int y, int flag, void* param)

使用範例:

sample13_Mouse_滑鼠動作

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/opencv.hpp>
#include <iostream>
#include <string.h>
#include <fstream>
using namespace cv;


IplImage * image;


void mymouse(int event, int x, int y, int flag, void* param)
{
    
    static int mousedown=0;
    
    if(event==CV_EVENT_LBUTTONDOWN){
        cvCircle(image, cvPoint(x,y), 8,cvScalar(0,0,255));
        mousedown=1;
    }
    if(event==CV_EVENT_MOUSEMOVE && mousedown==1){
        cvCircle(image, cvPoint(x,y), 2,cvScalar(0,255,0));
    }
    if(event==CV_EVENT_LBUTTONUP){
        cvCircle(image, cvPoint(x,y), 8,cvScalar(255,0,0));
        mousedown=0;
    }
    cvShowImage("image", image);
}


int main(int argc,char **argv)
{
    int width,height;
    if(argc<=1)
    {
        std::cout<<"Error:Please Load a picture!"<<std::endl;
        return 0;
    }
    //建立視窗
    namedWindow("image",CV_WINDOW_AUTOSIZE);
    //讀取圖片
    image=cvLoadImage(argv[1]);
    width=image->width;
    height=image->height;
    
    cvShowImage("image", image);
    cvSetMouseCallback("image", mymouse);
    waitKey(0);
    cvDestroyAllWindows();
    return 0;
}



結果:

Screen Shot 2015-03-01 at 1.26.11 PM

補充資料:

OpenCV
CH01 簡介CH02 OpenCV APICH03 繪圖CH04 應用範例CH06 haarcascadesCH06 surf和siftCH07 OCRCH09 FaceCH5 輪廓(findContours、drawContours)