在一个图形中抠出一个区域

//代码有错误
#include <cv.h>
#include <highgui.h>
IplImage* img;
void mouse_set(int event, int x, int y, int flags, void* param);
int main()
{
    img = cvLoadImage("001.jpg", 1);
    cvNamedWindow("main",1);
    cvSetMouseCallback("main", mouse_set, 0);
    cvShowImage("main", img);
    cvWaitKey(0);
    //cvReleaseImage(&img);
    //cvDestroyAllWindows();
}
void mouse_set(int event, int x, int y, int flags, void* param)
{
    IplImage* image = cvCloneImage(img);
    IplImage* src = cvCloneImage(img);
    IplImage* des = cvCloneImage(img);
    IplImage* finn = 0;
    static CvPoint src_point = { -1, -1 };
    static CvPoint des_point = { -1, -1};
    if (event == CV_EVENT_LBUTTONDOWN)
    {
        cvCopy(image, src);
        printf("%d %d\n", x, y);
        cvShowImage("main", src);
        src_point = cvPoint(x, y);
        cvCopy(des, src);
    }
    else if (event == CV_EVENT_MOUSEMOVE &&!(flags & CV_EVENT_FLAG_LBUTTON))
    {
        cvCopy(des, src);
        //printf("%d %d\n", x, y);
        //printf("--->%d %d\n", src_point.x, src_point.y);
        des_point = cvPoint(x, y);
        cvCopy(src, des);
    }
    else if (event == CV_EVENT_MOUSEMOVE &&(flags & CV_EVENT_FLAG_LBUTTON))
    {
        cvCopy(des, src);
        //printf("%d %d\n", x, y);
        //printf("--->%d %d\n", src_point.x, src_point.y);
        des_point = cvPoint(x, y);
        cvRectangle(src, src_point, des_point, cvScalar(0xff, 0, 0), 1, 8, 0);
        cvShowImage("main", src);
    }
    else if (event == CV_EVENT_LBUTTONUP )
    {
        cvCopy(des, src);
        //printf("%d %d\n", x, y);
        des_point = cvPoint(x, y);
        cvShowImage("main", src);
        cvCopy(src, des);
        CvRect rectangle;
        cvRectangle(src, src_point, des_point, cvScalar(0, 255, 0), 1, 8, 0);
        int width = abs(des_point.x - src_point.x);
        int height = abs(des_point.y - src_point.y);
        printf("src_point.x  = %d  src_point.y = %d , des_point.x = %d des_point.y = %d\n", src_point.x, src_point.y, des_point.x, des_point.y);
        finn = cvCreateImage(cvSize(width, height), img->depth, img->nChannels);
        if (src_point.x<des_point.x && src_point.y<des_point.y)
        {
            rectangle = cvRect(src_point.x, src_point.y, width, height);
        }
        else if (src_point.x>des_point.x && src_point.y<des_point.y)
        {
            rectangle = cvRect(des_point.x, src_point.y, width, height);
        }
        else if (src_point.x>des_point.x && src_point.y>des_point.y)
        {
            rectangle = cvRect(des_point.x, des_point.y, width, height);
        }
        else if (src_point.x<des_point.x && src_point.y>des_point.y)
        {
            rectangle = cvRect(src_point.x, des_point.y, width, height);
        }

        cvSetImageROI(img, rectangle);
        cvCopy(img, finn);
        cvResetImageROI(img);
        cvNamedWindow("show", 1);
        cvShowImage("show", finn);
        //cvSaveImage("finn1.jpg", finn);
        cvWaitKey(0);
        cvDestroyWindow("show");
    }
}

 

posted on 2016-04-06 08:53  `Elaine  阅读(180)  评论(0编辑  收藏  举报

导航