The GetDPI Photography Forum

Great to see you here. Join our insightful photographic forum today and start tapping into a huge wealth of photographic knowledge. Completing our simple registration process will allow you to gain access to exclusive content, add your own topics and posts, share your work and connect with other members through your own private inbox! And don’t forget to say hi!

sharpening images via weighted discrete cosine transforms

James L. Dean

New member
I've written a C command line program to sharpen images.

Around each pixel in the image, this program calculates the fast 2D discrete cosine transform for the shades of the pixels in a rectangle of a specified size around the given pixel, multiplies the AC components of the transform by a specified value, inverts the 2D DCT, and sets the shade of the given pixel to the resulting shade while preserving the hue of the pixel.

The program handles the grayscale version of the image similar to the way described by "Discrete Cosine Transform for Enhancing the Contrast of Gray Image" by Nivedita V. Deshmukh (Hippalgaonkar) in the International Journal of Novel Research and Development, Volume 10, Issue 4, April 2025.

Here is an example of an image and the output from my program:
SharpenViaWeightedDCTs4.demo1.jpg
I have attached the C source code.
 

Attachments

  • SharpenViaWeightedDCTs.txt
    61.4 KB · Views: 5
What's the benefit vs the algos in PS or HP sharpening via special blend modes?
I don't have PS or HP so I can't say. This algorithm is very CPU intensive. Chances are it's much slower than the blend modes; it's not likely an interactive user would be willing to wait for the result.
 
I don't get it.

Photoshop is very good at sharpening, especially if you do high-pass sharpening. Is there anything new in this algorithm?
 
I don't get it.

Photoshop is very good at sharpening, especially if you do high-pass sharpening. Is there anything new in this algorithm?
I suspect this is a completely different way of doing sharpening. I discovered it while playing with discrete cosine transforms. Later, I discovered that Nivedita V. Deshmukh (Hippalgaonkar) had published a paper describing a similar algorithm. Not wanting to pay for PhotoShop, it is what I use.
 
I don't get it.

Photoshop is very good at sharpening, especially if you do high-pass sharpening. Is there anything new in this algorithm?
If you want to try HP sharpening, https://www.dropbox.com/scl/fi/yp95...n20e.bmp?rlkey=k6tnk7ldbioxyvsqnpucfaba4&dl=0 is the original image and https://www.dropbox.com/scl/fi/nghw...16x4.bmp?rlkey=p56y414ni3clodc8ev19k6whe&dl=0 is the corresponding output from my program. I used the command "SharpenViaWeightedDCTs.exe moon20e.bmp moon20e.SharpenViaWeightedDCTs.16x16x4.bmp 16 16 4 16". It took 27 seconds on using sixteen 5 GHz CPUs.
 
If you want to try HP sharpening, https://www.dropbox.com/scl/fi/yp95...n20e.bmp?rlkey=k6tnk7ldbioxyvsqnpucfaba4&dl=0 is the original image and https://www.dropbox.com/scl/fi/nghw...16x4.bmp?rlkey=p56y414ni3clodc8ev19k6whe&dl=0 is the corresponding output from my program. I used the command "SharpenViaWeightedDCTs.exe moon20e.bmp moon20e.SharpenViaWeightedDCTs.16x16x4.bmp 16 16 4 16". It took 27 seconds on using sixteen 5 GHz CPUs.
Using one 5 GHz CPU, it takes 4 minutes 18 seconds.
 
I ran it through Claude AI.

It basically says it's a useless algorithm which in the middle of the code does computationally expensive operations without benefit, really.

"The per-pixel sliding window DCT is what makes it so slow, and that computational cost doesn't buy you anything proportional in quality. For every single pixel, it extracts a window, runs a full 2D DCT, multiplies, inverts, and keeps only one output value. On a 4000×3000 image with a 32×32 window, that's 12 million DCT/IDCT pairs. Minutes of processing for a result that a Gaussian-based local contrast tool achieves in under a second."

This said, the AI made some suggestions for you to further spend time on if you have it:

"What would make a DCT-based approach genuinely advanced and worth the computational cost would be things like frequency-dependent weighting curves (boost high frequencies more for sharpening, mid-frequencies for clarity), adaptive multipliers based on local signal-to-noise ratio, thresholding small coefficients to suppress noise while boosting significant detail, or edge-aware windowing. None of that is present here — the ModifyDCT function is four lines that multiply everything identically."

I've attached the suggestions to modify and speed up your code, let me know if it is transforming your tool.
 

Attachments

  • DCT_Sharpening_Suggestions.txt
    7.4 KB · Views: 3
I ran it through Claude AI.

It basically says it's a useless algorithm which in the middle of the code does computationally expensive operations without benefit, really.

"The per-pixel sliding window DCT is what makes it so slow, and that computational cost doesn't buy you anything proportional in quality. For every single pixel, it extracts a window, runs a full 2D DCT, multiplies, inverts, and keeps only one output value. On a 4000×3000 image with a 32×32 window, that's 12 million DCT/IDCT pairs. Minutes of processing for a result that a Gaussian-based local contrast tool achieves in under a second."

This said, the AI made some suggestions for you to further spend time on if you have it:

"What would make a DCT-based approach genuinely advanced and worth the computational cost would be things like frequency-dependent weighting curves (boost high frequencies more for sharpening, mid-frequencies for clarity), adaptive multipliers based on local signal-to-noise ratio, thresholding small coefficients to suppress noise while boosting significant detail, or edge-aware windowing. None of that is present here — the ModifyDCT function is four lines that multiply everything identically."
I tried different weighting curves. Nothing I tried worked better than a constant multiplier.

I suspect some calculations in one window can be applied to the next.

I'd be interested in seeing the result of HP sharpening on my sample image.
 
There are free photoshop altenratives with built in sharpening, just google and download. Sharpening is a knwon topic. As mentioned, the analysis of your code yielded that its uselessly doing transforms - ie you have some bad code in there. Implement the suggested changes in the text.
 
I'm 74, retired, on a fixed income, and suffering from inflation
I admire the science, but at some point sharpening becomes less about the image and more about demonstrating how sharpening works. Fortunately, clarity in photography doesn’t seem to inflate at the same rate as explanations.
 
I respect also the willingness to do sth different and code a sharpening algorithm at 74; but nowadays I can sharpen in real time a 150 MPX image so 4 minutes for a moon pic is off and shows the algorithm is not really useful.

The analysis by the AI yielded that essentially it is a 1000x slower than alternate approaches with no benefit, but could become a bit more interesting with some tweaks as attached above and by eliminating so code segments which are not doing anything but creating computational overhead.

Sharpening of a small moon image should be buttery smooth in 2026 on a modern machine.
 
Thanks for taking time on this.

I'm reading Anthony Turner's "The Fifth Paradigm: The Last Year That Made Sense". It indicates retraining won't keep up with the dislocations caused by Claude and associated AIs. But that's a subject for a different forum.
 
No chance. The AI analyzed your code in its totality in 3 seconds and spat out an analysis of why it takes so long, where it fails vs. the basic approach + suggestions for changes. Coding is done.
 
I tried different weighting curves. Nothing I tried worked better than a constant multiplier.

I suspect some calculations in one window can be applied to the next.

I'd be interested in seeing the result of HP sharpening on my sample image.
The attached version of the program calculates the inverse DCT only for the pixel being processed. It takes about three fifths of the time to run. Still, the calculation of DCTs is way too expensive.
 

Attachments

  • SharpenViaWeightedDCTs.txt
    59.9 KB · Views: 0
Attached is a new version of my command line sharpening program (written in C) that applies more sharpening to less sharp pixels.

Also attached is a screen shot of an example of before and after processing.
 

Attachments

  • AdaptiveSharpening.Example.jpg
    AdaptiveSharpening.Example.jpg
    184.7 KB · Views: 7
  • AdaptiveSharpening.txt
    74.1 KB · Views: 1
Top