問題描述
我有一個需要檢查相機焦點的應用程序.為此,我想在單個軸 (1D) 上的幾個預定義位置測量邊緣強度(梯度大小).圖像目標將是在一段時間背景上的黑色對象的簡單打印輸出.
I have an application where I need to check the focus of a camera. For this, I want to measure edge strength (magnitude of gradient) in several predefined locations on a single axis (1D). The image target will be a simple printout of black objects on a while background.
我在 Python 中使用 OpenCV.我知道 OpenCV 中有幾種邊緣檢測算法,例如 Canny、Sobel、laplace,但所有這些都是為了過濾圖像.我想實際測量邊緣的強度.OpenCV 中是否有任何算法可以提供此功能?還是我只是編寫自己的算法來測量邊緣強度?
I am using OpenCV with Python. I know there are several edge detection algorithms within OpenCV like Canny, Sobel, laplace but all of these are to filter the image. I want to actually measure the strength of an edge. Are there any algorithms within OpenCV that can provide this? Or do I just write my own algorithm to measure edge strength?
推薦答案
你可以像這樣計算量級:
You can compute the magnitude like:
- 計算
dx
和dy
導數(使用cv::Sobel
) - 計算幅度
sqrt(dx^2 + dy^2)
(使用cv::magnitude
)
- Compute
dx
anddy
derivatives (usingcv::Sobel
) - Compute the magnitude
sqrt(dx^2 + dy^2)
(usingcv::magnitude
)
這是一個計算梯度大小的簡單 C++ 代碼.您可以輕松移植到 Python,因為它只是對 OpenCV 函數的幾次調用:
This is a simple C++ code that compute the magnitude of the gradient. You can easily port to Python, since it's just a few calls to OpenCV functions:
#include <opencv2/opencv.hpp>
using namespace cv;
int main()
{
//Load image
Mat3b img = imread("path_to_image");
//Convert to grayscale
Mat1b gray;
cvtColor(img, gray, COLOR_BGR2GRAY);
//Compute dx and dy derivatives
Mat1f dx, dy;
Sobel(gray, dx, CV_32F, 1, 0);
Sobel(gray, dy, CV_32F, 0, 1);
//Compute gradient
Mat1f magn;
magnitude(dx, dy, magn);
//Show gradient
imshow("Magnitude", magn);
waitKey();
return 0;
}
這篇關于在 OpenCV 中測量邊緣強度,梯度大小的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!