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
Here's the code what I have done. I am using this code to implement Unsharp Mask and it is working well for me. Hope it is useful for you.
void USM(cv::Mat &O, int d, int amp, int threshold)
{
cv::Mat GB;
cv::Mat O_GB;
cv::subtract(O, GB, O_GB);
cv::Mat invGB = cv::Scalar(255) - GB;
cv::add(O, invGB, invGB);
invGB = cv::Scalar(255) - invGB;
for (int i = 0; i < O.rows; i++)
{
for (int j = 0; j < O.cols; j++)
{
unsigned char o_rgb = O.at(i, j);
unsigned char d_rgb = O_GB.at(i, j);
unsigned char inv_rgb = invGB.at(i, j);
int newVal = o_rgb;
if (d_rgb >= threshold)
{
newVal = o_rgb + (d_rgb - inv_rgb) * amp;
if (newVal < 0) newVal = 0;
if (newVal > 255) newVal = 255;
}
O.at(i, j) = unsigned char(newVal);
}
}
}