zoukankan      html  css  js  c++  java
  • 【OpenCV学习】图像填充

    作者:gnuhpc
    出处:http://www.cnblogs.com/gnuhpc/

     
    #include "cv.h"
    #include "highgui.h"
    #include <stdio.h>
    #include <stdlib.h>
    IplImage* color_img0;
    IplImage* mask;
    IplImage* color_img;
    IplImage* gray_img0 = NULL;
    IplImage* gray_img = NULL;
    int ffill_case = 1;
    int lo_diff = 20, up_diff = 20;
    int connectivity = 4;
    int is_color = 1;
    int is_mask = 0;
    int new_mask_val = 255;
    void on_mouse( int event, int x, int y, int flags, void* param )
    {
        if( !color_img )
            return;
        switch( event )
        {
        case CV_EVENT_LBUTTONDOWN:
            {
                CvPoint seed = cvPoint(x,y);
                int lo = ffill_case == 0 ? 0 : lo_diff;
                int up = ffill_case == 0 ? 0 : up_diff;
                int flags = connectivity + (new_mask_val << 8) +
                            (ffill_case == 1 ? CV_FLOODFILL_FIXED_RANGE : 0);
                int b = rand() & 255, g = rand() & 255, r = rand() & 255;
                //填充的RGB值
                CvConnectedComp comp;
                if( is_mask )
                    cvThreshold( mask, mask, 1, 128, CV_THRESH_BINARY );
               
                if( is_color )
                {
                    CvScalar color = CV_RGB( r, g, b );
                    cvFloodFill( color_img, seed, color, CV_RGB( lo, lo, lo ),
                                 CV_RGB( up, up, up ), &comp, flags, is_mask ? mask : NULL );
                    cvShowImage( "image", color_img );
                }
                else
                {
                    CvScalar brightness = cvRealScalar((r*2 + g*7 + b + 5)/10);
                    cvFloodFill( gray_img, seed, brightness, cvRealScalar(lo),
                                 cvRealScalar(up), &comp, flags, is_mask ? mask : NULL );
                    //
                    cvShowImage( "image", gray_img );
                }
                printf("%g pixels were repainted/n", comp.area );
                if( is_mask )
                    cvShowImage( "mask", mask );
            }
            break;
        }
    }
    int main( int argc, char** argv )
    {
        char* filename = argc >= 2 ? argv[1] : (char*)"lena.jpg";
        if( (color_img0 = cvLoadImage(filename,1)) == 0 )
            return 0;
        printf( "Hot keys: /n"
                "/tESC - quit the program/n"
                "/tc - switch color/grayscale mode/n"//彩色还是黑白图像
                "/tm - switch mask mode/n"//掩码模式
                "/tr - restore the original image/n"//保存原始图像
                "/ts - use null-range floodfill/n"//点填充
                "/tf - use gradient floodfill with fixed(absolute) range/n"
                //当前像素与种子之差
                "/tg - use gradient floodfill with floating(relative) range/n"
                //与相邻的像素之差
                "/t4 - use 4-connectivity mode/n"
               
                "/t8 - use 8-connectivity mode/n" );
        color_img = cvCloneImage( color_img0 );
        gray_img0 = cvCreateImage( cvSize(color_img->width, color_img->height), 8, 1 );
        cvCvtColor( color_img, gray_img0, CV_BGR2GRAY );//二值化
        gray_img = cvCloneImage( gray_img0 );
        mask = cvCreateImage( cvSize(color_img->width + 2, color_img->height + 2), 8, 1 );
        cvNamedWindow( "image", 0 );
        cvCreateTrackbar( "lo_diff", "image", &lo_diff, 255, NULL );
        cvCreateTrackbar( "up_diff", "image", &up_diff, 255, NULL );
        cvSetMouseCallback( "image", on_mouse, 0 );
        for(;;)
        {
            int c;
           
            if( is_color )
                cvShowImage( "image", color_img );
            else
                cvShowImage( "image", gray_img );
            c = cvWaitKey(0);
            switch( (char) c )
            {
            case '/x1b':
                printf("Exiting .../n");
                goto exit_main;
            case 'c':
                if( is_color )
                {
                    printf("Grayscale mode is set/n");
                    cvCvtColor( color_img, gray_img, CV_BGR2GRAY );
                    is_color = 0;
                }
                else
                {
                    printf("Color mode is set/n");
                    cvCopy( color_img0, color_img, NULL );
                    cvZero( mask );
                    is_color = 1;
                }
                break;
            case 'm':
                if( is_mask )
                {
                    cvDestroyWindow( "mask" );
                    is_mask = 0;
                }
                else
                {
                    cvNamedWindow( "mask", 0 );
                    cvZero( mask );
                    cvShowImage( "mask", mask );
                    is_mask = 1;
                }
                break;
            case 'r':
                printf("Original image is restored/n");
                cvCopy( color_img0, color_img, NULL );
                cvCopy( gray_img0, gray_img, NULL );
                cvZero( mask );
                break;
            case 's':
                printf("Simple floodfill mode is set/n");
                ffill_case = 0;
                break;
            case 'f':
                printf("Fixed Range floodfill mode is set/n");
                ffill_case = 1;
                break;
            case 'g':
                printf("Gradient (floating range) floodfill mode is set/n");
                ffill_case = 2;//这个2是随便设置的,其值并没有用
                break;
            case '4':
                printf("4-connectivity mode is set/n");
                connectivity = 4;
                break;
            case '8':
                printf("8-connectivity mode is set/n");
                connectivity = 8;
                break;
            }
        }
    exit_main:
        cvDestroyWindow( "test" );
        cvReleaseImage( &gray_img );
        cvReleaseImage( &gray_img0 );
        cvReleaseImage( &color_img );
        cvReleaseImage( &color_img0 );
        cvReleaseImage( &mask );
        return 1;
    }
    //函数 cvFloodFill 用指定颜色,从种子点开始填充一个连通域。连通性有象素值的接近程度来衡量。

    作者:gnuhpc
    出处:http://www.cnblogs.com/gnuhpc/


                   作者:gnuhpc
                   出处:http://www.cnblogs.com/gnuhpc/
                   除非另有声明,本网站采用知识共享“署名 2.5 中国大陆”许可协议授权。


    分享到:

  • 相关阅读:
    Java 运用流传输文件
    Java-Web 用html和css写一个EasyMall注册界面
    Java-Web 用html写一个简单的用户注册界面
    Java-Web DOM方式解析xml文件
    弹窗的淡入淡出
    html5 indexDB的使用
    nodejs生成UID(唯一标识符)——node-uuid模块
    CSS3 Flex布局(伸缩布局盒模型)学习
    map area 标签的使用
    CSS3 伸缩布局盒模型
  • 原文地址:https://www.cnblogs.com/gnuhpc/p/2722902.html
Copyright © 2011-2022 走看看