Jump to content
mylise

Opencv UDF

Recommended Posts

Can you show me simple example image compare with offset?

I have two image big and small(some part of big image) and want find coordinates small image after compare

Important moment compare must be with offset

If Opencv can make it please show me example.

PS now use BmpSearch.au3 for this purpose but without offset

 

Edited by nill

Share this post


Link to post
Share on other sites

It has been a long time since my last visit and I notice nill's request. This maybe too late but here is how you can search for an image inside another image. Opencv uses cvMatchTemplate and cvMinMaxLoc.

#include <GDIplus.au3>
#include <Memory.au3>
#include <GUIConstantsEx.au3>
#include <OpenCVFcns.au3>


;start dll opencv
_GDIPlus_Startup()
_OpenCV_Startup()


;// load IPL type image both images must have same number of bits and color channels!
$pimg = _cvLoadImage("whereWally3.jpg")
$ptemp = _cvLoadImage("wally3.jpg") ;image to look for

;// Create some windows to show the input
;// and output images in.
;//
_cvNamedWindow( "Where's Wally" )


;// show our input image
;//
_cvShowImage( "Where's Wally", $pimg)

Sleep(1000)

;// Determine images height and width to create results matrix
Local $width = DllStructGetData(_cvGetSize( $pimg ),"width")
Local $height = DllStructGetData(_cvGetSize( $pimg ),"height")
Local $width2 = DllStructGetData(_cvGetSize( $ptemp ),"width")
Local $height2 = DllStructGetData(_cvGetSize( $ptemp ),"height")
Local $rw =  $width - $width2 + 1
Local $rh =  $height - $height2 + 1

;// Create Opencv matrix object 32 bit floating
Local $presult=_cvCreateMat($rh,$rw,$CV_32FC1)


;// Template matching
_cvMatchTemplate( $pimg , $ptemp , $presult , 5 )
_cvNormalize($presult,$presult,0,1,$CV_MINMAX,Null)
_cvThreshold($presult,$presult,0.90,1,$CV_THRESH_BINARY)


;//locate matches
;
;// Create minmax variables to pass to opencv
Local $tmaxloc = DllStructCreate($tagCvPoint)
Local $tminloc = DllStructCreate($tagCvPoint)
Local $tmaxval = DllStructCreate("double max;")
Local $tminval = DllStructCreate("double min;")
Local $pmaxloc = DllStructGetPtr($tmaxloc)
Local $pminloc = DllStructGetPtr($tminloc)
Local $pmaxval = DllStructGetPtr($tmaxval)
Local $pminval = DllStructGetPtr($tminval)

;// create mask to find multiple matches
Local $pmask = _cvCreateImage(_cvSize($rw, $rh), 8, 1)
_cvSet($pmask,_cvScalar(1))

;// Find location of maximum
_cvMinMaxLoc( $presult, $pminval, $pmaxval, $pminloc, $pmaxloc, $pmask )

Do

;// Mask it to find others if exists and draw red rectangle on input image
_cvRectangle($pmask,_cvPoint(DllStructGetData($tmaxloc, "x")- 5,DllStructGetData($tmaxloc, "y")-5),_cvPoint(DllStructGetData($tmaxloc, "x")+$width2,DllStructGetData($tmaxloc, "y")+$height2),_cvScalar(0),-1,8,0)
_cvRectangle($pimg,_cvPoint(DllStructGetData($tmaxloc, "x")- 5,DllStructGetData($tmaxloc, "y")-5),_cvPoint(DllStructGetData($tmaxloc, "x")+$width2+10,DllStructGetData($tmaxloc, "y")+$height2+10),_CV_RGB(255, 0, 0),2,8,0)

;// Update input image
_cvShowImage( "Where's Wally", $pimg)

;// Used to show that only one match is located each time
sleep(3000)

;// Find location of maximum
_cvMinMaxLoc( $presult, $pminval, $pmaxval, $pminloc, $pmaxloc, $pmask )

Until (DllStructGetData($tmaxval, "max")<.99)


;// Wait for the user to hit a key, then clean up the windows
;//

_cvWaitKey( 0 )

;// Be tidy
;//
_cvReleaseImage( $pmask )
_cvReleaseMat( $presult )
_cvReleaseImage( $ptemp )
_cvReleaseImage( $pimg )
_cvDestroyAllWindows()

_Opencv_CloseDLL()
_GDIPlus_Shutdown()

Exit

 

whereWally3.jpg

wally3.jpg

Share this post


Link to post
Share on other sites

The example will detect the headlights of the car. Load the image and store in the same directory as the program.

The  _cvHoughCircles() function returns a pointer to memory sequence. Although the example will work with the image provided, lots of time is required to properly tune the function's constants to work properly. Use the _cvGetSeqElem() function to retrieve the information of the circle's location and radius.

Hope this helps and good luck

#include <Memory.au3>
#include <GUIConstantsEx.au3>
#include <OpenCVFcns.au3>

;start dll opencv
_OpenCV_Startup()

;// load IPL type image and convert to gray scale
$pimg = _cvLoadImage("fiat.jpg",$CV_LOAD_IMAGE_GRAYSCALE)

;// Create some windows to show the input
;// and output images in.
;//
_cvNamedWindow( "cvHoughCircles" )

;// Determine images height and width to create results matrix
Local $width = DllStructGetData(_cvGetSize( $pimg ),"width")

;// Create Opencv memory storage
Local $pstorage = _cvCreateMemStorage(0);

;//locate circles
Local $presults = _cvHoughCircles($pimg,$pstorage,$CV_HOUGH_GRADIENT,4,$width/4,400,120,40,60);

;// Determine amount of circles found
local $tresults = DllStructCreate($tagCvSeq,$presults)
Local $total = DllStructGetData($tresults,"total")

;//load color image
$pimg = _cvLoadImage("fiat.jpg")

;// Show image
_cvShowImage( "cvHoughCircles", $pimg)

;// Show with no circles
sleep(2000)

;// Draw circle matches
for $i = 0 to $total-1
$pp = _cvGetSeqElem( $presults, $i );
$vresults = DllStructCreate("float;float;float;",$pp)
;for $itt = 1 to 3
   Local $xc = DllStructGetData($vresults,1)
   Local $yc = DllStructGetData($vresults,2)
   Local $rc = DllStructGetData($vresults,3)

    ; Draw arcs

    _cvCircle($pimg, _cvPoint($xc,$yc), Round($rc),_CV_RGB(0, 0, 255),5,8,0)

sleep(1000)

;// Update input image
_cvShowImage( "cvHoughCircles", $pimg)

;// Used to show that only one match is located each time
sleep(2000)
Next

;// Wait for the user to hit a key, then clean up the windows
;//

_cvWaitKey( 0 )

;// Be tidy
;//
_cvReleaseMemStorage( $pstorage )
_cvReleaseImage( $pimg )
_cvDestroyAllWindows()

_Opencv_CloseDLL()

Exit

 

Fiat.jpg

Edited by mylise
forgot information

Share this post


Link to post
Share on other sites

Thank you mylise This is perfect

I use _cvCvtColor + _cvThreshold  output this image 

jgcnTrR.jpg

but i can't get hough cirlce from the image

Share this post


Link to post
Share on other sites

Hi mylise , I download the Opencv pack from the oficial site but it only contains:

opencv_ffmpeg310.dll

opencv_java310.dll

opencv_world310.dll

opencv_world310d.dll

Any idea where can I get the files requiered by _OpenCV_Startup?

Func _OpenCV_Startup($sVer = "248")

    $_opencv_core = DllOpen("opencv_core" & $sVer & ".dll")

    $_opencv_highgui = DllOpen("opencv_highgui" & $sVer & ".dll")

    $_opencv_imgproc = DllOpen("opencv_imgproc" & $sVer & ".dll")

    $_opencv_calib3d = DllOpen("opencv_calib3d" & $sVer & ".dll")

    $_opencv_features2d = DllOpen("opencv_features2d" & $sVer & ".dll")

EndFunc   ;==>_OpenCV_Startup

Thanks in advance and regards.

Share this post


Link to post
Share on other sites

Hi Alex1986, like I have mentioned, you need to change the constants in the function. Here is a link to a description of the function:
http://docs.opencv.org/2.4/doc/tutorials/imgproc/imgtrans/hough_circle/hough_circle.html

Try with these values:
Local $presults = _cvHoughCircles($pimg,$pstorage,$CV_HOUGH_GRADIENT,1,$width/4,300,20,10,30);

-------------------------------------

Hi jcpetu, I remember reading that in version 3 they would discontinue compiling C-API type libraries but I can not find it on the opencv web site :(

I know versions up to 2.4.11 have the binaries. Don't forget to change ($sVer = "248") to proper version you will download. Also you may require msvcr....dll and msvcp....dll files.

Edited by mylise
correction

Share this post


Link to post
Share on other sites

Opencv uses haar classifiers for detection. I don't know if they still release them, but you can find haar cascades to detect faces under

\opencv\data\haarcascades directory. The one I used in the example below is "haarcascade_frontalface_default.xml" and will work with your image.

The functions required are not in files provided so I included them in the example. These are _cvHaarDetectObjects and _cvload.

#include <GDIplus.au3>
#include <Memory.au3>
#include <GUIConstantsEx.au3>
#include <OpenCVFcns.au3>

$scale = 1.3

;start dll opencv
_GDIPlus_Startup()
_OpenCV_Startup()

Local $hPen = _GDIPlus_PenCreate(0xFFFF0000, 2)

;// start dll
$_opencv_objdetect = DllOpen("opencv_objdetect245.dll")

;//load haar data
$cascade = _cvLoad("haarcascade_frontalface_default.xml")

;// load IPL type image
Local $pimg = _cvLoadImage("lena.jpg");Opencv function to load image

;// Create Autoit windows to show the image
;//

Local $hGUI = GUICreate("GDI+", DllStructGetData(_cvGetSize( $pimg ),"width"), DllStructGetData(_cvGetSize( $pimg ),"height"))
GUISetState(@SW_SHOW)
Local $hGraphics = _GDIPlus_GraphicsCreateFromHWND($hGUI)

;// Convert result (IPL image) to Winapi Bitmap and convert to GDI bitmap
Local $hApiBmp = _OpenCv_IPL2BMP($pimg)
Local $hGdiBmp = _GDIPlus_BitmapCreateFromHBITMAP($hApiBmp)

;// Display image
_GDIPlus_GraphicsDrawImage($hGraphics,$hGdiBmp,0,0)

;// Create destination images
Local $pimgGrayScale = _cvCreateImage(_cvGetSize($pimg), 8, 1);
Local $pimgsmall = _cvCreateImage(_cvSize( Round(DllStructGetData(_cvGetSize( $pimg ),"width")/$scale,0), Round(DllStructGetData(_cvGetSize( $pimg ),"height")/$scale,0)), 8, 1);

;//converting the original image into grayscale, resize, and equalize

_cvCvtColor($pimg,$pimgGrayScale,$CV_BGR2GRAY);
_cvResize( $pimgGrayScale, $pimgsmall, $CV_INTER_LINEAR );
_cvEqualizeHist( $pimgsmall, $pimgsmall );

; //setup memory block for sequence storage
Local $pstorage = _cvCreateMemStorage(0); //storage area for all contours
_cvClearMemStorage( $pstorage )

;// Detect features using haar cascades store as CvSeq
Local $objects = _cvHaarDetectObjects($pimgsmall,$cascade,$pstorage, 1.1, 8, 0 ,_cvSize(30, 30))

;// Draw rectangles around detected features
for $itt = 1 to DllStructGetData(DllStructCreate($tagCvSeq,$objects),"total")
   Local $r = DllStructCreate($tagCvRect, _cvGetSeqElem($objects,$itt))
   _GDIPlus_GraphicsDrawRect ( $hGraphics, DllStructGetData($r,"x")*$scale, DllStructGetData($r,"y")*$scale, DllStructGetData($r,"width")*$scale, DllStructGetData($r,"height")*$scale, $hPen  )
Next

;// Wait for the user to hit a key, then clean up the windows
;//

Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE

;// Be tidy
;//
_GDIPlus_GraphicsDispose($hGraphics)
GUIDelete($hGUI)

_GDIPlus_PenDispose($hPen)
_cvReleaseImage( $pimgsmall );
_cvReleaseImage( $pimgGrayScale );
_cvReleaseImage( $pimg );
_cvDestroyAllWindows()
_cvClearMemStorage( $pstorage );
_cvClearSeq($objects)
_GDIPlus_BitmapDispose($hGdiBmp)
_WinAPI_DeleteObject($hApiBmp)
DllClose($_opencv_objdetect )
_Opencv_CloseDLL()

_GDIPlus_Shutdown()

Exit

;------------------------------------------------
;/*
;CVAPI(CvSeq*) cvHaarDetectObjects( const CvArr* image,
;                     CvHaarClassifierCascade* cascade, CvMemStorage* storage,
;                     double scale_factor CV_DEFAULT(1.1),
;                     int min_neighbors CV_DEFAULT(3), int flags CV_DEFAULT(0),
;                     CvSize min_size CV_DEFAULT(cvSize(0,0)), CvSize max_size CV_DEFAULT(cvSize(0,0)));
Func _cvHaarDetectObjects( $cvimage,$cvcascade, $cvstorage,$cvscale_factor = 1.1, $cvmin_neighbors =3, $cvflags =0, $cvmin_size =_cvSize(0,0), $cvmax_size = _cvSize(0,0))

local $_aResult = DllCall($_opencv_objdetect , "ptr:cdecl" , "cvHaarDetectObjects" , "ptr" , $cvimage, "ptr" , $cvcascade, "ptr" , $cvstorage ,"double" , $cvscale_factor, "int" , $cvmin_neighbors , "int" , $cvflags , "struct" , $cvmin_size , "struct" , $cvmax_size )
If @error Then ConsoleWrite( "error in cvHaarDetectObjects")

Return $_aResult[0]
EndFunc
;==>_cvHaarDetectObjects

;------------------------------------------------
;/*
;CVAPI(void*) cvLoad( const char* filename,
;                     CvMemStorage* memstorage CV_DEFAULT(NULL),
;                     const char* name CV_DEFAULT(NULL),
;                     const char** real_name CV_DEFAULT(NULL) );
;
;
Func _cvLoad( $cvfilename, $cvmemstorage = NULL, $cvname = NULL, $cvreal_name =NULL );

local $_aResult = DllCall($_opencv_core , "ptr:cdecl" , "cvLoad" , "str" , $cvfilename , "ptr" , $cvmemstorage , "str" , $cvname , "str*" ,$cvreal_name  )
If @error Then ConsoleWrite( "error in cvLoad")

Return $_aResult[0]
EndFunc ;==>_cvLoad

 

Share this post


Link to post
Share on other sites
11 hours ago, mylise said:

Opencv uses haar classifiers for detection. I don't know if they still release them, but you can find haar cascades to detect faces under

\opencv\data\haarcascades directory. The one I used in the example below is "haarcascade_frontalface_default.xml" and will work with your image.

The functions required are not in files provided so I included them in the example. These are _cvHaarDetectObjects and _cvload.

#include <GDIplus.au3>
#include <Memory.au3>
#include <GUIConstantsEx.au3>
#include <OpenCVFcns.au3>

$scale = 1.3

;start dll opencv
_GDIPlus_Startup()
_OpenCV_Startup()

Local $hPen = _GDIPlus_PenCreate(0xFFFF0000, 2)

;// start dll
$_opencv_objdetect = DllOpen("opencv_objdetect245.dll")

;//load haar data
$cascade = _cvLoad("haarcascade_frontalface_default.xml")

;// load IPL type image
Local $pimg = _cvLoadImage("lena.jpg");Opencv function to load image

;// Create Autoit windows to show the image
;//

Local $hGUI = GUICreate("GDI+", DllStructGetData(_cvGetSize( $pimg ),"width"), DllStructGetData(_cvGetSize( $pimg ),"height"))
GUISetState(@SW_SHOW)
Local $hGraphics = _GDIPlus_GraphicsCreateFromHWND($hGUI)

;// Convert result (IPL image) to Winapi Bitmap and convert to GDI bitmap
Local $hApiBmp = _OpenCv_IPL2BMP($pimg)
Local $hGdiBmp = _GDIPlus_BitmapCreateFromHBITMAP($hApiBmp)

;// Display image
_GDIPlus_GraphicsDrawImage($hGraphics,$hGdiBmp,0,0)

;// Create destination images
Local $pimgGrayScale = _cvCreateImage(_cvGetSize($pimg), 8, 1);
Local $pimgsmall = _cvCreateImage(_cvSize( Round(DllStructGetData(_cvGetSize( $pimg ),"width")/$scale,0), Round(DllStructGetData(_cvGetSize( $pimg ),"height")/$scale,0)), 8, 1);

;//converting the original image into grayscale, resize, and equalize

_cvCvtColor($pimg,$pimgGrayScale,$CV_BGR2GRAY);
_cvResize( $pimgGrayScale, $pimgsmall, $CV_INTER_LINEAR );
_cvEqualizeHist( $pimgsmall, $pimgsmall );

; //setup memory block for sequence storage
Local $pstorage = _cvCreateMemStorage(0); //storage area for all contours
_cvClearMemStorage( $pstorage )

;// Detect features using haar cascades store as CvSeq
Local $objects = _cvHaarDetectObjects($pimgsmall,$cascade,$pstorage, 1.1, 8, 0 ,_cvSize(30, 30))

;// Draw rectangles around detected features
for $itt = 1 to DllStructGetData(DllStructCreate($tagCvSeq,$objects),"total")
   Local $r = DllStructCreate($tagCvRect, _cvGetSeqElem($objects,$itt))
   _GDIPlus_GraphicsDrawRect ( $hGraphics, DllStructGetData($r,"x")*$scale, DllStructGetData($r,"y")*$scale, DllStructGetData($r,"width")*$scale, DllStructGetData($r,"height")*$scale, $hPen  )
Next

;// Wait for the user to hit a key, then clean up the windows
;//

Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE

;// Be tidy
;//
_GDIPlus_GraphicsDispose($hGraphics)
GUIDelete($hGUI)

_GDIPlus_PenDispose($hPen)
_cvReleaseImage( $pimgsmall );
_cvReleaseImage( $pimgGrayScale );
_cvReleaseImage( $pimg );
_cvDestroyAllWindows()
_cvClearMemStorage( $pstorage );
_cvClearSeq($objects)
_GDIPlus_BitmapDispose($hGdiBmp)
_WinAPI_DeleteObject($hApiBmp)
DllClose($_opencv_objdetect )
_Opencv_CloseDLL()

_GDIPlus_Shutdown()

Exit

;------------------------------------------------
;/*
;CVAPI(CvSeq*) cvHaarDetectObjects( const CvArr* image,
;                     CvHaarClassifierCascade* cascade, CvMemStorage* storage,
;                     double scale_factor CV_DEFAULT(1.1),
;                     int min_neighbors CV_DEFAULT(3), int flags CV_DEFAULT(0),
;                     CvSize min_size CV_DEFAULT(cvSize(0,0)), CvSize max_size CV_DEFAULT(cvSize(0,0)));
Func _cvHaarDetectObjects( $cvimage,$cvcascade, $cvstorage,$cvscale_factor = 1.1, $cvmin_neighbors =3, $cvflags =0, $cvmin_size =_cvSize(0,0), $cvmax_size = _cvSize(0,0))

local $_aResult = DllCall($_opencv_objdetect , "ptr:cdecl" , "cvHaarDetectObjects" , "ptr" , $cvimage, "ptr" , $cvcascade, "ptr" , $cvstorage ,"double" , $cvscale_factor, "int" , $cvmin_neighbors , "int" , $cvflags , "struct" , $cvmin_size , "struct" , $cvmax_size )
If @error Then ConsoleWrite( "error in cvHaarDetectObjects")

Return $_aResult[0]
EndFunc
;==>_cvHaarDetectObjects

;------------------------------------------------
;/*
;CVAPI(void*) cvLoad( const char* filename,
;                     CvMemStorage* memstorage CV_DEFAULT(NULL),
;                     const char* name CV_DEFAULT(NULL),
;                     const char** real_name CV_DEFAULT(NULL) );
;
;
Func _cvLoad( $cvfilename, $cvmemstorage = NULL, $cvname = NULL, $cvreal_name =NULL );

local $_aResult = DllCall($_opencv_core , "ptr:cdecl" , "cvLoad" , "str" , $cvfilename , "ptr" , $cvmemstorage , "str" , $cvname , "str*" ,$cvreal_name  )
If @error Then ConsoleWrite( "error in cvLoad")

Return $_aResult[0]
EndFunc ;==>_cvLoad

 

:lmao: WOW.

It works great.

Mylise,Thank you very much.

I will study your code carefully.

Thank you again!!!:)

Share this post


Link to post
Share on other sites
On 4/8/2016 at 4:47 AM, mylise said:

Opencv uses haar classifiers for detection. I don't know if they still release them, but you can find haar cascades to detect faces under

\opencv\data\haarcascades directory. The one I used in the example below is "haarcascade_frontalface_default.xml" and will work with your image.

The functions required are not in files provided so I included them in the example. These are _cvHaarDetectObjects and _cvload.

Thanks mylise so much, I have tried your code with OpenCV 2.4.9 and it works fine.

However, for OpenCV 2.4.13 (which is the latest one as of today), there are 2 different versions of the file 'haarcascade_frontalface_default.xml', located under:

  • opencv\sources\data\haarcascades\
  • opencv\sources\data\haarcascades_GPU\

The XML file of OpenCV 2.4.9 is actually the GPU version of the XML file of OpenCV 2.4.13.

For the non-GPU version, AutoIT crashed and gave this error message:

Quote

OpenCV Error: Unspecified error (The node does not represent a user object (unknown type?)) in cvRead, file C:\buildslave64\win64_amdocl\2_4_PackSlave-win32-vc11-shared\opencv\modules\core\src\persistence.cpp, line 5008

So are there by any chances you know why it failed like that and what are the different between these 2 versions?

Share this post


Link to post
Share on other sites
On 11/6/2015 at 1:00 AM, mylise said:

It has been a long time since my last visit and I notice nill's request. This maybe too late but here is how you can search for an image inside another image. Opencv uses cvMatchTemplate and cvMinMaxLoc.

#include <GDIplus.au3>
#include <Memory.au3>
#include <GUIConstantsEx.au3>
#include <OpenCVFcns.au3>


;start dll opencv
_GDIPlus_Startup()
_OpenCV_Startup()


;// load IPL type image both images must have same number of bits and color channels!
$pimg = _cvLoadImage("whereWally3.jpg")
$ptemp = _cvLoadImage("wally3.jpg") ;image to look for

;// Create some windows to show the input
;// and output images in.
;//
_cvNamedWindow( "Where's Wally" )


;// show our input image
;//
_cvShowImage( "Where's Wally", $pimg)

Sleep(1000)

;// Determine images height and width to create results matrix
Local $width = DllStructGetData(_cvGetSize( $pimg ),"width")
Local $height = DllStructGetData(_cvGetSize( $pimg ),"height")
Local $width2 = DllStructGetData(_cvGetSize( $ptemp ),"width")
Local $height2 = DllStructGetData(_cvGetSize( $ptemp ),"height")
Local $rw =  $width - $width2 + 1
Local $rh =  $height - $height2 + 1

;// Create Opencv matrix object 32 bit floating
Local $presult=_cvCreateMat($rh,$rw,$CV_32FC1)


;// Template matching
_cvMatchTemplate( $pimg , $ptemp , $presult , 5 )
_cvNormalize($presult,$presult,0,1,$CV_MINMAX,Null)
_cvThreshold($presult,$presult,0.90,1,$CV_THRESH_BINARY)


;//locate matches
;
;// Create minmax variables to pass to opencv
Local $tmaxloc = DllStructCreate($tagCvPoint)
Local $tminloc = DllStructCreate($tagCvPoint)
Local $tmaxval = DllStructCreate("double max;")
Local $tminval = DllStructCreate("double min;")
Local $pmaxloc = DllStructGetPtr($tmaxloc)
Local $pminloc = DllStructGetPtr($tminloc)
Local $pmaxval = DllStructGetPtr($tmaxval)
Local $pminval = DllStructGetPtr($tminval)

;// create mask to find multiple matches
Local $pmask = _cvCreateImage(_cvSize($rw, $rh), 8, 1)
_cvSet($pmask,_cvScalar(1))

;// Find location of maximum
_cvMinMaxLoc( $presult, $pminval, $pmaxval, $pminloc, $pmaxloc, $pmask )

Do

;// Mask it to find others if exists and draw red rectangle on input image
_cvRectangle($pmask,_cvPoint(DllStructGetData($tmaxloc, "x")- 5,DllStructGetData($tmaxloc, "y")-5),_cvPoint(DllStructGetData($tmaxloc, "x")+$width2,DllStructGetData($tmaxloc, "y")+$height2),_cvScalar(0),-1,8,0)
_cvRectangle($pimg,_cvPoint(DllStructGetData($tmaxloc, "x")- 5,DllStructGetData($tmaxloc, "y")-5),_cvPoint(DllStructGetData($tmaxloc, "x")+$width2+10,DllStructGetData($tmaxloc, "y")+$height2+10),_CV_RGB(255, 0, 0),2,8,0)

;// Update input image
_cvShowImage( "Where's Wally", $pimg)

;// Used to show that only one match is located each time
sleep(3000)

;// Find location of maximum
_cvMinMaxLoc( $presult, $pminval, $pmaxval, $pminloc, $pmaxloc, $pmask )

Until (DllStructGetData($tmaxval, "max")<.99)


;// Wait for the user to hit a key, then clean up the windows
;//

_cvWaitKey( 0 )

;// Be tidy
;//
_cvReleaseImage( $pmask )
_cvReleaseMat( $presult )
_cvReleaseImage( $ptemp )
_cvReleaseImage( $pimg )
_cvDestroyAllWindows()

_Opencv_CloseDLL()
_GDIPlus_Shutdown()

Exit

 

whereWally3.jpg

wally3.jpg

I have two questions please:

1. Is opencv faster than autoit imagesearch that uses ImageSearchDLL?

2. Can opencv find different sizes of the target image?
I don't know opencv, but if it matches a "template", I was thinking that it's similar to finding the same "pattern" (or not?).

Thanks anyway

Share this post


Link to post
Share on other sites
On ‎12‎/‎7‎/‎2016 at 8:22 PM, autog said:

I have two questions please:

1. Is opencv faster than autoit imagesearch that uses ImageSearchDLL?

2. Can opencv find different sizes of the target image?
I don't know opencv, but if it matches a "template", I was thinking that it's similar to finding the same "pattern" (or not?).

Thanks anyway

For question #1, I have not used ImageSearchDLL so I can not say if it is faster. :(

For question #2, template matching is used to match identical objects in size and orientation. There are tricks to use template matching to match objects of different sizes but what you might be looking for is Feature matching.

Share this post


Link to post
Share on other sites

Please help. Opencv dll files are moved to a folder "AutoIt3\Include". An error.

"C:\Program Files (x86)\AutoIt3\Include\CVcore_c.au3"(22,41) : warning: $_opencv_core: possibly used before declaration.

11.jpg

22.jpg

Share this post


Link to post
Share on other sites

Пожалуйста помоги. OpenCV DLL файлы перемещаются в папку "AutoIt3 \ Включить". Ошибка.

"C: \ Program Files (x86) \ AutoIt3 \ Include \ CVcore_c.au3" (22,41): предупреждение: $ _opencv_core: возможно, используется до объявления.

This code works. Opencv UDF not working. В чем проблема?

#include <GDIplus.au3>
#include <Memory.au3>
#include <GUIConstantsEx.au3>
Global $_opencvDll_1, $_opencvDll_2, $_opencvDll_3
;Different constant used by opencv for webcam settings
Global Const $CV_CAP_PROP_POS_MSEC     =0
Global Const $CV_CAP_PROP_POS_FRAMES     =1
Global Const $CV_CAP_PROP_POS_AVI_RATIO  =2
Global Const $CV_CAP_PROP_FRAME_WIDTH   =3
Global Const $CV_CAP_PROP_FRAME_HEIGHT   =4
Global Const $CV_CAP_PROP_FPS           =5
Global Const $CV_CAP_PROP_FOURCC         =6
Global Const $CV_CAP_PROP_FRAME_COUNT   =7
Global Const $CV_CAP_PROP_FORMAT         =8
Global Const $CV_CAP_PROP_MODE         =9
Global Const $CV_CAP_PROP_BRIGHTNESS    =10
Global Const $CV_CAP_PROP_CONTRAST    =11
Global Const $CV_CAP_PROP_SATURATION    =12
Global Const $CV_CAP_PROP_HUE          =13
Global Const $CV_CAP_PROP_GAIN        =14
Global Const $CV_CAP_PROP_EXPOSURE    =15
Global Const $CV_CAP_PROP_CONVERT_RGB   =16
Global Const $CV_CAP_PROP_WHITE_BALANCE_BLUE_U =17
Global Const $CV_CAP_PROP_RECTIFICATION =18
Global Const $CV_CAP_PROP_MONOCROME  =19
Global Const $CV_CAP_PROP_SHARPNESS  =20
Global Const $CV_CAP_PROP_AUTO_EXPOSURE =21
Global Const $CV_CAP_PROP_GAMMA      =22
Global Const $CV_CAP_PROP_TEMPERATURE   =23
Global Const $CV_CAP_PROP_TRIGGER      =24
Global Const $CV_CAP_PROP_TRIGGER_DELAY =25
Global Const $CV_CAP_PROP_WHITE_BALANCE_RED_V =26
Global Const $CV_CAP_PROP_ZOOM        =27
Global Const $CV_CAP_PROP_FOCUS      =28
Global Const $CV_CAP_PROP_GUID        =29
Global Const $CV_CAP_PROP_ISO_SPEED  =30
Global Const $CV_CAP_PROP_MAX_DC1394    =31
Global Const $CV_CAP_PROP_BACKLIGHT  =32
Global Const $CV_CAP_PROP_PAN          =33
Global Const $CV_CAP_PROP_TILT        =34
Global Const $CV_CAP_PROP_ROLL        =35
Global Const $CV_CAP_PROP_IRIS        =36
Global Const $CV_CAP_PROP_SETTINGS    =37
Dim $CV_CAP[39] = ['CV_CAP_PROP_POS_MSEC','CV_CAP_PROP_POS_FRAMES','CV_CAP_PROP_POS_AVI_RATIO', _
'CV_CAP_PROP_FRAME_WIDTH','CV_CAP_PROP_FRAME_HEIGHT','CV_CAP_PROP_FPS', _
'CV_CAP_PROP_FOURCC','CV_CAP_PROP_FRAME_COUNT','CV_CAP_PROP_FORMAT', _
'CV_CAP_PROP_MODE','CV_CAP_PROP_BRIGHTNESS','CV_CAP_PROP_CONTRAST', _
'CV_CAP_PROP_SATURATION','CV_CAP_PROP_HUE','CV_CAP_PROP_GAIN', _
'CV_CAP_PROP_EXPOSURE','CV_CAP_PROP_CONVERT_RGB','CV_CAP_PROP_WHITE_BALANCE_BLUE_U', _
'CV_CAP_PROP_RECTIFICATION','CV_CAP_PROP_MONOCROME','CV_CAP_PROP_SHARPNESS', _
'CV_CAP_PROP_AUTO_EXPOSURE','CV_CAP_PROP_GAMMA','CV_CAP_PROP_TEMPERATURE', _
'CV_CAP_PROP_TRIGGER','CV_CAP_PROP_TRIGGER_DELAY','CV_CAP_PROP_WHITE_BALANCE_RED_V', _
'CV_CAP_PROP_ZOOM','CV_CAP_PROP_FOCUS','CV_CAP_PROP_GUID','CV_CAP_PROP_ISO_SPEED', _
'CV_CAP_PROP_MAX_DC1394','CV_CAP_PROP_BACKLIGHT','CV_CAP_PROP_PAN','CV_CAP_PROP_TILT', _
'CV_CAP_PROP_ROLL','CV_CAP_PROP_IRIS','CV_CAP_PROP_SETTINGS']

Global $tagIplImage = _
"int  nSize;" & _         ;  /* sizeof(IplImage) */
"int  ID;" & _           ;  /* version (=0)*/
"int  nChannels;" & _     ;  /* Most of OpenCV functions support 1,2,3 or 4 channels */
"int  alphaChannel;" & _   ;  /* Ignored by OpenCV */
"int  depth;" & _         ;  /* Pixel depth in bits: IPL_DEPTH_8U, IPL_DEPTH_8S, IPL_DEPTH_16S, IPL_DEPTH_32S, IPL_DEPTH_32F and IPL_DEPTH_64F are supported.  */
"byte colorModel[4];" & _  ;  /* Ignored by OpenCV */
"byte channelSeq[4];" & _  ;  /* ditto */
"int  dataOrder;" & _     ;  /* 0 - interleaved color channels, 1 - separate color channels.cvCreateImage can only create interleaved images */
"int  origin;" & _       ;  /* 0 - top-left origin,1 - bottom-left origin (Windows bitmaps style).  */
"int  align;" & _         ;  /* Alignment of image rows (4 or 8). OpenCV ignores it and uses widthStep instead. */
"int  width;" & _         ;  /* Image width in pixels.                         */
"int  height;" & _       ;  /* Image height in pixels.                        */
"ptr IplROI;" & _         ;  /* Image ROI. If NULL, the whole image is selected. */
"ptr maskROI;" & _       ;  /* Must be NULL. */
"ptr  imageId;" & _     ;
"ptr tileInfo;" & _     ;
"int  imageSize;" & _     ;  /* Image data size in bytes (==image->height*image->widthStep in case of interleaved data)*/
"ptr imageData;" & _       ;  /* Pointer to aligned image data.      */
"int  widthStep;" & _     ;  /* Size of aligned image row in bytes. */
"int  BorderMode[4];" & _  ;  /* Ignored by OpenCV.                  */
"int  BorderConst[4];" & _ ;  /* Ditto.                              */
"ptr imageDataOrigin;"   ;  /* Pointer to very origin of image data (not necessarily aligned) - needed for correct deallocation */
Dim $nIplImage[23] = ["nSize=", "ID=", "nChannels=" , "alphaChannel=", "depth=","colorModel[4]=","channelSeq[4]=","dataOrder=" , _
"origin=", "align=", "width=", "height=", "IplROI=", "maskROI=","imageId=", "tileInfo=","imageSize=","imageData=","widthStep=","BorderMode[4]=", _
"BorderConst[4]=", "imageDataOrigin="]
Local $tagIplROI = _
"int  coi;" & _          ; /* 0 - no COI (all channels are selected), 1 - 0th channel is selected ...*/
"int  xOffset;" & _
"int  yOffset;" & _
"int  width;" & _
"int  height;"
;Different constant used by opencv for selecting webcam ie. dshow web cam index would be 700, 701, 702, etc...
Global Const $CV_CAP_ANY      =0;    // autodetect
Global Const $CV_CAP_MIL      =100;   // MIL proprietary drivers
Global Const $CV_CAP_VFW      =200;   // platform native
Global Const $CV_CAP_V4L      =200;
Global Const $CV_CAP_V4L2    =200;
Global Const $CV_CAP_FIREWARE =300;   // IEEE 1394 drivers
Global Const $CV_CAP_FIREWIRE =300;
Global Const $CV_CAP_IEEE1394 =300;
Global Const $CV_CAP_DC1394   =300;
Global Const $CV_CAP_CMU1394  =300;
Global Const $CV_CAP_STEREO   =400;   // TYZX proprietary drivers
Global Const $CV_CAP_TYZX    =400;
Global Const $CV_TYZX_LEFT  =400;
Global Const $CV_TYZX_RIGHT   =401;
Global Const $CV_TYZX_COLOR   =402;
Global Const $CV_TYZX_Z    =403;
Global Const $CV_CAP_QT    =500;   // QuickTime
Global Const $CV_CAP_UNICAP   =600;   // Unicap drivers
Global Const $CV_CAP_DSHOW  =700;   // DirectShow (via videoInput)
Global Const $CV_CAP_MSMF    =1400;  // Microsoft Media Foundation (via videoInput)
Global Const $CV_CAP_PVAPI  =800;   // PvAPI, Prosilica GigE SDK
Global Const $CV_CAP_OPENNI   =900;   // OpenNI (for Kinect)
Global Const $CV_CAP_OPENNI_ASUS =910;   // OpenNI (for Asus Xtion)
Global Const $CV_CAP_ANDROID  =1000;  // Android
Global Const $CV_CAP_XIAPI  =1100;   // XIMEA Camera API
Global Const $CV_CAP_AVFOUNDATION = 1200;  // AVFoundation framework for iOS (OS X Lion will have the same API)
Global Const $CV_CAP_GIGANETIX = 1300;  // Smartek Giganetix GigEVisionSDK

Local $device = $CV_CAP_ANY
Local $pIplImage
Local $hBMP
Local $x = 640
Local $y = 480
;------------------------------------------------
; Main
_OpenCV_Startup()
Local $pCvCapture = _OpenCV_WebCam($device)
_Opencv_SetWebCam($pCvCapture,$x,$y)
Local $hwnd = GUICreate("Opencv WebCam", $x, $y)
GUISetState(@SW_SHOW,$hwnd)

_GDIPlus_Startup()

While 1
Local $msg = GUIGetMsg()
$pIplImage = _Opencv_GetWebCamFrame($pCvCapture)
$hBMP = _Opencv_IPL2BMP($pIplImage)
Local $hImage = _GDIPlus_BitmapCreateFromHBITMAP($hBMP)
Global $hGraphics = _GDIPlus_GraphicsCreateFromHWND($hwnd)
_GDIPlus_GraphicsDrawImageRect($hGraphics, $hImage, 0, 0, $x, $y)
_GDIPlus_BitmapDispose($hImage)
_WinAPI_DeleteObject($hBMP)
If $msg = $GUI_EVENT_CLOSE Then ExitLoop
WEnd
DllCall($_opencvDll_1, "none:cdecl", "cvReleaseImage", "ptr*", $pIplImage)
If @error Then ConsoleWrite("error2")
_Opencv_CloseWebCam($pCvCapture)
Exit
;------------------------------------------------
; _OpenCV_WebCam($index = 0)
;
; $index is a value indicating the type of webcam and which webcam to select
; Returns a pointer to CVCapture
;
Func _OpenCV_WebCam($index = 0)
Local $_aResult = DllCall($_opencvDll_2, "int:cdecl", "cvCreateCameraCapture", "int", $index)
If @error Then MsgBox(0,"","Can not find webcam")
Return $_aResult[0]
EndFunc ;==>_OpenCV_WebCam
;------------------------------------------------
; _Opencv_SetWebCam($pCvCapture, $x=640, $y=480 )
;
; $pCvCapture is a a pointer to CVCapture linked to the webcam and obtained from _OpenCV_WebCam() function
; $x,$y are used to set the width and height of the webcam. If resolutionis not supported, it will select the next lowest one!
;
Func _Opencv_SetWebCam($pCvCapture, $x=640, $y=480 )
;Un-comment these lines of code if you want to see available setting for webcam
;For $i = 0 to 37
;   Local $_aResult = DllCall($_opencvDll_2, "double:cdecl", "cvGetCaptureProperty", "ptr", $pCvCapture, "int", $i)
; If @error Then ConsoleWrite("error1")
; ConsoleWrite($CV_CAP[$i] & "=" & $_aResult[0] & @CRLF)
;Next

Local $_aResult = DllCall($_opencvDll_2, "int:cdecl", "cvSetCaptureProperty", "ptr", $pCvCapture, "int", $CV_CAP_PROP_FRAME_WIDTH, "double", $x)
If @error Then ConsoleWrite("error2")

Local $_aResult = DllCall($_opencvDll_2, "int:cdecl", "cvSetCaptureProperty", "ptr", $pCvCapture, "int", $CV_CAP_PROP_FRAME_HEIGHT, "double", $y)
If @error Then ConsoleWrite("error3")

EndFunc ;==>_Opencv_SetWebCam

;------------------------------------------------
; _Opencv_GetWebCamFrame($pCvCapture)
;
; $pCvCapture is a a pointer to CVCapture linked to the webcam and obtained from _OpenCV_WebCam() function
;
; Returns a pointer to the captured frame in an IplImage format
;
Func _Opencv_GetWebCamFrame($pCvCapture)
$_aResult = DllCall($_opencvDll_2, "int:cdecl", "cvQueryFrame", "ptr", $pCvCapture)
If @error Then ConsoleWrite("error4")

Return $_aResult[0]
EndFunc ;==>_Opencv_GetWebCamFrame

;------------------------------------------------
; _Opencv_IPL2BMP($pIplImage)
;
; $pIplImage is a a pointer to IplImage of type 8UC3 (24bits) created by opencv
;
; Returns a pointer to GDI compatible bitmap image
; Author: Mylise
;
Func _Opencv_IPL2BMP($pIplImage)
Local $tIplImage = DllStructCreate($tagIplImage, $pIplImage)
;Un-comment these lines of code if you want to see IplImage header information
;for $i = 1 to 22
; ConsoleWrite( $nIplImage[$i-1] & DllStructGetData($tIplImage,$i) & @CRLF)
;Next
$_aResult = DllCall($_opencvDll_1, "int:cdecl", "cvCreateImage", "int", DllStructGetData($tIplImage,'width'), "int", DllStructGetData($tIplImage,'height'), "int", 8, "int", 4 )
If @error Then ConsoleWrite("error5")

Local $pIplImagedst = $_aResult[0]
Local $tIplImagedst = DllStructCreate($tagIplImage, $pIplImagedst)

DllCall($_opencvDll_3, "none:cdecl", "cvCvtColor", "ptr", $pIplImage, "ptr", $pIplImagedst, "int", 0)
If @error Then ConsoleWrite("error6" & @error & @CRLF)

Local $dataptr= DllStructGetData($tIplImagedst,'imageData')
Local $hBMP = _WinAPI_CreateBitmap(DllStructGetData($tIplImagedst,'width'), DllStructGetData($tIplImagedst,'height'), 1, 32 , $dataptr)
;memory cleanup
$tIplImage = 0
$tIplImagedst = 0
$dataptr = 0
DllCall($_opencvDll_1, "none:cdecl", "cvReleaseImage", "ptr*", $pIplImagedst)
If @error Then ConsoleWrite("error7")
Return $hBMP
EndFunc ;==>_Opencv_GetWebCamFrame
;------------------------------------------------
; _Opencv_CloseWebCam($pCvCapture)
;
; $pCvCapture is a a pointer to CVCapture linked to the webcam and obtained from _OpenCV_WebCam() function
;
Func _Opencv_CloseWebCam($pCvCapture)
DllCall($_opencvDll_2, "none:cdecl", "cvReleaseCapture", "ptr*", $pCvCapture)
If @error Then ConsoleWrite("error8")
DllClose($_opencvDll_1)
DllClose($_opencvDll_2)

DllClose($_opencvDll_3)

EndFunc ;==>_Opencv_CloseWebCam

;------------------------------------------------
; _OpenCV_Startup()
;
;
;
Func _OpenCV_Startup()

$_opencvDll_1 = DllOpen("opencv_core249.dll")

$_opencvDll_2 = DllOpen("opencv_highgui249.dll")

$_opencvDll_3 = DllOpen("opencv_imgproc249.dll")

EndFunc   ;==>_OpenCV_Startup

 

Share this post


Link to post
Share on other sites

Problem was solved by adding a file 'CVcore_c.au3

Global $_opencv_core, $_opencv_highgui, $_opencv_imgproc, $_opencv_calib3d, $_opencv_features2d

In OpenCVFcns.au3

Global $_opencv_core, $_opencv_highgui, $_opencv_imgproc, $_opencv_calib3d, $_opencv_features2d, $hwnd

 

Share this post


Link to post
Share on other sites

Hi mylise! I am trying to modify the face recognition example. But I need to use my webcam, not a jpg picture. And I want to show a rectangle around my face, and display x and y values (becouse I will send these values to another program). Can mylise or somebody help with this?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...