def calculate_center(image,imageMask=None): """| :param image: treated image with just the loop for finding center :type image: IplImage :returns: Coordinates from the center in pixels (x,y) """ i=0 left=-1 right=-1 up=-1 down=-1 if imageMask is None: imageMask = image while i<imageMask.width: if white_detect(imageMask[:,i])>0: left = i break i=i+1 while i<imageMask.width: if white_detect(imageMask[:,i])==0: right = i-1 break i=i+1 if right == -1: return (-1,-1) horizoncenter=right-round((right-left)/2.9) j=3 while j<image.height: if image[j,horizoncenter]>0: up = j break j=j+1 h=image.height-3 while h>j: if image[h,horizoncenter]>0: down = h break h=h-1 if down == -1: return (-2,-2) verticalcenter = down - round((down-up)/2) return (horizoncenter,verticalcenter)
def only_robot(image3): """| :param image3 : B/W Image with which it is used :type image3 : IplImage """ xcenter=image3.width ycenter=-1 i=image3.height-1 firstWhite = -1 lastWhite = -1 firstWhite2 = -1 lastWhite2 = -1 maxWhite=0 while i>=0: count=white_detect(image3[i,:]) if count>0: if firstWhite<0: firstWhite=i else: if lastWhite>0: if firstWhite2<0: firstWhite2=i if count>maxWhite: maxWhite=count else: if firstWhite>0: if lastWhite<0: lastWhite=i+1 else: if firstWhite2>0: if lastWhite2<0: lastWhite2=i+1 i=i-1 if maxWhite < (image3.width*90)//100: return (-1,-1) if firstWhite2==0 or (firstWhite2>0 and lastWhite2<0): lastWhite2=0 if firstWhite==0 or (firstWhite>0 and lastWhite<0): lastWhite=0 mid = (firstWhite+lastWhite)//2 mid2 = (firstWhite2+lastWhite2)//2 if mid2 <0: if mid > image3.height//2: ycenter = (mid+image3.height)//2 else: ycenter = mid//2 else: ycenter=(mid+mid2)//2 return (xcenter,ycenter)
def find_approx_loop(image): """| :param image : B/W Image with which is used for the approx :type image : IplImage """ i=image.width-1 while i>=0: if white_detect(image[:,i])>0: j=image.height-1 while j>=0: if image[j,i]>0: if white_detect(image[j-6:j+6,i-6:i+6])>17: return i,j j=j-1 i=i-1 return -1,-1
def face_detection(imgInfo,showVisuals=False,zoom=0): """| :param imgInfo: Information about the input image. Two types allowed yet : Image path or Numpy array :type imgInfo: String or Numpy array :param showVisuals: Display for debug :type showVisuals: Boolean :param zoom: Zoom level :type zoom: uint :returns: (label,x,y) result from loop_detection function """ image=imgInfo2cvImage(imgInfo) image2=cv.CloneImage(image) if zoom == 0: cv.AdaptiveThreshold(image2,image2,255,cv.CV_ADAPTIVE_THRESH_MEAN_C,cv.CV_THRESH_BINARY_INV,45,6) else: cv.AdaptiveThreshold(image2,image2,255,cv.CV_ADAPTIVE_THRESH_MEAN_C,cv.CV_THRESH_BINARY_INV,121,6) image3=cv.CloneImage(image2) cv.Zero(image3) storage = cv.CreateMemStorage() contours = cv.FindContours(cv.CloneImage (image2),storage,cv.CV_RETR_EXTERNAL,cv.CV_CHAIN_APPROX_SIMPLE,(0,0)) cv.DrawContours(image3,contours,cv.RGB(255,255,255),cv.RGB (255,255,255),2,cv.CV_FILLED) cv.Smooth(image3,image3,cv.CV_MEDIAN,11) image4 = cv.CloneImage(image3) if zoom == 0: erode(image4,image4,9) dilate(image4,image4,9) else: erode(image4,image4,25) dilate(image4,image4,25) count = white_detect(image4) if showVisuals: displayCv(('Resultat',image),('ErosionDilation',image4),('AdaptiveThresh',image2),('FinalTreatmentVisualisation',image3)) cv.WaitKey(0) return count
def clean_the_robot(image,imageMask): """| :param image : The image to modify :type image : IplImage :param imageMask : Image with morphological mathematic too clear the robot part :type imageMask : IplImage """ i=imageMask.width-1 wbegin=0 while i>=1: if white_detect(imageMask[:,i])>0: wbegin = 1 else: if wbegin==1: cv.Set(image[:,0:i],0) return i i=i-1 cv.Set(image[:,0:1],0) return -1
def findInternalPoints(image,image2,pointsPerArcs,center=(-1,-1)): """| :param image: Image after first treatment :type image: Opencv IplImage :param image2: Initial image treated :type image2: Opencv IplImage :param storage: Internal points of the loop :type storage: Array[(uint,uint)...(uint,uint)] :param storageInfo: Values of constant of line equation foreach internal points. a and b for an equation ax+b. :type storageInfo: Array[(float,float)...(float,float)]. :param center: Coordinate of the center of the image :type center: Tuple (uint,uint) :returns: With method LUCID_CENTER_PROC and LUCID_RECT_BBOX, return coordinates for a rectangle bounding box (xmin,ymin,xmax,ymax) :returns: With method LUCID_POLY_BBOX, return all points of the polygone """ #init list for points (storage) and for curve equation information storage = [] storageInfo = [] if center == (-1,-1): center = (image.width//2+50,image.height//2) i = center[1] while i>=0: if image[i,center[0]]>0: storage.append((center[0],i)) storageInfo.append((0,1)) break; else: i=i-1 #print storage[0][0] for degrees in np.arange(15,360,15): #for degrees in np.arange(5,360,5): x2 = storage[0][0]-center[0] y2 = storage[0][1]-center[1] xrot = x2*math.cos(toRadians(degrees))-y2*math.sin(toRadians(degrees)) yrot = y2*math.cos(toRadians(degrees))+x2*math.sin(toRadians(degrees)) xrot = round(xrot + center[0]) yrot = round(yrot + center[1]) #cv.Line( image2, (image.width//2,image.height//2), (xrot,yrot),cv.Scalar( 60, 60,60 ),2,4 ); #print "inter" if center[0]==xrot: m=1 p=0 i = center[1] while i<image.height: if image[i,center[0]]>0: storage.append((center[0],i)) storageInfo.append((m,p)) break; else: i=i+1 if i==image.height: storage.append((-1,-1)) else: m = (yrot - center[1])/(xrot - center[0]) p = -(m*xrot)+yrot #print center[0],center[1],xrot,yrot,m,p x = center[0] y = center[1] ok =False while 1: if degrees<190: x=x+1 else: x=x-1 y=m*x+p y = int(round(y)) if x<image.width-2 and x>=2 and y<image.height-2 and y>=2: if white_detect(image[(y-2):(y+2),(x-2):(x+2)])>0: storage.append((x,y)) storageInfo.append((m,p)) ok = True #cv.Circle( image,storage[0],5,cv.RGB(255,255,255 ),-1,8 ) break else: break if ok == False: x = center[0] y = center[1] while 1: if degrees<90 or degrees>270: y=y-1 else: y=y+1 if m==0: #storage.append((0,0)) break x=(y-p)/m x = int(round(x)) if x<image.width-2 and x>=2 and y<image.height-2 and y>=2: if white_detect(image[(y-2):(y+2),(x-2):(x+2)])>0: storage.append((x,y)) storageInfo.append((m,p)) #cv.Circle( image,storage[0],5,cv.RGB(255,255,255 ),-1,8 ) break else: #storage.append((0,0)) break return storage,storageInfo
def loop_detection(imgInfo,showVisuals=False,zoom=0): """| :param imgInfo: Information about the input image. Two types allowed yet : Image path or Numpy array :type imgInfo: String or Numpy array :param showVisuals: Display for debug :type showVisuals: Boolean :param zoom: Zoom level :type zoom: uint :returns: (label,x,y) result from loop_detection function """ image=imgInfo2cvImage(imgInfo) image2=cv.CloneImage(image) #if zoom == 0: cv.AdaptiveThreshold(image2,image2,255,cv.CV_ADAPTIVE_THRESH_MEAN_C,cv.CV_THRESH_BINARY_INV,45,6) #else: #cv.Threshold(image2,image2,30,255,cv.CV_THRESH_BINARY) image3=cv.CloneImage(image2) cv.Zero(image3) storage = cv.CreateMemStorage() contours = cv.FindContours(cv.CloneImage (image2),storage,cv.CV_RETR_EXTERNAL,cv.CV_CHAIN_APPROX_SIMPLE,(0,0)) cv.DrawContours(image3,contours,cv.RGB(255,255,255),cv.RGB (255,255,255),2,cv.CV_FILLED) cv.Smooth(image3,image3,cv.CV_MEDIAN,11) image4 = cv.CloneImage(image3) #check if the loop is in the image count = white_detect(image4[10:image4.height-10,10:image4.width-10]) if count < 50: return ("No loop detected",-1,-1) #dilation and erosion if there is no zoom if zoom == 0: erode(image4,image4,11) dilate(image4,image4,11) count = white_detect(image4[10:image4.height-10,10:image4.width-10]) if count < 50: (xcenter,ycenter) = find_approx_loop(image3) if showVisuals==1: cv.Line(image,(int(xcenter),int(0)),(int(xcenter),int(image.height)),cv.RGB(120.0,120.0,120.0)) cv.Line(image,(int(0),int(ycenter)),(int(image.width),int(ycenter)),cv.RGB(120.0,120.0,120.0)) displayCv(('Resultat',image),('ErosionDilation',image4),('AdaptiveThresh',image2),('FinalTreatmentVisualisation',image3)) cv.WaitKey(0) return ('ApproxCoord',xcenter-15,ycenter) if zoom == 0: (xcenter,ycenter) = only_robot(image3) if xcenter > 0: if showVisuals: cv.Line(image,(int(xcenter),int(0)),(int(xcenter),int(image.height)),cv.RGB(120.0,120.0,120.0)) cv.Line(image,(int(0),int(ycenter)),(int(image.width),int(ycenter)),cv.RGB(120.0,120.0,120.0)) displayCv(('Resultat',image),('ErosionDilation',image4),('AdaptiveThresh',image2),('FinalTreatmentVisualisation',image3)) return ("ArmRobot",xcenter,ycenter) clean_the_robot(image3,image4) adjust_detection(image3) if zoom == 0: (xcenter,ycenter) = calculate_center(image3,image3) else: (xcenter,ycenter) = calculate_center(image3,image3) if showVisuals: cv.Line(image,(int(xcenter),int(0)),(int(xcenter),int(image.height)),cv.RGB(120.0,120.0,120.0)) cv.Line(image,(int(0),int(ycenter)),(int(image.width),int(ycenter)),cv.RGB(120.0,120.0,120.0)) displayCv(('Resultat',image),('ErosionDilation',image4),('AdaptiveThresh',image2),('FinalTreatmentVisualisation',image3)) cv.WaitKey(0) return ("Coord",xcenter,ycenter)