Gupta

Gupta

Problem updating a variable in a loop

How i acn upadate the value of the variable “square=0” as mention in my code so that after every loop it will update it by addind + 1 in “square=0”

this is my code :

def findcontours(path) do
    #squares=0
    {:ok, imgGrey} = OpenCV.imread(path, flags: OpenCV.cv_IMREAD_GRAYSCALE())
    {:ok, {_, thrash}} = OpenCV.threshold(imgGrey, 240, 255, OpenCV.cv_THRESH_BINARY())
    {:ok, {contours, _}} =OpenCV.findContours(thrash, OpenCV.cv_RETR_TREE(), OpenCV.cv_CHAIN_APPROX_NONE())

    square = 0
    for contour <- contours do
      {:ok,peri} = OpenCV.arcLength(contour,true)
      {:ok,approx} = OpenCV.approxPolyDP(contour, 0.01 * peri,true)
      ap=OpenCV.Nx.to_nx(approx)
      {len,_,_}=Nx.shape(ap)


      if len==4 do #to bound squares from the countor
        {:ok,{x1, y1, w, h}}= OpenCV.boundingRect(approx)
        aspectRatio = (w/1)/h
        if aspectRatio >= 0.95 and aspectRatio <= 1.05 and (y1+h)*(x1+w) < 985000 do
          square=square +1
        end
      end
    end
  end

pls help me to update my value of square as i know there i a big problem of scope in elixir and also it is immutable so pls help me for this.

or suggest me another way so that i can do the whole process by another way if possible.

First Post!

Aetherus

Aetherus

In Elixir, variables are actually constants. You can’t reassign values to an existing variable. You can only create a new variable with the same name and shadow the old one.

This line square = square + 1 reads the value of square outside the for-loop, adds 1 to it, and creates a new variable also named square, which is not used at all, because this new square variable is inside a sub-sub scope of the for-loop, and the old square is outside the for-loop. They are in different scopes.

How to fix this?

square = for contour <- contours, reduce: 0 do
  acc ->
    {:ok,peri} = OpenCV.arcLength(contour,true)
    {:ok,approx} = OpenCV.approxPolyDP(contour, 0.01 * peri,true)
    ap=OpenCV.Nx.to_nx(approx)
    {len,_,_}=Nx.shape(ap)

    if len==4 do #to bound squares from the countor
      {:ok,{x1, y1, w, h}}= OpenCV.boundingRect(approx)
      aspectRatio = (w/1)/h
      if aspectRatio >= 0.95 and aspectRatio <= 1.05 and (y1+h)*(x1+w) < 985000 do
        acc + 1
      else
        acc
      end
    else
      acc
    end
end

Most Liked

Aetherus

Aetherus

Here’s a simple example of the for comprehension with the option :reduce:

for n <- 1..10, reduce: 0 do
  acc -> acc + n
end

# returns 55

It’s equivalent to this code (I’m using the same variable names as in the snippet above):

Enum.reduce(1..10, 0, fn n, acc ->
  acc + n
end)

The for comprehension first passes 0, the value of the option :reduce, to acc, the first element in 1..10 to n, and apply the anonymous function to acc and n. Then it passes the return value of the anonymous function to acc, the second element in 1..10 to n, and apply the anonymous function again. So on and so forth, until there’s no element to take from 1..10, then it returns the return value of the last call of that anonymous function.

Aetherus

Aetherus

And note that both for ... do ... end and if ... else ... end are expressions, and they have return values.

The if without else returns whatever inside the if block returns, if the condition is met, otherwise, it returns nil, which usually is not what you want, so be careful.

Last Post!

Gupta

Gupta

now its working

Where Next?

Popular in Questions Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
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
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New

Other popular topics Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
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

We're in Beta

About us Mission Statement