日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

Opencv-自定义梯度算子

發(fā)布時(shí)間:2024/3/24 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Opencv-自定义梯度算子 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

圖像梯度 – 更多梯度算子

    • 知識(shí)點(diǎn)
    • python代碼
    • c++代碼

知識(shí)點(diǎn)

圖像梯度 – 更多梯度算子
圖像的一階導(dǎo)數(shù)算子除了sobel算子之外,常見(jiàn)的還有robert算子prewitt算子,它們也都是非常好的可以檢測(cè)圖像的梯度邊緣信息。

通過(guò)OpenCV中自定義濾波器,使用自定義創(chuàng)建的robert與prewitt算子就可以實(shí)現(xiàn)圖像的rober與prewitt梯度邊緣檢測(cè),OpenCV中的自定義算子濾波函數(shù)如下:

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

運(yùn)行結(jié)果如下:

總結(jié)

以上是生活随笔為你收集整理的Opencv-自定义梯度算子的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。