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
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
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.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #javascript
- #code-sync
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








