Zi 字媒體
2017-07-25T20:27:27+00:00
fanfuhan OpenCV 教學034 ~ opencv-034-圖像銳化 ~邊緣檢測/失焦(對焦失敗) 的前置動作
資料來源: https://fanfuhan.github.io/
https://fanfuhan.github.io/2019/04/09/opencv-034/
GITHUB:https://github.com/jash-git/fanfuhan_ML_OpenCV
知識點
圖像卷積的主要具有三功能分別是圖像的模糊/去噪,圖像梯度/邊緣發現,圖像銳化/增強,前面的兩個功能我們以前通過相關知識點的分享分享了解,學習了相關API的使用。圖像銳化的本質是圖像拉普拉斯濾波加原圖權重表示疊加的輸出:
-1 -1 -1
-1 C -1
-1 -1 -1
當C值大於8時表示圖像銳化,越接近8表示銳化效果越好
當C值等於8時圖像的高通濾波
當C值最小,圖像銳化效果在衰減,中心呈現的作用在提升
C++
#include
#include
using namespace std;
using namespace cv;
/*
* 图像锐化
*/
int main() {
Mat src = imread("../images/test.png");
if (src.empty()) {
cout << "could not load image.." << endl;
}
imshow("input", src);
Mat sharpen_op = (Mat_(3,3) << -1, -1, -1,
-1, 9, -1,
-1, -1, -1);
// Mat sharpen_op1 = (Mat_(3,3) << 0, -1, 0,
// -1, 9, -1,
// 0, -1, 0);
Mat dst;
filter2D(src, dst, CV_32F, sharpen_op);
convertScaleAbs(dst, dst);
imshow("sharpen", dst);
waitKey(0);
return 0;
}
Python
import cv2 as cv
import numpy as np
src = cv.imread("D:/images/test.jpg")
cv.namedWindow("input", cv.WINDOW_AUTOSIZE)
cv.imshow("input", src)
# sharpen_op = np.array([[-1, -1, -1], [-1, 9, -1], [-1, -1, -1]], dtype=np.float32)
sharpen_op = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]], dtype=np.float32)
sharpen_image = cv.filter2D(src, cv.CV_32F, sharpen_op)
sharpen_image = cv.convertScaleAbs(sharpen_image)
cv.imshow("sharpen_image", sharpen_image)
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,:] = sharpen_image
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()
寫了
5860316篇文章,獲得
23313次喜歡