Jump to content

OpenCV v4 UDF


smbape
 Share

Recommended Posts

Hi @memerim

On 1/27/2022 at 3:47 AM, memerim said:

Do you know how to convert a opencv image to a gdi hbitmap

Local $img = $cv.imread("path image")
Local $hBitmap = $img.convertToBitmap()

 

On 1/27/2022 at 3:47 AM, memerim said:

would takes lesss size on RAM or at the end would be the same thing as using by default a bitmap>hbitmap using just GDI?

Probably more, depending on the color conversion and it is the same thing as converting bitmap to hbitmap.

 

On 1/27/2022 at 3:47 AM, memerim said:

I'm storing some hbitmaps in memory but they are using too much RAM, i wonder ifs possible somehow do any kind of compression on them converting opencv > hbitmap?

You did not give what you call too much RAM.
I assume your image is big ( > 100MB), if not, then you probably have a memory leak somewhere.
When using GDI+ with AutoIt, beware of memory leaks.

There are multiple ways to reduce memory usage, at the price of less accurate results. Some ways are:

  • Resize the image
  • Read the image in grascale
  • Threshold the image (may be canny)
  • Blur the image

You have to test those ways to see what combination gives the best results.

Edited by smbape
Link to comment
Share on other sites

  • 2 weeks later...

Hello smbape. I'd like to thank you for putting quite a bit of effort to bringing OpenCV to AutoIt. After some time I was able to get all of your examples to work as intended. In fact, I've carefully read every currently available OpenCV AutoIt thread but it feels as if I hit a brick wall. I'm using _OpenCV_FindTemplate for my driveway security camera feed. It functions well but various changes in weather and time of day really mess with it.

Let's say I'm trying to consistently detect my blue Ford on the driveway whether it's further away from the house or much closer to the camera. How do we apply scale and color to objects?

From my extensive search combined with trial and error I'm quite certain I need to use HSV. After studying the cv_enums.au3 I found $CV_COLOR_RGB2HSV. Afterwards, I've carefully looked into opencv_udf_utils.au3, specifically for the format inside the parameters it accepts. I've narrowed it down to the following four parameters:

$threshold
$aChannels
$aHistSize
$aRanges

Threshold is straightforward and I fully grasp the concept, however, the other three are confusing to me. I understand photo editing, RGB channels, alpha channels with transparency but using this in OpenCV is above my pay grade. I've tried using the following with every parameter I could think of but not to much difference in results:

; [0] for gray-scale images as there's only one channel and [0], [1] or [2] for color images
; if we want to consider the channel green, blue or red respectively
Local $aChannels[3] = [0, 1, 2]

; Quantize each color to 32 levels
; The higher the value, the closer to the template the matched color is
Local $aHistSize[3] = [32, 32, 32]

; the range of the possible pixel values which is [0, 256] in case of RGB color space (where 256 is not inclusive)
Local $aRanges[6] = [0, 256, 0, 256, 0, 256]

_OpenCV_FindTemplate($img, $tmpl, $threshold, $CV_TM_SQDIFF_NORMED, Default, 8, $CV_COLOR_BGRA2BGR, 1, $aChannels, $aHistSize, $aRanges)

I was hoping for some guidance from you given your experience with the topic. Thank you!

Link to comment
Share on other sites

19 minutes ago, OhItsThatGuy said:

various changes in weather and time of day really mess with it.

I dont know how to do it with OpenCV, but usually you would constantly update the reference image.

Like you have your refence image and a new image comes in that needs to be compared with the reference image, if there are no big changes, then the new image shall become the new reference image, automatically adapting to weather and time of day changes.

Some guy's script + some other guy's script = my script!

Link to comment
Share on other sites

Hi @OhItsThatGuy

On 2/9/2022 at 11:21 PM, OhItsThatGuy said:

How do we apply scale and color to objects?

There are 2 ways to resize an image:

  • Use opencv resize Ex: $img_resized = $cv.resize($img, _OpenCV_Size($iWidth, $iHeight), $flags = $CV_INTER_LINEAR, $borderMode = $CV_BORDER_CONSTANT)
  • Use GDI+ resize, Ex: $img.GdiplusResize($iWidth, $iHeight, $interpolation = 7). Better quality but slower.

Concerning colors, to convert an image between color spaces, you can use cvtColor

$cv.cvtColor($img, $CV_COLOR_RGB2HSV)

Try with HSV or Lab, you may have better results.
_OpenCV_FindTemplate has a color conversion parameter, $iCode, no need to do the conversion on your own

On 2/9/2022 at 11:21 PM, OhItsThatGuy said:

Threshold is straightforward and I fully grasp the concept, however, the other three are confusing to me. I understand photo editing, RGB channels, alpha channels with transparency but using this in OpenCV is above my pay grade. I've tried using the following with every parameter I could think of but not to much difference in results:

Those parameters are only used when the match method is not normed, meaning not one of $CV_TM_SQDIFF_NORMED Or $CV_TM_CCORR_NORMED Or $CV_TM_CCOEFF_NORMED
In those cases, an histogram comparison is done using those parameters.

More over, taking into account what @Werty said, try to have multiple templates for different weather and time.
A simple alorigthm will be, if not raining and time between 10AM and 2PM, use "template 1", if time between 6PM AND 23PM use "template 2"

I hope it will help.

Edited by smbape
Link to comment
Share on other sites

Hi @malcev

17 hours ago, malcev said:

Dont You plan to bind dlib or mediapipe to autoit in future?

I tried in the past, however, on windows, there was no dll for dlib.
I also read how it was bind to python, however, the python binding is manually written not generated.
Which means I will have to read all the source code of dlib python binding to do something similar in AutoIt.
Unfortunatly, I am not motivated enough to do it.

 

17 hours ago, malcev said:

tps://google.github.io/mediapipe/

Mediapipe + OpenCv will give us interesting opportunities with advanced copmuter vision.

I have not looked at the source code of mediapipe.
If the python binding is also manually written, it will be a problem similar to dlib.

In conclusion, the idea is still in my mind, but I am not motivated enough to spend time on it.

Edited by smbape
Link to comment
Share on other sites

  • 3 weeks later...

Hello, I wrote an issue/question in you're github repo. You maybe still haven't a chance to check. Just to make sure You did not miss it.

 

Saludos

Link to comment
Share on other sites

Hello @Danyfirex , please post questions on the forum. By doing so, you will increase your chances of having an answer.

@malcev is right, the first argument needs to be an array.

There is an issue in the generated documentation. An up to date documenation is available here.
If you have any other issue, please first check the official documentation of the functions, it may give you the answer. For example hconcat

Local $m1 = _OpenCV_ObjCreate("cv.Mat").zeros(_OpenCV_Size(50, 50), $CV_8UC4)
$m1 = $cv.add($m1, _OpenCV_Scalar(0, 255, 0))
Local $m2 = _OpenCV_ObjCreate("cv.Mat").zeros(_OpenCV_Size(50, 50), $CV_8UC4)
$m2 = $cv.add($m2, _OpenCV_Scalar(255, 0, 0))
Local $contact=$cv.hconcat(_OpenCV_Tuple($m1,$m2))

 

Edited by smbape
Link to comment
Share on other sites

@malcev I like to use github haha.

@smbape thank you It works correctly.

 

Saludos

Edited by Danyfirex
Link to comment
Share on other sites

  • 1 month later...

Hello Guys,

I don't know if it's appropriate to ask it here : but I can't figure out how to install this UDF :(

I downloaded autoit-opencv-4.5.5-com-v2.0.1 and extract it, and installed the opencv-4.5.5-vc14_vc15.exe.

I tried to run multiple examples that I found on the forum or the GitHub, but I always have this error

!>Error: unable to load 
!>Error: DllInstall: unable to use the DLL file
!>Error: DllInstall -1

I checked the path of the includes, and they are ok, so I'm a bit lost 😅

Can someone explain me how to install it properly ?

Thank you :) 

 

Link to comment
Share on other sites

17 hours ago, smbape said:

Hi @iZo

Can you follow these running examples instructions  and give a feedback?

Hi smbape,

So here is what I did :

- I downloaded 7 zip and add it to environment variable ("7z" command is available in cmd)

- I downloaded Git Bash, and executed the code from "Running examples" (after changing directory with a "CD "MyPath"")

To me everything has been running fine (no error), but how can I check if it worked ? (all files seems to have been extracted correctly)

I tried to run au.3 scripts (don't know if it's a stupid idea or not 😅)  "samples\tutorial_code\core\AddingImages\AddingImages.au3" as example. But still have the same error "unable to use the DLL file" 🤔

Link to comment
Share on other sites

Hi @iZo

Just be sure, is your directory structure:
My Path
   ---> autoit-addon
   ---> autoit-opencv-com
   ---> opencv-4.5.5-vc14_vc15
   ---> samples

Can you open the file in SciTe editor an run it from there?
Instead of "Run Script", can you use "Run Script (x64)" ?

Link to comment
Share on other sites

17 minutes ago, smbape said:

Hi @iZo

Just be sure, is your directory structure:
My Path
   ---> autoit-addon
   ---> autoit-opencv-com
   ---> opencv-4.5.5-vc14_vc15
   ---> samples

Can you open the file in SciTe editor an run it from there?
Instead of "Run Script", can you use "Run Script (x64)" ?

Yes, it's working! I'm sure it's a noob question, but do you know how I can change default settings to always run as x64?

edit : after a bit of research I found I needed to download a second .exe for Scite, in order to make the line "#AutoIt3Wrapper_UseX64=y" working :)

Thanks a lot for your help 😁

Edited by iZo
Link to comment
Share on other sites

3 hours ago, iZo said:

how I can change default settings to always run as x64?

To make the context menu "Run Script" to run as x64, edit the registry key HKEY_CLASSES_ROOT\AutoIt3Script\Shell\Run\Command and change AutoIt3.exe to AutoIt3_x64.exe

Link to comment
Share on other sites

23 hours ago, smbape said:

To make the context menu "Run Script" to run as x64, edit the registry key HKEY_CLASSES_ROOT\AutoIt3Script\Shell\Run\Command and change AutoIt3.exe to AutoIt3_x64.exe

I just did it! Thanks you for the tips and for the help, I appreciate 😇👍

Link to comment
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
 Share

×
×
  • Create New...