def correctImage(self, i_image, i_transform): roi = self.getRoi() tx = numpy.int(numpy.round(i_transform[0])) ty = numpy.int(numpy.round(i_transform[1])) roi.x += tx roi.y += ty self.setRoi(roi) 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) o_image = self.normalise(i_image) return cv.Ipl2NumPy(o_image)
def IplList2Numpy(i_data, i_dstack=True): o_data = None for current_frame in i_data: image = cv.Ipl2NumPy(current_frame) if o_data is None: if i_dstack: o_data = image else: o_data = image.flatten() else: #Either dstack or flatten and vstack if i_dstack: o_data = numpy.dstack([o_data, image]) else: o_data = numpy.vstack([o_data, image.flatten()]) return o_data
def Video2Numpy(i_file, i_scale=1.): from frame_grabber import FrameGrabberFile """Return a 3D numpy array from a video file""" frame_grabber = FrameGrabberFile(i_file, i_loop_back=False) o_data = None nframes = 0 while True: current_frame = frame_grabber.nextFrame() if current_frame == None: break else: current_frame = frame_grabber.currentFrame() w = int(i_scale * float(current_frame.width) + 0.5) h = int(i_scale * float(current_frame.height) + 0.5) image = IplResizeAndConvert(current_frame, w, h) image = cv.Ipl2NumPy(image) if o_data == None: o_data = image else: o_data = numpy.dstack([o_data, image]) return o_data
def PlotRoi(i_ipl_image=None, i_ipl_roi=None, i_numpy_img=None): import pylab if i_numpy_img is not None: img = i_numpy_img pylab.imshow(img) else: img = cv.Ipl2NumPy(i_ipl_image) pylab.imshow(img, cmap=pylab.cm.gray) x = [ i_ipl_roi.x, i_ipl_roi.x + i_ipl_roi.width, i_ipl_roi.x + i_ipl_roi.width, i_ipl_roi.x, i_ipl_roi.x ] y = [ i_ipl_roi.y, i_ipl_roi.y, i_ipl_roi.y + i_ipl_roi.height, i_ipl_roi.y + i_ipl_roi.height, i_ipl_roi.y ] pylab.plot(x, y, 'r') disp_str = "Roi: x=" + str(i_ipl_roi.x) + " y=" + str( i_ipl_roi.y) + " width=" + str(i_ipl_roi.width) + " height=" #disp_str += (str(i_ipl_roi.height) + "\n"+"Image: " + str(i_ipl_image.width) + " " + str(i_ipl_image.height)) pylab.title(disp_str) pylab.axis('image')
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)
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)