cocoa-xu / evision

Evision: An OpenCV-Erlang/Elixir binding
https://evision.app
Apache License 2.0
337 stars 22 forks source link

small doubt regarding contours in evision #114

Closed Jagan3534 closed 1 year ago

Jagan3534 commented 1 year ago

i get the values of findContours after getting is their any method to seperate contours using in built function like in python imutil.grabcontours

the code is here

`def grab_contours(cnts):
    # if the length the contours tuple returned by cv2.findContours
    # is 2 then we are using either OpenCV v2.4, v4-beta, or
    # v4-official
    if len(cnts) == 2:
        cnts = cnts[0]

    # if the length of the contours tuple is 3 then we are using
    # either OpenCV v3, v4-pre, or v4-alpha
    elif len(cnts) == 3:
        cnts = cnts[1]

    # otherwise OpenCV has changed their cv2.findContours return
    # signature yet again and I have no idea WTH is going on
    else:
        raise Exception(("Contours tuple must have length 2 or 3, "
            "otherwise OpenCV changed their cv2.findContours return "
            "signature yet again. Refer to OpenCV's documentation "
            "in that case"))

    # return the actual contours array
    return cnts`

could please help us sir @cocoa-xu @josevalim

cocoa-xu commented 1 year ago

Please use the ElixirForum for questions/help/feedback, where a wider community will be able to help you. Thank you!

Jagan3534 commented 1 year ago

sorry sir i think my question is not understand clear sir already i divided using {con,hei} = contours in shell i gave input like this but in color it getting error i check in evision but the example code in github its not working sir OpenCV.drawContours(img,key,-1,(0,255,0),-1) but my doubt is like in drawContour its getting an error like
image

cocoa-xu commented 1 year ago
  1. because color should be a tuple, like (0,255,0) is a tuple in python, {0,255,0} is the corresponding tuple in elixir
  2. the fifth argument is wrong
cocoa-xu commented 1 year ago
  1. the fifth argument

I guess you want to specify thickness, so:

Evision.drawContours(src, contours, index, {0, 0, 255}, thickness: 2)

The example works, I tested it again. And a minimal one.

import Bitwise

# Load image in color
src = Evision.imread("pca_test.jpg")

# Load image in grayscale
gray = Evision.imread("pca_test.jpg", flags: Evision.cv_IMREAD_GRAYSCALE())

# binarization
{_, bw} = Evision.threshold(gray, 50, 255, Evision.cv_THRESH_BINARY() ||| Evision.cv_THRESH_OTSU())

# Find all the contours in the thresholded image
{contours, _} = Evision.findContours(bw, Evision.cv_RETR_LIST(), Evision.cv_CHAIN_APPROX_NONE())

contours =
  contours
  # Calculate the area of each contour
  |> Enum.map(&{Evision.contourArea(&1), &1})
  # Ignore contours that are too small or too large
  |> Enum.reject(fn {area, _c} -> area < 100 or area > 100_000 end)
contours = Enum.map(contours, &elem(&1, 1))

# Draw each contour
src =
  for index <- 0..(Enum.count(contours) - 1), reduce: src do
    src ->
      Evision.drawContours(src, contours, index, {0, 0, 255}, thickness: 2)
  end
Jagan3534 commented 1 year ago

thanks a lot sir

creddyravindra commented 1 year ago

hello sir, we have done it in python, how to we use sorted(contours, key=cv2.contourArea, reverse=True)[:15] in elixir, is there any built in function to seperate edges in evision


location = None
# Finds rectangular contour
for contour in contours:
  approx = cv2.approxPolyDP(contour, 15, True)
  if len(approx) == 4:
    location = approx
    break```
kipcole9 commented 1 year ago

This issue tracker is for reporting bugs. Please use Elixir Forum for help requests. And why are you calling Cocoa "sir"?

Jagan3534 commented 1 year ago

Reply Removed