why this code loop forever after a draw a rectangle

meInvent bbird jobmattcon at gmail.com
Fri Sep 16 19:46:31 EDT 2016


thank you very much,
it out of the loop now.
because drawLine function return things

i just change drawLine to rectangle,
have not thought that rectangle not return thing, just edit the parameter



On Saturday, September 17, 2016 at 5:26:53 AM UTC+8, MRAB wrote:
> On 2016-09-16 20:14, Gary Herron wrote:
> > On 09/16/2016 04:24 AM, meInvent bbird wrote:
> >> im = img.copy()
> >> cntcounter = 0
> >> for cnt in contours:
> >>          epsilon = 0.1*cv2.arcLength(cnt,True)
> >>          approx = cv2.approxPolyDP(cnt,epsilon,True)	
> >>          #peri = cv2.arcLength(cnt, True)
> >>          #approx = cv2.approxPolyDP(c, 0.5 * peri, True)
> >>          #print("len(approx)="+str(len(approx)))
> >>          if len(approx) == 4:
> >>              print("approx=" + str(approx))
> >>              cntcounter = cntcounter + 1
> >>              print("here1")
> >>              x,y,w,h = cv2.boundingRect(cnt)
> >>              print("here2")
> >>              while im is None:
> >>                  time.sleep(1)
> >>              if im is not None:
> >>                  print("here3")
> >>                  im = cv2.rectangle(im.copy(), (x,y), (x+w, y+h), (0,255,0), 2)
> >>                  #im = cv2.line(im,(x,y),(x+w,y),(0,255,0),2)
> >>                  #im = cv2.line(im,(x+w,y),(x+w,y+h),(0,255,0),2)
> >>                  #im = cv2.line(im,(x,y+h),(x+w,y+h),(0,255,0),2)
> >>                  #im = cv2.line(im,(x,y),(x,y+h),(0,255,0),2)
> >>
> >>
> >> cv2.imwrite(r'C:\Users\tester\Documents\masda.png',im)
> >
> >
> > These two lines:
> >
> >    while im is None:
> >      time.sleep(1)
> >
> > are an infinite loop if im is None;
> >
> >
> > Since you haven't told us what im (or img, contours, cv2) are, I can't
> > tell how im might become None, but it does look like you (confusingly)
> > use im for two different things:  an img.copy() and a cv2.rectangle,
> > whatever those may be.
> >
> > Pure guesswork:  if cv2.rectangle draws a rectangle, what does it
> > return?  If it doesn't return anything, the line
> >      im = cv2.rectangle(...)
> > is how im gets the value of None.
> >
> It looks like the OP is using OpenCV.
> 
> You're right about cv2.rectangle; it does return None.
> 
> The line:
> 
>      im = cv2.rectangle(im.copy(), (x,y), (x+w, y+h), (0,255,0), 2)
> 
> makes a copy of the image im, draws a rectangle on it, and then binds 
> None to im.
> 
> The copied rectangle is discarded because there's no reference to it, so 
> the entire line in pointless.
> 
> It basically does the same thing as:
> 
>      im = None
> 
> only slower!




More information about the Python-list mailing list