像Adobe Photoshop一样的OpenCV和锐化蒙版

像Adobe Photoshop一样的OpenCV和锐化蒙版

问题描述:

我正在尝试像在Adobe Photoshop中一样实现不清晰的蒙版.我收集了很多有关Interent的信息,但不确定是否丢失了某些内容.这是代码:

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:

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->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 );
}

目前,我正在测试灰度图像.如果我在Photoshop中尝试完全相同的参数,我会得到更好的结果.我自己的代码导致图像嘈杂.我在做什么错了.

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.

第二个问题是,如何在RGB图像上应用不清晰的蒙版?我是否需要对3个通道的每个通道进行锐化遮罩,还是在其他颜色空间中更好?这些东西在Photoshop中是如何完成的?

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?

感谢您的帮助!

我也在尝试复制Photoshop的Unsharp Mask. 让我们忽略阈值一秒钟.

I'm trying to replicate Photoshop's Unsharp Mask as well. Let's ignore the Threshold for a second.

我将向您展示如何使用其高斯模糊来复制Photoshop的Unsharp Mask.

I will show you how to replicate Photoshop's Unsharp Mask using its Gaussian Blur.

假设O是原始图像层.

创建一个新层GB,它是应用于O的高斯模糊.
创建一个新的O层-GB(使用应用图像").
通过反转GB-invGB创建新层.
使用Image Apply创建一个新的O + invGB层.
创建一个新层,该层与上一层相反,即inv(O + invGB).
创建一个新层,即O +(O-GB)-inv(O + invGB).

Create a new layer GB which is a Gaussian Blur applied on O.
Create a new layer which is O - GB (Using Apply Image).
Create a new layer by inverting GB - invGB.
Create a new layer which is O + invGB using Image Apply.
Create a new layer which is inversion of the previous layer, namely inv(O + invGB).
Create a new layer which is O + (O - GB) - inv(O + invGB).

在Photoshop中进行此操作时,您将获得Unsharp Mask的完美复制.

When you do that in Photoshop you'll get a perfect reproduction of the Unsharp Mask.

如果做数学运算时回想起inv(L)= 1-L,您将得到锐化蒙版"为 USM(O)= 3O-2B.

If you do the math recalling that inv(L) = 1 - L you will get that the Unsharp Mask is USM(O) = 3O - 2B.

但是当我直接在MATLAB中执行此操作时,我没有得到Photoshop的结果.

Yet when I do that directly in MATLAB I don't get Photoshop's results.

希望有人会知道确切的数学.

Hopefully someone will know the exact math.

好,
我想通了.
在Photoshop中USM(O)= O +(2 *(数量/100)*(O-GB))
GB是O的高斯模糊版本.

OK,
I figured it out.
In Photoshop USM(O) = O + (2 * (Amount / 100) * (O - GB))
Where GB is a Gaussian Blurred version of O.

但是,要复制Photoshop的结果,您必须执行上述步骤,并将每个步骤的结果裁剪为[0,1],就像在Photoshop中一样.

Yet, in order to replicate Photoshop's results you must do the steps above and clip the result of each step into [0, 1] as done in Photoshop.