zoukankan      html  css  js  c++  java
  • 在一个图形中抠出一个区域

    //代码有错误
    #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
    ", 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
    ", x, y);
            //printf("--->%d %d
    ", 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
    ", x, y);
            //printf("--->%d %d
    ", 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
    ", 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
    ", 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");
        }
    }
  • 相关阅读:
    [语言基础] 我只想导入目标包中的一个模块,没想到目标包的其他非模块代码也被执行了。。
    [vscode] os.getcwd(),调试和命令行运行的结果不一致
    234. 回文链表
    不生成新数组的迭代器方法:forEach()&every()&some()&reduce()&reduceRight()
    合并数组并对数组排序
    为数组排序:sort()&reverse()
    从数组中间位置添加元素:unshift()方法的有一种运用
    从数组中删除元素:pop()&unshift()方法
    为数组添加元素:push()&unshift()方法
    由已有数组创建新数组:concat()&splice()方法
  • 原文地址:https://www.cnblogs.com/chenyang920/p/5357787.html
Copyright © 2011-2022 走看看