def size_image(img, imgsize):
    # check if we need to crop out ROI
    roiWidth = img.width
    roiHeight = img.height
    if (img.width > imgsize[1]):
        roiWidth = imgsize[1]

    if (img.height > imgsize[0]):
        roiHeight = imgsize[0]

    roi = (0, 0, roiWidth, roiHeight)
    cv.SetImageROI(img, roi)
    imgTrim = cv.CreateImage((roi[2], roi[3]), img.depth, img.nChannels)
    cv.Copy(img, imgTrim)

    # check if we need to pad
    padSize = 0
    padSize = max(padSize, imgsize[0] - imgTrim.height)
    padSize = max(padSize, imgsize[1] - imgTrim.width)

    if padSize == 0:  # no padding needed
        return imgTrim
    else:
        padSize = int(round((padSize + .5) / 2.))
        # copy make border
        imgPad = cv.CreateImage(
            (imgTrim.width + 2 * padSize, imgTrim.height + 2 * padSize),
            img.depth, img.nChannels)
        cv.CopyMakeBorder(imgTrim, imgPad, (0, 0), 0)
        roi = (0, 0, imgsize[1], imgsize[0])
        cv.SetImageROI(imgPad, roi)
        imgFinal = cv.CreateImage((roi[2], roi[3]), img.depth, img.nChannels)
        cv.Copy(imgPad, imgFinal)
        return imgFinal
Exemplo n.º 2
0
def publish_debug(img, results):
    imgsize = cv.GetSize(img)
    sizelist = [cv.GetSize(tmp[1]) for tmp in templates]
    width = max(imgsize[0], sum([s[0] for s in sizelist]))
    height = imgsize[1] + max([s[1] for s in sizelist])
    output = cv.CreateImage((width, height), cv.IPL_DEPTH_8U, 1)
    cv.Zero(output)
    cur_x = 0

    view_rect = (0, height-imgsize[1], imgsize[0], imgsize[1])
    cv.SetImageROI(output, view_rect)
    cv.Copy(img, output)
    for template in templates:
        size = cv.GetSize(template[1])
        cv.SetImageROI(output, (cur_x, 0, size[0], size[1]))
        cv.Copy(template[1], output)
        cur_x += size[0]

        #cv.PutText(output, tempname, (0,size[1]-16), font, cv.CV_RGB(255,255,255))
        #cv.PutText(output, str(tempthre)+'<'+str(status[1]), (0,size[1]-8), font, cv.CV_RGB(255,255,255))
        for _,status in [s for s in results if s[0] == template[3]]:
            print status
            cv.PutText(output, template[3], (0,size[1]-42), font, cv.CV_RGB(255,255,255))
            cv.PutText(output, "%7.5f"%(status[0]), (0,size[1]-24), font, cv.CV_RGB(255,255,255))
            cv.PutText(output, "%7.5f"%(status[2]), (0,size[1]-8), font, cv.CV_RGB(255,255,255))
            if status[3] : 
                cv.Rectangle(output, (0, 0), size, cv.RGB(255,255,255), 9)
        cv.SetImageROI(output, view_rect)
        for _,status in [s for s in results if s[0] == template[3]]:
            pt2 = (status[1][0]+size[0], status[1][1]+size[1])
            if status[3] : 
                cv.Rectangle(output, status[1], pt2, cv.RGB(255,255,255), 5)

    cv.ResetImageROI(output)
    debug_pub.publish(bridge.cv_to_imgmsg(output, encoding="passthrough"))
Exemplo n.º 3
0
def scale_32f_image(image):
    '''
    Scales the given cv.IPL_DEPTH_32F type image to an 8 bit image so that the
    smallest value maps to 0 and the largest maps to 255.  Used for displaying
    debugging images.

    Processes each channel separately, which can produce some useful, but
    esoteric results.

    '''
    if image.depth != cv.IPL_DEPTH_32F:
        return image
    result = cv.CreateImage(cv.GetSize(image), 8, image.channels)
    channel_image = cv.CreateImage(cv.GetSize(image), cv.IPL_DEPTH_32F, 1)
    channel_scaled = cv.CreateImage(cv.GetSize(image), 8, 1)
    for channel_num in xrange(1, image.channels + 1):

        cv.SetImageCOI(image, channel_num)
        cv.Copy(image, channel_image)
        minmaxloc = cv.MinMaxLoc(channel_image)
        minimum = minmaxloc[0]
        maximum = minmaxloc[1]
        if maximum - minimum > 0:
            cv.ConvertScale(channel_image, channel_scaled,
                            255 / (maximum - minimum),
                            -255 / (maximum - minimum) * minimum)
        else:
            cv.ConvertScale(channel_image, channel_scaled, 0, -255 / minimum)

        cv.SetImageCOI(result, channel_num)
        cv.Copy(channel_scaled, result)

    cv.SetImageCOI(image, 0)
    cv.SetImageCOI(result, 0)
    return result
Exemplo n.º 4
0
  def processMotion(self):
    """
    Take a raw input image frame from the camera and perform motion detection using
    the current frame plus considering several previous frames and return the CV
    image that should be given to the Region network.
    """
    #find motion image, then find feature corners from that
    if self._prevIplImage:
      cv.AbsDiff(self._inputImage, self._prevIplImage, self._diffImage)
    else:
      cv.Copy(self._inputImage, self._diffImage)
      
    cv.Copy(self._inputImage, self._prevIplImage) #save as t-1 image for next frame
    
    #(src, dest, threshold, maxVal, type)
    cv.Threshold(self._diffImage, self._threshImage, 16.0, 255.0, cv.CV_THRESH_BINARY)
    
    #For now, disable segmentMotion and return all motion in frame...
    if self._threshImage!=None:
      return (0,0, self._threshImage.width, self._threshImage.height)
    
    ###Experimental: segment motion to return only the most 'interesting' area
    tsec = clock()
    #(silhouette, mhi, timestamp, duration)
    cv.UpdateMotionHistory(self._threshImage, self._historyImage, tsec, 0.3)

    #(mhi, segMask, storage, timestamp, segThresh)
    #return: [tuple(area, value, rect)], (float, CvScalar, CvRect)
    seqs = cv.SegmentMotion(self._historyImage, self._segMaskImage, \
                            self._memStorage, tsec, 1.0)
    
    #cv.Copy(self._threshImage, self._inputImage)
    #cv.Threshold(self._segMaskImage, self._threshImage, 0.0, 250.0, CV_THRESH_BINARY)
    
    rects = []
    for seq in seqs:
      seqRect = seq[2] #CvRect = tuple (x, y, width, height)
      if(seqRect[2] > 4 and seqRect[3] > 4):
        rects.append(seqRect)
    
    #find the 3rd largest area and only keep those rects
    if len(rects) > 0:
      areas = [x[2]*x[3] for x in rects]
      areas.sort()
      minArea = areas[0]
      if len(areas) > 1:
        minArea = areas[len(areas)-1]
      
      rectsOk = [x for x in rects if x[2]*x[3] >= minArea]
      rect = rectsOk[0]
      
      #center the largest rect
      cRect = (rect[0]+(rect[2]/2), rect[1]+(rect[3]/2))
      return rect
    
    return None #none means no motion bounding box detected
Exemplo n.º 5
0
 def observation(self, meas):
     meas = np.float32([meas])
     if not self.initialized:
         cv.Copy(cv.fromarray(meas.T.copy()), self.kf.state_post)
         cv.Copy(cv.fromarray(meas.T.copy()), self.kf.state_pre)
         self.initialized = True
     if self.kf.CP == 0:
         cv.KalmanPredict(self.kf)
     corrected = np.asarray(
         cv.KalmanCorrect(self.kf, cv.fromarray(meas.T.copy())))
     return corrected.T[0].copy()
Exemplo n.º 6
0
def capture_draw():
    img = cv.QueryFrame(capture)
    # scale your big ole face down to something small
    thumb = cv.CreateMat(img.height / SCALE, img.width / SCALE, cv.CV_8UC3)
    cv.Resize(img, thumb)
    faces = get_face_roi(thumb)
    for (x, y, w, h), n in faces:
        temp_offset = (x * SCALE, y * SCALE)
        cv.SetImageROI(img,
                       ((x) * SCALE, (y) * SCALE, (w) * SCALE, (h) * SCALE))
        roi_image = cv.CreateImage(cv.GetSize(img), img.depth, img.nChannels)
        cv.Copy(img, roi_image)
        cv.ResetImageROI(img)

        cv.Rectangle(img, (x * SCALE, y * SCALE),
                     (x * SCALE + w * SCALE, y * SCALE + h * SCALE),
                     (255, 0, 0))
        cv.PutText(img, 'face', (x * SCALE, y * SCALE), font, (200, 200, 200))

        FEATURE_SCALE = (float(roi_image.width) / ROI_TARGET_SIZE[0],
                         float(roi_image.height) / ROI_TARGET_SIZE[1])
        roi_thumb = cv.CreateImage((int(roi_image.width / FEATURE_SCALE[0]),
                                    int(roi_image.height / FEATURE_SCALE[1])),
                                   cv.IPL_DEPTH_8U, 3)
        cv.Resize(roi_image, roi_thumb)

        features = get_features(roi_thumb)
        cv.ShowImage("ROI", roi_image)
        for name in features:
            if features[name] != None:
                for (x1, y1, w1, h1), n1 in features[name]:
                    cv.SetImageROI(
                        roi_image,
                        (x1 * FEATURE_SCALE[0], y1 * FEATURE_SCALE[1],
                         w1 * FEATURE_SCALE[0], h1 * FEATURE_SCALE[1]))
                    feature_image = cv.CreateImage(cv.GetSize(roi_image),
                                                   roi_image.depth,
                                                   roi_image.nChannels)
                    cv.Copy(roi_image, feature_image)
                    cv.ResetImageROI(feature_image)
                    cv.ShowImage(name, feature_image)
                    cv.PutText(img, name,
                               (temp_offset[0] + x1 * FEATURE_SCALE[0],
                                temp_offset[1] + y1 * FEATURE_SCALE[1]), font,
                               (200, 200, 200))
                    cv.Rectangle(
                        img, (temp_offset[0] + x1 * FEATURE_SCALE[0],
                              temp_offset[1] + y1 * FEATURE_SCALE[1]),
                        (temp_offset[0] +
                         (x1 + w1) * FEATURE_SCALE[0], temp_offset[1] +
                         (y1 + h1) * FEATURE_SCALE[1]), (0, 255, 255))
    cv.ShowImage("Whole Image", img)
Exemplo n.º 7
0
def merge_images(img1, img2, vertical=None):
    w, h = sizeOf(img2)
    new_size, second_roi = ((w * 2, h),
                            (w, 0, w,
                             h)) if h * 1.3 > w and not vertical else ((w,
                                                                        h * 2),
                                                                       (0, h,
                                                                        w, h))
    merged = cv.CreateImage(new_size, img1.depth, img1.channels)
    cv.SetImageROI(merged, (0, 0, w, h))
    cv.Copy(img1, merged)
    cv.SetImageROI(merged, second_roi)
    cv.Copy(img2, merged)
    cv.ResetImageROI(merged)
    return merged
Exemplo n.º 8
0
    def find_blob(self):

        #Convert Incoming image
        #self.image = self.bridge.imgmsg_to_cv(data, "bgr8")

        #self.image = cv.QueryFrame(self.capture)

        cv.Copy(self.image_color, self.image)

        #These shenanigans are to get input from the keyboard, cv.WaitKey gives
        #back the ascii value of the key pressed in the low 8 bits, but the rest
        #of the higher bits can be anything apparently.
        key_press_raw = cv.WaitKey(5)  #gets a raw key press
        key_press = key_press_raw & 255  #sets all but the low 8 bits to 0

        self.check_key_press(key_press)

        #Create the thresholded image
        self.threshold = self.create_image()

        #Find blob regions, draw a rectangle around the largest and a circle at
        #its center.
        self.find_regions()

        #Display the images
        cv.ShowImage('image', self.image)

        cv.ShowImage('threshold', self.threshold)

        cv.ShowImage('hsv', self.hsv)
Exemplo n.º 9
0
	def rotacion_y_posicion_robot(self,robo_x=200,robo_y=100,robo_th=80):
		"""
		\brief graficar el robot en el lienzo de mapa 
		\param self no se necesita incluirlo al utilizar la funcion ya que se lo pone solo por ser la definicion de una clase
		\param robo_x coordenada x de la odometria del robot 
		\param robo_y coordenada y de la odometria del robot 
		\param robo_th Valor Th del robot
		\return Nada
		"""
		image_mapa=cv.LoadImage(self.nombre_archivo1, cv.CV_LOAD_IMAGE_COLOR)
		dimensiones_robot=self.dimensiones_robot
		image1=cv.CreateImage(dimensiones_robot,8,3)
		image_mascara=cv.CreateImage(dimensiones_robot,8,1)
		
		##rotacion
		#Rotar el robot
		src_center=dimensiones_robot[0]/2,dimensiones_robot[1]/2
		rot_mat=cv.CreateMat( 2, 3, cv.CV_32FC1 )
		cv.GetRotationMatrix2D(src_center, robo_th, 1.0,rot_mat);
		cv.WarpAffine(self.robot,image1,rot_mat)
		#crear filtro para negro
		cv.InRangeS(image1,cv.RGB(0,0,0),cv.RGB(14,14,14),image_mascara)
		cv.Not(image_mascara,image_mascara)
		#cv.ReleaseImage(image1)
		
		#reducir y posicion
		cv.SetImageROI(image_mapa,(robo_x,robo_y, dimensiones_robot[0], dimensiones_robot[1]));
		cv.Copy(image1,image_mapa,mask=image_mascara)
		cv.ResetImageROI(image_mapa);
		cv.SaveImage(self.nombre_archivo, image_mapa) #Saves the image#
Exemplo n.º 10
0
 def acquire(self, img):
     self.found_data = False
     cv.SetImageROI(img, self.rect.ToCvRect())
     self.internal_img = cv.CreateImage(cv.GetSize(img), img.depth,
                                        img.nChannels)
     cv.Copy(img, self.internal_img)
     cv.ResetImageROI(img)
Exemplo n.º 11
0
    def redraw_monocular(self, drawable):
        width, height = cv.GetSize(drawable.scrib)

        display = cv.CreateMat(max(480, height), width + 100, cv.CV_8UC3)
        cv.Zero(display)
        cv.Copy(drawable.scrib, cv.GetSubRect(display, (0, 0, width, height)))
        cv.Set(cv.GetSubRect(display, (width, 0, 100, height)),
               (255, 255, 255))

        self.buttons(display)
        if not self.c.calibrated:
            if drawable.params:
                for i, (label, lo, hi, progress) in enumerate(drawable.params):
                    (w, _), _ = cv.GetTextSize(label, self.font)
                    cv.PutText(display, label,
                               (width + (100 - w) / 2, self.y(i)), self.font,
                               (0, 0, 0))
                    color = (0, 255, 0)
                    if progress < 1.0:
                        color = (0, int(progress * 255.), 255)
                    cv.Line(display, (int(width + lo * 100), self.y(i) + 20),
                            (int(width + hi * 100), self.y(i) + 20), color, 4)

        else:
            cv.PutText(display, "lin.", (width, self.y(0)), self.font,
                       (0, 0, 0))
            linerror = drawable.linear_error
            if linerror < 0:
                msg = "?"
            else:
                msg = "%.2f" % linerror
                #print "linear", linerror
            cv.PutText(display, msg, (width, self.y(1)), self.font, (0, 0, 0))

        self.show(display)
Exemplo n.º 12
0
def getIris(frame):
    iris = []
    copyImg = cv.CloneImage(frame)
    resImg = cv.CloneImage(frame)
    grayImg = cv.CreateImage(cv.GetSize(frame), 8, 1)
    mask = cv.CreateImage(cv.GetSize(frame), 8, 1)
    storage = cv.CreateMat(frame.width, 1, cv.CV_32FC3)
    cv.CvtColor(frame, grayImg, cv.CV_BGR2GRAY)
    cv.Canny(grayImg, grayImg, 5, 70, 3)
    cv.Smooth(grayImg, grayImg, cv.CV_GAUSSIAN, 7, 7)
    circles = getCircles(grayImg)
    iris.append(resImg)
    for circle in circles:
        rad = int(circle[0][2])
        global radius
        radius = rad
        cv.Circle(mask, centroid, rad, cv.CV_RGB(255, 255, 255), cv.CV_FILLED)
        cv.Not(mask, mask)
        cv.Sub(frame, copyImg, resImg, mask)
        x = int(centroid[0] - rad)
        y = int(centroid[1] - rad)
        w = int(rad * 2)
        h = w
        cv.SetImageROI(resImg, (x, y, w, h))
        cropImg = cv.CreateImage((w, h), 8, 3)
        cv.Copy(resImg, cropImg)
        cv.ResetImageROI(resImg)
        return (cropImg)
    return (resImg)
Exemplo n.º 13
0
def SubImage(img, region):
    '''return a subimage as a new image. This allows
	for the region going past the edges.
	region is of the form (x1,y1,width,height)'''
    (x1, y1, width, height) = region
    zeros = numpy.zeros((height, width, 3), dtype='uint8')
    ret = cv.GetImage(cv.fromarray(zeros))
    (img_width, img_height) = cv.GetSize(img)
    if x1 < 0:
        sx1 = 0
        xofs = -x1
    else:
        sx1 = x1
        xofs = 0
    if y1 < 0:
        sy1 = 0
        yofs = -y1
    else:
        sy1 = y1
        yofs = 0
    if sx1 + width <= img_width:
        w = width
    else:
        w = img_width - sx1
    if sy1 + height <= img_height:
        h = height
    else:
        h = img_height - sy1
    cv.SetImageROI(img, (sx1, sy1, w - xofs, h - yofs))
    cv.SetImageROI(ret, (xofs, yofs, w - xofs, h - yofs))
    cv.Copy(img, ret)
    cv.ResetImageROI(img)
    cv.ResetImageROI(ret)
    return ret
Exemplo n.º 14
0
  def find_biggest_region(self):
    """ this code should find the biggest region and
        then determine some of its characteristics, which
        will help direct the drone
    """
    # copy the thresholded image
    cv.Copy( self.threshed_image, self.copy )  # copy self.threshed_image
    # this is OpenCV's call to find all of the contours:
    contours = cv.FindContours(self.copy, self.storage, cv.CV_RETR_EXTERNAL,
                               cv.CV_CHAIN_APPROX_SIMPLE)

    # Next we want to find the *largest* contour
    if len(contours)>0:
      biggest = contours
      biggestArea=cv.ContourArea(contours)
      while contours != None:
        nextArea=cv.ContourArea(contours)
        if biggestArea < nextArea:
          biggest = contours
          biggestArea = nextArea
        contours=contours.h_next()
    
      #Use OpenCV to get a bounding rectangle for the largest contour
      self.br = cv.BoundingRect(biggest,update=0)
      
      #Publish the data.
      self.publishBoxData()
Exemplo n.º 15
0
    def __init__(self, frame, rect, time_makes_difference=False):
        self.rect = rect
        self.time_makes_difference = time_makes_difference
        self.face_photo_valid = False
        self.face_size = [rect[2], rect[3]]
        self.personID = -1
        self.white_level = 6
        try:
            sub_image = cv.GetSubRect(frame, rect)
        except:
            return None
        self.frame_copy = cv.CreateImage((rect[2], rect[3]), cv.IPL_DEPTH_8U,
                                         frame.nChannels)

        if frame.origin == cv.IPL_ORIGIN_TL:
            cv.Copy(sub_image, self.frame_copy)
        else:
            cv.Flip(sub_image, self.frame_copy, 0)
        self.find_eyes()

        if self.is_a_valid_face_photo():
            self.find_wrinkles()
            self.find_avarage_colors()
            self.find_gender_age_emotions()
            #self.count_face_vector()
            self.create_face_vector()
Exemplo n.º 16
0
 def save(self, filename, cropFlag = False):
     """save cropped and rotated image"""
     if not cropFlag:
         cv.SaveImage(filename +'-vectors.png',self.render())
     else:
         tmp = rotate(self.cImage, self.center, self.cardinalOffset +\
                 self.north)
         #mask horizon
         mask = cv.CreateImage(self.res, 8, 1)
         cv.Zero(mask)
         cv.Circle(mask, self.center, self.radius, (255,255,255))
         cv.FloodFill(mask,(1,1),(0,0,0))
         cv.FloodFill(mask, self.center,
                 (255,255,255),lo_diff=cv.RealScalar(5))
         masked = cv.CloneImage(tmp)
         cv.Zero(masked)
         cv.Copy(tmp, masked, mask)
         cv.SaveImage(filename +'-cropped.png',crop(masked, self))
     #CSV output
     array = magnitudeToTheta(self.polarArray,self.radius)
     f = open(filename + '.csv', 'w')
     f.write('00\n')
     f.write(','.join([str(v[0]) for v in array]))
     f.write('\n')
     f.write(','.join([str(v[1]) for v in array]))
     f.write('\n')
     f.flush()
     f.close()
Exemplo n.º 17
0
def preprocessImage(inputPath, outputPath):
    image = cv.LoadImage(inputPath, 0)

    #Find the most likely face
    (x, y, w, h), n = mostLikelyHaar(image, haarFace)
    croppedImage = cv.CreateImage((w, h), image.depth, image.nChannels)
    scaledImage = cv.CreateImage((256, 256), image.depth, image.nChannels)
    src_region = cv.GetSubRect(image, (x, y, w, h))
    cv.Copy(src_region, croppedImage)
    cv.Resize(croppedImage, scaledImage)
    image = scaledImage

    #Find each ficudial point
    leftEye = ImageObject(image, haarLeftEye,
                          inPercentRect(image, 0, 0, .6, .5))
    leftEyePoints = leftEye.getPoints([(.2, .5), (.8, .5)])
    rightEye = ImageObject(image, haarRightEye,
                           inPercentRect(image, .4, 0, 1, .5))
    rightEyePoints = rightEye.getPoints([(.2, .5), (.8, .5)])
    mouth = ImageObject(image, haarMouth, inPercentRect(image, 0, .6, 1, 1))
    mouthPoints = mouth.getPoints([(0, .3), (.5, .3), (1, .3)])
    nose = ImageObject(image, haarNose)
    nosePoints = nose.getPoints([(.2, .5), (.5, .5), (.8, .5)])

    #rotate each set of points by the tilt of the face
    tiltAngle = math.atan2(rightEyePoints[0][1] - leftEyePoints[0][1],
                           rightEyePoints[0][0] - leftEyePoints[0][0])
    leftEyePoint = tiltPoints(tiltAngle, leftEyePoints)
    rightEyePoints = tiltPoints(tiltAngle, rightEyePoints)
    mouthPoints = tiltPoints(tiltAngle, mouthPoints)
    nosePoints = tiltPoints(tiltAngle, nosePoints)

    image = rotateImage(image, tiltAngle, (w / 2, h / 2))

    leftEye = ImageObject(image, haarLeftEye,
                          inPercentRect(image, 0, 0, .6, .5))
    rightEye = ImageObject(image, haarRightEye,
                           inPercentRect(image, .4, 0, 1, .5))
    mouth = ImageObject(image, haarMouth, inPercentRect(image, 0, .6, 1, 1))
    nose = ImageObject(image, haarNose)

    rotation = math.log(leftEye.w) - math.log(rightEye.w)
    print rotation

    info = {
        'image': outputPath,
        'lbp-left-eye': calcLBP(image, leftEye),
        'left-eye': leftEye.getTuple(),
        'lbp-right-eye': calcLBP(image, rightEye),
        'right-eye': rightEye.getTuple(),
        'lbp-mouth': calcLBP(image, mouth),
        'mouth': mouth.getTuple(),
        'tilt': tiltAngle,
        'rotation': rotation
    }

    #save image of the cropped face
    cv.SaveImage(outputPath, image)

    return info
Exemplo n.º 18
0
def compose(a, b, d):
    w, h = (640, 480)
    r = cv.CreateImage((w * 3, h), cv.IPL_DEPTH_8U, 3)
    cv.SetZero(r)

    for (i, src) in enumerate([a, b, d]):
        if src != None:
            cv.SetImageROI(r, (w * i, 0, w, h))
            if src.nChannels == 1:
                deep = cv.CreateImage((w, h), cv.IPL_DEPTH_8U, 3)
                cv.CvtColor(src, deep, cv.CV_GRAY2BGR)
                cv.Copy(deep, r)
            else:
                cv.Copy(src, r)
    cv.ResetImageROI(r)
    return r
Exemplo n.º 19
0
    def find_new_markers(self, do_canny=True):
        '''
        Find markers totally without taking their previous position into
        consideration
        @param do_canny: perform edge recognition
        '''
        for scale in range(self.max_scale, -1, -1):
            if len(self.not_found) == 0:
                return
            self.set_scale(scale)
            if do_canny:
                cv.Canny(self.gray_img, self.bw_img, 100, 300)
            cv.Copy(self.bw_img, self.tmp_img)
            self.tmp_img[1, 1] = 255
            cont = cv.FindContours(
                self.tmp_img, cv.CreateMemStorage(), cv.CV_RETR_TREE)
            db.DrawContours(
                self.canny_img, cont, (255, 255, 255), (128, 128, 128), 10)
#            db.show([self.canny_img,self.tmp_img,self.bw_img], 'show', 0, 1)
#            cv.ShowImage("name", self.canny_img)
#            cv.ShowImage("name1", self.tmp_img)
            self.set_scale(0)
            self.scale_factor = 1 << scale
            self.test_contours(cont)
            #markers= filter(lambda sq:sq.check_square(self.img,self.gray_img),rects)

        return self.markers
Exemplo n.º 20
0
    def getForegroundPixels(self, bgcolor=None):
        '''
        @param bgcolor: The background color to use. Specify as an (R,G,B) tuple.
        Specify None for a blank/black background.
        @return: The full color foreground pixels on either a blank (black)
        background, or on a background color specified by the user.
        @note: You must call detect() before getForegroundPixels() to
        get updated information.
        '''
        if self._fgMask == None: return None

        #binary mask selecting foreground regions
        mask = self._fgMask.asOpenCVBW()

        #full color source image
        image = self._annotateImg.copy().asOpenCV()

        #dest image, full color, but initially all zeros (black/background)
        # we will copy the foreground areas from image to here.
        dest = cv.CloneImage(image)
        if bgcolor == None:
            cv.SetZero(dest)
        else:
            cv.Set(dest, cv.RGB(*bgcolor))

        cv.Copy(image, dest,
                mask)  #copy only pixels from image where mask != 0
        return pv.Image(dest)
Exemplo n.º 21
0
 def __refresh_canny(self):
     cv.Canny(self.res_smooth, self.canny, self.__canny_lo, self.__canny_hi, self.__canny_apeture * 2 + 3)
     #cv.Threshold(self.res_smooth, self.canny, self.__canny_lo * 2, 255, cv.CV_THRESH_BINARY)
     cv.ShowImage('Canny', self.canny)
     cv.Copy(self.canny, self.contour_in)
     self.contours = cv.FindContours(self.contour_in, self.c_storage, cv.CV_RETR_LIST, cv.CV_CHAIN_APPROX_NONE)
     self.__refresh_poly()
Exemplo n.º 22
0
def CompositeThumbnail(img, regions, thumb_size=100):
    '''extract a composite thumbnail for the regions of an image

    The composite will consist of N thumbnails side by side
    '''
    composite = cv.CreateImage((thumb_size*len(regions), thumb_size),8,3)
    for i in range(len(regions)):
        (x1,y1,x2,y2) = regions[i].tuple()
        midx = (x1+x2)/2
        midy = (y1+y2)/2

        if (x2-x1) > thumb_size or (y2-y1) > thumb_size:
            # we need to shrink the region
            rsize = max(x2+1-x1, y2+1-y1)
            src = cuav_util.SubImage(img, (midx-rsize/2,midy-rsize/2,rsize,rsize))
            thumb = cv.CreateImage((thumb_size, thumb_size),8,3)
            cv.Resize(src, thumb)
        else:
            x1 = midx - thumb_size/2
            y1 = midy - thumb_size/2
            thumb = cuav_util.SubImage(img, (x1, y1, thumb_size, thumb_size))
        cv.SetImageROI(composite, (thumb_size*i, 0, thumb_size, thumb_size))
        cv.Copy(thumb, composite)
        cv.ResetImageROI(composite)
    return composite
Exemplo n.º 23
0
def OverlayImage(img, img2, x, y):
    '''overlay a 2nd image on a first image, at position x,y
	on the first image'''
    (w, h) = cv.GetSize(img2)
    cv.SetImageROI(img, (x, y, w, h))
    cv.Copy(img2, img)
    cv.ResetImageROI(img)
Exemplo n.º 24
0
def getData():

  for i in range (0 , classes):
    for j in range (0, train_samples):
      if j < 10 :
        fichero = "OCR/"+str(i) + "/"+str(i)+"0"+str(j)+".pbm"
      else:
        fichero = "OCR/"+str(i) + "/"+str(i)+str(j)+".pbm"
      src_image = cv.LoadImage(fichero,0)
      prs_image = preprocessing(src_image, size, size)
  
         
      
      row = cv.GetRow(trainClasses, i*train_samples + j)
      cv.Set(row, cv.RealScalar(i))
      row = cv.GetRow(trainData,   i*train_samples + j)

      img = cv.CreateImage( ( size, size ), cv.IPL_DEPTH_32F, 1) 
      

      cv.ConvertScale(prs_image,img,0.0039215, 0)


      data = cv.GetSubRect(img,  (0,0, size,size))
      row1 = cv.Reshape( data, 0, 1 )
     
      cv.Copy(row1, row)
Exemplo n.º 25
0
 def image_to_show( self ):
     cv.Copy( self.image, self.view_image )
     if self.patch:
         draw_patch( self.view_image, *self.patch, filled=True )
     if self.click_pt:
         cv.Circle( self.view_image, self.click_pt, 5*self.zoom_out, cv.RGB(0,0,255), -1 )
     return self.view_image
Exemplo n.º 26
0
def motion_detector():
    global max_area, avg, prev_pos, largest_contour
    contour = cv.FindContours(
        temp_image,
        store,
        mode=cv.CV_RETR_EXTERNAL,
        method=cv.CV_CHAIN_APPROX_NONE)  #Findling contours
    cv.Copy(img, render_image)  #Copying for painting on the image
    if len(contour) != 0:
        temp_contour = contour
        area = 0
        max_area_test = max_area
        while temp_contour != None:  #Routine to find the largest contour
            area = cv.ContourArea(temp_contour)
            if area > max_area_test:
                largest_contour = temp_contour
                max_area_test = area
            temp_contour = temp_contour.h_next()
        rect = cv.BoundingRect(largest_contour)
        cv.DrawContours(render_image, largest_contour, (0, 255, 0),
                        (0, 0, 255), 1, 3)
        cv.Rectangle(render_image, (rect[0], rect[1]),
                     (rect[0] + rect[2], rect[1] + rect[3]), (255, 0, 0))
        avg = rect[0] + rect[2] / 2
    else:
        avg = img.width / 2
Exemplo n.º 27
0
def matchsize(A, B):
    """ Given two cvMats A, B, returns a cropped/padded version of
    A that has the same dimensions as B.
    """
    wA, hA = cv.GetSize(A)
    wB, hB = cv.GetSize(B)
    if wA == wB and hA == hB:
        return A
    SetImageROI = cv.SetImageROI
    out = cv.CreateImage((wB, hB), A.depth, A.channels)
    wOut, hOut = cv.GetSize(out)
    if wA < wOut and hA < hOut:
        SetImageROI(out, (0, 0, wA, hA))
    elif wA >= wOut and hA < hOut:
        SetImageROI(out, (0, 0, wOut, hA))
        SetImageROI(A, (0, 0, wOut, hA))
    elif wA < wOut and hA >= hOut:
        SetImageROI(out, (0, 0, wA, hOut))
        SetImageROI(A, (0, 0, wA, hOut))
    else:  # wA >= wOut and hA >= hOut:
        SetImageROI(A, (0, 0, wOut, hOut))
    cv.Copy(A, out)
    cv.ResetImageROI(out)
    cv.ResetImageROI(A)
    return out
Exemplo n.º 28
0
    def listen(self):

        if isinstance(self.background, np.ndarray):
            bgimg = cv.CreateImage(self.background.shape[:2], 8, 3)
            img = cv.CreateImage(self.background.shape[:2], 8, 3)
            theWidth = self.background.shape[1]
            theHeight = self.background[0]
        else:

            bgimg = cv.CreateImage(
                (self.background.width, self.background.height), 8, 3)
            img = cv.CreateImage(
                (self.background.width, self.background.height), 8, 3)
            theWidth = self.background.width
            theHeight = self.background.height

        cv.Copy(self.background, bgimg)
        smallimg = cv.CreateImage(
            (theWidth / self.zoom, theHeight / self.zoom), 8, 3)
        cv.GetRectSubPix(bgimg, smallimg,
                         (theWidth / (2 * self.zoom) + self.offset[0],
                          theHeight / (2 * self.zoom) + self.offset[1]))
        cv.Resize(smallimg, img)
        if (self.cp != False):
            cv.Circle(img, self.zoomPt(self.cp.x, self.cp.y), 3,
                      cv.RGB(0, 255, 0), -1)

        cv.Line(img, (self.ch_x - 25, self.ch_y), (self.ch_x + 25, self.ch_y),
                cv.RGB(255, 255, 0))
        cv.Line(img, (self.ch_x, self.ch_y - 25), (self.ch_x, self.ch_y + 25),
                cv.RGB(255, 255, 0))
        cv.ShowImage(self.name, img)
        cv.WaitKey(25)
Exemplo n.º 29
0
def image_processor():
    cv.Smooth(gray_image, gray_image, cv.CV_GAUSSIAN, 3,
              3)  #Blurring to remove some noise
    cv.AbsDiff(prev_image, gray_image,
               accumulator)  #Getting the difference image
    cv.InRangeS(accumulator, threshold_limit1_lower, threshold_limit1_upper,
                accumulator)  #Thresholding the difference image
    cv.Dilate(accumulator, accumulator, None,
              2)  #Dilating the thresholded difference image
    cv.Add(accumulator, sum_image, sum_image,
           accumulator)  #Adding the image to a register to use fading
    cv.SubS(sum_image, fading_factor, sum_image)  #Fading
    cv.InRangeS(sum_image, threshold_limit2_lower, threshold_limit2_upper,
                accumulator)  #Thresholding the fading image
    cv.Copy(gray_image, prev_image)
    cv.Copy(accumulator, temp_image)
Exemplo n.º 30
0
def setup(flipped, capture, thehandcolor):
    """Initializes camera and finds initial skin tone"""

    #creates initial window and prepares text
    color = (40, 0, 0)
    font = cv.InitFont(cv.CV_FONT_HERSHEY_SIMPLEX, 1.0, 1.0)
    textsize1 = (cv.GetSize(flipped)[0] / 2 - 150,
                 cv.GetSize(flipped)[1] / 2 - 140)
    textsize2 = (cv.GetSize(flipped)[0] / 2 - 150,
                 cv.GetSize(flipped)[1] / 2 - 110)
    point1 = (cv.GetSize(flipped)[0] / 2 - 25, cv.GetSize(flipped)[1] / 2 - 25)
    point2 = (cv.GetSize(flipped)[0] / 2 + 25, cv.GetSize(flipped)[1] / 2 + 25)

    #until Enter is pressed
    while (cv.WaitKey(10) != 10):

        #captures live video, and draws sub-box and text
        frame = cv.QueryFrame(capture)
        cv.Copy(frame, flipped)
        cv.Flip(flipped, flipped, 1)
        cv.Rectangle(flipped, point1, point2, color, 2)
        cv.PutText(flipped, "Put your hand in the box ", textsize1, font,
                   color)
        cv.PutText(flipped, "and press enter", textsize2, font, color)
        cv.ShowImage("w2", flipped)

    #Creates sub-image inside box, and returns average color in box
    sub = cv.GetSubRect(flipped, (cv.GetSize(flipped)[0] / 2 - 25,
                                  cv.GetSize(flipped)[1] / 2 - 25, 50, 50))
    cv.Set(thehandcolor, cv.Avg(sub))
    return cv.Avg(sub)