def setParams(self, i_resize_scale=1, i_filter_size=0, i_eq=False, i_roi=None):
     self.__resize_scale = i_resize_scale
     self.__filter_size =  i_filter_size
     if not i_roi == None:
         self.__roi = cv.cvRect( i_roi.x, i_roi.y, i_roi.width, i_roi.height )
     else:
         self.__roi = None
     self.__equalise_hist  = i_eq
Exemple #2
0
def Numpy2CvRect(i_min_row=None, i_min_col=None, i_max_row=None, i_max_col=None, i_face_roi=None):
    """Convert roi from matrix format (min_row, min_col, max_row, max_col) to opencv rect"""
    if i_face_roi is not None:
        (i_min_row, i_min_col, i_max_row, i_max_col) = i_face_roi
    y = i_min_row
    x = i_min_col
    height  = i_max_row - y
    width =i_max_col - x
    return cv.cvRect(x, y, width, height)     
 def jitter( self, i_image , i_n_examples ):
     """1) Apply various random affine transform to i_image (scale, rotation), 
           where i_n_examples is the number of transformations. The affine transforms happen 
           around the center of the original region of interest. 
        2) Translate roi with various values - crop the image from this region.
        3) The same normalisation is then applied to all the cropped images, specified by setParams"""
     
     #Store transforms applied in format tx ty scale angle
     o_transforms = numpy.array([0., 0.,  1.,  0.]) 
     
     if self.__roi == None:
         print "No region of interest - returning input image!"
         return None
     #Always return the normalised verion of the input image
     image = cv.Ipl2NumPy(self.normalise(i_image))
     o_data = numpy.tile( None, (image.shape[0], image.shape[1], 1))
     o_data[:,:,0] =  image
     if i_n_examples == 0:
         return ( o_data, o_transforms)
     #Rotation point should be around original roi center
     center = cv.cvPoint2D32f( self.__roi.x + self.__roi.width/2,self.__roi.y + self.__roi.height/2 )
     angles = numpy.random.uniform(-30.,30.,i_n_examples)
     scales = numpy.random.uniform(0.9,1.1, i_n_examples)
     tx = numpy.int32( numpy.round( numpy.random.uniform(-20,20,i_n_examples)))
     ty = numpy.int32( numpy.round( numpy.random.uniform(-20,20,i_n_examples)))
     x = self.__roi.x + tx
     y = self.__roi.y + ty
     #FIXME: Extend valid indices to outliers due to affine transform!!!
     min_x = 0; min_y = 0;
     max_x = i_image.width - self.__roi.width - 1
     max_y = i_image.height - self.__roi.height - 1  
     valid_x = numpy.hstack([numpy.nonzero( x >= min_x )[0], numpy.nonzero( x < max_x)[0]])
     valid_y = numpy.hstack([numpy.nonzero( y >= min_y )[0], numpy.nonzero( y < max_y)[0]])
     valid_idx = numpy.unique(numpy.hstack([valid_x, valid_y]))
     original_roi = cv.cvRect( self.__roi.x,  self.__roi.y,  self.__roi.width,  self.__roi.height)
     if self.__rot_mat == None:
         original_rot_matrix = None
     else:
         original_rot_matrix = cv.cvCloneImage(self.__rot_mat)
     for index in valid_idx:
         params = numpy.array([tx[index], ty[index], scales[index], angles[index] ]) 
         self.setAffineTransform( center, scales[index], angles[index])
         self.__roi.x = int( x[index] )
         self.__roi.y = int( y[index] )     
         image = cv.Ipl2NumPy(self.normalise( i_image ))
         o_data = numpy.dstack( [o_data, image])
         o_transforms = numpy.vstack( [o_transforms, params])  
     #Restore the original region of interest
     self.__roi.x = original_roi.x
     self.__roi.y = original_roi.y
     if original_rot_matrix == None:
         self.__rot_mat= None
     else:
         self.__rot_mat = cv.cvCloneImage(original_rot_matrix)
     return (o_data, o_transforms)
 def setParams(self,
               i_resize_scale=1,
               i_filter_size=0,
               i_eq=False,
               i_roi=None):
     self.__resize_scale = i_resize_scale
     self.__filter_size = i_filter_size
     if not i_roi == None:
         self.__roi = cv.cvRect(i_roi.x, i_roi.y, i_roi.width, i_roi.height)
     else:
         self.__roi = None
     self.__equalise_hist = i_eq
 def similarityTransform(self, i_image, i_transform, i_crop=True ):
     """Apply affine transform around center of region of interest, then translate roi"""
     tx = numpy.int32(numpy.round(i_transform[0]))
     ty = numpy.int32(numpy.round(i_transform[1]))
     scale = i_transform[2]
     angle = i_transform[3]
     center = cv.cvPoint2D32f( self.__roi.x + self.__roi.width/2,self.__roi.y + self.__roi.height/2 )
     self.setAffineTransform( center, scale, angle)
     original_roi = cv.cvRect( self.__roi.x,  self.__roi.y,  self.__roi.width,  self.__roi.height)
     
     if not i_crop:
         self.__roi = None
     else:
         self.__roi.x = self.__roi.x + int(tx)
         self.__roi.y = self.__roi.y + int(ty)
     filter_size = self.__filter_size
     self.__filter_size = 0
     o_image =  self.normalise(i_image)
     self.__filter_size = filter_size
     self.__roi = cv.cvRect(original_roi.x, original_roi.y, original_roi.width,  original_roi.height)
     return  cv.Ipl2NumPy(o_image)
Exemple #6
0
def Numpy2CvRect(i_min_row=None,
                 i_min_col=None,
                 i_max_row=None,
                 i_max_col=None,
                 i_face_roi=None):
    """Convert roi from matrix format (min_row, min_col, max_row, max_col) to opencv rect"""
    if i_face_roi is not None:
        (i_min_row, i_min_col, i_max_row, i_max_col) = i_face_roi
    y = i_min_row
    x = i_min_col
    height = i_max_row - y
    width = i_max_col - x
    return cv.cvRect(x, y, width, height)
Exemple #7
0
    def detect(self, camera):
        # Kamera veya görüntüden bir kare al
        frame = cvQueryFrame(camera)
        if not frame:
            return (None, None)
        cropped = cvCreateImage(cvSize(240, 240), 8, 3)
        src_region = cvGetSubRect(frame, opencv.cvRect(0, 0, 240, 240))
        cvCopy(src_region, cropped)
        frame = cropped
        # cvQueryFrame ile alınan görüntü değiştirilemeyeceğinden, kopyala
        copy = cvCreateImage(cvSize(frame.width, frame.height), IPL_DEPTH_8U,
                             frame.nChannels)

        # Eğer kameradan alınan görüntünün orijin noktası sol üst köşe ise(top down), kopyala
        if (frame.origin == IPL_ORIGIN_TL):
            cvCopy(frame, copy)
# Değilse(bottom up IPL_ORIGIN_BL ise), x ekseni etrafında döndür ve kopyala
        else:
            cvFlip(frame, copy, 0)

        # image_scale a göre küçült
        small_img = cvCreateImage(
            cvSize(cvRound(copy.width / self.image_scale),
                   cvRound(copy.height / self.image_scale)), 8, 1)
        # küçültülmemiş kopyasını yap
        gray = cvCreateImage(cvSize(copy.width, copy.height), 8, 1)
        # grileştir
        cvCvtColor(copy, gray, CV_BGR2GRAY)
        # küçük gri halini small_img'a koy
        cvResize(gray, small_img, CV_INTER_LINEAR)
        # parlaklığını normalleştir ve kontrastı arttır
        cvEqualizeHist(small_img, small_img)
        # storage'ı temizle. bu fonksiyon hafızadan alanı deallocate etmez!
        cvClearMemStorage(self.storage)

        # Yüz tanıma işlemi
        t = cvGetTickCount()
        faces = cvHaarDetectObjects(
            small_img,  # yüzlerin aranacağı görüntü
            self.cascade,  # önceden belirli tanıma verisi
            self.storage,  # sonuçların tutulacağı hafıza bölgesi
            # aşağıdakilerin açıklaması sınıf tanımlamasının başında
            self.haar_scale,
            self.min_neighbors,
            self.haar_flags,
            self.min_size)
        t = cvGetTickCount() - t
        #print "detection time = %gms" % (t/(cvGetTickFrequency()*1000.))
        return frame, faces
    def similarityTransform(self, i_image, i_transform, i_crop=True):
        """Apply affine transform around center of region of interest, then translate roi"""
        tx = numpy.int32(numpy.round(i_transform[0]))
        ty = numpy.int32(numpy.round(i_transform[1]))
        scale = i_transform[2]
        angle = i_transform[3]
        center = cv.cvPoint2D32f(self.__roi.x + self.__roi.width / 2,
                                 self.__roi.y + self.__roi.height / 2)
        self.setAffineTransform(center, scale, angle)
        original_roi = cv.cvRect(self.__roi.x, self.__roi.y, self.__roi.width,
                                 self.__roi.height)

        if not i_crop:
            self.__roi = None
        else:
            self.__roi.x = self.__roi.x + int(tx)
            self.__roi.y = self.__roi.y + int(ty)
        filter_size = self.__filter_size
        self.__filter_size = 0
        o_image = self.normalise(i_image)
        self.__filter_size = filter_size
        self.__roi = cv.cvRect(original_roi.x, original_roi.y,
                               original_roi.width, original_roi.height)
        return cv.Ipl2NumPy(o_image)
Exemple #9
0
    def detect(self,camera):
        # Kamera veya görüntüden bir kare al
        frame = cvQueryFrame(camera)
	if not frame:
	    return (None,None)
	cropped = cvCreateImage( cvSize(240, 240), 8, 3)
	src_region = cvGetSubRect(frame, opencv.cvRect(0, 0, 240, 240) )
	cvCopy(src_region, cropped)
	frame = cropped
	# cvQueryFrame ile alınan görüntü değiştirilemeyeceğinden, kopyala
        copy = cvCreateImage(cvSize(frame.width, frame.height), IPL_DEPTH_8U, frame.nChannels)
	
	
	# Eğer kameradan alınan görüntünün orijin noktası sol üst köşe ise(top down), kopyala
        if (frame.origin == IPL_ORIGIN_TL):
            cvCopy(frame, copy)
	# Değilse(bottom up IPL_ORIGIN_BL ise), x ekseni etrafında döndür ve kopyala
        else:
            cvFlip(frame, copy, 0)
        
        # image_scale a göre küçült
        small_img = cvCreateImage(cvSize(cvRound(copy.width / self.image_scale), cvRound(copy.height / self.image_scale)), 8, 1)
	# küçültülmemiş kopyasını yap
        gray = cvCreateImage(cvSize(copy.width, copy.height), 8, 1)
	# grileştir
        cvCvtColor(copy, gray, CV_BGR2GRAY)
	# küçük gri halini small_img'a koy
        cvResize(gray, small_img, CV_INTER_LINEAR)
	# parlaklığını normalleştir ve kontrastı arttır
        cvEqualizeHist(small_img, small_img)
        # storage'ı temizle. bu fonksiyon hafızadan alanı deallocate etmez!
        cvClearMemStorage(self.storage)
        
        # Yüz tanıma işlemi
        t = cvGetTickCount()
        faces = cvHaarDetectObjects(
            small_img, 		# yüzlerin aranacağı görüntü
            self.cascade,	# önceden belirli tanıma verisi
            self.storage,	# sonuçların tutulacağı hafıza bölgesi
	    # aşağıdakilerin açıklaması sınıf tanımlamasının başında
            self.haar_scale,		
            self.min_neighbors,
            self.haar_flags,
	    self.min_size
        )
        t = cvGetTickCount() - t
        #print "detection time = %gms" % (t/(cvGetTickFrequency()*1000.))
        return frame, faces
Exemple #10
0
def tile_images(img_width, img_height, num_width, num_height, images, channels=3):
    w = img_width * num_width
    h = img_height * num_height
    image = cv.cvCreateImage(cv.cvSize(int(w), int(h)), 8, channels)
    cv.cvSet(image, cv.cvScalar(255,255,255))
    while len(images) > 0:
        try:
            for y in range(int(num_height)):
                for x in range(int(num_width)):
                    small_tile = images.pop()
                    img_x = x * img_width
                    img_y = y * img_height
                    cropped = cv.cvGetSubRect(image, cv.cvRect(img_x, img_y, img_width,img_height))
                    cv.cvCopy(small_tile, cropped)
        except exceptions.IndexError, e:
            break
def tile_images(img_width,
                img_height,
                num_width,
                num_height,
                images,
                channels=3):
    w = img_width * num_width
    h = img_height * num_height
    image = cv.cvCreateImage(cv.cvSize(int(w), int(h)), 8, channels)
    cv.cvSet(image, cv.cvScalar(255, 255, 255))
    while len(images) > 0:
        try:
            for y in range(int(num_height)):
                for x in range(int(num_width)):
                    small_tile = images.pop()
                    img_x = x * img_width
                    img_y = y * img_height
                    cropped = cv.cvGetSubRect(
                        image, cv.cvRect(img_x, img_y, img_width, img_height))
                    cv.cvCopy(small_tile, cropped)
        except exceptions.IndexError, e:
            break
 def setRoi(self, i_roi):
     self.__roi = cv.cvRect(i_roi.x, i_roi.y, i_roi.width, i_roi.height)
 def getRoi(self):
     if self.__roi is None:
         return None
     return cv.cvRect(self.__roi.x, self.__roi.y, self.__roi.width,
                      self.__roi.height)
    def jitter(self, i_image, i_n_examples):
        """1) Apply various random affine transform to i_image (scale, rotation), 
              where i_n_examples is the number of transformations. The affine transforms happen 
              around the center of the original region of interest. 
           2) Translate roi with various values - crop the image from this region.
           3) The same normalisation is then applied to all the cropped images, specified by setParams"""

        #Store transforms applied in format tx ty scale angle
        o_transforms = numpy.array([0., 0., 1., 0.])

        if self.__roi == None:
            print "No region of interest - returning input image!"
            return None
        #Always return the normalised verion of the input image
        image = cv.Ipl2NumPy(self.normalise(i_image))
        o_data = numpy.tile(None, (image.shape[0], image.shape[1], 1))
        o_data[:, :, 0] = image
        if i_n_examples == 0:
            return (o_data, o_transforms)
        #Rotation point should be around original roi center
        center = cv.cvPoint2D32f(self.__roi.x + self.__roi.width / 2,
                                 self.__roi.y + self.__roi.height / 2)
        angles = numpy.random.uniform(-30., 30., i_n_examples)
        scales = numpy.random.uniform(0.9, 1.1, i_n_examples)
        tx = numpy.int32(
            numpy.round(numpy.random.uniform(-20, 20, i_n_examples)))
        ty = numpy.int32(
            numpy.round(numpy.random.uniform(-20, 20, i_n_examples)))
        x = self.__roi.x + tx
        y = self.__roi.y + ty
        #FIXME: Extend valid indices to outliers due to affine transform!!!
        min_x = 0
        min_y = 0
        max_x = i_image.width - self.__roi.width - 1
        max_y = i_image.height - self.__roi.height - 1
        valid_x = numpy.hstack(
            [numpy.nonzero(x >= min_x)[0],
             numpy.nonzero(x < max_x)[0]])
        valid_y = numpy.hstack(
            [numpy.nonzero(y >= min_y)[0],
             numpy.nonzero(y < max_y)[0]])
        valid_idx = numpy.unique(numpy.hstack([valid_x, valid_y]))
        original_roi = cv.cvRect(self.__roi.x, self.__roi.y, self.__roi.width,
                                 self.__roi.height)
        if self.__rot_mat == None:
            original_rot_matrix = None
        else:
            original_rot_matrix = cv.cvCloneImage(self.__rot_mat)
        for index in valid_idx:
            params = numpy.array(
                [tx[index], ty[index], scales[index], angles[index]])
            self.setAffineTransform(center, scales[index], angles[index])
            self.__roi.x = int(x[index])
            self.__roi.y = int(y[index])
            image = cv.Ipl2NumPy(self.normalise(i_image))
            o_data = numpy.dstack([o_data, image])
            o_transforms = numpy.vstack([o_transforms, params])
        #Restore the original region of interest
        self.__roi.x = original_roi.x
        self.__roi.y = original_roi.y
        if original_rot_matrix == None:
            self.__rot_mat = None
        else:
            self.__rot_mat = cv.cvCloneImage(original_rot_matrix)
        return (o_data, o_transforms)
 def setRoi(self, i_roi):
     self.__roi = cv.cvRect( i_roi.x, i_roi.y, i_roi.width, i_roi.height )
 def getRoi(self):
     if self.__roi is None:
         return None
     return cv.cvRect(self.__roi.x, self.__roi.y, self.__roi.width, self.__roi.height)