title: OpenCV and Unsharp Masking Like Adobe Photoshop
body: <p>I am trying to implement unsharp masking like it's done in Adobe Photoshop. I gathered a lot of information on the interent but I'm not sure if I'm missing something. Here's the code:</p>
  
  <pre><code>void unsharpMask( cv::Mat* img, double amount, double radius, double threshold ) {
  
  // create blurred img
  cv::Mat img32F, imgBlur32F, imgHighContrast32F, imgDiff32F, unsharpMas32F, colDelta32F, compRes, compRes32F, prod;
  double r = 1.5;
  img-&gt;convertTo( img32F, CV_32F );
  cv::GaussianBlur( img32F, imgBlur32F, cv::Size(0,0), radius );
  cv::subtract( img32F, imgBlur32F, unsharpMas32F );
  // increase contrast( original, amount percent ) 
  imgHighContrast32F = img32F * amount / 100.0f;
  cv::subtract( imgHighContrast32F, img32F, imgDiff32F );
  unsharpMas32F /= 255.0f;
  cv::multiply( unsharpMas32F, imgDiff32F, colDelta32F );
  cv::compare( cv::abs( colDelta32F ), threshold, compRes, cv::CMP_GT );
  compRes.convertTo( compRes32F, CV_32F );
  
  cv::multiply( compRes32F, colDelta32F, prod );
  cv::add( img32F, prod, img32F );
  
  img32F.convertTo( *img, CV_8U );
  }
  </code></pre>
  
  <p>At the moment I am testing with a grayscale image. If i try the exact same parameters in Photoshop I get much better result. My own code leads to noisy images. What am I doing wrong.</p>
  
  <p>The 2nd question is, how i can apply unsharp masking on RGB images? Do I have to unsharp mask each of the 3 channels or would it be better in another color space? How are these things done in Photoshop?</p>
  
  <p>Thanks for your help!</p>