why this code loop forever after a draw a rectangle
Gary Herron
gherron at digipen.edu
Fri Sep 16 15:21:14 EDT 2016
On 09/16/2016 05:18 AM, meInvent bbird wrote:
> i follow this post to give some time it to operate,
> wait a long time still looping
>
> http://answers.opencv.org/question/60094/libpng-warning-image-width-is-zero-in-ihdr/
>
>
> i can not stand this Ninja coding life any more,
> i have to open my code for ask this error
>
>
> import cv2
> import numpy as np
> #from matplotlib import pyplot as plt
> import time
>
> #print("1=" + str(int(sys.argv[1])))
> #print("2=" + str(int(sys.argv[2])))
> #print("3=" + str(int(sys.argv[3])))
>
> img_rgb = cv2.imread(r'C:\Users\martin\Documents\scree2.png')
> img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
> template = cv2.imread(r'C:\Users\martin\Documents\dragob.png',0)
> w, h = template.shape[::-1]
>
> res = cv2.matchTemplate(img_gray,template,cv2.TM_CCOEFF_NORMED)
> threshold = 0.64
>
> loc = np.where( res >= threshold)
> pt = [(0,0)]
>
> while not zip(*loc[::-1]):
> threshold = threshold - 0.02
> loc = np.where( res >= threshold)
>
> counter = 1
> print("threshold="+str(threshold))
> for pt2 in zip(*loc[::-1]):
> cv2.rectangle(img_rgb, pt2, (pt2[0] + w, pt2[1] + h), (0,0,255), 2)
> pt = pt2
> crop_img = img_rgb[pt[1]:(pt[1]+h), pt[0]:(pt[0]+w)]
> counter = counter + 1
>
> cv2.imwrite("C:\\Users\\tester\\Documents\\res.png",crop_img)
>
>
> #import cv2
> #winName = "Movement Indicator"
> #cv2.namedWindow(winName, cv2.WINDOW_NORMAL)
> img = cv2.imread(r'C:\Users\tester\Documents\res.png',1)
> gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
> height, width = gray.shape
> edges = cv2.Canny(gray,height,width,apertureSize = 3)
> #edges = cv2.Canny(gray,30,200)
>
> #gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
> #ret,thresh = cv2.threshold(edges.copy(),cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE,0)
> ret,thresh = cv2.threshold(edges,250,150,0)
> contours,hierarchy = cv2.findContours(thresh, 1, 2)
> #contours = sorted(contours, key = cv2.contourArea, reverse = True)[:10]
>
> 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")
> #im = img.copy()
> 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)
> #cv2.imwrite("C:\\Users\\martin\\Documents\\masda"+str(cntcounter)+".png",imi)
> #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)
>
>
>
>
>
>
>
>
> On Friday, September 16, 2016 at 7:34:04 PM UTC+8, Waffle wrote:
>> On 16 September 2016 at 14:24, meInvent bbird <jobmattcon at gmail.com> 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)
>>> --
>>> https://mail.python.org/mailman/listinfo/python-list
>> not sure but.. this bit reads really suspicious:
>>
>> while im is None:
>> time.sleep(1)
>>
>> if im is ever None then how will it ever become not None? unless there
>> is some other thread at work i can't really see this happening.
>> and if there is some other thread at work then there is probably some
>> better solution than sleep()
Reading the manual for opencv, we see that cv2.rectangle does indeed
return None:
Python: cv.Rectangle(img, pt1, pt2, color, thickness=1,
lineType=8, shift=0) → None
So the first pass through your loop does indeed set im to None with the line
im = cv2.rectangle(im.copy(), (x,y), (x+w, y+h), (0,255,0), 2)
and the next pass through the loop hits the infinite loop:
while im is None:
time.sleep(1)
--
Dr. Gary Herron
Professor of Computer Science
DigiPen Institute of Technology
(425) 895-4418
More information about the Python-list
mailing list