用opencv画矩形打上马赛克Mosaic

 


/*-----------------------------------------------------------------------------
*  
*   版权声明:
*   可以任意转载,转载时请务必以超链接形式标明文章原始出处和作者信息及本声明
*   http://www.cnblogs.com/yuliyang/
*   联系方式:
*   Mail:yuliyang@qq.com
*
*-----------------------------------------------------------------------------*/

代码:

 

/*
 * =====================================================================================
 *
 *       Filename:  mashaike.cpp
 *          
 *    Description:  选取矩形区域打上Mosaic 按        ESC退出保存打上马赛克的图像
 *
 *
 *        Version:  1.0
 *        Created:  2013/12/21 11:26:22
 *         Author:  yuliyang
 *
 *             Mail:  wzyuliyang911@gmail.com
 *             Blog:  http://www.cnblogs.com/yuliyang
 *
 * =====================================================================================
 */

#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <stdio.h>
#include <math.h>
#include <time.h>
#include "opencv\cv.h"                          /* cvresize函数要用到 */
#include "opencv\highgui.h"
using namespace cv;
using namespace std;
struct tm *mytm=NULL;                           /* 以时间来命名保存文件 */
IplImage* org = 0;                              /* 原图 */
IplImage* img = 0;                              /* 打上马赛克操作图 */
IplImage* resize_img = 0;                       /* 如果图像大小太大了就resize,现在手机摄像头像素太高了 */

static int n=0;
static char savename[30];                       /* 保存文件名 */
static time_t t;
void on_mouse( int event, int x, int y, int flags, void* ustc)
{
    static CvPoint pre_pt = {-1,-1};            /* 矩形区域的第一个点 */
    static CvPoint cur_pt = {-1,-1};            /* 第二个点 */
    if( event == CV_EVENT_LBUTTONDOWN )
    {
           pre_pt = cvPoint(x,y);
       }
    else if( event == CV_EVENT_LBUTTONUP )
    {
        cur_pt = cvPoint(x,y);
        int width=abs(pre_pt.x-cur_pt.x);
        int height=abs(pre_pt.y-cur_pt.y);
        CvRect rect;
        if(pre_pt.x<cur_pt.x && pre_pt.y<cur_pt.y)
        {
            rect=cvRect(pre_pt.x,pre_pt.y,width,height);
        }
        else if(pre_pt.x>cur_pt.x && pre_pt.y<cur_pt.y)
        {
            rect=cvRect(cur_pt.x,pre_pt.y,width,height);
        }
        else if(pre_pt.x>cur_pt.x && pre_pt.y>cur_pt.y)
        {
            rect=cvRect(cur_pt.x,cur_pt.y,width,height);
        }
        else if(pre_pt.x<cur_pt.x && pre_pt.y>cur_pt.y)
        {
            rect=cvRect(pre_pt.x,cur_pt.y,width,height);
        }
        cvSetImageROI(img,rect);
            int W=8;
            int H=8;

            /*------------------------------------------------------------------------------------------------------------
             *  打上马赛克                                                                                                  
             *------------------------------------------------------------------------------------------------------------*/
            for(int mi=W;mi<img->roi->width;mi+=W)
                for(int mj=H;mj<img->roi->height;mj+=H)
                {
                    CvScalar tmp=cvGet2D(img,mj-H/2,mi-W/2);
                    for(int mx=mi-W;mx<=mi;mx++)
                        for(int my=mj-H;my<=mj;my++)
                            cvSet2D(img,my,mx,tmp);
                }
        cvResetImageROI(img);

    }
}
int main(int argc, char *argv[])
{
    org=cvLoadImage(argv[1],1);                 /* 载入图像 */


    if(org->width > 1000)                       /* 太大缩放 */
        {
            CvSize dst_cvsize;
            dst_cvsize.width=org->width*0.2;
            dst_cvsize.height=org->height*0.2;
            img = cvCreateImage( dst_cvsize, org->depth, org->nChannels);
            resize_img=cvCloneImage(org);
            cvResize(resize_img, img, CV_INTER_LINEAR);
        }
        else{
            img=cvCloneImage(org);
            }


    cvNamedWindow("org",CV_WINDOW_AUTOSIZE);
    cvShowImage("org",org);
    cvNamedWindow("img",CV_WINDOW_AUTOSIZE);

    cvSetMouseCallback( "img", on_mouse, 0 );
    while(1){

    cvShowImage("img",img);
    char c=(char)waitKey(47);
    if (c==27)
    {

           time(&t);
           mytm = localtime(&t);
           sprintf(savename,"%d-%d-%d-%d.jpg",mytm->tm_mday,mytm->tm_hour,mytm->tm_min,mytm->tm_sec);
           cvSaveImage(savename,img);
           break;
    }


    }

    cvDestroyAllWindows();
    cvReleaseImage(&org);
    cvReleaseImage(&img);
    return 0;
}

 

效果:

21-11-37-40

posted @ 2013-12-21 11:39  小菜鸟_yang  阅读(4742)  评论(0编辑  收藏  举报