Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Quantize to Reference node #2071

Merged
merged 5 commits into from
Aug 12, 2023
Merged

Add Quantize to Reference node #2071

merged 5 commits into from
Aug 12, 2023

Conversation

joeyballentine
Copy link
Member

@joeyballentine joeyballentine commented Aug 10, 2023

I'm open to calling this node something else because I don't think it's very descriptive called this.

Anyway, this was an idea I had a while back when someone was trying to apply a color palette to an image but keep the colors as close to the original as possible (such as being in the same spots).

I accomplished this by just doing a basic convolution across the original image, taking the unique colors from the kernel, applying the palette to the equivalent piece of the recipient image, and then creating a result from piecing those together. This restricts the possible colors to only the colors that could possibly be in that part of the image, and as a result, manages to index the image pretty well in a way that keeps it faithful.

image
It's obviously not perfect, but it did what this person wanted more than dither (palette) did.

The biggest downside is it's pretty slow (On^2 time complexity), so maybe this is another good one to move to rust in the future. Alternatively, something could probably be made using the flatten/reshape stuff like the quantizing function does, but I don't wanna have to figure that out lol

@joeyballentine
Copy link
Member Author

Oh yeah, i was going to restrict the reference image type to be a multiple/ratio of the input image, but I couldn't figure that out

@RunDevelopment
Copy link
Member

Isn't this node equivalent to this chain?

image

@joeyballentine
Copy link
Member Author

joeyballentine commented Aug 10, 2023

No. That will apply the palette from the entire reference image to the entire target image. That's not what this does. This restricts the palette to just a small area at a time so there's no chance it'll use a color where it's not supposed to.

For example if the top third of an image is green colors, the middle third is reds, and the bottom is blues, it might use the green colors from the top section in the bottom blue section if it decides some of the colors are closer (for example if an upscale has color shift or something).

That's obviously a bit of an unlikely example, but in something like the image I posted above, you can see how those smooth gradients might end up having different colors from random points of the image, which we want to avoid.

@joeyballentine joeyballentine changed the title Add Repalettize node Add Quantize (local) node Aug 11, 2023
@joeyballentine
Copy link
Member Author

Maybe Quantize to Reference would be a better name?

@RunDevelopment
Copy link
Member

RunDevelopment commented Aug 11, 2023

I played around with it for a bit, and found an unfortunate issue. Since we only consider a neighborhood of exactly 3x3 pixels with a hard cutoff, the local color palette abruptly changes when we move the kernel. This change can be seen in the final image. Here, I upscaled the MC cobblestone texture 16x:

image
image

So I implemented the following idea to fix this: we add x,y normalized coords to each pixel. This will make far away pixels more distant when calculating the distance, which will prevent the abrupt cutoff.

Here's the result:

image
image

The artifacts are gone, but we lost a lot of detail. The solution for is to increase the kernel size and to decrease the weight of the x,y coords. Then we get this:

image
image

Now that's a good result. No artifacts and good contrast.

The spacial scale also gives us a lot of control over the palette. In the following images, the kernel radius is always 2:
image

Note that scale=0 is the same as not having spacial coords at all.

I'll make PR with my changes.

@joeyballentine
Copy link
Member Author

That's awesome, thanks for playing around and making those changes

@joeyballentine joeyballentine marked this pull request as draft August 11, 2023 16:03
* Add spacial component to local quantization

* Typo

* Some docs
@RunDevelopment
Copy link
Member

Is this ready?

@joeyballentine
Copy link
Member Author

No, I want to rename it to Quantize to Reference. I can't do that right now though, but I'll do it ASAP

@joeyballentine
Copy link
Member Author

Wait, i can abuse reviews

@joeyballentine joeyballentine changed the title Add Quantize (local) node Add Quantize to Reference node Aug 12, 2023
@joeyballentine joeyballentine marked this pull request as ready for review August 12, 2023 15:38
@joeyballentine
Copy link
Member Author

Ah damn the file name, can't change that via review :(

@RunDevelopment
Copy link
Member

There ya go

@joeyballentine joeyballentine merged commit 76584d0 into main Aug 12, 2023
11 checks passed
@joeyballentine joeyballentine deleted the repalletize branch August 12, 2023 18:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants