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.

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.

Where Next?

Popular in Questions Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
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
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
Phillipp
Hey, I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
gonzofish
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
New
chewm
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

Other popular topics Top

William
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
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
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
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
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

We're in Beta

About us Mission Statement