PixelMath atan2(image1, image2)

Ken89148

Active member
Was wondering if an atan2(image1, image2) function could be added to PixelMath and the likely time frame. If not, is there an efficient way to convert atan(image1/image2) to atan2 without using a pixel by pixel approach which tends to be slow in PixelMath. Was wondering if the new generators / op_ functions could do this ? They certainly look powerful but a little intimidating for someone who has not used them before.
Thanks Ken
 
I think I have partly answered my own question, I found a none conditional solution (no iif()'s) in Wikipedia with

sign( x )^2*atan( y / x )+0.5*(1-sign( x ))*(1+sign( y )-sign( y )^2)*pi() [*2/pi for scaling to 0 and 1] which runs very quickly in PixelMath.

I then realized for images with values 0 to 1 the result is identical to atan. My two images have had the convolution process applied using the built in Sobel kernals, if I select high pass re-scaling all values are preserved, but I don't see any output of the scaling factor. Don't suppose 0 always scales to 0.5 with +1/-1 to 1 and 0 ? If not is it possible to access the scaling values ?

I don't see any kernal.matrix operations in object explorer so not sure how fast this would run using nested loops. I know there are other programs for this, I have played with openCV for a couple of hours, but I wanted to see if the Canny Edge Detector could be run in PixInsight. I have a Hough line detector running but wanted to further refine the edges from the intial Sobel filters, hence the original atan2 question.
 
Last edited:
Hi Ken,

The atan2 function that you are asking for is an excellent suggestion. I am going to implement it in PixelMath, and will be available as an update in a few days.

I am not sure I'm understanding you. What scaling factor are you looking for exactly? Can you put an example with some source code?

A new 1.8.8-9 version of PixInsight will be released soon, so this is the perfect moment to implement new features in our JavaScript runtime if needed.
 
Hi Juan, thank you for your response.

I’m working on a script to automatically detect images with satellite trails, I’m a retired mechanical engineer and my only previous programing experience was Matlab. I’m thoroughly enjoying learning to write pixinsight scripts, I don’t mean any disrespect, but the lack of documentation makes it especially challenging and it can be quite satisfying finding out what a particular object does and how to format the inputs. Recently I was defeated and you helped me lean how to turn a matrix into an image.

My current script identifies about 50~60% of images containing trails, no false identifications, the trails that are not found are typically quite faint. Since time is not a constraint (being retired) I wanted to try to improve the initial edge detection by adding some further steps of the Canny edge detection method. My problem is as follows;

Ix and Iy are the images after Sobel Filter. The edge intensity is sqrt(Ix^2+Iy^2) and to further refine the edges I also need atan2(Iy/Ix).

Ix and Iy should have both positive and negative values, but they are always positive, the values are either truncated or rescaled when using the convolution process. I recently found a post of yours from Aug 25, 2010 which showed how to use the image.convolve command and I tried this, but again only positive numbers. I didn’t see any matrix.convolve commands where positive and negative values would no doubt be produced. I was thinking that calculating this manually using for loops would not be particularly elegant or efficient, but obviously it can be done.

I noticed that PixelMath has an option to disable both truncation and rescale. Your Jan 21, 2021 post showed how to use kconv so I tried ;

kconv( $T, 1.000000, 0.000000, -1.000000, 2.000000, 0.000000, -2.000000, 1.000000, 0.000000, -1.000000) (I think its a Sobel kernel)

With rescale and truncation set to false, I was surprised to find only positive values. Does kconv automatically truncate or rescale ?

Any suggestions you might have would be most welcome. Also thank you for looking at the atan2 option in a pixel math update.
 
Last edited:
Hi Ken,

Use the highPassMode parameter of Image.convolution() and Image.separableConvolution() to get the raw result of a convolution with a high-pass filter:

JavaScript:
var window = ImageWindow.activeWindow;
var view = window.mainView;
view.beginProcess();
view.image.convolve( [1, 0, -1, 2, 0, -2, 1, 0, -1], // Sobel edge North
                     2 // highPassMode = raw
                   );
view.endProcess();


The Sobel filters are known to be separable. The following script gives exactly the same result but is potentially much faster for relatively large filters—of course, not for a 3x3 filter, but I'm giving this information here for completeness:

JavaScript:
var window = ImageWindow.activeWindow;
var view = window.mainView;
view.beginProcess();
view.image.convolveSeparable( [ 0.759836, 1.519671, 0.759836], // Sobel edge North - row vector
                              [-1.316074, 0.000000, 1.316074], // Sobel edge North - col vector
                              2 // highPassMode = raw
                            );
view.endProcess();

For reference, the possible values of highPassMode are:

0 - Truncate to [0,1]
1 - Rescale to [0,1]
2 - Do nothing (preserve the result of the convolution)
 
It is not possible using PixelMath's graphical user interface, but you can disable both options by editing the instance's source code, or programmatically from a script.
Yes, you explained it well in the other thread. Sorry for the confusion.

Bernd
 
Hi Juan, Hi Bernd

That's perfect, I now have positive and negative numbers for Ix and Iy. Using PixelMath with above atan2 also gives max of plus pi and minus pi. Even the .image.toMatrix() works with the negative numbers. For sure every day with PixInsight is a learning experience, endless possibilities !
Thank you for your prompt answers.
Ken
 
Hi Ken,

The atan2 operation is now implemented in PixelMath for the combine() image generator. It will be distributed as an update tomorrow. Thank you!
 
Back
Top