Jump to content

OpenCV v4 UDF


smbape
 Share

Recommended Posts

1 hour ago, Lion66 said:

I'd rather Exit the loop. as "Return" or "Exit loop".

Now that you write it, it reminds me that in other synchronous programs, threre is try/catch, but not in AutoIt.
I was too immersed in AutoIt and forgot about it. Sorry.

Edited by smbape
Link to comment
Share on other sites

Hi @smbape

I have completed my study of the contours. I attach the resulting script.
About 90% probably - these are your answers! 😄
I managed to make this topic myself: Erosion, etc. I guess I learned a little bit. 😃
You can add it to your examples if you consider it necessary.
I'll be gone for a week, and then I'll be back with some new questions: I have some ideas in my head. 😉

Thanks.

Find-Contour-Draw-Demo.au3

Link to comment
Share on other sites

Hi @Lion66

On 9/4/2021 at 7:06 PM, Lion66 said:

I managed to make this topic myself: Erosion, etc. I guess I learned a little bit.

I am pleased to read that.

 

On 9/4/2021 at 7:06 PM, Lion66 said:

You can add it to your examples if you consider it necessary.

You did a great. I will add it after reveiwing it. I will probably split it in multiple files.

 

On 9/4/2021 at 7:06 PM, Lion66 said:

Thanks.

You are welcome.

Edited by smbape
Link to comment
Share on other sites

Hi @smbape

On 9/5/2021 at 6:11 PM, smbape said:

I will probably split it in multiple files.

On the contrary, I like to combine the same type of examples. Here I see eroding and others as an extension from threshold.

I'll try to add what I found recently bilateralFilter.

I am now trying to study the purpose of the mask.
I look at this example as well as your examples: MultiMatchTemplate_Demo.au3, WhereIsWally.au3
It works with the ready mask file, but I'm missing some things:

1.At the output of script with chessboard, the mask turns out to be a black square, although in that article they show a black square with a white pawn, as a mask. Or I confuse alpha channel and mask?

2.How does a black square as a mask help us find template? How does it work? I thought there should be a white figure to limit the search area.

3.How to build a mask is a black square. As in the python: mask = np.zeros(img.shape, dtype = "uint8") ?

4.Is there a way to show the number of channels. In the python: print(img.shape) - third parameter of output.

5.How do I keep the alpha channel as a mask? I found the _cveSplitTyped function, but I can't apply it.

Local $H = _cveMatCreate()
Local $o_arr_H = _cveOutputArrayFromMat($H)
Local $img2 = _cveSplitTyped("Mat", $img, "Array", $o_arr_H)
_cveImshowMat("", $img2[0])

I'm back. 🙂 Many questions. If you have time to answer.

Thank you.

Edited by Lion66
Link to comment
Share on other sites

Hi @Lion66

On 9/13/2021 at 1:05 PM, Lion66 said:

On the contrary, I like to combine the same type of examples. Here I see eroding and others as an extension from threshold.

I am not convinced. Your file has 2 sets of functions.

  • Contour features
  • Mophological operations

Don't you think it is more natural to have them in 2 files?
Your sources come into 2 links.
Having one file is like combining 2 links into one. For learning purpose, it will be difficult to spot what you need in bigger files.

On 9/13/2021 at 1:05 PM, Lion66 said:

1.At the output of script with chessboard, the mask turns out to be a black square, although in that article they show a black square with a white pawn, as a mask. Or I confuse alpha channel and mask?

I am not sure to understand the question.
A mask should be a grayscaled image.
Black pixel in the mask, means ignore the pixel, otherwhise. the pixel is not ignored.
A pixel that is transparent (opacity == 0), will appear as the background.
If the background is white, you will think that the pixel is white, even if it is not.
The pixel is simply not displayed.
That is why the mask provided does not work.

 

On 9/13/2021 at 1:05 PM, Lion66 said:

2.How does a black square as a mask help us find template? How does it work? I thought there should be a white figure to limit the search area.

When an image can be in multiple backgrounds, a mask can help you ignore the background.
Black pixel means ignore the pixel. A mask where the background is black, but the shape of the template is white, will help you find the template regardless of its background.

 

On 9/13/2021 at 1:05 PM, Lion66 said:

3.How to build a mask is a black square. As in the python: mask = np.zeros(img.shape, dtype = "uint8") ?

Yes

 

On 9/13/2021 at 1:05 PM, Lion66 said:

4.Is there a way to show the number of channels. In the python: print(img.shape) - third parameter of output.

_cveMatNumberOfChannels($mat)

 

On 9/13/2021 at 1:05 PM, Lion66 said:

5.How do I keep the alpha channel as a mask? I found the _cveSplitTyped function, but I can't apply it.

For an example of usage of _cveSplit, look at discrete_fourier_transform.au3#L100
Based of the example it should be (not tested)

Local $planes[3] = [_cveMatCreate(), _cveMatCreate(), _cveMatCreate()]
_cveSplitMat($img, $planes);

The alpha channel is the third channel.
Remember, values go from 0 to 255.
0 means transparent.
255 means opaque.

On 9/13/2021 at 1:05 PM, Lion66 said:

I'm back. 🙂 Many questions. If you have time to answer.

No problem. I will be busy for the next 4 weeks but I will try to answer your questions as fast as I can.

Edited by smbape
Link to comment
Share on other sites

Hi @smbape.  How are you?

Thank you for your answers. I'll think about their meaning.
But I didn't understand the answer to the question: how to create a black square object, or a transparent square.

Without file creation. To merge with another image.

51 minutes ago, smbape said:

The alpha channel is the third channel.

Channel three of four? RGBA.

 

Thank you.

Link to comment
Share on other sites

I am fine thank you. And you?

1 hour ago, Lion66 said:

But I didn't understand the answer to the question: how to create a black square object, or a transparent square.

I misread the question.
 

$mat = _cveMatCreate()
_cveMatOnes($img_height, $img_width, $CV_8U, $mat)
_cveMatZeros($img_height, $img_width, $CV_8U, $mat)

 

1 hour ago, Lion66 said:

Channel three of four? RGBA

I am wrong, it the fourth

Local $planes[4] = [_cveMatCreate(), _cveMatCreate(), _cveMatCreate(), _cveMatCreate()]
_cveSplitMat($img, $planes)
Local $alpha = $planes[3]

 

Link to comment
Share on other sites

18 hours ago, smbape said:

I will be busy for the next 4 weeks but I will try to answer your questions as fast as I can.

No problem. 3,000 functions - there will be many questions!
I appreciate your explanations and examples.
Answer when you have the opportunity.

It seems that the creation of a black rectangle was logically not the right direction.
I'm adding an alpha channel to the image.
Now I want to increase the size of the image by using transparent background, do not increasing original image.
I see here two ways:
1. Create a transparent object with an alpha channel larger than the image. I can create a _cveMatZeros, but how do I make it transparent?
2. Cut the alpha channel from converted RGBA image and resize it to a larger one. I do this, but I get an error when I try to resize, or view,  or save  the image to a file.

Local $matImg = _cveImreadAndCheck("Lena.png")
Local $planes[4] = [_cveMatCreate(), _cveMatCreate(), _cveMatCreate(), _cveMatCreate()]
_cveSplitMat($matImg, $planes)
Local $alpha = _cveMatCreate()
$alpha = $planes[3]

_cveImshowMat("", $alpha)
_cveWaitKey()
_cveDestroyAllWindows()

_cveImwriteMat(@ScriptDir & "\Alpha.png", $alpha)

Local $tDsize = _cvSize(0, 0)
$tDsize.width = $img_width + 200
$tDsize.height = $img_height + 200
Local $matImgResized = _cveMatCreate()
_cveResizeMat($alpha, $matImgResized, $tDsize)

After that, I intend to add the original image and the transparent background with function BitwiseAnd, but the function requires a mask. Can the mask be empty?

Local $matMask = _cveMatCreate()
Local $matDstImg = _cveMatCreate()
_cveBitwiseAndTyped("Mat", $matImgResized, "Mat", $matImg, "Mat", $matDstImg, "Mat", $matMask)

I am trying to streamline my many questions 😄

Thank you!

Link to comment
Share on other sites

Hi @Lion66

The example I gave you is not working.
This is a working example

Local $matImg = _cveImreadAndCheck("cards.png", $CV_IMREAD_UNCHANGED)
ConsoleWrite("channels " & _cveMatNumberOfChannels($matImg) & @CRLF & @CRLF)

Local $vectorMv = _VectorOfMatCreate()
_cveSplitTyped("Mat", $matImg, "VectorOfMat", $vectorMv)

Local $tPtr = DllStructCreate("ptr value")
For $i = 0 To _VectorOfMatGetSize($vectorMv) - 1
    _VectorOfMatGetItemPtr($vectorMv, $i, $tPtr)
    _cveImshowMat("Channel " & $i, $tPtr.value)
    ; _cveWaitKey()
Next

_cveWaitKey()

Not all png images have an alpha channel, so make sure to check that there are 4 channels before proceeding.
The alpha channel will usually give you a white image because all pixels are opaque.

 

On 9/16/2021 at 2:50 PM, Lion66 said:

Can the mask be empty?

By looking at the documentation, the mask can be empty.
An empty mask is a mask which value is _cveMatCreate() or _cveNoArrayMat()

Edited by smbape
Link to comment
Share on other sites

Hi @smbape

Thanks to your clues, I have advanced in my example.
I pulled out the alpha channel, but it's not transparent as I expected.
I am sure that it is easier to create a transparent background. It is possible to create a matrix and fill it with the necessary data.
Question one: Do you know how to do this? Or make the alpha channel transparent?
I planned to use BitwiseAnd to connect two images.
But when I dealt with it, I found that it adds only images of the same size.
Now I want to try to apply _cveAddTyped. Use directly causes an error.
I found an example on a python in which a small picture is placed in the center of a large one using ROI points.
Question two: I do not know how to create the array and fill it with ROI data.
I attach the python file and my file.
Thanks for your help.

JoinTransparentBG-and-Img-byROI_v2.au3 JoinImages.py

Link to comment
Share on other sites

Hi @Lion66

I am back, It took me longer than I expected.

Seeing how it was hard to use the UDF, I managed to make a version that is almost as easy to use as in python

There is a new implementation using COM. It is almost as easy as python to use

Here is the rewrite.
You will have to adjust the roi.
I read everything as gray scale because I had errors otherwise. Probably not using the correct images.

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_UseX64=y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****

#include "..\autoit-opencv-com\udf\opencv_udf_utils.au3"

_OpenCV_Open_And_Register(_OpenCV_FindDLL("opencv_world4*", "opencv-4.*\opencv"), _OpenCV_FindDLL("autoit_opencv_com4*"))

Local $cv = _OpenCV_get()

If IsObj($cv) Then
    $img1 = $cv.imread(_OpenCV_FindFile("samples\data\lena.jpg"), $CV_IMREAD_GRAYSCALE)
    $img2 = $cv.imread(_OpenCV_FindFile("samples\data\lena.jpg"), $CV_IMREAD_GRAYSCALE)

    $brows = $img1.rows
    $bcols = $img1.cols
    $rows = $img2.rows
    $cols = $img2.cols
    $channels = $img2.channels()

    ; A Rect is [x, y, width, height]
    ; img.rows == img.height
    ; img.cols == img.width
    ; Ниже я изменил roi, чтобы картинка выводилась посередине, а не в левом верхнем углу
    Local $rect[4] = [0 , 0, $bcols, $brows]
    $roi = ObjCreate("OpenCV.cv.Mat").create($img1, $rect)

    $img2gray = $img2; $cv.cvtColor($img2, $CV_COLOR_BGR2GRAY)
    $cv.threshold($img2gray, 10, 255, $CV_THRESH_BINARY)
    $mask = $cv.extended[1]
    $mask_inv = $cv.bitwise_not($mask)

    $img1_bg = $cv.bitwise_and($roi, $roi, $mask_inv)

    $img2_fg = $cv.bitwise_and($img2, $img2, $mask)

    $dst = $cv.add($img1_bg, $img2_fg)
    $dst.copyTo($roi)
    $cv.imshow('res.png', $img1)
    $cv.waitKey()
EndIf

_OpenCV_Unregister_And_Close()

 

Edited by smbape
Link to comment
Share on other sites

On 9/20/2021 at 3:10 PM, Lion66 said:

I planned to use BitwiseAnd to connect two images.

If your intention is to copy an image to a location of another image, here is a code for that

Local $big = $cv.imread("big.png")
Local $small = $cv.imread("small.png")
; Rect is [x, y, width, height]
Local $roi[4] = [($big.width - $small.width) / 2, ($big.height - $small.height) / 2, $small.width, $small.height ]
$small.copyTo(ObjCreate("OpenCV.cv.Mat").create($big, $roi))

$cv.imshow("copy", $big)
$cv.waitKey()
$cv.destroyAllWindows()

 

Edited by smbape
Link to comment
Share on other sites

  • smbape changed the title to OpenCV v4 UDF

Hi @smbape

Welcome back!
While there was a pause, I investigated the possibilities of Opencv with numerous examples on the python.
Now I need to switch back and remember what I was doing and what I asked. 🙂
I'll check your new versions and write.
Thanks for the answer.

Link to comment
Share on other sites

Now I see that this is not just another version. You completely changed the concept and created your own libraries.
Great respect for your work! 👏
I just started watching this development, a few first remarks.
1. In file Readme.md: it seems to me that the description of the python code and the autoit code is written on the contrary.

It is necessary on the contrary in several places. For example:

Quote

The AutoIt version of the function is



cv.GaussianBlur(  src, ksize, sigmaX[, sigmaY[, borderType[, dst]]] ) ->  dst

The python code will therefore become



$blurred = $cv.GaussianBlur($image, _OpenCV_Size(3, 3), 0)

And

Quote

The AutoIt version of the function is



cv.findContours(  image, mode, method[, offset[, contours[, hierarchy]]]  ) ->  contours, hierarchy

The python code will become



$cnts = $cv.findContours($thresh_img, $CV_RETR_EXTERNAL, $CV_CHAIN_APPROX_SIMPLE)
$_ = $cv.extended[1]

2. Your last example of connecting two photos works.
A long example (like a python) has errors and in this form does not work for two images of different sizes.
I managed to fix it in color and I attach the code.

PutImgIntoOtherImg_v2.au3

 3. I tried a few examples (one of them 04-find-template.au3).
For me don't work

Local $img = _OpenCV_imread_and_check(_OpenCV_FindFile("data\mario.png"))


instead works

Local $img = $cv.imread("data\mario.png")


Edited by Lion66
Link to comment
Share on other sites

19 minutes ago, Lion66 said:

Great respect for your work! 👏

Thank you.

 

20 minutes ago, Lion66 said:

It is necessary on the contrary in several places. For example:

The phrasing may not be correct. What I wanted people to understand is:
Given the above AutoIt version of the function, the translation of the python code will be.
What would have been a better phare for you?

 

27 minutes ago, Lion66 said:

I managed to fix it in color and I attach the code.

Great. I see that you had no difficulties while using the new version

Link to comment
Share on other sites

18 hours ago, smbape said:

The phrasing may not be correct. What I wanted people to understand is:

I have not yet had time to delve into the details, just by syntax, variables with a dollar sign at first are Autoit, and without a dollar it is python.

 

18 hours ago, smbape said:

Great. I see that you had no difficulties while using the new version

It's just a fix, but it took me a few hours.
But now the syntax is like a python and there is hope that it will be easier to use.

 

I tried several examples: in the new version it is more clear and less to write.
But use cases play a crucial role.
In the following example, I could not apply $cv.approxPolyDP, because I did not find an example of use.
In this case, I replaced with push_back (from example).

But the issue of using the function remains for other examples.

DrawContours.au3

Edited by Lion66
Link to comment
Share on other sites

9 hours ago, Lion66 said:

But the issue of using the function remains for other examples.

Well, it will be the same with python.
Until now, I have not found a working python example that was hard to translate.
The library is at fault if by following the opencv documentation, it doesn't work.

Corcerning your given example, it is not approxPolyDP that failed, it is the usage of the result in drawContours that fails.
approxPolyDP returns a contour.
drawContours draws vector/array of contours.
It is therefore expected to fail with a contour.
It will also fail in python.

You should do the following to draw the approxPolyDP contour

$apd.push_back($cv.approxPolyDP($contours[$i], 0.02*$sm, True))

 

Link to comment
Share on other sites

  • 2 weeks later...

Hi @smbape

Other interests got priority, and the autoit departed to the background .

I hope to get back to him later. 😉

I've looked at your new examples.
One example I know has a logical problem (extra\Find-Contour-Draw-Demo.au3) :
when the script is started, the picture is automatically called, but the object $sObject remains empty.

And until the picture is not changed, does not work functions:

Invert, Erosion, Dilation, Opening, Closing, Gradient.

Also for myself, I added the function of saving also a color picture with contours.
All the best.

Edited by Lion66
Link to comment
Share on other sites

Hi @Lion66

Thank you for your support.

extra\Find-Contour-Draw-Demo.au3 is a rewritting of the code you provided here.
I didn't test it thoroughly. The original file has the same error.
More over, the functions are not called at script start because the checkboxes are unchecked.

Your example has been splitted in 2 files

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...