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
Stephen23
2018년 12월 6일
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...
Nour Sd
2018년 12월 6일
Guillaume
2018년 12월 6일
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?
Nour Sd
2018년 12월 6일
Guillaume
2018년 12월 6일
Well, we can certainly help with writing matlab code. For help understanding python code, go to a python forum, they're the experts.
Nour Sd
2018년 12월 6일
Guillaume
2018년 12월 6일
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.
Nour Sd
2018년 12월 6일
Walter Roberson
2018년 12월 8일
the material I find says that convolution is same size .
Nour Sd
2018년 12월 8일
Walter Roberson
2018년 12월 8일
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.
Walter Roberson
2018년 12월 8일
We recommend against naming a variable diff as that is confusing because of the diff() function which exists in both numeric and symbolic form.
Nour Sd
2018년 12월 8일
답변 (1개)
madhan ravi
2018년 12월 6일
1 개 추천
카테고리
도움말 센터 및 File Exchange에서 Call Python from MATLAB에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!