USM sharpening enhancement algorithm
Knowledge points
USM sharpening enhancement algorithm
Image convolution processing to sharpen a common algorithm is called Unsharpen Mask method, the sharpening method is to make a blur on the original image, then subtract a coefficient from the original image by multiplying the image after blurring, and then put the value Scale to the RGB pixel value range of 0~255. The method based on USM sharpening can remove some small interference details and noise, which is more realistic and credible than the image sharpening results obtained by using convolution sharpening operator directly.
The USM sharpening formula is expressed as follows:
(source image - w* Gauss blur) / (1-w); where w indicates weight (0.1 to 0.9), default is 0.6.
Code implementation steps in OpenCV
- Blurred
- Weight superposition
- Output result
python code
import cv2 as cv import numpy as np src = cv.imread("C:/Users/qqxd/Desktop/opencvcode/images/master.jpg") cv.namedWindow("input", cv.WINDOW_AUTOSIZE) cv.imshow("input", src) # sigma = 5,15,25 blur_img = cv.GaussianBlur(src, (0, 0), 5) usm = cv.addWeighted(src, 1.5, blur_img, -0.5, 0) cv.imshow("mask image", usm) h, w = src.shape[:2] result = np.zeros([h, w*2, 3], dtype=src.dtype) result[0:h,0:w,:] = src result[0:h,w:2*w,:] = usm cv.putText(result, "original image", (10, 30), cv.FONT_ITALIC, 1.0, (0, 0, 255), 2) cv.putText(result, "sharpen image", (w+10, 30), cv.FONT_ITALIC, 1.0, (0, 0, 255), 2) cv.imshow("sharpen_image", result) # cv.imwrite("D:/result.png", result) cv.waitKey(0) cv.destroyAllWindows()
c++ code
#include <opencv2/opencv.hpp> #include <iostream> using namespace cv; using namespace std; int main(int artc, char** argv) { Mat src = imread("C:/Users/qqxd/Desktop/opencvcode/images/master.jpg"); if (src.empty()) { printf("could not load image...\n"); return -1; } namedWindow("input", WINDOW_AUTOSIZE); imshow("input", src); Mat blur_img, usm; GaussianBlur(src, blur_img, Size(0, 0), 25); addWeighted(src, 1.5, blur_img, -0.5, 0, usm); imshow("mask image", usm); waitKey(0); return 0; }
The results are as follows: