Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I doesn't know anything about pyhton. But I want to implement " Joachim Weickert 'Coherence-Enhancing Shock Filters' " for C++ and openCV. Only source code that I found written in pyhton.

opencv functions are OK . I can adapt but some variable structure which pyhton uses, I can't convert them.

Orginal code https://github.com/opencv/opencv/blob/3.2.0/samples/python/coherence.py, and I need only this part.

<pre lang="Python"><pre>def coherence_filter(img, sigma = 11, str_sigma = 11, blend = 0.5, iter_n = 4):
h, w = img.shape[:2]

for i in xrange(iter_n):
    print(i)

    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    eigen = cv2.cornerEigenValsAndVecs(gray, str_sigma, 3)
    eigen = eigen.reshape(h, w, 3, 2)  # [[e1, e2], v1, v2]
    x, y = eigen[:,:,1,0], eigen[:,:,1,1]

    gxx = cv2.Sobel(gray, cv2.CV_32F, 2, 0, ksize=sigma)
    gxy = cv2.Sobel(gray, cv2.CV_32F, 1, 1, ksize=sigma)
    gyy = cv2.Sobel(gray, cv2.CV_32F, 0, 2, ksize=sigma)
    gvv = x*x*gxx + 2*x*y*gxy + y*y*gyy
    m = gvv < 0

    ero = cv2.erode(img, None)
    dil = cv2.dilate(img, None)
    img1 = ero
    img1[m] = dil[m]
    img = np.uint8(img*(1.0 - blend) + img1*blend)
print('done')
return img


Especially I don't understand this part :
<pre lang="Python">eigen = eigen.reshape(h, w, 3, 2)  # [[e1, e2], v1, v2]
x, y = eigen[:,:,1,0], eigen[:,:,1,1]


and
m = gvv < 0 and after this it uses m for img1 array. img1[m] = dil[m]


Anybody can help about this?

What I have tried:

I can convert opencv functions but I stucked at arrays.
Posted
Updated 28-Aug-20 7:00am
Comments
Richard MacCutchan 28-Aug-20 10:37am    
You cannot convert that to C++ as there is no direct equivalent code. You first need to understand what the program is trying to do, you can then research how to do the same in C++.

This is not a code conversion service: we are not here to translate code for you.
Even if we did, what you would end up with would not be “good code” in the target language – they are based on very different frameworks, and what makes something work in one language does not always “translate” directly into another.
So what you end up with is very poor code, that is difficult if not impossible to maintain, that can’t be upgraded nicely, and that will cause you immense headaches if the original is changed. And it’ll be a nightmare to debug if it doesn’t work “straight out of the box”.
Instead, use the source code as a specification for a new app written in and for the target language / framework and write it w=from scratch using the original as a “template”. You will get a much, much better result that will save you a lot of time in the long run.
 
Share this answer
 
Comments
wiseman s 28-Aug-20 10:03am    
Thank you for your comment. But all I want is understand this part;
x, y = eigen[:,:,1,0], eigen[:,:,1,1]
and m = gvv < 0 means. I don't ask for a complete code conversion :)
Dave Kreskowiak 28-Aug-20 11:32am    
Well, you got the answer you did because, almost without fail, the people asking "How do I convert this code ..." are student who are trying to breeze through an assignment without doing any of the actual work of figuring out an algorithm and writing some code.

The standard answer to questions like this is you have to completely understand the code you're converting and the language it's written in and completely understand the language you're trying to rewrite it in. This includes any libraries the source code is using and any available equivalent libraries or functionality in the destination language. And, frankly, if you knew those things, you wouldn't be asking the "how do I convert ..." question in the first place.
To start with, I suggest to find C++ equivalents for Python libraries which you do not mention in your sample ... like this equivalent of Numpy

GitHub - dpilger26/NumCpp: C++ implementation of the Python Numpy library[^]

Researching needs to be done for all lines of your Python code, as mentioned in other posts over here ...

and ... it seems that there exists a C++ implementation over here
utils/coherence_filter.hpp · fdde4b7614968cb465b642955789a171b63d3b53 · Franziska Kahlert / study thesis · GitLab[^]
Perhaps this helps
Cheers
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900