0

I tried using NPP to create an "unsharp mask" but my image is not coming out sharpened, just a little brighter in some areas. Any idea what's wrong with this code?

    npp::loadImage("Lena.pgm", hostSrc);

    // put two copies of the image in GPU memory
    // one we'll turn into the unsharp mask                                                                                                                      
    npp::ImageNPP_8u_C1 deviceSrc(hostSrc);
    npp::ImageNPP_8u_C1 deviceUnsharpMask(hostSrc);

    // 5x5 box for mask 
    NppiSize maskSize = {5, 5};

    // create ROI based on image size and mask size                                                                                              
    NppiSize maskedROI = {deviceSrc.width() - maskSize.width + 1,
                          deviceSrc.height() - maskSize.height + 1};

    // allocate device blurred image                                                                                              
    npp::ImageNPP_8u_C1 deviceBlurred(maskedROI.width, maskedROI.height);

    NppiPoint anchor = {0, 0};

    // run box filter                                                                                                                                     
    nppiFilterBox_8u_C1R(deviceSrc.data(), deviceSrc.pitch(),
                         deviceBlurred.data(), deviceBlurred.pitch(),
                         maskedROI, maskSize, anchor);

    // subtract the masked image from the scratch image                                                                                                   
    eStatusNPP = nppiSub_8u_C1IRSfs(deviceBlurred.data(), deviceBlurred.pitch(),
                                    deviceUnsharpMask.data(), deviceUnsharpMask.pitch(),
                                    maskedROI, 1);


    // now add the mask to the src image                                                                                                                  
    eStatusNPP = nppiAdd_8u_C1IRSfs(deviceUnsharpMask.data(), deviceUnsharpMask.pitch(),
                                    deviceSrc.data(), deviceSrc.pitch(),
                                    maskedROI, 0);

    // then copy back to host and save to file
OneSolitaryNoob
  • 5,423
  • 3
  • 25
  • 43

1 Answers1

-1

Unsharp Mask works as following:

  1. Blur the Original Image - Let's call it BI.
  2. Subtract the Blurred Image form the Original Image (Details) - DI = OI - BI.
  3. Amplify the details and add it to the original Image - USMI = OI + alpha * DI.

Are you sure that's what you do?

Here's a reference MATLAB Code:

function [ mUsmImage ] = Usm( mInputImage, usmAmount, usmRadius )

gaussianKernelRadius = ceil(6 * usmRadius);

mGaussianKernel = exp(-([-gaussianKernelRadius:gaussianKernelRadius] .^ 2) / (2 * usmRadius * usmRadius));

mGaussianKernel = mGaussianKernel.' * mGaussianKernel;
mGaussianKernel = mGaussianKernel / sum(mGaussianKernel(:));

mBlurredLayer = imfilter(mInputImage, mGaussianKernel, 'replicate');

mUsmImage = mInputImage + (usmAmount * (mInputImage - mBlurredLayer ));

end

The code works on Grayscale image. It can be easily adopted for RGB.

Enjoy.

Royi
  • 4,640
  • 6
  • 46
  • 64
  • If you can't tell by looking at the code, then why did you answer? – Mark Ransom Jun 02 '14 at 21:21
  • Because for sure I couldn't see the multiplication by Alpha. If you couldn't see it, why the '-1' :-)? – Royi Jun 02 '14 at 21:24
  • `Lena.pgm` is unlikely to have any alpha, so that step can be skipped. And anything that isn't an actual answer to the question should be left as a comment instead. If you had some code after the general algorithm description, *that* would be an answer. – Mark Ransom Jun 02 '14 at 22:56
  • @MarkRansom, I think you got it wrong. My alpha isn't the Alpha channel. It's the amount of sharpening of the USM. I looked into the code and it doesn't apply USM, I focused the user on how USM is defined. – Royi Jun 03 '14 at 15:33
  • Again, it's an optional step. You can certainly build a USM where alpha=1 always. – Mark Ransom Jun 03 '14 at 15:43