News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

To vortex, about opencv

Started by Farabi, January 20, 2008, 03:21:53 AM

Previous topic - Next topic

Farabi

Hallo mister vortex, I've try your tools and it is working fine. But I dont understand why all of the function on the opencv library have only one parameter, but maybe I think it is because the function is really only have one function (if i making mistake please tell me, i need your assitance).
I upload all the include file, and I'll try to search about the structure for passing the variable.

[attachment deleted by admin]
Those who had universe knowledges can control the world by a micro processor.
http://www.wix.com/farabio/firstpage

"Etos siperi elegi"

Farabi

To others:
If you'd like to help, please download the library at this link http://switch.dl.sourceforge.net/sourceforge/opencvlibrary/chopencv-2.5.0-win-binary.zip
My goal is making the face detection example from that library to run perfectly using MASM.


#include "cv.h"
#include "highgui.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <math.h>
#include <float.h>
#include <limits.h>
#include <time.h>
#include <ctype.h>

#ifdef _EiC
#define WIN32
#endif

static CvMemStorage* storage = 0;
static CvHaarClassifierCascade* cascade = 0;

void detect_and_draw( IplImage* image );

const char* cascade_name =
    "haarcascade_frontalface_alt.xml";
/*    "haarcascade_profileface.xml";*/

int main( int argc, char** argv )
{
    CvCapture* capture = 0;
    IplImage *frame, *frame_copy = 0;
    int optlen = strlen("--cascade=");
    const char* input_name;

    if( argc > 1 && strncmp( argv[1], "--cascade=", optlen ) == 0 )
    {
        cascade_name = argv[1] + optlen;
        input_name = argc > 2 ? argv[2] : 0;
    }
    else
    {
        cascade_name = "../../data/haarcascades/haarcascade_frontalface_alt2.xml";
        input_name = argc > 1 ? argv[1] : 0;
    }

    cascade = (CvHaarClassifierCascade*)cvLoad( cascade_name, 0, 0, 0 );
   
    if( !cascade )
    {
        fprintf( stderr, "ERROR: Could not load classifier cascade\n" );
        fprintf( stderr,
        "Usage: facedetect --cascade=\"<cascade_path>\" [filename|camera_index]\n" );
        return -1;
    }
    storage = cvCreateMemStorage(0);
   
    if( !input_name || (isdigit(input_name[0]) && input_name[1] == '\0') )
        capture = cvCaptureFromCAM( !input_name ? 0 : input_name[0] - '0' );
    else
        capture = cvCaptureFromAVI( input_name );

    cvNamedWindow( "result", 1 );

    if( capture )
    {
        for(;;)
        {
            if( !cvGrabFrame( capture ))
                break;
            frame = cvRetrieveFrame( capture );
            if( !frame )
                break;
            if( !frame_copy )
                frame_copy = cvCreateImage( cvSize(frame->width,frame->height),
                                            IPL_DEPTH_8U, frame->nChannels );
            if( frame->origin == IPL_ORIGIN_TL )
                cvCopy( frame, frame_copy, 0 );
            else
                cvFlip( frame, frame_copy, 0 );
           
            detect_and_draw( frame_copy );

            if( cvWaitKey( 10 ) >= 0 )
                break;
        }

        cvReleaseImage( &frame_copy );
        cvReleaseCapture( &capture );
    }
    else
    {
        const char* filename = input_name ? input_name : (char*)"lena.jpg";
        IplImage* image = cvLoadImage( filename, 1 );

        if( image )
        {
            detect_and_draw( image );
            cvWaitKey(0);
            cvReleaseImage( &image );
        }
        else
        {
            /* assume it is a text file containing the
               list of the image filenames to be processed - one per line */
            FILE* f = fopen( filename, "rt" );
            if( f )
            {
                char buf[1000+1];
                while( fgets( buf, 1000, f ) )
                {
                    int len = (int)strlen(buf);
                    while( len > 0 && isspace(buf[len-1]) )
                        len--;
                    buf[len] = '\0';
                    image = cvLoadImage( buf, 1 );
                    if( image )
                    {
                        detect_and_draw( image );
                        cvWaitKey(0);
                        cvReleaseImage( &image );
                    }
                }
                fclose(f);
            }
        }

    }
   
    cvDestroyWindow("result");

    return 0;
}

void detect_and_draw( IplImage* img )
{
    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}}
    };

    double scale = 1.3;
    IplImage* gray = cvCreateImage( cvSize(img->width,img->height), 8, 1 );
    IplImage* small_img = cvCreateImage( cvSize( cvRound (img->width/scale),
                         cvRound (img->height/scale)),
                     8, 1 );
    int i;

    cvCvtColor( img, gray, CV_BGR2GRAY );
    cvResize( gray, small_img, CV_INTER_LINEAR );
    cvEqualizeHist( small_img, small_img );
    cvClearMemStorage( storage );

    if( cascade )
    {
        double t = (double)cvGetTickCount();
        CvSeq* faces = cvHaarDetectObjects( small_img, cascade, storage,
                                            1.1, 2, 0/*CV_HAAR_DO_CANNY_PRUNING*/,
                                            cvSize(30, 30) );
        t = (double)cvGetTickCount() - t;
        printf( "detection time = %gms\n", t/((double)cvGetTickFrequency()*1000.) );
        for( i = 0; i < (faces ? faces->total : 0); i++ )
        {
            CvRect* r = (CvRect*)cvGetSeqElem( faces, i );
            CvPoint center;
            int radius;
            center.x = cvRound((r->x + r->width*0.5)*scale);
            center.y = cvRound((r->y + r->height*0.5)*scale);
            radius = cvRound((r->width + r->height)*0.25*scale);
            cvCircle( img, center, radius, colors[i%8], 3, 8, 0 );
        }
    }

    cvShowImage( "result", img );
    cvReleaseImage( &gray );
    cvReleaseImage( &small_img );
}
Those who had universe knowledges can control the world by a micro processor.
http://www.wix.com/farabio/firstpage

"Etos siperi elegi"

Vortex

Hi Onan,

I checked your include files. It looks like that all the functions are following the C calling convention :

cvCompleteSymm PROTO C :VARARG

This means that the functions have variable number of parameters but to make life easier, the lib2def tool marks all the C functions as :VARARG It's not sure that all those functions are invoked with only parameter.
Examining a library, it's not always possible to tell the exact number of parameters. Probably, those functions have a definite number of parameters.
Checking the file chopencv-2.5.0-win-binary.zip, I could not find any lib files. Could you point me in the right direction to find the correct lib files?

Farabi

Quote from: Vortex on January 20, 2008, 06:09:53 PM
Hi Onan,

I checked your include files. It looks like that all the functions are following the C calling convention :

cvCompleteSymm PROTO C :VARARG

This means that the functions have variable number of parameters but to make life easier, the lib2def tool marks all the C functions as :VARARG It's not sure that all those functions are invoked with only parameter.
Examining a library, it's not always possible to tell the exact number of parameters. Probably, those functions have a definite number of parameters.
Checking the file chopencv-2.5.0-win-binary.zip, I could not find any lib files. Could you point me in the right direction to find the correct lib files?

In my harddisk it is at C:\Program Files\OpenCV\lib
I upload the lib, if it is against the rule please remove it.

[attachment deleted by admin]
Those who had universe knowledges can control the world by a micro processor.
http://www.wix.com/farabio/firstpage

"Etos siperi elegi"

Farabi

Thanks, I think I dont need to know about the parameter anymore.

Maybe you know how to translate this code to MASM

cascade = (CvHaarClassifierCascade*)cvLoad( cascade_name, 0, 0, 0 );
Those who had universe knowledges can control the world by a micro processor.
http://www.wix.com/farabio/firstpage

"Etos siperi elegi"

Vortex

Hi Onan,

Checking the libs, I see that they contain a lot C++ functions. lib2inc skips C++ functions, the tool supports only functions following the C calling convention so keep in mind that the include files generated with lib2inc is not %100 complete.

cascade = (CvHaarClassifierCascade*)cvLoad( cascade_name, 0, 0, 0 );

Checking your .h files, could you provide the original prototype of this function?

Farabi

Quote from: Vortex on January 21, 2008, 08:52:06 PM
Hi Onan,

Checking the libs, I see that they contain a lot C++ functions. lib2inc skips C++ functions, the tool supports only functions following the C calling convention so keep in mind that the include files generated with lib2inc is not %100 complete.

cascade = (CvHaarClassifierCascade*)cvLoad( cascade_name, 0, 0, 0 );

Checking your .h files, could you provide the original prototype of this function?

Is this is what you mean?
Quote
/* Loads haar classifier cascade from a directory.
   It is obsolete: convert your cascade to xml and use cvLoad instead */
CVAPI(CvHaarClassifierCascade*) cvLoadHaarClassifierCascade(
                    const char* directory, CvSize orig_window_size);

I've tryed tha cvLoad function but it show an error messagebox, I also debug the facedetect.exe but I dont find anything different compared to my code.  :dazzled:
here is the code

invoke cvLoad,CADD("data\haarcascades\haarcascade_frontalface_alt.xml"),0,0,0
.if eax==0
invoke MessageBox,hWnd,CADD("Cannot load cascade"),0,MB_OK
.endif


invoke cvNamedWindow,CADD("test"),1
; Img Structure
; +40 := width
; +44 := height
invoke cvLoadImage,CADD("S2020019.JPG"),1
.if eax!=0
mov img,eax
invoke cvShowImage,CADD("test"),img
.endif
Those who had universe knowledges can control the world by a micro processor.
http://www.wix.com/farabio/firstpage

"Etos siperi elegi"

Farabi


Facedetectl proc uses esi hWnd:dword
LOCAL buff[256]:dword
LOCAL buff2[256]:dword
LOCAL RegH:dword
LOCAL tmp,smallimg,gray:dword
LOCAL p:POINT
LOCAL face:dword
LOCAL img,cascade,storage
; Set Up everything

invoke cvLoad,CADD("data\haarcascades\haarcascade_frontalface_alt2.xml"),0,0,0
.if eax==0
invoke MessageBox,hWnd,CADD("Cannot load cascade"),0,MB_OK
.endif
mov cascade,eax
invoke cvCreateMemStorage,0
mov storage,eax

invoke cvNamedWindow,CADD("test"),1

; Img Structure
; +40 := width
; +44 := height
invoke cvLoadImage,CADD("Lena.JPG"),1
.if eax!=0
mov img,eax

push [eax+40]
pop p.x
push [eax+44]
pop p.y
invoke cvCreateImage,p,8,1
mov gray,eax

invoke cvCreateImage,p,8,1
mov smallimg,eax

invoke cvCvtColor,img,gray,6 ;CV_BGR2GRAY
invoke cvResize,gray,smallimg,1
invoke cvEqualizeHist,smallimg,smallimg
invoke cvClearMemStorage,storage
mov p.x,30
mov p.y,30
invoke cvHaarDetectObjects,smallimg,cascade,storage, sc,2,1 , p ; sc is a double with value 1.2
invoke cvShowImage,CADD("test"),img
.endif


ret
Facedetect endp


Im stuck at this code
Quote
invoke cvHaarDetectObjects,smallimg,cascade,storage, sc,2,1 , p
It make my application hang. Where is my mistake. I think I will made a new thread about this topic.
Those who had universe knowledges can control the world by a micro processor.
http://www.wix.com/farabio/firstpage

"Etos siperi elegi"

Farabi

I made it.
Put all the OpenCV dll in the same folder of your exe or put it on system32 folder. Copy paste this code and run it.


   include highgui.inc
   include cv.inc
   include cxcore.inc
   include cvcam.inc
   include cvhaartraining.inc
   include cxts.inc
   include cvaux.inc
   include ml.inc

includelib cv.lib
includelib highgui.lib
includelib cxcore.lib
    includelib cvcam.lib
    includelib cvhaartraining.lib
   includelib cxts.lib
   includelib cvaux.lib
   includelib ml.lib

scalar struct
v1 qword 0
v2 qword 0
v3 qword 0
v4 qword 0
scalar ends

.data
sc qword 1.2
sc_3 qword 3.0
sc_8 qword 8.0
sc_clr qword 225.0,225.0,225.0,225.0

.code
FaceDetect proc uses esi edi lpFileName: dword
LOCAL buff[256]:dword
LOCAL buff2[256]:dword
LOCAL RegH:dword
LOCAL tmp,smallimg,gray:dword
LOCAL p:POINT
LOCAL face:dword
LOCAL img,cascade,storage,rct
LOCAL r:RECT
LOCAL s:scalar

; Set Up everything
invoke GetAppPath,addr buff
invoke SetCurrentDirectory,addr buff

cmp cascade,0
jnz @f
invoke cvLoad,CADD("data\haarcascades\haarcascade_frontalface_alt2.xml"),0,0,0
.if eax==0
invoke MessageBox,hwnd,CADD("Cannot load cascade"),0,MB_OK
.endif
mov cascade,eax
invoke cvCreateMemStorage,0
mov storage,eax
@@:


; Img Structure
; +40 := width
; +44 := height
invoke cvLoadImage,lpFileName,1
.if eax!=0
mov img,eax

push [eax+40]
pop p.x
push [eax+44]
pop p.y
invoke cvCreateImage,p,8,1
mov gray,eax

invoke cvCreateImage,p,8,1
mov smallimg,eax

invoke cvCvtColor,img,gray,6 ;CV_BGR2GRAY
invoke cvResize,gray,smallimg,1
invoke cvEqualizeHist,smallimg,smallimg
invoke cvClearMemStorage,storage
mov p.x,20
mov p.y,20
; invoke GetTickCount
; push eax
invoke cvHaarDetectObjects,smallimg,cascade,storage, sc,2,0 , p
mov face,eax
; invoke GetTickCount
; xchg eax,edx
; pop ecx
; sub edx,ecx
; invoke dw2a,edx,addr buff
; invoke MessageBox,hwnd,addr buff,0,MB_OK
; mov esi,face
; invoke dw2a,[esi+68],addr buff
; invoke MessageBox,hWnd,addr buff,0,MB_OK

xor ecx,ecx
@@:
push ecx
invoke cvGetSeqElem,face,ecx
.if eax!=0
mov rct,eax
mov esi,eax
assume esi:ptr RECT

push [esi].left
pop p.x
push [esi].top
pop p.y

mov edx,[esi].right
mov eax,[esi].bottom
shr edx,1
shr eax,1
add p.x,edx
add p.y,eax
mov edx,[esi].right
add edx,[esi].bottom
shr edx,2

invoke cvCircle,img,p,edx,s,sc_3,sc_8,0

assume esi:nothing
invoke cvNamedWindow,CADD("test"),1
invoke cvShowImage,CADD("test"),img
.else
invoke MessageBox,hwnd,CADD("Cannot detect anything"),0,MB_OK
.endif
pop ecx
inc ecx
mov esi,face
cmp ecx,[esi+68] ; +68 a value of how much face detected
jl @b
.endif

invoke cvReleaseImage,addr gray
invoke cvReleaseImage,addr smallimg
invoke cvReleaseImage,addr img

ret
FaceDetect endp

Those who had universe knowledges can control the world by a micro processor.
http://www.wix.com/farabio/firstpage

"Etos siperi elegi"

Farabi

Those who had universe knowledges can control the world by a micro processor.
http://www.wix.com/farabio/firstpage

"Etos siperi elegi"

Vortex

Hi Onan,

Could you attach the executable?

Farabi

Quote from: Vortex on February 16, 2008, 09:27:15 AM
Hi Onan,

Could you attach the executable?

Sure. But Im not sure the avi will work on other machine. I also can send you the full source if I know your active email.

[attachment deleted by admin]
Those who had universe knowledges can control the world by a micro processor.
http://www.wix.com/farabio/firstpage

"Etos siperi elegi"

Vortex

Hi Onan,

Thanks for posting the executable, it works fine on my Xp Pro Sp2 system.