简体   繁体   English

Python中的imgradient matlab等价物

[英]imgradient matlab equivalent in Python

I am searching for an imgradient MATLAB equivalent in Python. 我在Python中搜索一个imgradient MATLAB等价物。 I am aware of cv2.Sobel() and cv2.Laplacian() but it doesn't work as imgradient works in MATLAB. 我知道cv2.Sobel()cv2.Laplacian()但它不能在MATLAB中作为imgradient工作。 If I could get source code of imgradient.m function that would also be a great help. 如果我能获得imgradient.m函数的源代码,这也将是一个很大的帮助。

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? 另外,我知道也可以使用cv2.Scharr()但是我不确定我应该在参数中放入什么值来获得与MATLAB中的imgradient相当的结果?

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. 由于版权,我们不允许从您必须在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. imgradient只返回边缘图的大小和角度。 All you need to do is apply cv2.Sobel in the x and y directions separately, then calculate the magnitude and angle yourself. 您需要做的就是cv2.Sobelxy方向上应用cv2.Sobel ,然后自己计算幅度和角度。 You can do this using the standard formulae: 您可以使用标准公式执行此操作:

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

Gx and Gy are the derivatives in the x and y direction respectively, or the output of cv2.Sobel for each direction. GxGy分别是xy方向的导数,或者是每个方向的cv2.Sobel的输出。 Take note that atan2 will give you angles in radians. 请注意, atan2将以弧度为单位给出角度。 MATLAB reports the angle in degrees so you'd have to additionally multiply by 180 / pi . MATLAB以度为单位报告角度,因此您必须另外乘以180 / pi

Suppose your image is stored in img . 假设您的图像存储在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. 然后,您将在此图像上运行两次cv2.Sobel ,确保在每次调用时指定要查找的派生方向。 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM