Measure edge strength in OpenCV, magnitude of gradient(在 OpenCV 中测量边缘强度,梯度大小)
问题描述
我有一个需要检查相机焦点的应用程序.为此,我想在单个轴 (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 中测量边缘强度,梯度大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 OpenCV 中测量边缘强度,梯度大小


- 如何在 python3 中将 OrderedDict 转换为常规字典 2022-01-01
- 使用Heroku上托管的Selenium登录Instagram时,找不到元素';用户名'; 2022-01-01
- 沿轴计算直方图 2022-01-01
- 如何在 Python 的元组列表中对每个元组中的第一个值求和? 2022-01-01
- pytorch 中的自适应池是如何工作的? 2022-07-12
- padding='same' 转换为 PyTorch padding=# 2022-01-01
- python check_output 失败,退出状态为 1,但 Popen 适用于相同的命令 2022-01-01
- 如何将一个类的函数分成多个文件? 2022-01-01
- 分析异常:路径不存在:dbfs:/databricks/python/lib/python3.7/site-packages/sampleFolder/data; 2022-01-01
- python-m http.server 443--使用SSL? 2022-01-01