Got a link to a good programming challenge?
Got a programming challenge you've written yourself?
Post it here!

This subreddit is for programming challenges. Got a link to a good tutorial? You should try posting it in r/learnprogramming instead.

Unless it's an integral part of the challenge, please try to stay language-agnostic. It helps reduce the "help me with my programming chores" vibe. This subreddit is not a place to post your programming homework!

Say we have stored two square grids of greyscale pixels as "float's" such that each pixel runs from 0.0 (black) to 1.0 (white). To avoid confusion, refer to these squares of pixels as "blocks" for now on. These blocks are portions of natural images taken from photographs. At any given time, I must compare a "Template Block" against a "Test Block" The comparison is a measurement of Root-mean-squared between their pixels, 1-to-1. This comparison is meant to measure the "visual similarity" between the blocks, and is the roughest approximation of visual similarity.

The Template_blk cannot change and does not change. However, the Test_blk can be altered with contrast and brightness adjustments.

"Contrast" means all pixels are scaled closer to 0.5(grey). Given any starting block, if its contrast is maxed out, it is the original block. If contrast=0.0 the square is just a flat square of 0.5(grey). You can then imagine what in-between values would look like in this scenario. Contrast parameter, CONTp, runs from 0.0 to 1.0. For the mathematically curious, actual formula for contrast will be given below.

"Brightness" means a value that is simply added into, or subtracted from the pixel value after contrast adjustments have been performed. Brightness parameter, BRIGHTp, runs from -1.0 to 1.0. For the mathematically curious, actual formula for brightness will be given below.

Our job is to perform a minimization operation. That is, we want to find the CONTp and BRIGHTp such that the Root-mean-squared between these blocks is minimized. In other words, we must tweak CONTp and BRIGHTp on the Test Block, such that it matches the Template Block as closely as possible. We quantify "matches closely" using Root-mean-squared distance between their pixels.

As of today, I am performing this minimization procedure by literally blindly searching the CONTp,BRIGHTp space of parameters. This requires me to perform the RMS algorithm thousands and thousands of times to find the best CONTp, BRIGHTp combination.

This is hideously slow. I have to sit waiting over an hour to process an entire image. Is there a faster way to find the minimal CONTp,BRIGHTp parameters?

Is there, perhaps, a way to calculate them in closed form?

For the mathematically curious, here is the actual formula for contrast and brightness.