~siretart/opencv/bug.791527

« back to all changes in this revision

Viewing changes to samples/c/mser_sample.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Nobuhiro Iwamatsu
  • Date: 2010-07-16 13:12:28 UTC
  • mfrom: (4.1.7 sid)
  • Revision ID: james.westby@ubuntu.com-20100716131228-95963rebybpghxef
Tags: 2.1.0-1
* New upstream release. (Closes: #577594, #587232, #563717)
* Update debian/rules.
  - Update build-system to debhelper v7.
* Update debian/control.
  - Bumped standards-version to 3.9.0. No changes needed.
  - library package name update.
    Soname of opencv library changed from 4 to 2.1.
  - Add cmake, liblapack-dev, texlive-fonts-extra, texlive-latex-extra,
    texlive-latex-recommended, latex-xcolor and  texlive-fonts-recommended
    to Build-depends.
  - Add arch depends to libraw1394-dev and libdc1394-22-dev.
    Thanks to Pino Toscano. (Closes: #581210)
* Add opencv-doc.lintian-overrides.
  opencv-doc has some sample program of python.
* Update man files.
* Update patches
  - Update and rename 500_remove_bashism.patch.
    And rename to remove_bashism.patch.
* Add new patches.
  - Enable build static library.
    enable_static.patch
  - Disable build 3rd party library.
    Use zlib and lapack in debian package.
    fix_3rdparty_build.patch
  - Fix build pdf.
    fix_build_pdf.patch
  - Remove cvconfig.h
    remove_cvconfig.h.patch
* Remove some patches.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* This sample code was originally provided by Liu Liu
 
2
 * Copyright� 2009, Liu Liu All rights reserved.
 
3
 */
 
4
 
 
5
#include <iostream>
 
6
#include <cstdio>
 
7
#include <stdio.h>
 
8
#include "cv.h"
 
9
#include "highgui.h"
 
10
 
 
11
    static CvScalar colors[] = 
 
12
    {
 
13
        {{0,0,255}},
 
14
        {{0,128,255}},
 
15
        {{0,255,255}},
 
16
        {{0,255,0}},
 
17
        {{255,128,0}},
 
18
        {{255,255,0}},
 
19
        {{255,0,0}},
 
20
        {{255,0,255}},
 
21
        {{255,255,255}},
 
22
        {{196,255,255}},
 
23
        {{255,255,196}}
 
24
    };
 
25
    
 
26
    static uchar bcolors[][3] = 
 
27
    {
 
28
        {0,0,255},
 
29
        {0,128,255},
 
30
        {0,255,255},
 
31
        {0,255,0},
 
32
        {255,128,0},
 
33
        {255,255,0},
 
34
        {255,0,0},
 
35
        {255,0,255},
 
36
        {255,255,255}
 
37
    };
 
38
 
 
39
int main( int argc, char** argv )
 
40
{
 
41
        char path[1024];
 
42
        IplImage* img;
 
43
        if (argc!=2)
 
44
        {
 
45
                strcpy(path,"puzzle.png");
 
46
                img = cvLoadImage( path, CV_LOAD_IMAGE_GRAYSCALE );
 
47
                if (!img)
 
48
                {
 
49
                        printf("\nUsage: mser_sample <path_to_image>\n");
 
50
                        return 0;
 
51
                }
 
52
        }
 
53
        else
 
54
        {
 
55
                strcpy(path,argv[1]);
 
56
                img = cvLoadImage( path, CV_LOAD_IMAGE_GRAYSCALE );
 
57
        }
 
58
        
 
59
        if (!img)
 
60
        {
 
61
                printf("Unable to load image %s\n",path);
 
62
                return 0;
 
63
        }
 
64
        IplImage* rsp = cvLoadImage( path, CV_LOAD_IMAGE_COLOR );
 
65
        IplImage* ellipses = cvCloneImage(rsp);
 
66
        cvCvtColor(img,ellipses,CV_GRAY2BGR);
 
67
        CvSeq* contours;
 
68
        CvMemStorage* storage= cvCreateMemStorage();
 
69
        IplImage* hsv = cvCreateImage( cvGetSize( rsp ), IPL_DEPTH_8U, 3 );
 
70
        cvCvtColor( rsp, hsv, CV_BGR2YCrCb );
 
71
        CvMSERParams params = cvMSERParams();//cvMSERParams( 5, 60, cvRound(.2*img->width*img->height), .25, .2 );
 
72
 
 
73
        double t = (double)cvGetTickCount();
 
74
        cvExtractMSER( hsv, NULL, &contours, storage, params );
 
75
        t = cvGetTickCount() - t;
 
76
        printf( "MSER extracted %d contours in %g ms.\n", contours->total, t/((double)cvGetTickFrequency()*1000.) );
 
77
        uchar* rsptr = (uchar*)rsp->imageData;
 
78
        // draw mser with different color
 
79
        for ( int i = contours->total-1; i >= 0; i-- )
 
80
        {
 
81
                CvSeq* r = *(CvSeq**)cvGetSeqElem( contours, i );
 
82
                for ( int j = 0; j < r->total; j++ )
 
83
                {
 
84
                        CvPoint* pt = CV_GET_SEQ_ELEM( CvPoint, r, j );
 
85
                        rsptr[pt->x*3+pt->y*rsp->widthStep] = bcolors[i%9][2];
 
86
                        rsptr[pt->x*3+1+pt->y*rsp->widthStep] = bcolors[i%9][1];
 
87
                        rsptr[pt->x*3+2+pt->y*rsp->widthStep] = bcolors[i%9][0];
 
88
                }
 
89
        }
 
90
        // find ellipse ( it seems cvfitellipse2 have error or sth?
 
91
        for ( int i = 0; i < contours->total; i++ )
 
92
        {
 
93
                CvContour* r = *(CvContour**)cvGetSeqElem( contours, i );
 
94
                CvBox2D box = cvFitEllipse2( r );
 
95
                box.angle=(float)CV_PI/2-box.angle;
 
96
                
 
97
                if ( r->color > 0 )
 
98
                        cvEllipseBox( ellipses, box, colors[9], 2 );
 
99
                else
 
100
                        cvEllipseBox( ellipses, box, colors[2], 2 );
 
101
                        
 
102
        }
 
103
 
 
104
        cvSaveImage( "rsp.png", rsp );
 
105
 
 
106
        cvNamedWindow( "original", 0 );
 
107
        cvShowImage( "original", img );
 
108
        
 
109
        cvNamedWindow( "response", 0 );
 
110
        cvShowImage( "response", rsp );
 
111
 
 
112
        cvNamedWindow( "ellipses", 0 );
 
113
        cvShowImage( "ellipses", ellipses );
 
114
 
 
115
        cvWaitKey(0);
 
116
 
 
117
        cvDestroyWindow( "original" );
 
118
        cvDestroyWindow( "response" );
 
119
        cvDestroyWindow( "ellipses" );
 
120
        cvReleaseImage(&rsp);
 
121
        cvReleaseImage(&img);
 
122
        cvReleaseImage(&ellipses);
 
123
        
 
124
}