Example #1
0
    def frame(self):
        _ = self.stdout.readline()
        #print line
        #print self.w,self.h
        y = self.stdout.read(self.w * self.h)
        u = self.stdout.read(self.w * self.h / 4)
        v = self.stdout.read(self.w * self.h / 4)
        if len(y) < self.w * self.h:
            raise EOFError

        cv.SetData(self.frame_y, y)
        cv.SetData(self.frame_u2, u)
        cv.SetData(self.frame_v2, v)

        cv.Resize(self.frame_u2, self.frame_u)
        cv.Resize(self.frame_v2, self.frame_v)

        cv.Merge(self.frame_y, self.frame_u, self.frame_v, None,
                 self.frame_col)
        cv.CvtColor(self.frame_col, self.frame_col, cv.CV_YCrCb2RGB)

        out = self.frame_col

        if self.size != None:
            cv.Resize(self.frame_col, self.frame_resized)
            out = self.frame_resized

        return pv.Image(self.frame_y), pv.Image(self.frame_u), pv.Image(
            self.frame_v), pv.Image(out)
Example #2
0
def meanshiftUsingPCA(path):
    # Load original image given the image path
    im = cv.LoadImageM(path)
    #convert image to YUV color space
    cv.CvtColor(im, im, cv.CV_BGR2YCrCb)
    # Load bank of filters
    filterBank = lmfilters.loadLMFilters()
    # Resize image to decrease dimensions during clustering
    resize_factor = 1
    thumbnail = cv.CreateMat(im.height / resize_factor,
                             im.width / resize_factor, cv.CV_8UC3)
    cv.Resize(im, thumbnail)
    # now work with resized thumbnail image
    response = np.zeros(shape=((thumbnail.height) * (thumbnail.width), 51),
                        dtype=float)
    for f in xrange(0, 48):
        filter = filterBank[f]
        # Resize the filter with the same factor for the resized image
        dst = cv.CreateImage(cv.GetSize(thumbnail), cv.IPL_DEPTH_32F, 3)
        resizedFilter = cv.CreateMat(filter.height / resize_factor,
                                     filter.width / resize_factor, filter.type)
        cv.Resize(filter, resizedFilter)
        # Apply the current filter
        cv.Filter2D(thumbnail, dst, resizedFilter)
        for j in xrange(0, thumbnail.height):
            for i in xrange(0, thumbnail.width):
                # Select the max. along the three channels
                maxRes = max(dst[j, i])
                if math.isnan(maxRes):
                    maxRes = 0.0
                if maxRes > response[thumbnail.width * j + i, f]:
                    # Store the max. response for the given feature index
                    response[thumbnail.width * j + i, f] = maxRes

    #YUV features
    count = 0
    for j in xrange(0, thumbnail.height):
        for i in xrange(0, thumbnail.width):
            response[count, 48] = thumbnail[j, i][0]
            response[count, 49] = thumbnail[j, i][1]
            response[count, 50] = thumbnail[j, i][2]
            count += 1

    #get the first 4 primary components using pca
    pca = PCA(response)
    pcaResponse = zeros([thumbnail.height * thumbnail.width, 4])

    for i in xrange(0, thumbnail.height * thumbnail.width):
        pcaResponse[i] = pca.getPCA(response[i], 4)

    # Create new mean shift instance
    ms = MeanShift(bandwidth=10, bin_seeding=True)
    # Apply the mean shift clustering algorithm
    ms.fit(pcaResponse)
    labels = ms.labels_
    n_clusters_ = np.unique(labels)
    print "Number of clusters: ", len(n_clusters_)
    repaintImage(thumbnail, labels)
    cv.Resize(thumbnail, im)
    return im
Example #3
0
def main():
    # load the specified image
    filename = sys.argv[1]
    original = cv.LoadImage(filename)
    # resample to W columns wide
    aspect = float(original.width) / float(original.height)
    print original.width, original.height
    print W, int(W / aspect)
    resized = cv.CreateImage((W, int(W / aspect)), cv.IPL_DEPTH_8U, 3)
    cv.Resize(original, resized, cv.CV_INTER_NN)
    quick_show(resized)
    resized = extract_v(resized)
    for y in range(resized.height - 1):
        for x in range(resized.width - 1):
            p1 = resized[y, x]
            p2 = resized[y, x + 1]
            p3 = resized[y + 1, x]
            p4 = resized[y + 1, x + 1]
            resized[y, x] = abs(p1 - p4) + abs(p2 - p3)
    mean = cv.CreateImage((resized.width, resized.height), cv.IPL_DEPTH_8U, 1)
    for y in range(1, resized.height - 3):
        for x in range(1, resized.width - 3):
            a = 0.0
            for yy in range(y - 1, y + 3):
                for xx in range(x - 1, x + 3):
                    a += resized[yy, xx]
            mean[y, x] = a / 16.0
    gray = cv.CreateImage((original.width, original.height), cv.IPL_DEPTH_8U,
                          1)
    cv.Resize(mean, gray)
    quick_show(gray)
Example #4
0
def dewarp(imagedir):
  # Loading from json file
  C = CameraParams()
  C.load(imagedir+"/params.json")
  K = cv.fromarray(C.K)
  D = cv.fromarray(C.D)
  print "loaded camera parameters"
  mapx = None
  mapy = None
  for f in os.listdir(imagedir):
    if (f.find('pgm')<0):
      continue
    image = imagedir+'/'+f
    print image
    original = cv.LoadImage(image,cv.CV_LOAD_IMAGE_GRAYSCALE)
    dewarped = cv.CloneImage(original);
    # setup undistort map for first time
    if (mapx == None or mapy == None):
      im_dims = (original.width, original.height)
      mapx = cv.CreateImage( im_dims, cv.IPL_DEPTH_32F, 1 );
      mapy = cv.CreateImage( im_dims, cv.IPL_DEPTH_32F, 1 );
      cv.InitUndistortMap(K,D,mapx,mapy)

    cv.Remap( original, dewarped, mapx, mapy )

    tmp1=cv.CreateImage((im_dims[0]/2,im_dims[1]/2),8,1)
    cv.Resize(original,tmp1)
    tmp2=cv.CreateImage((im_dims[0]/2,im_dims[1]/2),8,1)
    cv.Resize(dewarped,tmp2)

    cv.ShowImage("Original", tmp1 )
    cv.ShowImage("Dewarped", tmp2)
    cv.WaitKey(-1)
Example #5
0
def fastResize(I,w,h):
    Icv = cv.fromarray(I)
    I1cv=cv.CreateMat(h, w, Icv.type)
    if w < I.shape[1]:
        cv.Resize(Icv,I1cv,interpolation=cv.CV_INTER_AREA)
    else:
        cv.Resize(Icv,I1cv,interpolation=cv.CV_INTER_CUBIC)
    Iout = numpy.asarray(I1cv)
    return Iout
Example #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)
def rescale_img(I, c):
    """ Scales image I by factor C. For instance, to upscale an image
    by a factor of 2, pass in c=2.0. """
    if c == 1.0:
        return cv.CloneImage(I)
    w_cur, h_cur = cv.GetSize(I)
    w_new, h_new = int(round(w_cur * float(c))), int(round(h_cur * float(c)))
    Iout = cv.CreateImage((w_new, h_new), I.depth, I.channels)
    if c > 1.0:
        # Downsizing
        cv.Resize(I, Iout, interpolation=cv.CV_INTER_AREA)
    else:
        cv.Resize(I, Iout, interpolation=cv.CV_INTER_CUBIC)
    return Iout
Example #8
0
def fastResize(I, rszFac):
    Icv = cv.fromarray(np.copy(I))
    I1cv = cv.CreateMat(int(math.floor(I.shape[0] * rszFac)),
                        int(math.floor(I.shape[1] * rszFac)), Icv.type)
    cv.Resize(Icv, I1cv)
    Iout = np.asarray(I1cv)
    return Iout
Example #9
0
def resize_image(
        source, size
):  #size[0], size[1] represent the new height and width respectively
    thumbnail = cv.CreateImage((size[0], size[1]), source.depth,
                               source.nChannels)
    cv.Resize(source, thumbnail, interpolation=cv.CV_INTER_CUBIC)
    return thumbnail
Example #10
0
File: test.py Project: wy51r/kod
    def run(self):
        while True:
            frame = cv.QueryFrame(self.capture)
            image_size = cv.GetSize(frame)
            gray = cv.CreateImage((image_size[0], image_size[1]), 8, 1)
            cv.CvtColor(frame, gray, cv.CV_BGR2GRAY)
            # scale input image for faster processing
            small_img = cv.CreateImage((cv.Round(image_size[0] / __scale__),
                                        cv.Round(image_size[1] / __scale__)),
                                       8, 1)
            cv.Resize(gray, small_img, cv.CV_INTER_LINEAR)
            cv.EqualizeHist(small_img, small_img)

            s_res = sift(np.array(cv.GetMat(small_img)).flatten('C'))
            n_res = np.array(s_res)
            for item in n_res:
                xx = item[0] * __scale__
                yy = item[1] * __scale__
                label_sift_point(frame, xx, yy)

            cv.ShowImage("CamShiftDemo", frame)

            c = cv.WaitKey(7)
            if c == 27:
                break
            elif c == ord("t"):
                frame = cv.QueryFrame(self.capture)
                self.save(frame)
                self.i += 1
Example #11
0
def imagen_homografia(n):
     
    global points, width, height,teclado
     
    urllib.urlretrieve("http://192.168.0.100:8080/photo.jpg", "foto.jpg")
    foto=cv.LoadImage('foto.jpg')
    im_in= cv.CloneImage(foto)
    #CALIBRACION
    width, height = cv.GetSize(im_in)
    im = cv.CloneImage(foto)
    if n == 0 and (teclado ==1 or teclado ==3):
      cv.ShowImage('Escoger 4 puntos',im)
      cv.SetMouseCallback('Escoger 4 puntos', mouse, im)
      cv.WaitKey(0)
      guardarpoints(points)      
      h**o = homografia(im_in.width, im_in.height, im.width, im.height)
      cv.Save('homography.cvmat', h**o)
    else:
      points = cargarpoints()
      h**o = homografia(im_in.width, im_in.height, im.width, im.height)
    out_big = cv.CloneImage(im_in)
    cv.WarpPerspective(im_in, out_big, h**o)
    out_small = cv.CloneImage(im)
    cv.Resize(out_big, out_small)
    return out_small, out_big
Example #12
0
    def render(self, window):
        with self.lock:
            if self.image and self.image_time + rospy.Duration(2.0) > rospy.Time.now() and self.info_time + rospy.Duration(2.0) > rospy.Time.now():
                cv.Resize(self.bridge.imgmsg_to_cv(self.image, 'rgb8'), window)
                interval = min(1,(self.interval / self.max_interval))
                cv.Rectangle(window,
                             (int(0.05*window.width), int(window.height*0.9)),
                             (int(interval*window.width*0.9+0.05*window.width), int(window.height*0.95)),
                             (0, interval*255, (1-interval)*255), thickness=-1)
                cv.Rectangle(window,
                             (int(0.05*window.width), int(window.height*0.9)),
                             (int(window.width*0.9+0.05*window.width), int(window.height*0.95)),
                             (0, interval*255, (1-interval)*255))
                cv.PutText(window, self.ns, (int(window.width * .05), int(window.height * 0.1)), self.font1, (0,0,255))
                if self.features and self.features.header.stamp + rospy.Duration(4.0) > self.image.header.stamp:
                    w_scaling =  float (window.width) / self.image.width
                    h_scaling =  float (window.height) / self.image.height
                    if self.features.success:
                        corner_color = (0,255,0)
                        for cur_pt in self.features.image_points:
                            cv.Circle(window, (int(cur_pt.x*w_scaling), int(cur_pt.y*h_scaling)), int(w_scaling*5), corner_color)
                    else:
                        window = add_text(window, ["Could not detect", "checkerboard"], False)
                else:
                    window = add_text(window, ["Timed out waiting", "for checkerboard"], False)

            else:
                # Generate random white noise (for fun)
                noise = numpy.random.rand(window.height, window.width)*256
                numpy.asarray(window)[:,:,0] = noise;
                numpy.asarray(window)[:,:,1] = noise;
                numpy.asarray(window)[:,:,2] = noise;
                cv.PutText(window, self.ns, (int(window.width * .05), int(window.height * .95)), self.font, (0,0,255))
Example #13
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
Example #14
0
def overlay_image(frame, image, x, y, w, h):
    """resize and overlay an image where a feature is detected

    This resizes the corresponding image to a matched feature, then loops
    through all of its pixels to superimpose the image on the frame
    """
    # resize the image to fit the detected feature
    new_feature = cv.CreateImage((w, h), 8, 3)
    cv.Resize(image, new_feature, interpolation=cv.CV_INTER_AREA)

    # overlay the image on the frame
    for py in xrange(h):
        for px in xrange(w):
            pixel = cv.Get2D(new_feature, py, px)

            # don't map the whitespace surrounding the image
            if pixel != (255.0, 255.0, 255.0, 0.0):
                if image is tophat:
                    # above feature
                    new_y = y - py
                elif image is moustache:
                    # bottom half of feature
                    new_y = (h / 2) + y + py
                else:
                    # over feature
                    new_y = y + py
                new_x = x + px

                # make sure the image is in the frame
                if 0 < new_x < frame.width and 0 < new_y < frame.height:
                    cv.Set2D(frame, new_y, new_x, pixel)
Example #15
0
def draw_vertical_sample_boundaries(curr_image_cv, warp_matrix, square_length):

    #TODO:  IF THIS IS EVER USED, DO NOT USE THIS VALUE
    assert (False)
    scale_factor = 0.5

    curr_image = np.array(curr_image_cv)
    full_width = curr_image.shape[1]
    full_height = curr_image.shape[0]
    width = int(curr_image.shape[1] * scale_factor)
    height = int(curr_image.shape[0] * scale_factor)

    full_left_bound = int(full_width * (0.5 - square_length / 2))
    full_right_bound = int(full_width * (0.5 + square_length / 2))
    left_bound = int(full_left_bound * scale_factor)
    right_bound = int(full_right_bound * scale_factor)

    full_line1 = ((full_left_bound, 0), (full_left_bound, full_height - 1))
    full_line2 = ((full_right_bound, 0), (full_right_bound, full_height - 1))
    line1 = ((left_bound, 0), (left_bound, height - 1))
    line2 = ((right_bound, 0), (right_bound, height - 1))

    if curr_image.ndim == 2:
        sample_image = curr_image[:, :, np.newaxis]
        sample_image = np.tile(sample_image, (1, 1, 3))
    sample_image = cv.fromarray(sample_image)
    resized_sample_image = cv.CreateMat(height, width, sample_image.type)
    cv.Resize(sample_image, resized_sample_image)
    cv.Line(resized_sample_image, line1[0], line1[1], cv.CV_RGB(0, 255, 0))
    cv.Line(resized_sample_image, line2[0], line2[1], cv.CV_RGB(0, 255, 0))
    boundary_pts = line1 + line2

    return resized_sample_image, \
        cvutils.reorder_boundary_points(
            np.array(full_line1 + full_line2))
Example #16
0
    def _selectTrackingPoints(self,frame):
        '''
        This uses the OpenCV get good features to track to initialize a set of tracking points_b.
        '''
        quality = 0.01
        min_distance = 15
        
        w,h = self.tile_size
        tw = w//self.grid
        th = h//self.grid

        for i in range(self.grid):
            for j in range(self.grid):
                ul = pv.Point(i*tw,j*th)
                rect = pv.Rect(i*tw,j*th,tw,th)
                count = 0
                for pt in self.tracks:
                    if rect.containsPoint(pt):
                        count += 1
                    
                if count < self.min_points:
                    gray = cv.CreateImage ((tw,th), 8, 1)
                    
                    faceim = cv.GetSubRect(frame, rect.asOpenCV())
                    cv.Resize(faceim,gray)

                    eig = cv.CreateImage ((tw,th), 32, 1)
                    temp = cv.CreateImage ((tw,th), 32, 1)
                
                    # search the good points_b
                    points_b = cv.GoodFeaturesToTrack (gray, eig, temp, 2*self.min_points, quality, min_distance, None, 3, 0, 0.04)
                    
                    for pt in points_b:
                        self.tracks.append(ul+pv.Point(pt))
Example #17
0
def detect_and_draw(img, cascade):
    gray = cv.CreateImage((img.width,img.height), 8, 1)
    small_img = cv.CreateImage((cv.Round(img.width / image_scale),
			       cv.Round (img.height / image_scale)), 8, 1)
    # convert color input image to grayscale
    cv.CvtColor(img, gray, cv.CV_BGR2GRAY)
    # scale input image for faster processing
    cv.Resize(gray, small_img, cv.CV_INTER_LINEAR)
    cv.EqualizeHist(small_img, small_img)
    if(cascade):
        t = cv.GetTickCount()
        faces = cv.HaarDetectObjects(small_img, cascade, cv.CreateMemStorage(0),
                                     haar_scale, min_neighbors, haar_flags, min_size)
        index = 0 
        if faces:
            for ((x, y, w, h), n) in faces:
                # the input to cv.HaarDetectObjects was resized, so scale the 
                # bounding box of each face and convert it to two CvPoints
                x1, y1 = (int(x * image_scale), int(y * image_scale))
                x2, y2 = (int((x + w) * image_scale), int((y + h) * image_scale))

                pi = Image.fromstring("L", cv.GetSize(gray), gray.tostring())
                pi = pi.crop((x1, y1, x2, y2))
                pi = pi.resize((64, 64), Image.ANTIALIAS)
                path = os.path.join(sys.argv[1])
                pi.save(path)
                index += 1 
        else:
            os.remove(sys.argv[1])
Example #18
0
def load_sets(base_dir, set_names):
	cards = []
	for dir, subdirs, fnames in os.walk(base_dir):
		set = os.path.split(dir)[1]
		if set in set_names:
			print "Loading set %s" % (set)
			for fname in fnames:
				path = os.path.join(dir, fname)
				#img = cv.LoadImage(path.encode('utf-8'),0)
				try:
					img = cv.LoadImage(path,0)
				except IOError:
					print >> sys.stderr, "\tload_sets() Loading image %s failed" % (path)
					next
				if cv.GetSize(img) != (223, 310):
					tmp = cv.CreateImage((223, 310), 8, 1)
					cv.Resize(img,tmp)
					img = tmp
				phash = dct_hash(img)
				
				cards.append((
					fname.replace('.full.jpg',''),
					set,
					phash
				))
			print "\tDone loading set %s" % (set)

	return cards
    def angle(self, img):
	# extract position of red blue yellow markers
	# find distance between pairs
	# return angle from inverse cosine
	
	imgHSV = cv.CreateImage(cv.GetSize(img), 8, 3)
	cv.CvtColor(img, imgHSV, cv.CV_BGR2HSV)
	cv.NamedWindow("red", cv.CV_WINDOW_AUTOSIZE)
	cv.MoveWindow("red", 800, 0)
	cv.NamedWindow("blue", cv.CV_WINDOW_AUTOSIZE)
	cv.MoveWindow("blue", 800, 100)
	cv.NamedWindow("yellow", cv.CV_WINDOW_AUTOSIZE)
	cv.MoveWindow("yellow", 800, 200)
	
	dot_coords = []
	# use the corresponding thresholds for each color of marker #
	for h_low, h_high, col in [self.red_hues, self.yellow_hues, self.blue_hues]:
	    imgThresh = cv.CreateImage(cv.GetSize(img), 8, 1)
	    cv.InRangeS(imgHSV, cv.Scalar(h_low, 70, 70), cv.Scalar(h_high, 255, 255), imgThresh)
 	    moments = cv.Moments(cv.GetMat(imgThresh))
	    x_mov = cv.GetSpatialMoment(moments, 1, 0)
	    y_mov = cv.GetSpatialMoment(moments, 0, 1)
	    area = cv.GetCentralMoment(moments, 0, 0)
            small_thresh = cv.CreateImage((self.fit_camera_width, self.fit_camera_height), 8, 1)
	    cv.Resize(imgThresh, small_thresh)

	    if col == "r":
		cv.ShowImage("red", small_thresh)
	    elif col == "b":
		cv.ShowImage("blue", small_thresh)
	    elif col == "y":
		cv.ShowImage("yellow", small_thresh) 
	    if area > 0:
		posX = float(x_mov)/float(area)
	    	posY = float(y_mov)/float(area)
	    else:
		posX = 0
		posY = 0
	    dot_coords.append([posX, posY])	 
	
	r = dot_coords[0]
	y = dot_coords[1]
	b = dot_coords[2]
	# get side lengths
	y_r = self.dist(r[0], r[1], y[0], y[1])
	r_b = self.dist(b[0], b[1], r[0], r[1])
	y_b = self.dist(b[0], b[1], y[0], y[1])
	# apply law of cosines
	angle_in_rads = math.pow(y_r, 2) + math.pow(r_b, 2) - math.pow(y_b, 2)
	denom = 2.0 * y_r * r_b
	if denom > 0:
	     angle_in_rads /= 2.0 * y_r * r_b
	else:
	     angle_in_rads = 0
	rads = math.acos(angle_in_rads)
	# convert to degrees
	degs = rads * float(180.0 / math.pi)
	if degs < 0 or degs > 360: 
	     degs = 0
	return degs	
Example #20
0
def blob_to_input_instance(image, blob, classification_window_width):
    patch_size = classification_window_width * 2 + 1
    small_r = blob_to_rect(
        blob, classification_window_width=classification_window_width)
    big_r = blob_to_rect(
        blob, classification_window_width=classification_window_width * 2)
    if big_r == None or small_r == None:
        return None
    small_patch = cv.CloneMat(cv.GetSubRect(image, small_r.as_cv_rect()))
    big_patch = cv.CloneMat(cv.GetSubRect(image, big_r.as_cv_rect()))
    #cv.ShowImage('patch', small_patch)
    #cv.ShowImage('big_patch', big_patch)
    big_patch_rescaled = cv.CreateImage((int(classification_window_width / 2),
                                         int(classification_window_width / 2)),
                                        8, 3)
    cv.Resize(big_patch, big_patch_rescaled, cv.CV_INTER_LINEAR)

    np_patch_small = np.asarray(small_patch)
    np_patch_big = ad.cv2array(big_patch_rescaled)
    np_resized_small = np.matrix(
        np_patch_small.reshape(patch_size * patch_size * 3, 1))
    np_resized_big = np.matrix(
        np_patch_big.reshape(np_patch_big.shape[0] * np_patch_big.shape[1] * 3,
                             1))
    return np.concatenate((np_resized_small, np_resized_big), axis=0)
Example #21
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
    def __init__(self, image_name, cascade):
        try:
            image = cv.LoadImage(image_name, 1)
        except IOError:
            return
        except:
            return
        else:
            self.faces = []
            #Allocate Space for grayscale image and tiny image
            #Dramatically reduces computation time in exchange for temporary space
            grayscale = cv.CreateImage((image.width, image.height), 8, 1)
            img = cv.CreateImage((cv.Round(image.width / IMAGE_SCALE),
                                  cv.Round(image.height / IMAGE_SCALE)), 8, 1)

            cv.CvtColor(image, grayscale, cv.CV_BGR2GRAY)
            cv.Resize(grayscale, img, cv.CV_INTER_LINEAR)
            cv.EqualizeHist(img, img)

            matches = cv.HaarDetectObjects(img, cascade,
                                           cv.CreateMemStorage(0), HAAR_SCALE,
                                           IMAGE_SCALE, HAAR_FLAGS, MIN_SIZE)
            for ((x, y, width, height), wat) in matches:
                self.faces.append({
                    "x": x,
                    "y": y,
                    "width": width,
                    "height": height
                })
            self.name = image_name
Example #23
0
def detect_and_draw(img, cascade):
    # allocate temporary images
    gray = cv.CreateImage((img.width, img.height), 8, 1)
    small_img = cv.CreateImage((cv.Round(
        img.width / image_scale), cv.Round(img.height / image_scale)), 8, 1)

    # convert color input image to grayscale
    cv.CvtColor(img, gray, cv.CV_BGR2GRAY)

    # scale input image for faster processing
    cv.Resize(gray, small_img, cv.CV_INTER_LINEAR)

    cv.EqualizeHist(small_img, small_img)

    if (cascade):
        t = cv.GetTickCount()
        faces = cv.HaarDetectObjects(small_img, cascade,
                                     cv.CreateMemStorage(0), haar_scale,
                                     min_neighbors, haar_flags, min_size)
        t = cv.GetTickCount() - t
        print "detection time = %gms" % (t / (cv.GetTickFrequency() * 1000.))
        if faces:
            for ((x, y, w, h), n) in faces:
                # the input to cv.HaarDetectObjects was resized, so scale the
                # bounding box of each face and convert it to two CvPoints
                pt1 = (int(x * image_scale), int(y * image_scale))
                pt2 = (int((x + w) * image_scale), int((y + h) * image_scale))
                cv.Rectangle(img, pt1, pt2, cv.RGB(255, 0, 0), 3, 8, 0)

    cv.ShowImage("result", img)
Example #24
0
def resize_mat(mat, shape, rszFac=None):
    """ Takes in an NxM matrix 'mat', and either truncates or pads
    it so that it has the 'shape'.
    Input:
        obj mat: an NxM numpy array
        tuple shape: (h,w)
        float rszFac: If given, then this first rescales MAT by RSZFAC,
            followed by truncation/padding.
    Output:
        An hxw sized array.
    """
    h, w = mat.shape
    if rszFac is not None:
        w_new, h_new = int(round(rszFac * w)), int(round(rszFac * h))
        if w_new != w or h_new != h:
            Icv = cv.fromarray(mat)
            Icv_rsz = cv.CreateMat(h_new, w_new, Icv.type)
            cv.Resize(Icv, Icv_rsz, cv.CV_INTER_CUBIC)
            mat_rsz = np.asarray(Icv_rsz)
        else:
            mat_rsz = mat
    else:
        mat_rsz = mat

    out = np.zeros(shape, dtype=mat_rsz.dtype)
    i = min(mat_rsz.shape[0], out.shape[0])
    j = min(mat_rsz.shape[1], out.shape[1])
    out[0:i, 0:j] = mat_rsz[0:i, 0:j]
    return out
Example #25
0
def crop(im, size, pos=(0, 0)):
    """
	This function is very similar to :func:`sample()` in that it returns a specified subsection of the image.  The main difference here is that if the region is too big to fit, the returned image is made smaller.  There is no size default here and pos defaults to the top-left corner of the image.
	
	**Parameters:**
		* im (cvArr) - The source image.
		* size (tuple) - The cropped image size (w, h).
		* pos (tuple) - The position of the top-left corner of the cropped image.
	
	**Returns:**
		The cropped image.
	
	.. seealso::
		:func:`sample()`
	"""
    warn = False
    if pos[0] + size[0] > im.width:
        size = (im.width - 1 - pos[0], size[1])
        warn = True
    if pos[1] + size[1] > im.height:
        size = (size[0], im.height - 1 - pos[1])
        warn = True
    if warn:
        warnings.warn(
            "Cropped region went off the edge of the image.  The cropped size has been reduced to %dx%d."
            % (size[0], size[1]),
            stacklevel=2)

    rect = (pos[0], pos[1], size[0], size[1])
    cropped = create(im, size=size)
    dst = cv.GetSubRect(im, rect)
    cv.Resize(dst, cropped)
    return cropped
Example #26
0
def fastResize(I, rszFac, sig=-1):
    """ Resizes the input image I by factor 'rszFac'. 'rszFac' should be
    a float greater than 0.0, where smaller values leads to shrunken
    versions of I, and values greater than 1.0 lead to bigger-versions of
    I.
    Input:
        obj I: scipy img
        float rszFac:
    Output:
        A resized image Iout.
    """
    if rszFac == 1:
        return I
    else:
        # Almost certain that this np.copy(I) is not necessary, since cv.Resize
        # will interpolate new pixel values in Iout. Removing np.copy(I) didn't
        # seem to affect results, and speeds things up. -- EK
        # Icv=cv.fromarray(np.copy(I))
        if not I.flags.c_contiguous:
            # numpy requires arrays be contiguous in memory - when you
            # pass it to OpenCV, then try to re-convert back to numpy,
            # you get an error in np.asarray if I isn't contiguous.
            I = np.copy(I)  # Makes I contiguous
        Icv = cv.fromarray(I)
        I1cv = cv.CreateMat(int(math.floor(I.shape[0] * rszFac)),
                            int(math.floor(I.shape[1] * rszFac)), Icv.type)
        cv.Resize(Icv, I1cv, interpolation=cv.CV_INTER_AREA)
        Iout = np.asarray(I1cv)
        if sig > 0:
            Iout = gaussian_filter(Iout, sig)

        return Iout
Example #27
0
def normalisation(src):

    res = []

    # On fait une copie l'image pour le traitement (en gris)
    gris = cv.CreateImage((src.width, src.height), cv.IPL_DEPTH_8U, 1)
    normal = cv.CreateImage((NORM_W, NORM_H), cv.IPL_DEPTH_8U, 1)
    cv.CvtColor(src, gris, cv.CV_BGR2GRAY)

    # On detecte les visages (objects) sur l'image copiee
    faces = cv.HaarDetectObjects(gris, face_path, cv.CreateMemStorage())
    print "Nombre faces detectes : " + str(len(faces))
    for (x, y, w, h), n in faces:
        tmp = cv.CreateImage((w, h), cv.IPL_DEPTH_8U, 1)
        cv.GetRectSubPix(gris, tmp, (float(x + w / 2), float(y + h / 2)))

        cv.Resize(tmp, normal)
        cv.EqualizeHist(normal, normal)

        #Detection oeil nez bouche sur l'image source:
        d = detection(normal)

        # On detecte au moins 2 yeux "normaux", au moins un oeil avec lunette, au moins une bouche et au moins un nez
        if ((len(d['eyes']) >= 2 or len(d['eyes2']) >= 1)
                and len(d['mouth']) >= 1 and len(d['nose']) >= 1):
            print "Visage detecte dans la photo"
            res.append((normal, (x, y, w, h)))
    return res
Example #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)
Example #29
0
def detect_and_save(img, cascade, output_name):
    # allocate temporary images
    gray = cv.CreateImage((img.width, img.height), 8, 1)
    small_img = cv.CreateImage((cv.Round(
        img.width / image_scale), cv.Round(img.height / image_scale)), 8, 1)

    # convert color input image to grayscale
    cv.CvtColor(img, gray, cv.CV_BGR2GRAY)

    # scale input image for faster processing
    cv.Resize(gray, small_img, cv.CV_INTER_LINEAR)

    cv.EqualizeHist(small_img, small_img)

    if (cascade):
        t = cv.GetTickCount()
        faces = cv.HaarDetectObjects(small_img, cascade,
                                     cv.CreateMemStorage(0), haar_scale,
                                     min_neighbors, haar_flags, min_size)
        t = cv.GetTickCount() - t
        print "detection time = %gms" % (t / (cv.GetTickFrequency() * 1000.))
        f = open(output_name, 'w')
        if faces:
            for ((x, y, w, h), n) in faces:
                f.write("%d %d %d %d\n" % (x, y, w, h))
Example #30
0
    def DetectFace(self,image, faceCascade):
     
        min_size = (20,20)
        image_scale = 2
        haar_scale = 1.1
        min_neighbors = 3
        haar_flags = 0
     
        # Allocate the temporary images
        grayscale = cv.CreateImage((image.width, image.height), 8, 1)
        #grayscale = cv.CreateImage((image.shape[1], image.shape[0]), 8, 1)
        smallImage = cv.CreateImage(
                (
                    cv.Round(image.width / image_scale),
                    cv.Round(image.height / image_scale)
                ), 8 ,1)
     
        # Convert color input image to grayscale
        cv.CvtColor(image, grayscale, cv.CV_BGR2GRAY)
     
        # Scale input image for faster processing
        cv.Resize(grayscale, smallImage, cv.CV_INTER_LINEAR)
     
        # Equalize the histogram
        cv.EqualizeHist(smallImage, smallImage)
     
        # Detect the faces
        faces = cv.HaarDetectObjects(
                smallImage, faceCascade, cv.CreateMemStorage(0),
                haar_scale, min_neighbors, haar_flags, min_size
            )
     
        # If faces are found
        if faces:
            payload=targets()
            payload.ref_img_width=smallImage.width
            payload.ref_img_height=smallImage.height
            for ((x, y, w, h), n) in faces:
                # the input to cv.HaarDetectObjects was resized, so scale the
                # bounding box of each face and convert it to two CvPoints
                pt1 = (int(x * image_scale), int(y * image_scale))
                pt2 = (int((x + w) * image_scale), int((y + h) * image_scale))
                cv.Rectangle(image, pt1, pt2, cv.RGB(255, 0, 0), 5, 8, 0)
                fbox=face_box()
                fbox.top_left.x=float(x)/float(smallImage.width)
                fbox.top_left.y=float(y)/float(smallImage.height)
                fbox.width_height.x=float(w)/float(smallImage.width)
                fbox.width_height.y=float(h)/float(smallImage.height)
                payload.faces.append(fbox)
                #fpt=Point()
                #fpt.x=(float(x) + float(w)/2.0)/float(smallImage.width)
                #fpt.y=(float(y) + float(h)/2.0)/float(smallImage.height)
                #payload.faces.append(fpt)

            msg = payload

            rospy.loginfo(msg)
            self.pub.publish(msg)
     
        return image