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
dxanddyderivatives (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 中测量边缘强度,梯度大小
基础教程推荐
- 在Python中从Azure BLOB存储中读取文件 2022-01-01
- 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01
- PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01
- Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01
- 包装空间模型 2022-01-01
- 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01
- 修改列表中的数据帧不起作用 2022-01-01
- 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01
- 求两个直方图的卷积 2022-01-01
- PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01
