How could I convert this Python code into Matlab?

Hello, I'm working on edge detection and I want to convert a python code that runs perfectly to a matlab code because I can't call python from matlab since I don't have python installed in my own PC. I started converting the code but since I'm not familiar with python I had some difficulties to convert it all. Can you please help me out? This is the part of the python code I didn't know how to convert:
for r in range(iterations):
# approximate gradients
nabla
= [ ndimage.filters.convolve(u, w) for w in windows ]
# approximate diffusion function
diff
= [ 1./(1 + (n/kappa)**2) for n in nabla]
# update image
terms
= [diff[i]*nabla[i] for i in range(4)]
terms
+= [(1/(dd**2))*diff[i]*nabla[i] for i in range(4, 8)]
u
= u + delta*(sum(terms))
# Kernel for Gradient in x-direction
Kx = np.array(
[[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]], np.int32
)
# Kernel for Gradient in y-direction
Ky = np.array(
[[1, 2, 1], [0, 0, 0], [-1, -2, -1]], np.int32
)
# Apply kernels to the image
Ix = ndimage.filters.convolve(u, Kx)
Iy = ndimage.filters.convolve(u, Ky)
Please help me converting it, I tried to understand how it works in python so I convert it to matlab but I couldn't. Thank you :)

14 个评论

Good programmers are lazy. They do not repeat code or tasks that have already been solved. Installing Python is simpler than trying to reverse engineer that (already working) code...
Yes you're right it's easier to do so but I have to work with Matlab :/
In that case, I would suggest that you ask a Python forum for help in understanding the python code. What good is using some code if you don't even understand what it does? How do you know it produces the correct result for your input if you don't even know how it works?
Yes I'm not familiar with python but I saw the results in the school computer and it gave the results I want to have, and since I have to work with Matlab on my own PC I wanted to take advantage from it and convert it to Matlab rather than coding another one. I searched a lot about how to convert codes from python to matlab and I have started doing it but at some parts I didn't know how to do it that's why I asked for help here because for me, matlab forum is the best in giving precise and helpful answer.
Well, we can certainly help with writing matlab code. For help understanding python code, go to a python forum, they're the experts.
Yes thank you a lot. That's what I'm asking for. I didn't understand how to convert that python part to Matlab, so if you could help me converting it, it would be very kind of you! Thank you :)
I don't think you understood me. I can certainly help writing code in matlab as soon as you explain what the code should do. For that you need to understand the python code. I don't know python. I can guess at what it does, but the best people to ask are on a python forum.
For example, I understand that the code uses convolution but I have no idea whether the python convolve returns the full, valid, or same size convolution. A python expert can tell you that.
okay I understand, thank you.
So could you help me writing a Matlab code that do edge detecting using the anisotropic diffusion? This is what the python code is globally doing.
the material I find says that convolution is same size .
Thank you for your comment :) So if I understood correctly, I can use "conv2(A,B)" the predifined Matlab command for similar results to the python command "ndimage.filters.convolve(u, w)"?
Probably more like
conv2(A, B, 'same')
and possibly it would have to be something like
conv2(A, fliplr(flipud(B)), 'same')
you would have to test or read the details to be sure.
The material I find also talks about the origin option being important for scipy.ndimage.filters.convolve when the size of the kernel is odd, and I did not chase down to find out what the origin option corresponds to in MATLAB.
Thank you so much for your comment :)
I tried with conv2(A,B) and I think that it works right but I will try what you have suggest and compare.
My try to convert that part of the python code gave me the following :
for r = 1:iterations
for w = 1:windows
%approximate gradients
nabla = conv2(u,windows);
end
end
for n = 1:nabla
for i = 1:range(4)
%approximate diffusion function
A = (1./(1 + (n/kappa)*2));
%update image
terms = (1/(dd.^2))*A(i)*nabla(i);
end
for i = 1:range(4, 8)
u = u + delta*(sum(terms));
end
end
%Kernel for Gradient in x-direction
Kx = [-1 0 1; -2 0 2; -1 0 1];
%Kernel for Gradient in y-direction
Ky = [1 2 1; 0 0 0; -1 -2 -1];
%Apply kernels to the image
Ix = conv2(u, Kx);
Iy = conv2(u, Ky);
It seems giving the same result but I'm still working on make it better.
Thank you MATLAB forum!
We recommend against naming a variable diff as that is confusing because of the diff() function which exists in both numeric and symbolic form.
Okay I understand, I just thought it refers to diffusion, I will change it. I'm very thankful to you.

请先登录,再进行评论。

回答(1 个)

1 个评论

Thank you for your answer. I tried what it said ( system('python python_script.py');) but I had this error:
AttributeError: 'module' object has no attribute 'imread'
Could you please tell me what to do?

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Call Python from MATLAB 的更多信息

产品

版本

R2018a

提问:

2018-12-6

编辑:

2018-12-8

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by