How to find contours in image using evision and nx?

How to detect square region in an image using nx?

@Manish, welcome to the forum. You’ll find this a very welcoming and supportive community. However, this question is far too broad encourage community engagement. You are encouraged to show what you have tried, what you are trying to achieve and to be clearer about what specifically you are asking for help with.

If you’ve not got any background in image processing then I suggest reading a few articles on “edge detection”. Then look at the OpenCV functions for edge detection which map to the eVision/OpenCV ones.


@kip I am trying to detect all the squares in a sudoku image so that I can crop them individually.

if this the input image
output should be all the blocks saved as separate images

@Manish did you found any way!?


I am new to Elixir and facing problem with Evision library, would you please help me out. i need to read image using Evision and then crop the image so can do that.

Thanking you,

I apologise if i sound rude, i did not know english that well may be this is why i sounds rude.

here same we got the error in the elixir in python we wrote like this
in elixir we tried to give but in sort 2 nd parameter is doubted sir in Evision hexdoxs we tried to see the example but in new version they dont show sir

location = None
# Finds rectangular contour
for contour in contours:
approx = cv2.approxPolyDP(contour, 15, True)
if len(approx) == 4:
location = approx

could you please help us sir

Import cv2

# Load image, convert to grayscale, and find edges
image = cv2.imread('1.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_OTSU + cv2.THRESH_BINARY)[1]

# Find contour and sort by contour area
cnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
cnts = sorted(cnts, key=cv2.contourArea, reverse=True)

# Find bounding box and extract ROI
for c in cnts:
    x,y,w,h = cv2.boundingRect(c)
    ROI = image[y:y+h, x:x+w]


I have merged questions related to evision into one topic… all related to the same e-yantra challenge.

By the way, You should at least provide some of your own code to increase the chance to have an answer. Asking for a direct answer might not work.

Hi all, welcome to elixir community.

Are these queries part of some course work or assignment?

1 Like

In python/OpenCV the edges of a contour are calculated using contourArea, how do we do the same in Evision?. Is it possible to get the edges of box?

Here is the python example:

contours = sorted(contours, key=cv2.contourArea, reverse=True)[:15]
location = None
# Finds rectangular contour
for contour in contours:
  approx = cv2.approxPolyDP(contour, 15, True)
  if len(approx) == 4:
    location = approx
1 Like

Some helpful resources for evision…


These queries are related to an ongoing competition organised by e-yantra.

You can use the same function in evision, Evision.contourArea/1.

Is it possible to get the edges of box?

It is possible. And it’s still the same function, Evision.approxPolyDP/3.

1 Like

I have tried various methods of finding contours of an image in elixir and i am still stuck at the same point. This is the error it shows when I tried to use findContours function in Evision Library. Can someone please help me out.

The error message already tells you your issue. The first parameter cannot be a path – you’re passing a path.

You likely want OpenCV.findContours(im, … not OpenCV.findContours(image, …

Segmentation fault (core dumped)

0.1.7 is outdated, please use the latest one.

Other than that, Evision.findContours/3 expects the first argument, im (in your case), to be a 1-channel image. That means the coloured 3-channel image must be processed to a binary(1-channel) image. That’s one reason why you got a segmentation fault. (the other reason is that 0.1.7 is outdated and you won’t get a segmentation fault in the latest version even if you passed in an unexpected input.)

For your reference, Evision.threshold/{4,5} is a frequently used technique to get you a binary image. And of course, the value of the second parameter thresh (in Evision.threshold) could vary a lot based on your source image like its brightness and contrast.

Also, please read the inline docs. Pulling up the inline docs and presenting them in a friendly way is a built-in feature of IEx. It only requires one to type h ModuleName.FunctionName. For example

iex> h Evision.findContours

                     def findContours(image, mode, method)

  @spec findContours(Evision.Mat.maybe_mat_in(), integer(), integer()) ::
          {[Evision.Mat.t()], Evision.Mat.t()} | {:error, String.t()}

Finds contours in a binary image.

##### Positional Arguments

  • **image**: `Evision.Mat`.
    Source, an 8-bit single-channel image. Non-zero pixels are treated as 1's.
    Zero pixels remain 0's, so the image is treated as binary . You can use
    #compare, #inRange, #threshold , #adaptiveThreshold, #Canny, and others to
    create a binary image out of a grayscale or color one. If mode equals to
    #RETR_CCOMP or #RETR_FLOODFILL, the input can also be a 32-bit integer
    image of labels (CV_32SC1).

  • **mode**: `int`.
    Contour retrieval mode, see #RetrievalModes

  • **method**: `int`.
    Contour approximation method, see #ContourApproximationModes

##### Keyword Arguments

In latest version.0.1.12
Ehen i use imshow throwing some error
But when imshow in 0.1.0 its working fine!?