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

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
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
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
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
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
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New

Other popular topics Top

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
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
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
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
marick
I had some trouble figuring out how to make many-to-many associations work. Once I got it working, I wrote a blog post. Because I’m a nov...
New
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 126479 1222
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

We're in Beta

About us Mission Statement