imgradient matlab equivalent in Python

2024/10/8 0:29:30

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.

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?

Answer

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);

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.

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 npimg = 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)
https://en.xdnf.cn/q/70180.html

Related Q&A

Error: astype() got an unexpected keyword argument categories

df = pd.DataFrame([A+, A, A-, B+, B, B-, C+, C, C-, D+, D],index=[excellent, excellent, excellent, good, good, good, ok, ok, ok, poor, poor])df.rename(columns={0: Grades}, inplace=True)dfI am trying to…

python: regular expression search pattern for binary files (half a byte)

I am using the following regular expression pattern for searching 0xDEAD4FAD in a binary file:my_pattern = re.compile(b"\xDE\xAD\x4F\xAD")but how do I generalize the search pattern for search…

pandas: selecting rows in a specific time window

I have a dataset of samples covering multiple days, all with a timestamp. I want to select rows within a specific time window. E.g. all rows that were generated between 1pm and 3 pm every day.This is a…

How to visualize (dendrogram) a dictionary of hierarchical items?

This is my first time of doing visualization from hierarchical data in dictionary format with Python. Last part of the data looks like this:d = {^2820: [^391, ^1024], ^2821: [^759, w, ^118, ^51], ^2822…

How to unfocus (blur) Python-gi GTK+3 window on Linux

What I want to do and whyI want my window to unfocus, so the previous focused window is selected.Why? I want to interact with the previously selected window (from other programs). My current plan is: …

SyntaxError: multiple exception types must be parenthesized

I am a beginner and have a problem after installing pycaw for the audio control using python, on putting the basic initialization code for pycaw, i get the following error:- Traceback (most recent call…

Python find difference between file paths

I have a bunch of file paths, such as:path1 = "./base/folder1/subfolder" path2 = "./base/folder2/"I am trying to write a function that can give me the relative difference between th…

Update range of colorbar in matplotlib

I want to update a contourf plot within a function, which works fine. However, the range of the data changes and I therefore also have to update the colorbar. That is where I fail to do so.Please see f…

Anaconda - arrow keys not work well in python shell

I installed Anaconda3 on manjaro (with i3wm and Urxvt). When I go into python interpreter, it is OK to type python script and execute. But when key arrows are pressed to call up history, everything mes…

couldnt remove origin point in matplotlib polycollection

I have tried an example with PolyCollection from matplotlib tutorials and noticed one strange thing. I couldnt remove this points from axes origin see fig. How do I manage this?from mpl_toolkits.mplot…