Opencv-自定义梯度算子
生活随笔
收集整理的這篇文章主要介紹了
Opencv-自定义梯度算子
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
圖像梯度 – 更多梯度算子
- 知識點
- python代碼
- c++代碼
知識點
圖像梯度 – 更多梯度算子
圖像的一階導數算子除了sobel算子之外,常見的還有robert算子與prewitt算子,它們也都是非常好的可以檢測圖像的梯度邊緣信息。
通過OpenCV中自定義濾波器,使用自定義創建的robert與prewitt算子就可以實現圖像的rober與prewitt梯度邊緣檢測,OpenCV中的自定義算子濾波函數如下:
filter2D(
InputArray src,
OutputArray dst,
int ddepth,
InputArray kernel,
Point anchor = Point(-1,-1),
double delta = 0,
int borderType = BORDER_DEFAULT
)
Python:
dst =cv.filter2D(src, ddepth, kernel[, dst[, anchor[, delta[, borderType]]]])
python代碼
import cv2 as cv import numpy as npsrc = cv.imread("C:/Users/qqxd/Desktop/opencvcode/images/lena.png") cv.namedWindow("input", cv.WINDOW_AUTOSIZE) cv.imshow("input", src)robert_x = np.array([[1, 0],[0, -1]], dtype=np.float32) robert_y = np.array([[0, -1],[1, 0]], dtype=np.float32)prewitt_x = np.array([[-1, 0, 1], [-1, 0, 1], [-1, 0, 1]], dtype=np.float32) prewitt_y = np.array([[-1, -1, -1], [0, 0, 0], [1, 1, 1]], dtype=np.float32)robert_grad_x = cv.filter2D(src, cv.CV_16S, robert_x) robert_grad_y = cv.filter2D(src, cv.CV_16S, robert_y) robert_grad_x = cv.convertScaleAbs(robert_grad_x) robert_grad_y = cv.convertScaleAbs(robert_grad_y)prewitt_grad_x = cv.filter2D(src, cv.CV_32F, prewitt_x) prewitt_grad_y = cv.filter2D(src, cv.CV_32F, prewitt_y) prewitt_grad_x = cv.convertScaleAbs(prewitt_grad_x) prewitt_grad_y = cv.convertScaleAbs(prewitt_grad_y)# cv.imshow("robert x", robert_grad_x); # cv.imshow("robert y", robert_grad_y); # cv.imshow("prewitt x", prewitt_grad_x); # cv.imshow("prewitt y", prewitt_grad_y);h, w = src.shape[:2] robert_result = np.zeros([h, w*2, 3], dtype=src.dtype) robert_result[0:h,0:w,:] = robert_grad_x robert_result[0:h,w:2*w,:] = robert_grad_y cv.imshow("robert_result", robert_result)prewitt_result = np.zeros([h, w*2, 3], dtype=src.dtype) prewitt_result[0:h,0:w,:] = prewitt_grad_x prewitt_result[0:h,w:2*w,:] = prewitt_grad_y cv.imshow("prewitt_result", prewitt_result)cv.imwrite("D:/prewitt.png", prewitt_result) cv.imwrite("D:/robert.png", robert_result)cv.waitKey(0) cv.destroyAllWindows()c++代碼
#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/lena.png");if (src.empty()) {printf("could not load image...\n");return -1;}namedWindow("input", WINDOW_AUTOSIZE);imshow("input", src);Mat robert_x = (Mat_<int>(2, 2) << 1, 0, 0, -1);Mat robert_y = (Mat_<int>(2, 2) << 0, -1, 1, 0);Mat prewitt_x = (Mat_<char>(3, 3) << -1, 0, 1,-1, 0, 1,-1, 0, 1);Mat prewitt_y = (Mat_<char>(3, 3) << -1, -1, -1,0, 0, 0,1, 1, 1);Mat robert_grad_x, robert_grad_y, prewitt_grad_x, prewitt_grad_y;filter2D(src, robert_grad_x, CV_16S, robert_x);filter2D(src, robert_grad_y, CV_16S, robert_y);convertScaleAbs(robert_grad_x, robert_grad_x);convertScaleAbs(robert_grad_y, robert_grad_y);filter2D(src, prewitt_grad_x, CV_32F, prewitt_x);filter2D(src, prewitt_grad_y, CV_32F, prewitt_y);convertScaleAbs(prewitt_grad_x, prewitt_grad_x);convertScaleAbs(prewitt_grad_y, prewitt_grad_y);printf("image gradient...");imshow("robert x", robert_grad_x);imshow("robert y", robert_grad_y);imshow("prewitt x", prewitt_grad_x);imshow("prewitt y", prewitt_grad_y);waitKey(0);return 0; }運行結果如下:
總結
以上是生活随笔為你收集整理的Opencv-自定义梯度算子的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Dell C系列(Cloud)服务器BI
- 下一篇: 电子地图