1 | initial version |
This actually also surprised me a few years back - there are blurring filters but not sharpening ones...
One way of sharpening an image is doing this:
Mat sharp;
Mat sharpening_kernel = (Mat_<double>(3, 3) << -1, -1, -1,
-1, 9, -1,
-1, -1, -1);
filter2D(img, sharp, -1, sharpening_kernel);
The sharpening strength depends on the values of the kernel which should sum up to 1).
Another way is subtracting a smoothed image from its original:
double sigma=1, amount=1;
Mat blurry, sharp;
GaussianBlur(img, blurry, Size(), sigma);
addWeighted(img, 1 + amount, blurry, -amount, 0, sharp);
Play with blur strength and weights for different results.
If you add the following, you will get unsharp mask algorithm, but the differences are not significant or even none with conservative settings:
Mat lowContrastMask = abs(img - blurry) < 5; //experiment with values
sharp = img*(1+amount) + blurry*(-amount); //this is the same as addWeighted - is addWeightd obsolete??
img.copyTo(sharp, lowContrastMask);
The above code is in C++ but you should be able to translate it to Python easily.