From 426dddd553d71070f53bd2d04454bf28a5983aee Mon Sep 17 00:00:00 2001 From: "N.N." Date: Mon, 13 Jul 2009 18:17:19 +0000 Subject: added a simple OCR object based on cvKNearest : pdp_opencv_cvk svn path=/trunk/externals/pdp_opencv/; revision=11858 --- Makefile | 4 +- pdp_opencv_cvk-help.pd | 111 +++++++++++ pdp_opencv_cvk.cc | 532 +++++++++++++++++++++++++++++++++++++++++++++++++ plus/000.png | Bin 0 -> 367 bytes plus/001.png | Bin 0 -> 436 bytes plus/002.png | Bin 0 -> 407 bytes plus/003.png | Bin 0 -> 454 bytes plus/004.png | Bin 0 -> 421 bytes plus/005.png | Bin 0 -> 416 bytes plus/006.png | Bin 0 -> 429 bytes plus/007.png | Bin 0 -> 408 bytes plus/008.png | Bin 0 -> 373 bytes plus/009.png | Bin 0 -> 390 bytes plus/010.png | Bin 0 -> 406 bytes plus/011.png | Bin 0 -> 429 bytes plus/012.png | Bin 0 -> 460 bytes plus/013.png | Bin 0 -> 546 bytes plus/014.png | Bin 0 -> 486 bytes plus/015.png | Bin 0 -> 459 bytes plus/016.png | Bin 0 -> 440 bytes plus/017.png | Bin 0 -> 372 bytes plus/018.png | Bin 0 -> 434 bytes plus/019.png | Bin 0 -> 370 bytes plus/020.png | Bin 0 -> 399 bytes plus/021.png | Bin 0 -> 374 bytes plus/022.png | Bin 0 -> 452 bytes plus/023.png | Bin 0 -> 434 bytes plus/024.png | Bin 0 -> 417 bytes plus/025.png | Bin 0 -> 374 bytes plus/026.png | Bin 0 -> 381 bytes plus/027.png | Bin 0 -> 373 bytes plus/028.png | Bin 0 -> 412 bytes plus/029.png | Bin 0 -> 446 bytes plus/030.png | Bin 0 -> 373 bytes plus/031.png | Bin 0 -> 487 bytes plus/032.png | Bin 0 -> 410 bytes plus/033.png | Bin 0 -> 390 bytes plus/034.png | Bin 0 -> 454 bytes plus/035.png | Bin 0 -> 373 bytes plus/036.png | Bin 0 -> 406 bytes plus/037.png | Bin 0 -> 390 bytes plus/038.png | Bin 0 -> 381 bytes plus/039.png | Bin 0 -> 400 bytes plus/040.png | Bin 0 -> 413 bytes plus/041.png | Bin 0 -> 370 bytes plus/042.png | Bin 0 -> 447 bytes plus/043.png | Bin 0 -> 411 bytes plus/044.png | Bin 0 -> 502 bytes plus/045.png | Bin 0 -> 406 bytes plus/046.png | Bin 0 -> 417 bytes plus/047.png | Bin 0 -> 463 bytes plus/048.png | Bin 0 -> 438 bytes plus/049.png | Bin 0 -> 457 bytes 53 files changed, 645 insertions(+), 2 deletions(-) create mode 100755 pdp_opencv_cvk-help.pd create mode 100755 pdp_opencv_cvk.cc create mode 100644 plus/000.png create mode 100644 plus/001.png create mode 100644 plus/002.png create mode 100644 plus/003.png create mode 100644 plus/004.png create mode 100644 plus/005.png create mode 100644 plus/006.png create mode 100644 plus/007.png create mode 100644 plus/008.png create mode 100644 plus/009.png create mode 100644 plus/010.png create mode 100644 plus/011.png create mode 100644 plus/012.png create mode 100644 plus/013.png create mode 100644 plus/014.png create mode 100644 plus/015.png create mode 100644 plus/016.png create mode 100644 plus/017.png create mode 100644 plus/018.png create mode 100644 plus/019.png create mode 100644 plus/020.png create mode 100644 plus/021.png create mode 100644 plus/022.png create mode 100644 plus/023.png create mode 100644 plus/024.png create mode 100644 plus/025.png create mode 100644 plus/026.png create mode 100644 plus/027.png create mode 100644 plus/028.png create mode 100644 plus/029.png create mode 100644 plus/030.png create mode 100644 plus/031.png create mode 100644 plus/032.png create mode 100644 plus/033.png create mode 100644 plus/034.png create mode 100644 plus/035.png create mode 100644 plus/036.png create mode 100644 plus/037.png create mode 100644 plus/038.png create mode 100644 plus/039.png create mode 100644 plus/040.png create mode 100644 plus/041.png create mode 100644 plus/042.png create mode 100644 plus/043.png create mode 100644 plus/044.png create mode 100644 plus/045.png create mode 100644 plus/046.png create mode 100644 plus/047.png create mode 100644 plus/048.png create mode 100644 plus/049.png diff --git a/Makefile b/Makefile index a2246b7..0806f67 100644 --- a/Makefile +++ b/Makefile @@ -37,9 +37,9 @@ endif .SUFFIXES = $(EXTENSION) -SOURCES = pdp_opencv_threshold.c pdp_opencv_edge.c pdp_opencv_distrans.c pdp_opencv_laplace.c pdp_opencv_motempl.c pdp_opencv_morphology.c pdp_opencv_haarcascade.c pdp_opencv_contours_convexity.c pdp_opencv_contours_boundingrect.c pdp_opencv_bgsubstract.c pdp_opencv_lk.c pdp_opencv_floodfill.c pdp_opencv_histo.c pdp_opencv_dft.c +SOURCES = pdp_opencv_threshold.c pdp_opencv_edge.c pdp_opencv_distrans.c pdp_opencv_laplace.c pdp_opencv_motempl.c pdp_opencv_morphology.c pdp_opencv_haarcascade.c pdp_opencv_contours_convexity.c pdp_opencv_contours_boundingrect.c pdp_opencv_bgsubstract.c pdp_opencv_lk.c pdp_opencv_floodfill.c pdp_opencv_histo.c pdp_opencv_dft.c pdp_opencv_cvk.cc -all: $(SOURCES:.c=.$(EXTENSION)) +all: $(SOURCES:.c=.$(EXTENSION)) $(SOURCES:.cc=.$(EXTENSION)) %.$(EXTENSION): %.o gcc $(LDFLAGS) -o $*.$(EXTENSION) $*.o $(LIBS) diff --git a/pdp_opencv_cvk-help.pd b/pdp_opencv_cvk-help.pd new file mode 100755 index 0000000..118284a --- /dev/null +++ b/pdp_opencv_cvk-help.pd @@ -0,0 +1,111 @@ +#N canvas 257 49 833 785 10; +#X obj -17 -54 cnv 15 700 350 empty empty empty 20 12 0 14 -260097 +-66577 0; +#X obj 226 -30 tgl 15 0 empty empty empty 17 7 0 10 -262144 -1 -1 1 +1; +#X obj 226 -4 metro 40; +#X obj -15 303 cnv 15 621 300 empty empty empty 20 12 0 14 -258113 +-66577 0; +#X obj 26 626 pdp_xv; +#X msg 116 610 cursor 1; +#X floatatom 163 456 10 0 0 0 - - -; +#X obj 225 21 pdp_background; +#X obj 223 190 pdp_xv; +#X msg 273 189 cursor 1; +#X msg 317 166 rgb 0 0 0; +#X obj 449 166 loadbang; +#X msg 110 375 bang; +#X text -13 -110 pdp_opencv_cvk : OCR-like pattern recognition; +#X text -13 -99 based on basic OCR with Open CV tutorial; +#X text -13 -88 by damiles : http://blog.damiles.com/?p=93; +#X text -13 -75 adapted to PD by Yves Degoyon ( ydegoyon@gmail.com +); +#X text 253 -32 Hand drawing input; +#X msg 255 107 clear; +#X obj 48 -6 openpanel; +#X msg 29 -35 bang; +#X obj 28 62 pdp_background; +#X obj 30 139 pdp_imgloader; +#X obj 28 39 metro 40; +#X msg 47 18 load \$1 0 0; +#X floatatom 123 64 5 0 0 0 - - -; +#X obj 56 166 pdp_xv; +#X obj 29 86 pdp_convert image/YCrCb/*; +#X obj 3 11 tgl 15 0 empty empty empty 17 7 0 10 -262144 -1 -1 0 1 +; +#X obj 28 109 pdp_scale 128 128; +#X text 65 -35 Test with an image; +#X text 235 457 Distance of the incoming symbol compared to the patterns +; +#X obj 29 226 pdp_binary; +#X text 147 376 compare the incoming frame to the patterns; +#X text 359 108 <-- clear the input and try again; +#X obj 222 167 pdp_pen ------; +#X obj 226 48 pdp_convert image/YCrCb/*; +#X text 340 76 <-- this must be the same size as your patterns; +#X obj 226 78 pdp_scale 128 128; +#X obj 167 397 s pclear; +#X obj 298 108 r pclear; +#X obj 109 398 del 1000; +#X msg 384 167 width 8; +#N canvas 5 49 634 415 thingsidontwanttosee 0; +#X msg 267 202 draw \$1 \$2; +#X obj 358 202 unpack f f; +#X obj 343 228 f; +#X obj 370 228 f; +#X msg 380 179 fill \$1 \$2; +#X obj 342 248 pack f f; +#X obj 227 179 route press drag release; +#X obj 230 148 inlet; +#X obj 258 274 outlet; +#X connect 0 0 8 0; +#X connect 1 0 2 1; +#X connect 1 1 3 1; +#X connect 2 0 5 0; +#X connect 3 0 5 1; +#X connect 4 0 8 0; +#X connect 5 0 4 0; +#X connect 6 0 1 0; +#X connect 6 1 0 0; +#X connect 7 0 6 0; +#X restore 270 138 pd thingsidontwanttosee; +#X text 421 137 <-- mouse events; +#X obj 19 423 pdp_opencv_cvk plus 50; +#X connect 1 0 2 0; +#X connect 2 0 7 0; +#X connect 5 0 4 0; +#X connect 7 0 36 0; +#X connect 8 0 43 0; +#X connect 9 0 8 0; +#X connect 10 0 35 0; +#X connect 11 0 10 0; +#X connect 11 0 42 0; +#X connect 12 0 41 0; +#X connect 12 0 45 0; +#X connect 18 0 35 0; +#X connect 19 0 24 0; +#X connect 20 0 19 0; +#X connect 20 0 23 0; +#X connect 20 0 28 0; +#X connect 21 0 27 0; +#X connect 22 0 32 0; +#X connect 22 0 26 0; +#X connect 23 0 21 0; +#X connect 24 0 22 0; +#X connect 25 0 21 3; +#X connect 27 0 29 0; +#X connect 28 0 23 0; +#X connect 29 0 22 0; +#X connect 32 0 45 0; +#X connect 35 0 8 0; +#X connect 35 0 9 0; +#X connect 35 0 32 0; +#X connect 36 0 38 0; +#X connect 38 0 35 0; +#X connect 40 0 18 0; +#X connect 41 0 39 0; +#X connect 42 0 35 0; +#X connect 43 0 35 0; +#X connect 45 0 4 0; +#X connect 45 0 5 0; +#X connect 45 1 6 0; diff --git a/pdp_opencv_cvk.cc b/pdp_opencv_cvk.cc new file mode 100755 index 0000000..e75accd --- /dev/null +++ b/pdp_opencv_cvk.cc @@ -0,0 +1,532 @@ +/* + * Pure Data Packet module. + * Copyright (c) by Tom Schouten + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + * + * + * pdp_opencv_cvk : OCR like pattern recognition + * based on basic OCR with Open CV tutorial + * by damiles : http://blog.damiles.com/?p=93 + * + * + */ + +#include +#include +#include +#include +#include +#include +#include + +#include "pdp.h" + +#include "cv.h" +#include "highgui.h" +#include "ml.h" + +#define MAX_MARKERS 10 + +const int MAX_COUNT = 500; + +typedef struct pdp_opencv_cvk_struct +{ + t_object x_obj; + t_float x_f; + + t_outlet *x_outlet0; + t_outlet *x_outlet1; + + int x_packet0; + int x_packet1; + int x_dropped; + int x_queue_id; + + int x_width; + int x_height; + int x_size; + + int x_classify; + int x_ROIx; + int x_ROIy; + int x_ROIw; + int x_ROIh; + + // The output and temporary images + IplImage *rgb, *grey; + + // open cv classifier data + char *x_filepath; + int x_nsamples; + int x_rsamples; + CvMat* trainData; + CvMat* trainClasses; + CvMat* x_nearest; + CvMat* x_dist; + int x_pwidth; + int x_pheight; + CvKNearest *knn; + +} t_pdp_opencv_cvk; + +IplImage pdp_opencv_cvk_preprocessing(IplImage* imgSrc,int new_width, int new_height); + +static void pdp_opencv_cvk_process_rgb(t_pdp_opencv_cvk *x) +{ + t_pdp *header = pdp_packet_header(x->x_packet0); + short int *data = (short int *)pdp_packet_data(x->x_packet0); + t_pdp *newheader = pdp_packet_header(x->x_packet1); + short int *newdata = (short int *)pdp_packet_data(x->x_packet1); + int i,j,k,im; + + if ((x->x_width != (t_int)header->info.image.width) || + (x->x_height != (t_int)header->info.image.height)) + { + + post("pdp_opencv_cvk :: resizing"); + + x->x_width = header->info.image.width; + x->x_height = header->info.image.height; + x->x_size = x->x_width*x->x_height; + x->x_ROIx = 0; + x->x_ROIy = 0; + x->x_ROIw = header->info.image.width; + x->x_ROIh = header->info.image.height; + + cvReleaseImage( &x->rgb ); + cvReleaseImage( &x->grey ); + + // create cv_images + x->rgb = cvCreateImage( cvSize(x->x_width, x->x_height), 8, 3 ); + x->grey = cvCreateImage( cvSize(x->x_width, x->x_height), 8, 1 ); + + } + + newheader->info.image.encoding = header->info.image.encoding; + newheader->info.image.width = x->x_width; + newheader->info.image.height = x->x_height; + + memcpy( x->rgb->imageData, data, x->x_size*3 ); + + cvCvtColor( x->rgb, x->grey, CV_BGR2GRAY ); + + if ( x->x_classify ) + { + IplImage prs_image; + float result; + CvMat row_header, *row1, odata; + + // post( "pdp_opencv_cvk : size : (%dx%d)", x->x_pwidth, x->x_pheight); + + // process file + prs_image = pdp_opencv_cvk_preprocessing(x->grey, x->x_pwidth, x->x_pheight); + + //Set data + IplImage* img32 = cvCreateImage( cvSize( x->x_pwidth, x->x_pheight ), IPL_DEPTH_32F, 1 ); + cvConvertScale(&prs_image, img32, 0.0039215, 0); + cvGetSubRect(img32, &odata, cvRect(0,0, x->x_pwidth, x->x_pheight)); + row1 = cvReshape( &odata, &row_header, 0, 1 ); + + result=x->knn->find_nearest(row1,x->x_nsamples,0,0,x->x_nearest,x->x_dist); + for ( i=0; ix_nsamples; i++ ) + { + // post( "pdp_opencv_cvk : distance : %f", x->x_dist->data.fl[i] ); + } + outlet_float(x->x_outlet1, x->x_dist->data.fl[0]); + + // cvReleaseImage( &img32 ); + x->x_classify = 0; + } + + memcpy( newdata, x->rgb->imageData, x->x_size*3 ); + return; +} + +static void pdp_opencv_cvk_sendpacket(t_pdp_opencv_cvk *x) +{ + /* release the packet */ + pdp_packet_mark_unused(x->x_packet0); + x->x_packet0 = -1; + + /* unregister and propagate if valid dest packet */ + pdp_packet_pass_if_valid(x->x_outlet0, &x->x_packet1); +} + +static void pdp_opencv_cvk_process(t_pdp_opencv_cvk *x) +{ + int encoding; + t_pdp *header = 0; + char *parname; + unsigned pi; + int partype; + float pardefault; + t_atom plist[2]; + t_atom tlist[2]; + t_atom vlist[2]; + + /* check if image data packets are compatible */ + if ( (header = pdp_packet_header(x->x_packet0)) + && (PDP_BITMAP == header->type)){ + + /* pdp_opencv_cvk_process inputs and write into active inlet */ + switch(pdp_packet_header(x->x_packet0)->info.image.encoding){ + + case PDP_BITMAP_RGB: + x->x_packet1 = pdp_packet_clone_rw(x->x_packet0); + pdp_queue_add(x, (void*)pdp_opencv_cvk_process_rgb, (void*)pdp_opencv_cvk_sendpacket, &x->x_queue_id); + break; + + default: + /* don't know the type, so dont pdp_opencv_cvk_process */ + break; + + } + } + +} + +static void pdp_opencv_cvk_input_0(t_pdp_opencv_cvk *x, t_symbol *s, t_floatarg f) +{ + /* if this is a register_ro message or register_rw message, register with packet factory */ + + if (s == gensym("register_rw")) + x->x_dropped = pdp_packet_convert_ro_or_drop(&x->x_packet0, (int)f, pdp_gensym("bitmap/rgb/*") ); + + if ((s == gensym("process")) && (-1 != x->x_packet0) && (!x->x_dropped)) + { + /* add the process method and callback to the process queue */ + pdp_opencv_cvk_process(x); + } +} + +static void pdp_opencv_cvk_free(t_pdp_opencv_cvk *x) +{ + int i; + + pdp_queue_finish(x->x_queue_id); + pdp_packet_mark_unused(x->x_packet0); + + // destroy cv_images + cvReleaseImage( &x->rgb ); + cvReleaseImage( &x->grey ); + +} + +t_class *pdp_opencv_cvk_class; + +// +// +// Find the min box. The min box respect original aspect ratio image +// The image is a binary data and background is white. +// +// + + +void pdp_opencv_cvk_findX(IplImage* imgSrc,int* min, int* max) +{ + int i; + int minFound=0; + CvMat data; + CvScalar maxVal=cvRealScalar(imgSrc->width * 255); + CvScalar val=cvRealScalar(0); + + // for each col sum, if sum < width*255 then we find the min + // then continue to end to search the max, if sum< width*255 then is new max + for (i=0; i< imgSrc->width; i++) + { + cvGetCol(imgSrc, &data, i); + val= cvSum(&data); + if(val.val[0] < maxVal.val[0]) + { + *max= i; + if(!minFound) + { + *min= i; + minFound= 1; + } + } + } +} + +void pdp_opencv_cvk_findY(IplImage* imgSrc,int* min, int* max) +{ + int i; + int minFound=0; + CvMat data; + CvScalar maxVal=cvRealScalar(imgSrc->width * 255); + CvScalar val=cvRealScalar(0); + + // for each col sum, if sum < width*255 then we find the min + // then continue to end to search the max, if sum< width*255 then is new max + for (i=0; i< imgSrc->height; i++) + { + cvGetRow(imgSrc, &data, i); + val= cvSum(&data); + if(val.val[0] < maxVal.val[0]) + { + *max=i; + if(!minFound) + { + *min= i; + minFound= 1; + } + } + } +} + +// +// +// Find the bounding box. +// +// + +CvRect pdp_opencv_cvk_findBB(IplImage* imgSrc) +{ + CvRect aux; + int xmin, xmax, ymin, ymax; + xmin=xmax=ymin=ymax=0; + + pdp_opencv_cvk_findX(imgSrc, &xmin, &xmax); + pdp_opencv_cvk_findY(imgSrc, &ymin, &ymax); + + aux=cvRect(xmin, ymin, xmax-xmin, ymax-ymin); + + return aux; +} + +IplImage pdp_opencv_cvk_preprocessing(IplImage* imgSrc,int new_width, int new_height) +{ + IplImage* result; + IplImage* scaledResult; + + CvMat data; + CvMat dataA; + CvRect bb;//bounding box + CvRect bba;//boundinb box maintain aspect ratio + + // find bounding box + bb=pdp_opencv_cvk_findBB(imgSrc); + + if ( ( bb.width == 0 ) || ( bb.height == 0 ) ) + { + bb.x = 0; + bb.y = 0; + bb.width = imgSrc->width; + bb.height = imgSrc->height; + } + + // get bounding box data and no with aspect ratio, the x and y can be corrupted + cvGetSubRect(imgSrc, &data, cvRect(bb.x, bb.y, bb.width, bb.height)); + // create image with this data with width and height with aspect ratio 1 + // then we get highest size betwen width and height of our bounding box + int size=(bb.width>bb.height)?bb.width:bb.height; + result=cvCreateImage( cvSize( size, size ), 8, 1 ); + cvSet(result,CV_RGB(255,255,255),NULL); + // copy de data in center of image + int x=(int)floor((float)(size-bb.width)/2.0f); + int y=(int)floor((float)(size-bb.height)/2.0f); + cvGetSubRect(result, &dataA, cvRect(x,y,bb.width, bb.height)); + cvCopy(&data, &dataA, NULL); + // scale result + scaledResult=cvCreateImage( cvSize( new_width, new_height ), 8, 1 ); + cvResize(result, scaledResult, CV_INTER_NN); + + // return processed data + return *scaledResult; + +} + +void pdp_opencv_cvk_load(t_pdp_opencv_cvk *x) +{ + IplImage* src_image; + IplImage prs_image; + CvMat row,data; + char file[255]; + int i=0,j; + CvMat row_header, *row1; + + x->x_rsamples = 0; + + for( j = 0; j< x->x_nsamples; j++) + { + + // load file + sprintf(file,"%s/%03d.png",x->x_filepath, j); + src_image = cvLoadImage(file,0); + if(!src_image) + { + post("pdp_opencv_cvk : error: couldn't load image %s\n", file); + continue; + } + if ( ( x->x_pwidth == -1 ) || ( x->x_pheight == -1 ) ) + { + x->x_pwidth = src_image->width; + x->x_pheight = src_image->height; + // post( "pdp_opencv_cvk : loaded : %s (%dx%d)", file, src_image->width, src_image->height); + x->x_rsamples++; + } + else if ( ( src_image->width != x->x_pwidth ) || ( src_image->height != x->x_pheight ) ) + { + post( "pdp_opencv_cvk : error : %s (%dx%d) : wrong size ( should be %dx%d )", file, src_image->width, src_image->height, x->x_pwidth, x->x_pheight); + continue; + } + else + { + // post( "pdp_opencv_cvk : loaded : %s (%dx%d)", file, src_image->width, src_image->height); + x->x_rsamples++; + } + + // process file + prs_image = pdp_opencv_cvk_preprocessing(src_image, x->x_pwidth, x->x_pheight); + // post( "pdp_opencv_cvk : preprocessed : %s (%dx%d)", file, x->x_pwidth, x->x_pheight); + + if ( ( x->trainData == NULL ) || ( x->trainClasses == NULL )) + { + x->trainData = cvCreateMat(x->x_nsamples, x->x_pwidth*x->x_pheight, CV_32FC1); + x->trainClasses = cvCreateMat(x->x_nsamples, 1, CV_32FC1); + } + + // set class label + cvGetRow(x->trainClasses, &row, j); + cvSet(&row, cvRealScalar(i), NULL); + // set data + cvGetRow(x->trainData, &row, j); + + IplImage* img = cvCreateImage( cvSize( x->x_pwidth, x->x_pheight ), IPL_DEPTH_32F, 1 ); + // convert 8 bits image to 32 float image + cvConvertScale(&prs_image, img, 0.0039215, 0); + + cvGetSubRect(img, &data, cvRect( 0, 0, x->x_pwidth, x->x_pheight) ); + + // convert data matrix sizexsize to vecor + row1 = cvReshape( &data, &row_header, 0, 1 ); + cvCopy(row1, &row, NULL); + } + + // create the classifier + post( "pdp_opencv_cvk : loaded : %d samples", x->x_rsamples); + if ( x->x_rsamples == x->x_nsamples ) + { + x->knn=new CvKNearest( x->trainData, x->trainClasses, 0, false, x->x_nsamples ); + x->x_nearest=cvCreateMat(1,x->x_nsamples,CV_32FC1); + x->x_dist=cvCreateMat(1,x->x_nsamples,CV_32FC1); + } +} + +static void pdp_opencv_cvk_classify(t_pdp_opencv_cvk *x) +{ + if ( x->trainData == NULL ) + { + post( "pdp_opencv_cvk : no patterns loaded : cannot process" ); + return; + } + x->x_classify=1; +} + +void *pdp_opencv_cvk_new(t_symbol *s, int argc, t_atom *argv ) +{ + int i; + + t_pdp_opencv_cvk *x = (t_pdp_opencv_cvk *)pd_new(pdp_opencv_cvk_class); + + x->x_outlet0 = outlet_new(&x->x_obj, &s_anything); + x->x_outlet1 = outlet_new(&x->x_obj, &s_anything); + + x->x_packet0 = -1; + x->x_packet1 = -1; + x->x_queue_id = -1; + + x->x_width = 320; + x->x_height = 240; + x->x_size = x->x_width * x->x_height; + x->x_ROIx = 0; + x->x_ROIy = 0; + x->x_ROIw = x->x_width; + x->x_ROIh = x->x_height; + + x->rgb = cvCreateImage( cvSize(x->x_width, x->x_height), 8, 3 ); + x->grey = cvCreateImage( cvSize(x->x_width, x->x_height), 8, 1 ); + + x->x_filepath = ( char * ) getbytes( 1024 ); + sprintf( x->x_filepath, "./data" ); + x->x_nsamples = 10; + + if ( argc >= 1 ) + { + if ( argv[0].a_type != A_SYMBOL ) + { + error( "pdp_opencv_cvk : wrong argument (file path : 1)" ); + return NULL; + } + if ( !strcmp( argv[0].a_w.w_symbol->s_name, "" ) ) + { + error( "pdp_opencv_cvk : no extension specified" ); + error( "pdp_opencv_cvk : usage : pdp_opencv_cvk_new " ); + return NULL; + } + strcpy( x->x_filepath, argv[0].a_w.w_symbol->s_name ); + } + if ( argc >= 2 ) + { + if ( argv[1].a_type != A_FLOAT ) + { + error( "pdp_opencv_cvk : wrong argument (nsamples : 2)" ); + return NULL; + } + if ( (int)argv[1].a_w.w_float <= 0 ) + { + error( "pdp_opencv_cvk : wrong number of samples (%d)", (t_int)(int)argv[1].a_w.w_float ); + error( "pdp_opencv_cvk : usage : pdp_opencv_cvk_new " ); + return NULL; + } + x->x_nsamples = (int)argv[1].a_w.w_float; + } + + x->x_classify = 0; + x->x_pwidth = -1; + x->x_pheight = -1; + + x->trainData = NULL; + x->trainClasses = NULL; + + error( "pdp_opencv_cvk : loading %d samples from : %s", x->x_nsamples, x->x_filepath ); + pdp_opencv_cvk_load( x ); + + return (void *)x; +} + + +#ifdef __cplusplus +extern "C" +{ +#endif + + +void pdp_opencv_cvk_setup(void) +{ + + post( " pdp_opencv_cvk"); + pdp_opencv_cvk_class = class_new(gensym("pdp_opencv_cvk"), (t_newmethod)pdp_opencv_cvk_new, + (t_method)pdp_opencv_cvk_free, sizeof(t_pdp_opencv_cvk), CLASS_DEFAULT, A_GIMME, A_NULL); + + class_addmethod(pdp_opencv_cvk_class, (t_method)pdp_opencv_cvk_input_0, gensym("pdp"), A_SYMBOL, A_DEFFLOAT, A_NULL); + class_addmethod(pdp_opencv_cvk_class, (t_method)pdp_opencv_cvk_classify, gensym("bang"), A_NULL); + +} + +#ifdef __cplusplus +} +#endif diff --git a/plus/000.png b/plus/000.png new file mode 100644 index 0000000..87a27dc Binary files /dev/null and b/plus/000.png differ diff --git a/plus/001.png b/plus/001.png new file mode 100644 index 0000000..c79a7dc Binary files /dev/null and b/plus/001.png differ diff --git a/plus/002.png b/plus/002.png new file mode 100644 index 0000000..90e59f9 Binary files /dev/null and b/plus/002.png differ diff --git a/plus/003.png b/plus/003.png new file mode 100644 index 0000000..45385cb Binary files /dev/null and b/plus/003.png differ diff --git a/plus/004.png b/plus/004.png new file mode 100644 index 0000000..6f1c6d4 Binary files /dev/null and b/plus/004.png differ diff --git a/plus/005.png b/plus/005.png new file mode 100644 index 0000000..e58cb30 Binary files /dev/null and b/plus/005.png differ diff --git a/plus/006.png b/plus/006.png new file mode 100644 index 0000000..8ae55a5 Binary files /dev/null and b/plus/006.png differ diff --git a/plus/007.png b/plus/007.png new file mode 100644 index 0000000..e344d92 Binary files /dev/null and b/plus/007.png differ diff --git a/plus/008.png b/plus/008.png new file mode 100644 index 0000000..009f83e Binary files /dev/null and b/plus/008.png differ diff --git a/plus/009.png b/plus/009.png new file mode 100644 index 0000000..84c0867 Binary files /dev/null and b/plus/009.png differ diff --git a/plus/010.png b/plus/010.png new file mode 100644 index 0000000..1652678 Binary files /dev/null and b/plus/010.png differ diff --git a/plus/011.png b/plus/011.png new file mode 100644 index 0000000..e79637f Binary files /dev/null and b/plus/011.png differ diff --git a/plus/012.png b/plus/012.png new file mode 100644 index 0000000..4a24b39 Binary files /dev/null and b/plus/012.png differ diff --git a/plus/013.png b/plus/013.png new file mode 100644 index 0000000..ba9715a Binary files /dev/null and b/plus/013.png differ diff --git a/plus/014.png b/plus/014.png new file mode 100644 index 0000000..833d68d Binary files /dev/null and b/plus/014.png differ diff --git a/plus/015.png b/plus/015.png new file mode 100644 index 0000000..3cf9aa7 Binary files /dev/null and b/plus/015.png differ diff --git a/plus/016.png b/plus/016.png new file mode 100644 index 0000000..0b9e7d0 Binary files /dev/null and b/plus/016.png differ diff --git a/plus/017.png b/plus/017.png new file mode 100644 index 0000000..dfce6fc Binary files /dev/null and b/plus/017.png differ diff --git a/plus/018.png b/plus/018.png new file mode 100644 index 0000000..b88c6a3 Binary files /dev/null and b/plus/018.png differ diff --git a/plus/019.png b/plus/019.png new file mode 100644 index 0000000..c6aadf4 Binary files /dev/null and b/plus/019.png differ diff --git a/plus/020.png b/plus/020.png new file mode 100644 index 0000000..d93c43c Binary files /dev/null and b/plus/020.png differ diff --git a/plus/021.png b/plus/021.png new file mode 100644 index 0000000..1123c49 Binary files /dev/null and b/plus/021.png differ diff --git a/plus/022.png b/plus/022.png new file mode 100644 index 0000000..a211747 Binary files /dev/null and b/plus/022.png differ diff --git a/plus/023.png b/plus/023.png new file mode 100644 index 0000000..611e234 Binary files /dev/null and b/plus/023.png differ diff --git a/plus/024.png b/plus/024.png new file mode 100644 index 0000000..8f2507e Binary files /dev/null and b/plus/024.png differ diff --git a/plus/025.png b/plus/025.png new file mode 100644 index 0000000..0db7dc7 Binary files /dev/null and b/plus/025.png differ diff --git a/plus/026.png b/plus/026.png new file mode 100644 index 0000000..6f2e564 Binary files /dev/null and b/plus/026.png differ diff --git a/plus/027.png b/plus/027.png new file mode 100644 index 0000000..3726a5a Binary files /dev/null and b/plus/027.png differ diff --git a/plus/028.png b/plus/028.png new file mode 100644 index 0000000..caa3dc1 Binary files /dev/null and b/plus/028.png differ diff --git a/plus/029.png b/plus/029.png new file mode 100644 index 0000000..087a71c Binary files /dev/null and b/plus/029.png differ diff --git a/plus/030.png b/plus/030.png new file mode 100644 index 0000000..ab5e6f4 Binary files /dev/null and b/plus/030.png differ diff --git a/plus/031.png b/plus/031.png new file mode 100644 index 0000000..a8d3771 Binary files /dev/null and b/plus/031.png differ diff --git a/plus/032.png b/plus/032.png new file mode 100644 index 0000000..a94110b Binary files /dev/null and b/plus/032.png differ diff --git a/plus/033.png b/plus/033.png new file mode 100644 index 0000000..63e2200 Binary files /dev/null and b/plus/033.png differ diff --git a/plus/034.png b/plus/034.png new file mode 100644 index 0000000..889d5f5 Binary files /dev/null and b/plus/034.png differ diff --git a/plus/035.png b/plus/035.png new file mode 100644 index 0000000..b552f67 Binary files /dev/null and b/plus/035.png differ diff --git a/plus/036.png b/plus/036.png new file mode 100644 index 0000000..73190dd Binary files /dev/null and b/plus/036.png differ diff --git a/plus/037.png b/plus/037.png new file mode 100644 index 0000000..9e885b5 Binary files /dev/null and b/plus/037.png differ diff --git a/plus/038.png b/plus/038.png new file mode 100644 index 0000000..16eaf56 Binary files /dev/null and b/plus/038.png differ diff --git a/plus/039.png b/plus/039.png new file mode 100644 index 0000000..32e8def Binary files /dev/null and b/plus/039.png differ diff --git a/plus/040.png b/plus/040.png new file mode 100644 index 0000000..1b7bbfa Binary files /dev/null and b/plus/040.png differ diff --git a/plus/041.png b/plus/041.png new file mode 100644 index 0000000..d8b970e Binary files /dev/null and b/plus/041.png differ diff --git a/plus/042.png b/plus/042.png new file mode 100644 index 0000000..540920f Binary files /dev/null and b/plus/042.png differ diff --git a/plus/043.png b/plus/043.png new file mode 100644 index 0000000..4691fce Binary files /dev/null and b/plus/043.png differ diff --git a/plus/044.png b/plus/044.png new file mode 100644 index 0000000..110a46f Binary files /dev/null and b/plus/044.png differ diff --git a/plus/045.png b/plus/045.png new file mode 100644 index 0000000..ef2e42e Binary files /dev/null and b/plus/045.png differ diff --git a/plus/046.png b/plus/046.png new file mode 100644 index 0000000..d173fa7 Binary files /dev/null and b/plus/046.png differ diff --git a/plus/047.png b/plus/047.png new file mode 100644 index 0000000..7fec41a Binary files /dev/null and b/plus/047.png differ diff --git a/plus/048.png b/plus/048.png new file mode 100644 index 0000000..62c00f9 Binary files /dev/null and b/plus/048.png differ diff --git a/plus/049.png b/plus/049.png new file mode 100644 index 0000000..a9f41a4 Binary files /dev/null and b/plus/049.png differ -- cgit v1.2.1