Ask all general Gimp related questions here
Post a reply

Algorithms for selection subtraction or intersectoin

Thu May 09, 2024 3:48 pm

GIMP Version: 2.8.14
Operating System: Windows
GIMP Experience: Basic Level



I have been experimenting with luminosity masks, and I've been struggling to understand exactly what happens when I have a current selection, click on a selection mask in the channels dialog, and select "Subtract from Selection" or "Intersect with Selection".

For subtractions, does Gimp replace each grayscale value with the numeric difference between the two selection masks? If so, are values less than zero set to zero?

For intersections, how are the new values calculated? I'm guessing there's some kind of nonlinear function (?), but I can't guess what it is.

Any clarification would help, since that's how I make sense of what I'm seeing.

Thanks!
Bruce

Re: Algorithms for selection subtraction or intersectoin

Fri May 10, 2024 3:06 am

* Create two channels (Vertical and Horizontal, here) and fill them with a gradient.
* Right-click one of the channels, and "Channel to selection"
* RIght-click the other, and "Add|Subtract |Intersect to selection"
* "Select > Save to channel". You now have a thumbnail image of the result.
ChannelOps.png
ChannelOps.png (21.3 KiB) Viewed 487 times


If you do the same with a pair of layers, and change the blend mode of the top layer, you get similar results, where

* Channel addition is layer addition
* Channel subtraction is layer subtraction
* Channel intersection is "Darken only"

Note however that layer blend modes are computed after being converted to "linear light" values while it is possible that the Channel values are applied directly (which is why you get similar shapes, but not the exact same result).

Re: Algorithms for selection subtraction or intersectoin

Fri May 10, 2024 11:24 am

Thanks, ofnuts. I've been trying various experiments like that, but while I can see the results, I'm still having a hard time understanding why they are what they are. That's an issue only because I'm hoping eventually to be able to look at an image and have an intuitive feel for what kind of mask(s) I'll want to use to achieve some desired result.

I've tried to reproduce the results on my own, just by combining ranges of values between 0.0 and 1.0, which I map linearly to grayscale values, but the intersection results in particular don't look anything like what I get in Gimp.

For example - using the naming convention from Pat David's fine tutorial (I can't post links yet - too new - it's on the Gimp site under Tutorials | Luminosity_Masks) - the M, MM, and MM channels get progressively more narrow in terms of the midtones. But when I try to find the intersection between, say DDD and LLL, there's virtually no overlap, so my attempts to recreate these channels get progressively darker, with no selected pixels by the time I reach MMM.

I'm using a linear grayscale, though, which I don't think is right, so I suspect that's the issue. How are values 0 - 1 mapped to grayscale, and how are the intersection values computed? I think that's what I'm asking.

Re: Algorithms for selection subtraction or intersectoin

Fri May 10, 2024 11:32 am

GAMMA CORRECTION!! I've often heard of it, but before now I've never taken the dive into learning what it means.

I think that's exactly what I need to understand in order to answer my question.

Re: Algorithms for selection subtraction or intersectoin

Fri May 10, 2024 7:51 pm

Bruce E wrote:GAMMA CORRECTION!! I've often heard of it, but before now I've never taken the dive into learning what it means.

I think that's exactly what I need to understand in order to answer my question.

Some explanations here until I find the time to make a more complete tutorial.

Re: Algorithms for selection subtraction or intersectoin

Sun May 12, 2024 10:54 am

ofnuts wrote:
Bruce E wrote:GAMMA CORRECTION!! I've often heard of it, but before now I've never taken the dive into learning what it means.

I think that's exactly what I need to understand in order to answer my question.

Some explanations here until I find the time to make a more complete tutorial.


That thread answers my fundamental question perfectly. I've updated my spreadsheet experiment, and it now looks right for the addition function.

I still haven't reproduced the intersection function, probably because I just haven't guessed the correct algebraic expression. Taking the mean of the values seems close, but I'm not sure that's right.

Re: Algorithms for selection subtraction or intersectoin

Sun May 12, 2024 6:06 pm

Bruce E wrote:
ofnuts wrote:
Bruce E wrote:GAMMA CORRECTION!! I've often heard of it, but before now I've never taken the dive into learning what it means.

I think that's exactly what I need to understand in order to answer my question.

Some explanations here until I find the time to make a more complete tutorial.


That thread answers my fundamental question perfectly. I've updated my spreadsheet experiment, and it now looks right for the addition function.

I still haven't reproduced the intersection function, probably because I just haven't guessed the correct algebraic expression. Taking the mean of the values seems close, but I'm not sure that's right.


If you are using a spreadsheet, the exact formulas for sRGB (which is what Gimp uses by default) are:

sRGB (0. to 1.) -> Linear (0. to 1.): =IF(XX<=0.04045,XX/12.92,POWER((XX+0.055)/1.055,2.4))
Linear (0. to 1.) -> sRGB (0. to 1.): =IF(XX<0.0031308,XX*12.92,1.055*POWER(XX,1/2.4)-0.055)

Multiply/divide by 255 if you use the [0..255] range.

Re: Algorithms for selection subtraction or intersectoin

Mon May 13, 2024 12:40 pm

ofnuts wrote:If you are using a spreadsheet, the exact formulas for sRGB (which is what Gimp uses by default) are:

sRGB (0. to 1.) -> Linear (0. to 1.): =IF(XX<=0.04045,XX/12.92,POWER((XX+0.055)/1.055,2.4))
Linear (0. to 1.) -> sRGB (0. to 1.): =IF(XX<0.0031308,XX*12.92,1.055*POWER(XX,1/2.4)-0.055)

Multiply/divide by 255 if you use the [0..255] range.

Excellent - thank you! I had been hoping to see the actual algorithm or code but didn't know if anyone would be able to provide it. In fact, I had been thinking of downloading the source code to see if I could locate the functions, but I haven't gotten that far yet.

I am using a spreadsheet, so I'll use the formulas you provided. I'm sure they'll work as expected, and then I'll enjoy solving the puzzle of reverse-engineering the formulas to be sure I understand how the hard-coded parameters map to the gamma correction function. I'm guessing that they use gamma = 2.4 (?).

I'm still not sure how the intersect function works, since that takes two input values, but maybe it will become clear once I've updated my spreadsheet.

Re: Algorithms for selection subtraction or intersectoin

Tue May 14, 2024 2:25 am

Bruce E wrote:
ofnuts wrote:If you are using a spreadsheet, the exact formulas for sRGB (which is what Gimp uses by default) are:

sRGB (0. to 1.) -> Linear (0. to 1.): =IF(XX<=0.04045,XX/12.92,POWER((XX+0.055)/1.055,2.4))
Linear (0. to 1.) -> sRGB (0. to 1.): =IF(XX<0.0031308,XX*12.92,1.055*POWER(XX,1/2.4)-0.055)

Multiply/divide by 255 if you use the [0..255] range.

Excellent - thank you! I had been hoping to see the actual algorithm or code but didn't know if anyone would be able to provide it. In fact, I had been thinking of downloading the source code to see if I could locate the functions, but I haven't gotten that far yet.

I am using a spreadsheet, so I'll use the formulas you provided. I'm sure they'll work as expected, and then I'll enjoy solving the puzzle of reverse-engineering the formulas to be sure I understand how the hard-coded parameters map to the gamma correction function. I'm guessing that they use gamma = 2.4 (?).

I'm still not sure how the intersect function works, since that takes two input values, but maybe it will become clear once I've updated my spreadsheet.


Actually I just implemented the formulas described here, and to my great joy, found that it produced the same values as Gimp. Also have the Python code if you are interested.

Intersection is "Darken only" so it just takes the lower of the two values (and this doesn't require a conversion to linear since the conversion is strictly monotone)

Re: Algorithms for selection subtraction or intersectoin

Tue May 14, 2024 11:34 am

ofnuts wrote:
Actually I just implemented the formulas described here, and to my great joy, found that it produced the same values as Gimp. Also have the Python code if you are interested.

Intersection is "Darken only" so it just takes the lower of the two values (and this doesn't require a conversion to linear since the conversion is strictly monotone)

Ah! That's a good link. I've been doing all this only with grayscale images, since I'm trying to create luminosity masks, and so far I'd read only this page: https://en.wikipedia.org/wiki/Gamma_correction. Diving into color spaces etc. is going to be a next step.

... Along with learning more about Gimp's blend modes - including "Darken only", which I haven't really investigated yet.

So much to learn, but you've been a great help - thanks again!

(And I might take you up on the offer of Python code once I've gotten a little farther.)

Bruce

Re: Algorithms for selection subtraction or intersectoin

Tue May 14, 2024 2:46 pm

Bruce E wrote:(And I might take you up on the offer of Python code once I've gotten a little farther.)


Best place to ask when you get there (faster answers, usually).

Re: Algorithms for selection subtraction or intersectoin

Tue May 14, 2024 6:19 pm

ofnuts wrote:
Bruce E wrote:(And I might take you up on the offer of Python code once I've gotten a little farther.)


Best place to ask when you get there (faster answers, usually).


Good to know. I haven't figured out which of the three forums that I know of is best for which kind of inquiry just yet (gimpchat.com, gimp-forum.net, or discuss.pixls.us), but I'll get the hang of it.
Post a reply