Author Topic: PixelMath (A Learning Story)  (Read 3009 times)

Offline ngc1535

  • PixInsight Old Hand
  • ****
  • Posts: 326
PixelMath (A Learning Story)
« on: 2017 October 03 12:11:56 »
I am not certain where to put this anecdote- but perhaps it might be of interest either here or if promoted in the general forum.
This is the story of how I learned to use Pixel Math. I presented this story during the recent AIC conference in San Jose.

When learning this seemingly complex tool- I decided to make a problem for myself and proceed to solve it. The problem I made was to use PixelMath to create
a checkerboard (chess board) pattern of pixels. Going through this exercise taught me how to define variables, use functions and understand the meaning of accessing
a pixel's value or position in the image array.



To solve the problem I knew I had to figure out a way to make the values of a pixel oscillate between 0 and 1 based on the position of the pixels as PixelMath rasters the image.
I recognized that the even or oddness of the x and y positions would give me a hook. This instantly triggered a memory that computer programmers use the modulus operator (%)
to check for the even or oddness of numbers... and that was my starting point. The Modulus operator returns the value that is the integer remainder of the division of two numbers.
In the special case of any x%2 the remainder is either 0 or 1. So not only can I use the returned value as the value I assign to the pixel... it is also in some sense a boolean result!
That is when I settled on the expression:

x=x();
y=y();
iif(y%2, ~(x%2), x%2)

By checking whether the row defined by y is even or odd- I determine whether the row starts with a black pixel or a white pixel (by inverting the result).
I personally thought this was clever and it was a happy day for me!
However... I presented this last weekend (September 30th 2017) at the AIC conference and a gentleman in the audience pointed out I could create an even simpler expression, namely:

x=x();
y=y();
(x+y)%2

In this sense the even or oddness of y is being used in the addition to change the even or oddness of the expression yielding an identical result.
There is always someone more clever out there!

Just for completeness I queried Carlos M. and ask what expression he might come up with... his solution was

x=x();
y=y();
i=-1;
i^(x+y)

However, I found his solution too complex for my tastes.  :P

-Adam Block

Offline Juan Conejero

  • PTeam Member
  • PixInsight Jedi Grand Master
  • ********
  • Posts: 7111
    • http://pixinsight.com/
Re: PixelMath (A Learning Story)
« Reply #1 on: 2017 October 03 14:14:55 »
Interesting :)

Here is a logic-based solution:

Symbols:

   p = init( 0 )

RGB/K Expression:

   p = !p; iif( w()%2 || y()%2, p, ~p )

Note that for odd image width, the above expression simplifies to:

   p = !p
Juan Conejero
PixInsight Development Team
http://pixinsight.com/

Offline ngc1535

  • PixInsight Old Hand
  • ****
  • Posts: 326
Re: PixelMath (A Learning Story)
« Reply #2 on: 2017 October 03 14:53:14 »
Juan,

First...I am so pleased you saw this post!
Your answer is pretty cool. I just looked at init... and now I see how to initialize a variable.

The idea of flipping the value using p=!p is really quite nice.

Of course it reminds me of the P vs NP problems...
This one being a P type problem which you clearly showed (QED)!

-adam
« Last Edit: 2017 October 03 15:03:22 by ngc1535 »