Gupta

Gupta

How to Convert Python program to Elixir code

How to convert following code to elixir code.
I have made this program to detect digit and its position in a grid.

I know how to make simple elixir programs but I dont know how to make such complex programs in elixir because I dont know the syntax and about the libraries used in elixir.

I also searched about evision but very less reference I got, but not sufficient…

Here is my Python code:-

img = cv2.imread('digit_1.jpeg')
imgGrey = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
_, thrash = cv2.threshold(imgGrey, 240, 255, cv2.THRESH_BINARY)
contours, _ = cv2.findContours(thrash, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
squares=0
for contour in contours:
    approx = cv2.approxPolyDP(contour, 0.01* cv2.arcLength(contour, True), True)
    if len(approx)==4:
    
        cv2.drawContours(img, [approx],0, (255, 0, 0),2)
    x = approx.ravel()[0]
    y = approx.ravel()[1] - 5
    if len(approx) == 4:
        x1 ,y1, w, h = cv2.boundingRect(approx)
        
        aspectRatio = float(w)/h
        if aspectRatio >= 0.95 and aspectRatio <= 1.05 and (y1+h)*(x1+w) <985000:
            squares +=1
#cv2.imshow("img", img)
for contour in contours:
    approx = cv2.approxPolyDP(contour, 0.01* cv2.arcLength(contour, True), True)
    if len(approx)==4:
        cv2.drawContours(img, [approx],0, (255, 0, 0),2)
    x = approx.ravel()[0]
    y = approx.ravel()[1] - 5
    if len(approx) == 4:
        x1 ,y1, w, h = cv2.boundingRect(approx)

Please help me to convert this code into elixir code.

First Post!

dimitarvp

dimitarvp

That’s not just converting Python to Elixir code, you also need an image processing library.

This thread has a few references: Image processing library

Though I’d probably prefer the one described here: Image - an image processing library based upon Vix

…and by the same author, it even mentions eVision: GitHub - elixir-image/image: Image processing for Elixir · GitHub

Most Liked

LostKobrakai

LostKobrakai

The docs should provide that:

cocoa

cocoa

As shown in your code, the python code is drawing on a copy of the original image (3-channel BGR) while you were drawing contours on the thresholded image (1-channel image).

So you can either draw contours on the original BGR image or change the color from {0, 255, 0} to maybe {128}, but it could be very hard to see the color {128} in a gray scale image.

Last Post!

cocoa

cocoa

For question 1, Nx.slice can only slice a rectangular area for you. To get the puzzle, you can use Evision.warpPerspective. There is an example of this function, evision/examples/warp_perspective.livemd at main · cocoa-xu/evision · GitHub.

For question 2, there could be 2 possible reasons,

  • perhaps you forgot to change the second parameter of Evision.findContours/3 to Evision.cv_RETR_EXTERNAL()
  • you will need to find the contour that has shape {4, 1, 2}. And based on the sample image, I guess you were following a blog post and if so, I think you missed some intermediate steps.

Where Next?

Popular in Questions Top

joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 130579 1222
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 44608 311
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44167 214
New

We're in Beta

About us Mission Statement