金子邦彦研究室研究道具箱と教材オープンデータとビッグデータ処理OpenCV の SURF

OpenCV の SURF

OpenCV の SURF を使う

OpenCV の samples/c/find_obj プログラムを使ってみる

ビルド手順例

cd /usr/local/share/OpenCV/samples/c/
g++ -o /tmp/find_obj find_obj.cpp -I/usr/local/include/opencv2 -I/usr/local/include/opencv -L/usr/local/lib -lopencv_nonfree -lopencv_legacy -lopencv_calib3d -lopencv_flann -lopencv_objdetect -lopencv_highgui -lopencv_imgproc -lopencv_core

実行手順例

cd /tmp
wget http://133.5.18.161/rinkou/od/2013-05-23-sozai/DSC00227.JPG 
wget http://133.5.18.161/rinkou/od/2013-05-23-sozai/DSC00228.JPG 
# 11520 x 1080 -> 2880 x 270
convert -resize 2880x DSC00227.JPG /tmp/DSC00227s.JPG 
convert -resize 2880x DSC00228.JPG /tmp/DSC00228s.JPG 
/tmp/find_obj /tmp/DSC00227s.JPG /tmp/DSC00228s.JPG  

[image]

[image]

SURF 情報取得プログラム

ビルド手順例

cd /tmp
g++ -o /tmp/opencv_surf opencv_surf.cpp -I/usr/local/include/opencv2 -I/usr/local/include/opencv -L/usr/local/lib -lopencv_nonfree -lopencv_legacy -lopencv_calib3d -lopencv_flann -lopencv_objdetect -lopencv_highgui -lopencv_imgproc -lopencv_core

実行手順例

cd /tmp
wget http://133.5.18.161/rinkou/od/2013-05-23-sozai/DSC00227.JPG 
wget http://133.5.18.161/rinkou/od/2013-05-23-sozai/DSC00228.JPG 
# 11520 x 1080 -> 2880 x 270
convert -resize 2880x DSC00227.JPG /tmp/DSC00227s.JPG 
convert -resize 2880x DSC00228.JPG /tmp/DSC00228s.JPG 
/tmp/opencv_surf /tmp/DSC00227s.JPG /tmp/DSC00228s.JPG  

ソースコード (ファイル名: opencv_surf.cpp)

/*
 * A Demo to OpenCV Implementation of SURF
 * Further Information Refer to "SURF: Speed-Up Robust Feature"
 * Author: Liu Liu
 * liuliu.1987+opencv@gmail.com
 */
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/calib3d/calib3d.hpp"
#include "opencv2/nonfree/nonfree.hpp"
#include "opencv2/imgproc/imgproc_c.h"
#include "opencv2/legacy/legacy.hpp"
#include "opencv2/legacy/compat.hpp"

#include<iostream>
#include<vector>
#include<stdio.h>

using namespace std;
static void help()
{
    printf(
        "This program demonstrated the use of the SURF Detector and Descriptor using\n"
        "either FLANN (fast approx nearst neighbor classification) or brute force matching\n"
        "on planar objects.\n"
        "Usage:\n"
        "./find_obj <object_filename> <scene_filename>, default is box.png  and box_in_scene.png\n\n");
    return;
}

// define whether to use approximate nearest-neighbor search
#define USE_FLANN

#ifdef USE_FLANN
static void
flannFindPairs( const CvSeq*, const CvSeq* objectDescriptors,
		const CvSeq*, const CvSeq* imageDescriptors, vector<int>& ptpairs )
{
    int length = (int)(objectDescriptors->elem_size/sizeof(float));

    cv::Mat m_object(objectDescriptors->total, length, CV_32F);
    cv::Mat m_image(imageDescriptors->total, length, CV_32F);


    // copy descriptors
    CvSeqReader obj_reader;
    float* obj_ptr = m_object.ptr<float>(0);
    cvStartReadSeq( objectDescriptors, &obj_reader );
    for(int i = 0; i < objectDescriptors->total; i++ )
    {
        const float* descriptor = (const float*)obj_reader.ptr;
        CV_NEXT_SEQ_ELEM( obj_reader.seq->elem_size, obj_reader );
        memcpy(obj_ptr, descriptor, length*sizeof(float));
        obj_ptr += length;
    }
    CvSeqReader img_reader;
    float* img_ptr = m_image.ptr<float>(0);
    cvStartReadSeq( imageDescriptors, &img_reader );
    for(int i = 0; i < imageDescriptors->total; i++ )
    {
        const float* descriptor = (const float*)img_reader.ptr;
        CV_NEXT_SEQ_ELEM( img_reader.seq->elem_size, img_reader );
        memcpy(img_ptr, descriptor, length*sizeof(float));
        img_ptr += length;
    }

    // find nearest neighbors using FLANN
    cv::Mat m_indices(objectDescriptors->total, 2, CV_32S);
    cv::Mat m_dists(objectDescriptors->total, 2, CV_32F);
    cv::flann::Index flann_index(m_image, cv::flann::KDTreeIndexParams(4));  // using 4 randomized kdtrees
    flann_index.knnSearch(m_object, m_indices, m_dists, 2, cv::flann::SearchParams(64) ); // maximum number of leafs checked

    int* indices_ptr = m_indices.ptr<int>(0);
    float* dists_ptr = m_dists.ptr<float>(0);
    // kaneko
    fprintf( stderr, "pairnum, objkeypoint, imagekeypoint, dist\n" );
    int j=0; 
    for (int i=0;i<m_indices.rows;++i) {
        if (dists_ptr[2*i]<0.6*dists_ptr[2*i+1]) {
            ptpairs.push_back(i);
            ptpairs.push_back(indices_ptr[2*i]);
      // kaneko
	    fprintf( stderr, "%d, %d, %d, %f\n", j, i, indices_ptr[2*i], dists_ptr[2*i] ); 
	    j++;
        }
    }
}
#else

static double
compareSURFDescriptors( const float* d1, const float* d2, double best, int length )
{
    double total_cost = 0;
    assert( length % 4 == 0 );
    for( int i = 0; i < length; i += 4 )
    {
        double t0 = d1[i  ] - d2[i  ];
        double t1 = d1[i+1] - d2[i+1];
        double t2 = d1[i+2] - d2[i+2];
        double t3 = d1[i+3] - d2[i+3];
        total_cost += t0*t0 + t1*t1 + t2*t2 + t3*t3;
        if( total_cost > best )
            break;
    }
    return total_cost;
}

static int
// kaneko
naiveNearestNeighbor( const float* vec, int laplacian,
                      const CvSeq* model_keypoints,
                      const CvSeq* model_descriptors, const int i )
{
    int length = (int)(model_descriptors->elem_size/sizeof(float));
    int i, neighbor = -1;
    double d, dist1 = 1e6, dist2 = 1e6;
    CvSeqReader reader, kreader;
    cvStartReadSeq( model_keypoints, &kreader, 0 );
    cvStartReadSeq( model_descriptors, &reader, 0 );

    for( i = 0; i < model_descriptors->total; i++ )
    {
        const CvSURFPoint* kp = (const CvSURFPoint*)kreader.ptr;
        const float* mvec = (const float*)reader.ptr;
        CV_NEXT_SEQ_ELEM( kreader.seq->elem_size, kreader );
        CV_NEXT_SEQ_ELEM( reader.seq->elem_size, reader );
        if( laplacian != kp->laplacian )
            continue;
        d = compareSURFDescriptors( vec, mvec, dist2, length );
        if( d < dist1 )
        {
            dist2 = dist1;
            dist1 = d;
            neighbor = i;
        }
        else if ( d < dist2 )
            dist2 = d;
    }
    if ( dist1 < 0.6*dist2 ) {
      // kaneko
      fprintf( stderr, "%d, %d, %f \n", i, neighbor, dist1 );
      return neighbor;
    }
    return -1;
}

static void
findPairs( const CvSeq* objectKeypoints, const CvSeq* objectDescriptors,
           const CvSeq* imageKeypoints, const CvSeq* imageDescriptors, vector<int>& ptpairs )
{
    int i;
    CvSeqReader reader, kreader;
    cvStartReadSeq( objectKeypoints, &kreader );
    cvStartReadSeq( objectDescriptors, &reader );
    ptpairs.clear();

    for( i = 0; i < objectDescriptors->total; i++ )
    {
        const CvSURFPoint* kp = (const CvSURFPoint*)kreader.ptr;
        const float* descriptor = (const float*)reader.ptr;
        CV_NEXT_SEQ_ELEM( kreader.seq->elem_size, kreader );
        CV_NEXT_SEQ_ELEM( reader.seq->elem_size, reader );
	// kaneko
        int nearest_neighbor = naiveNearestNeighbor( descriptor, kp->laplacian, imageKeypoints, imageDescriptors, i );
        if( nearest_neighbor >= 0 )
        {
            ptpairs.push_back(i);
            ptpairs.push_back(nearest_neighbor);
        }
    }
}
#endif

/* a rough implementation for object location */
static int
locatePlanarObject( const CvSeq* objectKeypoints, const CvSeq* objectDescriptors,
                    const CvSeq* imageKeypoints, const CvSeq* imageDescriptors,
                    const CvPoint src_corners[4], CvPoint dst_corners[4] )
{
    double h[9];
    CvMat _h = cvMat(3, 3, CV_64F, h);
    vector<int> ptpairs;
    vector<CvPoint2D32f> pt1, pt2;
    CvMat _pt1, _pt2;
    int i, n;

#ifdef USE_FLANN
    flannFindPairs( objectKeypoints, objectDescriptors, imageKeypoints, imageDescriptors, ptpairs );
#else
    findPairs( objectKeypoints, objectDescriptors, imageKeypoints, imageDescriptors, ptpairs );
#endif

    n = (int)(ptpairs.size()/2);
    if( n < 4 )
        return 0;

    pt1.resize(n);
    pt2.resize(n);
    for( i = 0; i < n; i++ )
    {
        pt1[i] = ((CvSURFPoint*)cvGetSeqElem(objectKeypoints,ptpairs[i*2]))->pt;
        pt2[i] = ((CvSURFPoint*)cvGetSeqElem(imageKeypoints,ptpairs[i*2+1]))->pt;
    }

    _pt1 = cvMat(1, n, CV_32FC2, &pt1[0] );
    _pt2 = cvMat(1, n, CV_32FC2, &pt2[0] );
    if( !cvFindHomography( &_pt1, &_pt2, &_h, CV_RANSAC, 5 ))
        return 0;

    for( i = 0; i < 4; i++ )
    {
        double x = src_corners[i].x, y = src_corners[i].y;
        double Z = 1./(h[6]*x + h[7]*y + h[8]);
        double X = (h[0]*x + h[1]*y + h[2])*Z;
        double Y = (h[3]*x + h[4]*y + h[5])*Z;
        dst_corners[i] = cvPoint(cvRound(X), cvRound(Y));
    }

    return 1;
}

int main(int argc, char** argv)
{
    const char* object_filename = argc == 3 ? argv[1] : "box.png";
    const char* scene_filename = argc == 3 ? argv[2] : "box_in_scene.png";

    cv::initModule_nonfree();
    help();

    IplImage* object = cvLoadImage( object_filename, CV_LOAD_IMAGE_GRAYSCALE );
    IplImage* image = cvLoadImage( scene_filename, CV_LOAD_IMAGE_GRAYSCALE );
    if( !object || !image )
    {
        fprintf( stderr, "Can not load %s and/or %s\n",
            object_filename, scene_filename );
        exit(-1);
    }

    CvMemStorage* storage = cvCreateMemStorage(0);

    cvNamedWindow("Object Correspond", 1);

    static CvScalar colors[] =
    {
        {{0,0,255}},
        {{0,128,255}},
        {{0,255,255}},
        {{0,255,0}},
        {{255,128,0}},
        {{255,255,0}},
        {{255,0,0}},
        {{255,0,255}},
        {{255,255,255}}
    };

    IplImage* object_color = cvCreateImage(cvGetSize(object), 8, 3);
    cvCvtColor( object, object_color, CV_GRAY2BGR );

    CvSeq* objectKeypoints = 0, *objectDescriptors = 0;
    CvSeq* imageKeypoints = 0, *imageDescriptors = 0;
    int i;
    CvSURFParams params = cvSURFParams(500, 1);

    double tt = (double)cvGetTickCount();
    cvExtractSURF( object, 0, &objectKeypoints, &objectDescriptors, storage, params );
    printf("Object Descriptors: %d\n", objectDescriptors->total);

    cvExtractSURF( image, 0, &imageKeypoints, &imageDescriptors, storage, params );
    printf("Image Descriptors: %d\n", imageDescriptors->total);
    tt = (double)cvGetTickCount() - tt;

    // printf( "Extraction time = %gms\n", tt/(cvGetTickFrequency()*1000.));

    CvPoint src_corners[4] = {{0,0}, {object->width,0}, {object->width, object->height}, {0, object->height}};
    CvPoint dst_corners[4];
    IplImage* correspond = cvCreateImage( cvSize(image->width, object->height+image->height), 8, 1 );
    cvSetImageROI( correspond, cvRect( 0, 0, object->width, object->height ) );
    cvCopy( object, correspond );
    cvSetImageROI( correspond, cvRect( 0, object->height, correspond->width, correspond->height ) );
    cvCopy( image, correspond );
    cvResetImageROI( correspond );

#ifdef USE_FLANN
    printf("Using approximate nearest neighbor search\n");
#endif

    if( locatePlanarObject( objectKeypoints, objectDescriptors, imageKeypoints,
        imageDescriptors, src_corners, dst_corners ))
    {
        for( i = 0; i < 4; i++ )
        {
            CvPoint r1 = dst_corners[i%4];
            CvPoint r2 = dst_corners[(i+1)%4];
            cvLine( correspond, cvPoint(r1.x, r1.y+object->height ),
                cvPoint(r2.x, r2.y+object->height ), colors[8] );
        }
    }
    vector<int> ptpairs;
#ifdef USE_FLANN
    flannFindPairs( objectKeypoints, objectDescriptors, imageKeypoints, imageDescriptors, ptpairs );
#else
    findPairs( objectKeypoints, objectDescriptors, imageKeypoints, imageDescriptors, ptpairs );
#endif
    // kaneko
    fprintf( stderr, "id, objkeypoint, imagekeypoint, p1x, p1y, radius1, p2x, p2y, radius2\n" );
    for( i = 0; i < (int)ptpairs.size(); i += 2 )
    {
        CvSURFPoint* r1 = (CvSURFPoint*)cvGetSeqElem( objectKeypoints, ptpairs[i] );
        CvSURFPoint* r2 = (CvSURFPoint*)cvGetSeqElem( imageKeypoints, ptpairs[i+1] );
        cvLine( correspond, cvPointFrom32f(r1->pt), cvPoint(cvRound(r2->pt.x), cvRound(r2->pt.y+object->height)), colors[8] );
	//
        int radius;
        radius = cvRound(r1->size*1.2/9.*2);
        cvCircle( correspond, cvPointFrom32f(r1->pt), radius, colors[0], 1, 8, 0 );
		
	// kaneko
        int radius1 = cvRound(r1->size*1.2/9.*2);;
        int radius2 = cvRound(r2->size*1.2/9.*2);;
	fprintf( stderr, "%d, %d, %d, %f, %f, %d, %f, %f, %d\n", i/2, ptpairs[i], ptpairs[i+1], r1->pt.x, r1->pt.y, radius1, r2->pt.x, r2->pt.y, radius2 );
    }

    cvShowImage( "Object Correspond", correspond );
    fprintf( stderr, "done\n" );

    cvWaitKey(0);

    cvDestroyWindow("Object Correspond");

    return 0;
}

ソースコード (ファイル名: opencv_surf.cpp)

今度は、 ウインドウを開かない、 「fprintf( stderr, "%d, %d, %d, %f, %f, %d, %f, %f, %d\n", i/2, ptpairs[i], ptpairs[i+1], r1->pt.x, r1->pt.y, radius1, r2->pt.x, r2->pt.y, radius2 );」で表示のみ、 というように書き換え

/*
 * A Demo to OpenCV Implementation of SURF
 * Further Information Refer to "SURF: Speed-Up Robust Feature"
 * Author: Liu Liu
 * liuliu.1987+opencv@gmail.com
 */
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/calib3d/calib3d.hpp"
#include "opencv2/nonfree/nonfree.hpp"
#include "opencv2/imgproc/imgproc_c.h"
#include "opencv2/legacy/legacy.hpp"
#include "opencv2/legacy/compat.hpp"

#include 
#include 
#include 

using namespace std;
static void help()
{
    printf(
        "This program demonstrated the use of the SURF Detector and Descriptor using\n"
        "either FLANN (fast approx nearst neighbor classification) or brute force matching\n"
        "on planar objects.\n"
        "Usage:\n"
        "./find_obj  , default is box.png  and box_in_scene.png\n\n");
    return;
}

// define whether to use approximate nearest-neighbor search
#define USE_FLANN

#ifdef USE_FLANN
static void
flannFindPairs( const CvSeq*, const CvSeq* objectDescriptors,
		const CvSeq*, const CvSeq* imageDescriptors, vector& ptpairs )
{
    int length = (int)(objectDescriptors->elem_size/sizeof(float));

    cv::Mat m_object(objectDescriptors->total, length, CV_32F);
    cv::Mat m_image(imageDescriptors->total, length, CV_32F);


    // copy descriptors
    CvSeqReader obj_reader;
    float* obj_ptr = m_object.ptr(0);
    cvStartReadSeq( objectDescriptors, &obj_reader );
    for(int i = 0; i < objectDescriptors->total; i++ )
    {
        const float* descriptor = (const float*)obj_reader.ptr;
        CV_NEXT_SEQ_ELEM( obj_reader.seq->elem_size, obj_reader );
        memcpy(obj_ptr, descriptor, length*sizeof(float));
        obj_ptr += length;
    }
    CvSeqReader img_reader;
    float* img_ptr = m_image.ptr(0);
    cvStartReadSeq( imageDescriptors, &img_reader );
    for(int i = 0; i < imageDescriptors->total; i++ )
    {
        const float* descriptor = (const float*)img_reader.ptr;
        CV_NEXT_SEQ_ELEM( img_reader.seq->elem_size, img_reader );
        memcpy(img_ptr, descriptor, length*sizeof(float));
        img_ptr += length;
    }

    // find nearest neighbors using FLANN
    cv::Mat m_indices(objectDescriptors->total, 2, CV_32S);
    cv::Mat m_dists(objectDescriptors->total, 2, CV_32F);
    cv::flann::Index flann_index(m_image, cv::flann::KDTreeIndexParams(4));  // using 4 randomized kdtrees
    flann_index.knnSearch(m_object, m_indices, m_dists, 2, cv::flann::SearchParams(64) ); // maximum number of leafs checked

    int* indices_ptr = m_indices.ptr(0);
    float* dists_ptr = m_dists.ptr(0);
    // kaneko
    //    fprintf( stderr, "objkeypoint, imagekeypoint, dist\n" );
    int j=0; 
    for (int i=0;i best )
            break;
    }
    return total_cost;
}

static int
// kaneko
naiveNearestNeighbor( const float* vec, int laplacian,
                      const CvSeq* model_keypoints,
                      const CvSeq* model_descriptors, const int i )
{
    int length = (int)(model_descriptors->elem_size/sizeof(float));
    int i, neighbor = -1;
    double d, dist1 = 1e6, dist2 = 1e6;
    CvSeqReader reader, kreader;
    cvStartReadSeq( model_keypoints, &kreader, 0 );
    cvStartReadSeq( model_descriptors, &reader, 0 );

    for( i = 0; i < model_descriptors->total; i++ )
    {
        const CvSURFPoint* kp = (const CvSURFPoint*)kreader.ptr;
        const float* mvec = (const float*)reader.ptr;
        CV_NEXT_SEQ_ELEM( kreader.seq->elem_size, kreader );
        CV_NEXT_SEQ_ELEM( reader.seq->elem_size, reader );
        if( laplacian != kp->laplacian )
            continue;
        d = compareSURFDescriptors( vec, mvec, dist2, length );
        if( d < dist1 )
        {
            dist2 = dist1;
            dist1 = d;
            neighbor = i;
        }
        else if ( d < dist2 )
            dist2 = d;
    }
    if ( dist1 < 0.6*dist2 ) {
      // kaneko
      //      fprintf( stderr, "%d, %d, %f \n", i, neighbor, dist1 );
      return neighbor;
    }
    return -1;
}

static void
findPairs( const CvSeq* objectKeypoints, const CvSeq* objectDescriptors,
           const CvSeq* imageKeypoints, const CvSeq* imageDescriptors, vector& ptpairs )
{
    int i;
    CvSeqReader reader, kreader;
    cvStartReadSeq( objectKeypoints, &kreader );
    cvStartReadSeq( objectDescriptors, &reader );
    ptpairs.clear();

    for( i = 0; i < objectDescriptors->total; i++ )
    {
        const CvSURFPoint* kp = (const CvSURFPoint*)kreader.ptr;
        const float* descriptor = (const float*)reader.ptr;
        CV_NEXT_SEQ_ELEM( kreader.seq->elem_size, kreader );
        CV_NEXT_SEQ_ELEM( reader.seq->elem_size, reader );
	// kaneko
        int nearest_neighbor = naiveNearestNeighbor( descriptor, kp->laplacian, imageKeypoints, imageDescriptors, i );
        if( nearest_neighbor >= 0 )
        {
            ptpairs.push_back(i);
            ptpairs.push_back(nearest_neighbor);
        }
    }
}
#endif

/* a rough implementation for object location */
static int
locatePlanarObject( const CvSeq* objectKeypoints, const CvSeq* objectDescriptors,
                    const CvSeq* imageKeypoints, const CvSeq* imageDescriptors,
                    const CvPoint src_corners[4], CvPoint dst_corners[4] )
{
    double h[9];
    CvMat _h = cvMat(3, 3, CV_64F, h);
    vector ptpairs;
    vector pt1, pt2;
    CvMat _pt1, _pt2;
    int i, n;

#ifdef USE_FLANN
    flannFindPairs( objectKeypoints, objectDescriptors, imageKeypoints, imageDescriptors, ptpairs );
#else
    findPairs( objectKeypoints, objectDescriptors, imageKeypoints, imageDescriptors, ptpairs );
#endif

    n = (int)(ptpairs.size()/2);
    if( n < 4 )
        return 0;

    pt1.resize(n);
    pt2.resize(n);
    for( i = 0; i < n; i++ )
    {
        pt1[i] = ((CvSURFPoint*)cvGetSeqElem(objectKeypoints,ptpairs[i*2]))->pt;
        pt2[i] = ((CvSURFPoint*)cvGetSeqElem(imageKeypoints,ptpairs[i*2+1]))->pt;
    }

    _pt1 = cvMat(1, n, CV_32FC2, &pt1[0] );
    _pt2 = cvMat(1, n, CV_32FC2, &pt2[0] );
    if( !cvFindHomography( &_pt1, &_pt2, &_h, CV_RANSAC, 5 ))
        return 0;

    for( i = 0; i < 4; i++ )
    {
        double x = src_corners[i].x, y = src_corners[i].y;
        double Z = 1./(h[6]*x + h[7]*y + h[8]);
        double X = (h[0]*x + h[1]*y + h[2])*Z;
        double Y = (h[3]*x + h[4]*y + h[5])*Z;
        dst_corners[i] = cvPoint(cvRound(X), cvRound(Y));
    }

    return 1;
}

int main(int argc, char** argv)
{
    const char* object_filename = argc == 3 ? argv[1] : "box.png";
    const char* scene_filename = argc == 3 ? argv[2] : "box_in_scene.png";

    cv::initModule_nonfree();
    help();

    IplImage* object = cvLoadImage( object_filename, CV_LOAD_IMAGE_GRAYSCALE );
    IplImage* image = cvLoadImage( scene_filename, CV_LOAD_IMAGE_GRAYSCALE );
    if( !object || !image )
    {
        fprintf( stderr, "Can not load %s and/or %s\n",
            object_filename, scene_filename );
        exit(-1);
    }

    CvMemStorage* storage = cvCreateMemStorage(0);

    cvNamedWindow("Object Correspond", 1);

    static CvScalar colors[] =
    {
        {{0,0,255}},
        {{0,128,255}},
        {{0,255,255}},
        {{0,255,0}},
        {{255,128,0}},
        {{255,255,0}},
        {{255,0,0}},
        {{255,0,255}},
        {{255,255,255}}
    };

    IplImage* object_color = cvCreateImage(cvGetSize(object), 8, 3);
    cvCvtColor( object, object_color, CV_GRAY2BGR );

    CvSeq* objectKeypoints = 0, *objectDescriptors = 0;
    CvSeq* imageKeypoints = 0, *imageDescriptors = 0;
    int i;
    CvSURFParams params = cvSURFParams(500, 1);

    double tt = (double)cvGetTickCount();
    cvExtractSURF( object, 0, &objectKeypoints, &objectDescriptors, storage, params );
    //    printf("Object Descriptors: %d\n", objectDescriptors->total);

    cvExtractSURF( image, 0, &imageKeypoints, &imageDescriptors, storage, params );
    //    printf("Image Descriptors: %d\n", imageDescriptors->total);
    tt = (double)cvGetTickCount() - tt;

    // printf( "Extraction time = %gms\n", tt/(cvGetTickFrequency()*1000.));

    CvPoint src_corners[4] = {{0,0}, {object->width,0}, {object->width, object->height}, {0, object->height}};
    CvPoint dst_corners[4];
    //    IplImage* correspond = cvCreateImage( cvSize(image->width, object->height+image->height), 8, 1 );
    //    cvSetImageROI( correspond, cvRect( 0, 0, object->width, object->height ) );
    //    cvCopy( object, correspond );
    //    cvSetImageROI( correspond, cvRect( 0, object->height, correspond->width, correspond->height ) );
    //    cvCopy( image, correspond );
    //    cvResetImageROI( correspond );

#ifdef USE_FLANN
    //    printf("Using approximate nearest neighbor search\n");
#endif

    if( locatePlanarObject( objectKeypoints, objectDescriptors, imageKeypoints,
        imageDescriptors, src_corners, dst_corners ))
    {
        for( i = 0; i < 4; i++ )
        {
            CvPoint r1 = dst_corners[i%4];
            CvPoint r2 = dst_corners[(i+1)%4];
	    //            cvLine( correspond, cvPoint(r1.x, r1.y+object->height ),
	    //                cvPoint(r2.x, r2.y+object->height ), colors[8] );
        }
    }
    vector ptpairs;
#ifdef USE_FLANN
    flannFindPairs( objectKeypoints, objectDescriptors, imageKeypoints, imageDescriptors, ptpairs );
#else
    findPairs( objectKeypoints, objectDescriptors, imageKeypoints, imageDescriptors, ptpairs );
#endif
    // kaneko
    fprintf( stderr, "id, objkeypoint, imagekeypoint, p1x, p1y, radius1, p2x, p2y, radius2\n" );
    for( i = 0; i < (int)ptpairs.size(); i += 2 )
    {
        CvSURFPoint* r1 = (CvSURFPoint*)cvGetSeqElem( objectKeypoints, ptpairs[i] );
        CvSURFPoint* r2 = (CvSURFPoint*)cvGetSeqElem( imageKeypoints, ptpairs[i+1] );
	//        cvLine( correspond, cvPointFrom32f(r1->pt), cvPoint(cvRound(r2->pt.x), cvRound(r2->pt.y+object->height)), colors[8] );
	//
        int radius;
        radius = cvRound(r1->size*1.2/9.*2);
	//        cvCircle( correspond, cvPointFrom32f(r1->pt), radius, colors[0], 1, 8, 0 );
		
	// kaneko
        int radius1 = cvRound(r1->size*1.2/9.*2);;
        int radius2 = cvRound(r2->size*1.2/9.*2);;
	fprintf( stderr, "%d, %d, %d, %f, %f, %d, %f, %f, %d\n", i/2, ptpairs[i], ptpairs[i+1], r1->pt.x, r1->pt.y, radius1, r2->pt.x, r2->pt.y, radius2 );
    }

    // cvShowImage( "Object Correspond", correspond );
    //    fprintf( stderr, "done\n" );

    //    cvWaitKey(0);

    // cvDestroyWindow("Object Correspond");

    return 0;
}

surf 実験. まず、エンコード.5フレームごとに1フレームを /tmp/w に保存.

cd /tmp
mkdir /tmp/w
cp /home/kkaneko/rinkou/od/data/recorder2013/10540510/*.AVI /tmp
ffmpeg -i 03430001.AVI 03430001-%07d.png
cp *0.png *5.png /tmp/w
rm /tmp/*.png
ffmpeg -i 03430002.AVI 03430002-%07d.png
cp *0.png *5.png /tmp/w
rm /tmp/*.png
ffmpeg -i 03430003.AVI 03430003-%07d.png
cp *0.png *5.png /tmp/w
rm /tmp/*.png
ffmpeg -i 03430004.AVI 03430004-%07d.png
cp *0.png *5.png /tmp/w
rm /tmp/*.png
ffmpeg -i 03430005.AVI 03430005-%07d.png
cp *0.png *5.png /tmp/w
rm /tmp/*.png
ffmpeg -i 03430006.AVI 03430006-%07d.png
cp *0.png *5.png /tmp/w
rm /tmp/*.png
ffmpeg -i 03430007.AVI 03430007-%07d.png
cp *0.png *5.png /tmp/w
rm /tmp/*.png
ffmpeg -i 03430008.AVI 03430008-%07d.png
cp *0.png *5.png /tmp/w
rm /tmp/*.png
ffmpeg -i 03430009.AVI 03430009-%07d.png
cp *0.png *5.png /tmp/w
rm /tmp/*.png
# 総フレーム数 1810, fps は多分 10 [fps]

たくさんできる。1秒あたり2フレームに間引きされていることに注意

surf


export f=`echo *.png`
export f2=''
for i in $f; do
   if [ ! f2 = '' ]; then 
     /tmp/opencv_surf $f2 $i > $f2.csv
     export f2=$i
   fi
done

# number of keypoints 
rm -f lines.csv
touch lines.csv
for i in *.csv; do
  wc $i >> lines.csv
done

R
require(data.table)
x <- read.table("lines.csv", as.is=TRUE)
# number of keypoint pairs
plot( x[,1] - 7 )
lines( lowess( x[,1] - 7, f=0.01 ) )

[image]

inlier and outlier example

R
x <- read.csv("03430001-0000405.png.csv", as.is=TRUE, skip=7, header=FALSE)
d <- rbind(x$V4 - x$V7, x$V5 - x$V7)
require(mvoutlier)
corr.plot( x=d[1,], y=d[2,] )

[image]
#!/bin/bash
rm -f lines.csv
rm -f /tmp/1
touch /tmp/1
for i in *.csv; do 
  echo $i 
  rm -f /tmp/a.r
cat > /tmp/a.r <<-RCOMMAND
x <- read.csv("$i", as.is=TRUE, skip=7, header=FALSE)
d <- rbind(x\$V4 - x\$V7, x\$V5 - x\$V7)
require(mvoutlier)
# corr.plot( x=d[1,], y=d[2,] )
# hist( sign2(t(d))\$x.dist )
a <- c( sum( sign2(t(d), qcrit=0.975)\$wfinal01 ),
sum( sign2(t(d), qcrit=0.95)\$wfinal01 ),
sum( sign2(t(d), qcrit=0.9)\$wfinal01 ),
sum( sign2(t(d), qcrit=0.8)\$wfinal01 ))
print(a)
RCOMMAND
  cat /tmp/a.r | R --vanilla >> /tmp/1
done 
fgrep '[1]' /tmp/1 > /tmp/2
R
x <- read.table("/tmp/2", header=FALSE, as.is=TRUE)
plot( lowess( x[,2], f=0.01 ), type="l", col=1, xlim=c(0,362), ylim=c(0, 1400) )
par(new=T)
plot( lowess( x[,3], f=0.01 ), type="l", col=2, xlim=c(0,362), ylim=c(0, 1400) )
par(new=T)

Fast algorithm for identifying multivariate outliers in high-dimensional and/or large datasets, using spatial signs, see Filzmoser, Maronna, and Werner (CSDA, 2007)

[image]
#!/bin/bash
rm -f lines.csv
rm -f /tmp/3
touch /tmp/3
for i in *.csv; do 
  echo $i 
  rm -f /tmp/a.r
cat > /tmp/a.r <<-RCOMMAND
x <- read.csv("$i", as.is=TRUE, skip=7, header=FALSE)
d <- rbind(x\$V4 - x\$V7, x\$V5 - x\$V7)
require(mvoutlier)
# corr.plot( x=d[1,], y=d[2,] )
# hist( sign2(t(d))\$x.dist )
a <- sign2(t(d), qcrit=0.975)\$wfinal01 
d2 <- rbind(x[a==1,]\$V4 - x[a==1,]\$V7, x[a==1,]\$V5 - x[a==1,]\$V7)
print( c( mean(d2[1,]), mean(d2[2,]) ) )
RCOMMAND
  cat /tmp/a.r | R --vanilla >> /tmp/3
done 
fgrep '[1]' /tmp/3 > /tmp/4
R
x2 <- read.table("/tmp/4", header=FALSE, as.is=TRUE)
plot( x2[,2], type="l", col=2, xlim=c(0,362) )
plot( x2[,3], type="l", col=4, xlim=c(0,362) )

time change of mean value of x

[image]

time change of mean value of y

[image]
x <- read.table("/tmp/2", header=FALSE, as.is=TRUE)
x2 <- read.table("/tmp/4", header=FALSE, as.is=TRUE)
d <- rbind(x[,2], x2[,2], x2[,3])

# 状態
# 1 左に曲がる、2 右に曲がる, 3 止まっている
y <- rep(0, 722)
y[18:28] <- 1
y[58:66] <- 1
y[67:76] <- 1
y[94:104] <- 1
y[104:116] <- 1
y[246:254] <- 1
y[340:350] <- 1

y[46:56] <- 2
y[128:138] <- 2
y[164:172] <- 2
y[194:208] <- 2
y[492:502] <- 2

y[1:8] <- 3
y[272:334] <- 3
y[406:432] <- 3

plot(x=d[2,], y=d[3,], type="p", col=y+1)
par(new=T)
plot(x=d[2,], y=d[3,], type="l", lwd=0.2)

[image]

引き続き

# 正規化
d[1,] <- scale( d[1,] ) 
d[2,] <- scale( d[2,] ) 
d[3,] <- scale( d[3,] )
p1 <- cmdscale(dist(t(d)))
plot(x=p1[,1], y=p1[,2], type="p", col=y+1)
par(new=T)
plot(x=p1[,1], y=p1[,2], type="l", lwd=0.2)

[image]