本文介绍了Matlab中等效的matlab的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在搜索Python中的imgradient MATLAB等效项.我知道cv2.Sobel()cv2.Laplacian(),但由于imgradient在MATLAB中有效,所以它不起作用.如果我可以获取imgradient.m函数的源代码,那也将是一个很大的帮助.

I am searching for an imgradient MATLAB equivalent in Python. I am aware of cv2.Sobel() and cv2.Laplacian() but it doesn't work as imgradient works in MATLAB. If I could get source code of imgradient.m function that would also be a great help.

此外,我知道也可以使用cv2.Scharr(),但是我不确定应该在参数中输入哪些值以获得与MATLAB中的imgradient等效的结果?

Also, I know cv2.Scharr() can also be used but I am not sure what values should I put in parameter to get results equivalent to imgradient in MATLAB?

推荐答案

由于受版权保护,我们不允许从您必须在MATLAB中获得许可的任何工具箱中发布任何代码.相反,我能做的是提供执行等效操作的代码. imgradient仅返回边缘图的大小和角度.您要做的就是分别在xy方向上应用cv2.Sobel,然后自己计算大小和角度.您可以使用标准公式执行此操作:

Because of copyright, we are not allowed to post any code from any of the toolboxes that you'd have to get a license for in MATLAB. Instead what I can do is provide the code that performs the equivalent operations. imgradient simply returns the magnitude and angle of the edge map. All you need to do is apply cv2.Sobel in the x and y directions separately, then calculate the magnitude and angle yourself. You can do this using the standard formulae:

magnitude = sqrt(Gx.^2 + Gy.^2);
angle = atan2(Gy, Gx);

GxGy分别是在xy方向上的导数,或者是在每个方向上cv2.Sobel的输出.请注意,atan2将为您提供以弧度为单位的角度. MATLAB以度为单位报告角度,因此您必须另外乘以180 / pi.

Gx and Gy are the derivatives in the x and y direction respectively, or the output of cv2.Sobel for each direction. Take note that atan2 will give you angles in radians. MATLAB reports the angle in degrees so you'd have to additionally multiply by 180 / pi.

假设您的图像存储在img中.然后,在此图像上运行cv2.Sobel两次,以确保每次调用时都指定要查找的派生词的方向.之后,您可以自己计算大小和角度.因此:

Suppose your image is stored in img. You'd then run cv2.Sobel twice on this image, ensuring that with each invocation, you specify the direction of the derivative you want to find. After that, you calculate the magnitude and angle yourself. Therefore:

import cv2
import numpy as np

img = cv2.imread('....') # Read in the image
sobelx = cv2.Sobel(img,cv2.CV_64F,1,0) # Find x and y gradients
sobely = cv2.Sobel(img,cv2.CV_64F,0,1)

# Find magnitude and angle
magnitude = np.sqrt(sobelx**2.0 + sobely**2.0)
angle = np.arctan2(sobely, sobelx) * (180 / np.pi)

这篇关于Matlab中等效的matlab的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 09:06