Ejemplo n.º 1
0
    def apply_net(self, input_image, perform_downsample=True, perform_pad=True, perform_upsample=True, perform_blur=True, perform_offset=True):

        if perform_pad:
            input_image = np.pad(input_image, ((self.pad_by, self.pad_by), (self.pad_by, self.pad_by)), 'symmetric')

        if perform_downsample:
            input_image = np.float32(mahotas.imresize(input_image, 1.0/self.downsample))

        layer_temp = input_image.reshape(1, input_image.shape[0], input_image.shape[1])

        for layeri in range(len(self.all_layers)):
            layer_temp = self.all_layers[layeri].apply_layer(layer_temp)

        output_image = layer_temp[0,:,:]

        if perform_upsample:
            output_image = np.float32(mahotas.imresize(output_image, self.downsample))

        if perform_blur:
            output_image = scipy.ndimage.filters.gaussian_filter(output_image, self.best_sigma)

        if perform_offset:
            #Translate
            output_image = np.roll(output_image, self.best_offset[0], axis=0)
            output_image = np.roll(output_image, self.best_offset[1], axis=1)

        # Crop to valid size
        output_image = output_image[self.pad_by:-self.pad_by,self.pad_by:-self.pad_by]

        return output_image
Ejemplo n.º 2
0
def test_imresize():
    img = np.repeat(np.arange(100), 10).reshape((100, 10))
    assert imresize(img, (1024, 104)).shape == (1024, 104)
    assert imresize(img, (10., 10.)).shape == (1000, 100)
    assert imresize(
        img,
        .2,
    ).shape == (20, 2)
    assert imresize(img, (10., 2.)).shape == (1000, 20)
Ejemplo n.º 3
0
    def apply_net(self, input_image, perform_downsample=False, perform_pad=False, perform_upsample=False, perform_blur=False, perform_offset=False):

        if perform_pad:
            input_image = np.pad(input_image, ((self.pad_by, self.pad_by), (self.pad_by, self.pad_by)), 'symmetric')

        if perform_downsample and self.downsample != 1:
            input_image = np.float32(mahotas.imresize(input_image, 1.0/self.downsample))

        nx = input_image.shape[0] - self.pad_by*2
        ny = input_image.shape[1] - self.pad_by*2
        nbatches = nx * ny

        output = np.zeros((nx, ny), dtype=np.float32)

        t_input_image = theano.shared(np.asarray(input_image,dtype=theano.config.floatX),borrow=True)

        index_x = T.lscalar()
        index_y = T.lscalar()

        # eval_network_l0 = theano.function([index_x, index_y], self.all_layers[0].output,
        #     givens={self.x: t_input_image[index_x:index_x + self.pad_by * 2 + 1, index_y:index_y + self.pad_by * 2 + 1]})
        # eval_network_l1 = theano.function([index_x, index_y], self.all_layers[1].output,
        #     givens={self.x: t_input_image[index_x:index_x + self.pad_by * 2 + 1, index_y:index_y + self.pad_by * 2 + 1]})
        # eval_network_l2 = theano.function([index_x, index_y], self.all_layers[2].output,
        #     givens={self.x: t_input_image[index_x:index_x + self.pad_by * 2 + 1, index_y:index_y + self.pad_by * 2 + 1]})
        eval_network = theano.function([index_x, index_y], self.all_layers[-1].output,
            givens={self.x: t_input_image[index_x:index_x + self.pad_by * 2 + 1, index_y:index_y + self.pad_by * 2 + 1]})

        for xi in range(nx):
            for yi in range(ny):
                # print eval_network_l0(xi, yi)[0,0,:,:]
                # print eval_network_l1(xi, yi)[0,0,:,:]
                # print eval_network_l2(xi, yi)[0,0,:,:]
                # print eval_network(xi, yi)[0,0]
                output[xi, yi] = eval_network(xi, yi)[0,0]
            print "up to x={0} of {1}".format(xi+1, nx)


        if perform_upsample:
            output = np.float32(mahotas.imresize(output, self.downsample))

        if perform_blur and self.best_sigma != 0:
            output = scipy.ndimage.filters.gaussian_filter(output, self.best_sigma)

        if perform_offset:
            #Translate
            output = np.roll(output, self.best_offset[0], axis=0)
            output = np.roll(output, self.best_offset[1], axis=1)

        # Crop to valid size
        #output = output[self.pad_by:-self.pad_by,self.pad_by:-self.pad_by]

        return output
def write_image (output_path, data, image_num=0, downsample=1):
    if downsample != 1:
        data = np.float32(mahotas.imresize(data, downsample))
    maxdata = np.max(data)
    mindata = np.min(data)
    normdata = (np.float32(data) - mindata) / (maxdata - mindata)
    mahotas.imsave(output_path, np.uint16(normdata * 65535))
Ejemplo n.º 5
0
def write_image(output_path, data, image_num=0, downsample=1):
    if downsample != 1:
        data = np.float32(mahotas.imresize(data, downsample))
    maxdata = np.max(data)
    mindata = np.min(data)
    normdata = (np.float32(data) - mindata) / (maxdata - mindata)
    mahotas.imsave(output_path, np.uint16(normdata * 65535))
Ejemplo n.º 6
0
  def get_image_block(self):
    """ Loops through all images belonging to a Site, flattens pixel values,
    and returns a pandas DataFrame for all values

    If image dimensions are inconsistent, the images will be rescaled to the
    largest image size.

    :return pandas DataFrame of flattened pixel values, along with image
    coordinates and Site name
    """
    imgs = {i.name if i.maskName is None else i.maskName:i.get_image()
            for i in self.images}
    mask_names = [i.maskName for i in self.images
                  if i.maskName is not None]
    maxRes = max((i.shape for i in imgs.values()))
    rr, cc = np.mgrid[0:maxRes[0], 0:maxRes[1]]
    imgBlock = {'imgRow': rr.astype(np.uint16).flatten(),
                "imgCol": cc.astype(np.uint16).flatten()}
    for k, v in imgs.items():
      if v.dtype != np.bool and v.shape != maxRes:
        imgBlock.update({k: mh.imresize(v, maxRes, order=3).flatten()
                     })
      else:
        imgBlock.update({k: v.flatten()})

    df = pd.DataFrame(imgBlock)

    df.loc[:, mask_names] = df.loc[:, mask_names].fillna(False).astype(np.bool)

    df['site'] = self.name
    return df
 def output_image (data, layer, index, unpad_by, image_num=0, downsample=1):
     data = data[unpad_by:data.shape[0]-unpad_by,unpad_by:data.shape[1]-unpad_by]
     if downsample != 1:
         data = np.float32(mahotas.imresize(data, downsample))
     maxdata = np.max(data)
     mindata = np.min(data)
     normdata = (np.float32(data) - mindata) / (maxdata - mindata)
     mahotas.imsave(output_path + '/{0:04d}_classify_output_layer{1}_{2}.tif'.format(image_num, layer, index), np.uint16(normdata * 65535))
Ejemplo n.º 8
0
    def preprocess(self, frame):
        # Uncomment the below if using an environment other than Atari Breakout
        # In the standard preprocessing, the bottom and top 13 pixels are cropped out, but this crops the agent's paddle
        # out of the frame in Breakout, making it impossible to converge. For all other environments, you should uncomment
        # the line below to use the standard preprocessing algorithm, and comment out the alternative currently in use.

        # return mahotas.imresize(mahotas.colors.rgb2grey(frame), (84, 110))[:, 13:-13] / 255.0
        return mahotas.imresize(mahotas.colors.rgb2grey(frame),
                                (84, 84)) / 255.0
Ejemplo n.º 9
0
    def apply_net(self, input_image, perform_downsample=False, perform_pad=False, perform_upsample=False, perform_blur=False, perform_offset=False):

        if perform_pad:
            input_image = np.pad(input_image, ((self.pad_by, self.pad_by), (self.pad_by, self.pad_by)), 'symmetric')

        if perform_downsample and self.downsample != 1:
            input_image = np.float32(mahotas.imresize(input_image, 1.0/self.downsample))

        nx = input_image.shape[0] - self.all_layers[0].input_footprint + 1
        ny = input_image.shape[1] - self.all_layers[0].input_footprint + 1
        nbatches = nx * ny
        layer_temp = np.zeros((nbatches, 1, self.all_layers[0].input_footprint, self.all_layers[0].input_footprint), dtype=np.float32)

        batchi = 0
        for x in range(nx):
            for y in range(ny):
                #print (x,y)
                layer_temp[batchi, :, :, :] = input_image[x:(x + self.all_layers[0].input_footprint), y:(y + self.all_layers[0].input_footprint)]
                batchi += 1

        assert batchi == nbatches

        for layeri in range(len(self.all_layers)):
            print 'Layer {0}.'.format(layeri)
            layer_temp = self.all_layers[layeri].apply_layer(layer_temp)

        output_image = layer_temp[:,0,0,0].reshape(nx, ny)

        if perform_upsample:
            output_image = np.float32(mahotas.imresize(output_image, self.downsample))

        if perform_blur and self.best_sigma != 0:
            output_image = scipy.ndimage.filters.gaussian_filter(output_image, self.best_sigma)

        if perform_offset:
            #Translate
            output_image = np.roll(output_image, self.best_offset[0], axis=0)
            output_image = np.roll(output_image, self.best_offset[1], axis=1)

        # Crop to valid size
        #output_image = output_image[self.pad_by:-self.pad_by,self.pad_by:-self.pad_by]

        return output_image
Ejemplo n.º 10
0
    def apply_net(self,
                  input_image,
                  perform_downsample=True,
                  perform_pad=True,
                  perform_upsample=True,
                  perform_blur=True,
                  perform_offset=True):

        if perform_pad:
            input_image = np.pad(input_image, ((self.pad_by, self.pad_by),
                                               (self.pad_by, self.pad_by)),
                                 'symmetric')

        if perform_downsample:
            input_image = np.float32(
                mahotas.imresize(input_image, 1.0 / self.downsample))

        layer_temp = input_image.reshape(1, input_image.shape[0],
                                         input_image.shape[1])

        for layeri in range(len(self.all_layers)):
            layer_temp = self.all_layers[layeri].apply_layer(layer_temp)

        output_image = layer_temp[0, :, :]

        if perform_upsample:
            output_image = np.float32(
                mahotas.imresize(output_image, self.downsample))

        if perform_blur:
            output_image = scipy.ndimage.filters.gaussian_filter(
                output_image, self.best_sigma)

        if perform_offset:
            #Translate
            output_image = np.roll(output_image, self.best_offset[0], axis=0)
            output_image = np.roll(output_image, self.best_offset[1], axis=1)

        # Crop to valid size
        output_image = output_image[self.pad_by:-self.pad_by,
                                    self.pad_by:-self.pad_by]

        return output_image
Ejemplo n.º 11
0
 def output_image(data, layer, index, unpad_by, image_num=0, downsample=1):
     data = data[unpad_by:data.shape[0] - unpad_by,
                 unpad_by:data.shape[1] - unpad_by]
     if downsample != 1:
         data = np.float32(mahotas.imresize(data, downsample))
     maxdata = np.max(data)
     mindata = np.min(data)
     normdata = (np.float32(data) - mindata) / (maxdata - mindata)
     mahotas.imsave(
         output_path + '/{0:04d}_classify_output_layer{1}_{2}.tif'.format(
             image_num, layer, index), np.uint16(normdata * 65535))
Ejemplo n.º 12
0
def resized_img(url , percentage , output_url):

    # image = cv2.imread(url)
    image = mh.imread(url)
    image = image[:,:,0]
    # w , h = image.size
    width = int(image.shape[1] * percentage)
    height = int(image.shape[0] * percentage)
    dim = (width , height)
    # resized = cv2.resize(image , dim , interpolation=cv2.INTER_AREA)
    resized = mh.imresize(image , nsize=dim,order=3)
    cv2.imwrite(output_url,resized , [int(cv2.IMWRITE_PNG_COMPRESSION) , 10])
Ejemplo n.º 13
0
    def extract(self):
        '''Extracts morphology features to measure the size and shape of objects.

        Returns
        -------
        pandas.DataFrame
            extracted feature values for each object in `label_image`
        '''
        logger.info('extract morphology features')
        features = list()
        cm = mh.center_of_mass(img=self.label_image > 0,labels=self.label_image)
        for obj in self.object_ids:
            mask = self.get_object_mask_image(obj)
            local_centroid_x = cm[obj][1]
            local_centroid_y = cm[obj][0]
            area = np.float64(np.count_nonzero(mask))
            perimeter = mh.labeled.perimeter(mask)
            if perimeter == 0:
                circularity = np.nan
            else:
                circularity = (4.0 * np.pi * area) / (perimeter**2)
            convex_hull = mh.polygon.fill_convexhull(mask)
            area_convex_hull = np.count_nonzero(convex_hull)
            convexity = area / area_convex_hull
            eccentricity = mh.features.eccentricity(mask)
            major_axis, minor_axis = mh.features.ellipse_axes(mask)
            if major_axis == 0:
                elongation = np.nan
            else:
                elongation = (major_axis - minor_axis) / major_axis
            values = [
                local_centroid_x, local_centroid_y, area, eccentricity, convexity, circularity, perimeter,
                elongation
            ]
            if self.compute_zernike:
                logger.debug('extract Zernike moments for object #%d', obj)
                r = 100
                mask_rs = mh.imresize(mask, [r*2, r*2])
                zernike_values = mh.features.zernike_moments(
                    mask_rs, degree=self._degree, radius=r
                )
                values.extend(zernike_values)
            features.append(values)
        return pd.DataFrame(features, columns=self.names, index=self.object_ids)
def get_images(data):

    n_samples = len(data)
    
    size = image_shape[0] * image_shape[1]
    
    images = np.empty((n_samples, size))
    for i, image in enumerate(data):
        im = mh.imread(image)
        im_grey = mh.colors.rgb2grey(im)
        im_grey= mh.imresize(im_grey, image_shape)
        images[i] = im_grey.ravel()
    
    # global centering
    images_centered = images - images.mean(axis=0)
    
    # local centering
    images_centered -= images_centered.mean(axis=1).reshape(n_samples, -1)
    return images, images_centered
Ejemplo n.º 15
0
def imread(filename, longest=None):
    if mahotas:
        a = mahotas.imread(filename)
        resize, width, height = newsize(a.shape[0], a.shape[1], longest)
        if resize:
            shape = list(a.shape)
            a[0] = width
            a[1] = height
            a = mahotas.imresize(a, shape)
        is16bit = a.dtype in (np.int16,np.uint16)
        a = a.astype(np.float32)
        if is16bit:
            a /= 256.0
        return a
    img = Image.open(filename)
    width, height = im.size
    resize = newsize(width, height, longest)
    if resize:
        img = img.resize((width,height))
    a = fromimage(img).astype(np.float32)
    if len(a.shape) == 3 and a.shape[2] == 4:
        print "Dropping alpha"
        a = a[:,:,0:3]
    return a
Ejemplo n.º 16
0
def salvatoriSnowCover(img_imglist, datetimelist, mask, settings, logger, red,
                       green, blue, middata, rectsw, extent, extent_proj, res,
                       dem, C, C_proj, Cz, hd, td, vd, f, w, interpolate, flat,
                       origin, ax, ay):
    rectsw = bool(float(rectsw))
    middata = bool(float(middata))
    dummyImg = False
    for img in img_imglist:
        try:
            mahotas.imread(img)
            dummyImg = img
            break
        except:
            pass
    if not dummyImg:
        logger.set("All images invalid.")
        return False
    if rectsw:
        logger.set("Obtaining weight mask...")
        params = map(np.copy, [
            extent, extent_proj, res, dem, C, C_proj, Cz, hd, td, vd, f, w,
            interpolate, flat, origin, ax, ay
        ])
        auxfilename = False
        from definitions import AuxDir, TmpDir
        readydata = False
        for hdf in os.listdir(AuxDir):
            if "SNOWCOV001" in hdf:
                try:
                    auxF = h5py.File(os.path.join(AuxDir, hdf), 'r')
                    readyfile = True
                    for i in range(len(params)):
                        attr = auxF['metadata'].attrs["param" + str(i)]
                        if np.prod(np.array([attr]).shape) == 1:
                            if (attr != params[i]):
                                readyfile = False
                        else:
                            if (attr != params[i]).any():
                                readyfile = False
                    if readyfile:
                        logger.set(
                            "Calculation has done before with same parameters, auxillary info is being read from file..."
                        )
                        tiles = np.copy(auxF['metadata'][...]).tolist()
                        for d in auxF:
                            if str(d) == 'metadata':
                                continue
                            varname = str(d).split()[0]
                            tilename = str(d).split()[1]
                            if len(tiles) == 1:
                                exec(varname + "= np.copy(auxF[d])")
                            else:
                                if varname not in locals():
                                    exec(varname + '=None')
                                exec(varname + "=writeData(np.copy(auxF[d])," +
                                     varname +
                                     ",map(int,tilename.split('-')))[0]")
                        auxF.close()
                        logger.set("\tRead.")
                        readydata = True
                        auxfilename = hdf
                        break
                    auxF.close()
                except:
                    try:
                        auxF.close()
                    except:
                        continue
        if not readydata:
            Wp = Georectify1([dummyImg], [datetimelist[0]], mask, settings,
                             logger, extent, extent_proj, res, dem, C, C_proj,
                             Cz, hd, td, vd, f, w, interpolate, flat, origin,
                             ax, ay)[0][1][5]
            logger.set('Writing results for next run...')
            auxfilename = 'SNOWCOV001_' + str(uuid4()) + '.h5'
            auxF = h5py.File(os.path.join(AuxDir, auxfilename), 'w')
            tiles = [[0, 0, Wp.shape[0], Wp.shape[1]]]
            auxF.create_dataset('metadata', data=np.array(tiles))
            for i, p in enumerate(params):
                auxF['metadata'].attrs.create("param" + str(i), p)
            for i, tile in enumerate(tiles):
                Wp_ = readData(Wp, tile)[0]
                auxF.create_dataset('Wp ' + str(tile).replace(
                    ', ', '-').replace('[', '').replace(']', ''),
                                    Wp_.shape,
                                    data=Wp_)
                Wp_ = None
            auxF.close()
        Wp = Wp[::-1]
    else:
        Wp = np.ones(mahotas.imread(dummyImg).shape[:2])
    mask, pgs, th = mask
    mask = LensCorrRadial(mask, '0', logger, origin, ax, ay, 0)[0][1][1]
    Wp *= (mask.transpose(2, 0, 1)[0] == 1)
    if np.mean(mask) == 1:
        logger.set("Weightmask quality: " + str(
            np.sum(Wp[-100:, Wp.shape[1] / 2 - 50:Wp.shape[1] / 2 + 50] != 0) /
            10000))
    else:
        logger.set("Weightmask quality: " +
                   str(1 - np.sum((Wp == 0) *
                                  (mask.transpose(2, 0, 1)[0] == 1)) /
                       float(np.sum((mask.transpose(2, 0, 1)[0] == 1)))))
    logger.set("Calculating snow cover fractions...")
    scr = []
    ssr = []
    snr = []
    mar = []

    scn = []
    ssn = []
    snn = []
    man = []

    time = []
    thr = []
    thg = []
    thb = []

    Wp_full = deepcopy(Wp)
    for i_img, imgf in enumerate(img_imglist):
        try:
            snow = 0
            nosnow = 0
            img = mahotas.imread(imgf)
            if mask.shape != img.shape:
                mask = maskers.polymask(img, pgs, logger)
                Wp = mahotas.imresize(Wp_full, img.shape[:2])
            (img,
             thv) = salvatoriSnowDetect(img, mask * maskers.thmask(img, th),
                                        settings, logger, red, green, blue)
            # mimg = np.dstack((img==1,img==0,img==2)).astype(np.uint8)*255
            if -1 in thv:
                continue
            time = np.append(time, (str(datetimelist[i_img])))
            img = LensCorrRadial(img, str(datetimelist[i_img]), logger, origin,
                                 ax, ay, 0)[0][1][1]
            snow = np.sum(((img == 1) * Wp).astype(int))
            nosnow = np.sum(((img == 0) * Wp).astype(int))
            masked = np.sum(((img == 2) * Wp).astype(int))
            scr = np.append(scr, snow / float(snow + nosnow))
            if middata:
                ssr = np.append(ssr, snow)
                snr = np.append(snr, nosnow)
                mar = np.append(mar, masked)
                snow = np.sum(((img == 1)).astype(int))
                nosnow = np.sum(((img == 0)).astype(int))
                masked = np.sum(((img == 2)).astype(int))
                scn = np.append(scn, snow / float(snow + nosnow))
                ssn = np.append(ssn, snow)
                snn = np.append(snn, nosnow)
                man = np.append(man, masked)
                thr = np.append(thr, thv[0])
                thg = np.append(thg, thv[1])
                thb = np.append(thb, thv[2])
        except Exception as e:
            print(e)
            logger.set("Processing " + imgf + " failed.")
        logger.set('Image: |progress:4|queue:' + str(i_img + 1) + '|total:' +
                   str(len(img_imglist)))
    scr = np.round(scr * 100).astype(np.int32)
    scn = np.round(scn * 100).astype(np.int32)
    if middata:
        return [[
            "Snow Cover Fraction",
            [
                "Time", time, "Snow Cover Fraction", scr,
                "Snow Cover Fraction - Non-Rectified", scn, "Threshold - Red",
                thr, "Threshold - Green", thg, "Threshold - Blue", thb,
                "Snow - Rectified", ssr, "Nosnow - Rectified", snr,
                "Masked - Rectified", mar, "Snow - Non-Rectified", ssn,
                "Nosnow - Non-Rectified", snn, "Masked - Non-Rectified", man
            ]
        ]]
    else:
        return [[
            "Snow Cover Fraction", ["Time", time, "Snow Cover Fraction", scr]
        ]]
Ejemplo n.º 17
0
    def apply_combo_net(self,
                        input_image,
                        block_size=400,
                        stump_input=None,
                        return_parts=False):

        average_image = np.zeros(input_image.shape, dtype=np.float32)

        parts = []

        prev_downsample = 0
        prev_pad_by = 0

        start_time = time.clock()

        for net_i in range(self.nnets):

            net_input = stump_input if self.all_nets[
                net_i].stumpin else input_image

            downsample = self.all_nets[net_i].downsample
            pad_by = self.all_nets[net_i].pad_by

            # Downsample and pad
            if prev_downsample != downsample or prev_pad_by != pad_by:
                preprocessed_image = np.pad(net_input, ((pad_by, pad_by),
                                                        (pad_by, pad_by)),
                                            'symmetric')
                preprocessed_image = np.float32(
                    mahotas.imresize(preprocessed_image, 1.0 / downsample))

            halo = int((pad_by + downsample - 1) / downsample)

            # Compute in blocks (small edges)
            block_x = range(halo, preprocessed_image.shape[0], block_size)
            block_y = range(halo, preprocessed_image.shape[1], block_size)

            # (full edges)
            # block_x = range(halo, preprocessed_image.shape[0] - block_size + 1, block_size)
            # block_y = range(halo, preprocessed_image.shape[1] - block_size + 1, block_size)
            # if preprocessed_image.shape[0] % block_size > 0:
            #     block_x.append(max(halo, preprocessed_image.shape[0] - block_size - halo))
            # if preprocessed_image.shape[1] % block_size > 0:
            #     block_y.append(max(halo, preprocessed_image.shape[1] - block_size - halo))

            blocki = 0
            nblocks = len(block_x) * len(block_y)

            output_image = np.zeros(input_image.shape, dtype=np.float32)

            for from_x in block_x:
                for from_y in block_y:

                    # Crop out a padded input block
                    block = preprocessed_image[from_x - halo:from_x +
                                               block_size + halo, from_y -
                                               halo:from_y + block_size + halo]

                    # Apply network
                    output_block = self.all_nets[net_i].apply_net(
                        block, perform_downsample=False, perform_pad=False)

                    # Output block is not padded
                    to_x = (from_x - halo) * downsample
                    to_y = (from_y - halo) * downsample
                    output_image[to_x:to_x + output_block.shape[0], to_y:to_y +
                                 output_block.shape[1]] = output_block

                    blocki += 1
                    print 'Block {0} of {1} complete.'.format(blocki, nblocks)

            average_image += output_image

            if return_parts:
                parts.append(output_image)

            print 'Net {0} of {1} complete.'.format(net_i + 1, self.nnets)

        average_image /= self.nnets

        end_time = time.clock()

        print('Classification complete.')
        print('Classification code ran for %.2fm' %
              ((end_time - start_time) / 60.))

        return (average_image, parts) if return_parts else average_image
Ejemplo n.º 18
0
def get_blob_element(mask, rect, skel, num_blob_pixels, color, color_variance, grey, depth, label, is_subblob = False):
    # Scale image for scale-invariant shape features
    # Make it so the longest edge is equal to SHAPE_FEATURE_SIZE
    resized_image = mahotas.imresize(mask,
                                     float(SHAPE_FEATURE_SIZE) / max(mask.shape))

    z_moments = zernike_moments(resized_image, SHAPE_FEATURE_SIZE, degree = Z_ORDER)

    blob_element = xml.Element(label)
    
    x_1 = rect[0]
    y_1 = rect[1]
    x_2 = x_1 + rect[2]
    y_2 = y_1 + rect[3]

    blob_element.attrib['x'] = str(rect[0])
    blob_element.attrib['y'] = str(rect[1])
    blob_element.attrib['width'] = str(rect[2])
    blob_element.attrib['height'] = str(rect[3])
    rect_center = (rect[0] + .5 * rect[2],
                   rect[1] + .5 * rect[3])
    if (skel is not None and len(skel) > 0 ):
        blob_element.attrib['head_dist'] = str(distance(rect_center,
                                                skel['HEAD']['2d']))
        blob_element.attrib['right_hand_dist'] = str(distance(rect_center,
                                                skel['RIGHT_HAND']['2d']))
        blob_element.attrib['left_hand_dist'] = str(distance(rect_center,
                                                skel['LEFT_HAND']['2d']))
    
    

    
    features_element = xml.Element('features')
    blob_element.append(features_element)

    size_element = xml.SubElement(features_element, 'size')
    xml.SubElement(size_element, 'pixels').text = str(num_blob_pixels)

    hu_element = get_hu_moments_element(mask)

    features_element.append(hu_element)

    grey_masked_image = grey & mask

    blob_depth = depth & mask

    if is_subblob:
        blob_depth_rect = blob_depth
        blob_mask_rect = mask
    else:
        blob_depth_rect = blob_depth[y_1:y_2, x_1:x_2]
        blob_mask_rect = mask[y_1:y_2, x_1:x_2]


    normal_vector_histogram = get_normal_vector_histogram(blob_depth_rect, blob_mask_rect.astype(numpy.uint8))
    
    #print normal_vector_histogram

    if normal_vector_histogram is None:
        print 'Error calculating normal_vector_histogram'
        return None

    normal_histogram_element = xml.Element('normalhistogram')

    for i in xrange(normal_vector_histogram.size):
        xml.SubElement(normal_histogram_element, 'a_' + str(i)).text = str(normal_vector_histogram[i])

    features_element.append(normal_histogram_element)

    haralick_element = xml.Element('haralick')

    haralick_features = haralick(grey_masked_image)
    # Average the rows (the different directions of the features)
    haralick_features_averaged = numpy.mean(haralick_features,axis=0)
    #print len(haralick_features_averaged)

    for i in xrange(NUM_HARALICK_FEATURES):
        xml.SubElement(haralick_element, 'a_' + str(i)).text = str(haralick_features_averaged[i])

    features_element.append(haralick_element)

    zernike_element = xml.Element('zernike')
        
    for i in xrange(Z_FEATURES):
        xml.SubElement(zernike_element, 'a_' + str(i)).text = str(z_moments[i])

    features_element.append(zernike_element)
    
    rgb_element = xml.Element('rgb')
    xml.SubElement(rgb_element, 'r').text = str(color[0])
    xml.SubElement(rgb_element, 'g').text = str(color[1])
    xml.SubElement(rgb_element, 'b').text = str(color[2])

    features_element.append(rgb_element)
    
    rgb_variance_element = xml.Element('rgb_var')
    xml.SubElement(rgb_variance_element, 'r').text = str(color_variance[0])
    xml.SubElement(rgb_variance_element, 'g').text = str(color_variance[1])
    xml.SubElement(rgb_variance_element, 'b').text = str(color_variance[2])
    
    features_element.append(rgb_variance_element)

    return blob_element
Ejemplo n.º 19
0
def test_imresize():
    img = np.repeat(np.arange(100), 10).reshape((100,10))
    assert imresize(img, (1024,104)).shape == (1024,104)
    assert imresize(img, (10.,10.)).shape == (1000,100)
    assert imresize(img, .2,).shape == (20,2)
    assert imresize(img, (10.,2.)).shape == (1000,20)
Ejemplo n.º 20
0
    def apply_combo_net(self, input_image, block_size=400, stump_input=None, return_parts=False):

        average_image = np.zeros(input_image.shape, dtype=np.float32)

        parts = []

        prev_downsample = 0
        prev_pad_by = 0

        start_time = time.clock()

        for net_i in range(self.nnets):

            net_input = stump_input if self.all_nets[net_i].stumpin else input_image

            downsample = self.all_nets[net_i].downsample
            pad_by = self.all_nets[net_i].pad_by

            # Downsample and pad
            if prev_downsample != downsample or prev_pad_by != pad_by:
                preprocessed_image = np.pad(net_input, ((pad_by, pad_by), (pad_by, pad_by)), 'symmetric')
                preprocessed_image = np.float32(mahotas.imresize(preprocessed_image, 1.0 / downsample))

            halo = int((pad_by + downsample - 1) / downsample)

            # Compute in blocks (small edges)
            block_x = range(halo, preprocessed_image.shape[0], block_size)
            block_y = range(halo, preprocessed_image.shape[1], block_size)

            # (full edges)
            # block_x = range(halo, preprocessed_image.shape[0] - block_size + 1, block_size)
            # block_y = range(halo, preprocessed_image.shape[1] - block_size + 1, block_size)
            # if preprocessed_image.shape[0] % block_size > 0:
            #     block_x.append(max(halo, preprocessed_image.shape[0] - block_size - halo))
            # if preprocessed_image.shape[1] % block_size > 0:
            #     block_y.append(max(halo, preprocessed_image.shape[1] - block_size - halo))

            blocki = 0
            nblocks = len(block_x) * len(block_y)

            output_image = np.zeros(input_image.shape, dtype=np.float32)

            for from_x in block_x:
                for from_y in block_y:

                    # Crop out a padded input block
                    block = preprocessed_image[from_x-halo:from_x+block_size+halo, from_y-halo:from_y+block_size+halo]

                    # Apply network
                    output_block = self.all_nets[net_i].apply_net(block, perform_downsample=False, perform_pad=False)

                    # Output block is not padded
                    to_x = (from_x - halo) * downsample
                    to_y = (from_y - halo) * downsample
                    output_image[to_x:to_x + output_block.shape[0], to_y:to_y + output_block.shape[1]] = output_block

                    blocki += 1
                    print 'Block {0} of {1} complete.'.format(blocki, nblocks)

            average_image += output_image

            if return_parts:
                parts.append(output_image)

            print 'Net {0} of {1} complete.'.format(net_i + 1, self.nnets)

        average_image /= self.nnets

        end_time = time.clock()

        print('Classification complete.')
        print('Classification code ran for %.2fm' % ((end_time - start_time) / 60.))

        return (average_image, parts) if return_parts else average_image
Ejemplo n.º 21
0
    # Main classification loop
    for image_index in range(classify_start, classify_start + classify_n):

        # Normalized training
        input_image, target_image = open_image_and_gold(
            image_index, crop_from, crop_size)

        # Direct pixel intensity training
        #input_image = np.float32(255-mahotas.imread(image_path_format_string.format(image_index))[crop_from[0]:crop_from[0]+crop_size,crop_from[1]:crop_from[1]+crop_size])

        downsample = 1
        if param_file.find('_ds2') != -1:
            downsample = 2
            input_image = np.float32(
                mahotas.imresize(input_image, 1.0 / downsample))
        elif param_file.find('_ds4') != -1:
            downsample = 4
            input_image = np.float32(
                mahotas.imresize(input_image, 1.0 / downsample))

        # Random rotate / mirror
        # mirror = np.random.choice(2)
        # rotate = np.random.choice(4)
        # input_image = rotmir(input_image, mirror, rotate)
        # target_image = rotmir(input_image, mirror, rotate)

        #Pad the image borders so we get a full image output and to avoid edge effects
        pad_image = np.pad(input_image, ((pad_by, pad_by), (pad_by, pad_by)),
                           'symmetric')
        layer0_in = pad_image.reshape(1, pad_image.shape[0],
Ejemplo n.º 22
0
def get_features(train, images):
    ''' 
    Extract features for train or test images
    ------------------------------
    Parameters
    ----------
    train : Boolean
        if Train, get features for train

    images : ndarray
        1-D array of image filepaths

    Returns 
    ----------
    haralicks : ndarray
        1-D flattened array of haralicks features

    lbps : ndarray
        1-D flattened array of linear binary patterns
    
    labels : ndarray
        1-D array of labels for train images

    surf_descriptors : ndarray
        1-D flattened array of surf descriptors feature
    '''

    haralicks = []
    lbps = []
    labels = []
    alldescriptors = []

    if train:
        k = math.sqrt(22425/2)
        path = 'objects/train/'
    else:
        k = math.sqrt(79727/2)
        path = 'objects/test/'

    object_dir_file_num = len([name for name in os.listdir(path) if name.endswith('.obj')])

    if object_dir_file_num == 5:
        haralicks = get_obj(train, 'haralicks')
        lbps = get_obj(train, 'lbps')
        labels = get_obj(train, 'labels')
        surf_descriptors = get_obj(train, 'surfdescriptors')
    else:
        for i, fname in enumerate(images):
            texture = compute_texture(fname)
            binary_patt = compute_lbp(fname)
            haralicks.append(texture)
            lbps.append(binary_patt)
            if train:
                label = fname.split('/')[2]
                labels.append(label)
            
            im = mh.imresize(mh.imread(fname, as_grey=True), (600, 450))
            im = im.astype(np.uint8)

            # Dense sampling of surf 
            surf_desc = surf.dense(im, spacing=16)
            # regular surf 
            # surf.surf(im, descriptor_only=True)
            print('Image {}: {}'.format(i, surf_desc.shape))
            alldescriptors.append(surf_desc)
    
        concatenated = np.concatenate(alldescriptors)
        print('Number of descriptors: {}'.format(
                len(concatenated)))
        concatenated = concatenated[::64]
        
        km = get_kmeans(k, train, concatenated)
        surf_descriptors = []
        for d in alldescriptors:
            c = km.predict(d)
            surf_descriptors.append(np.bincount(c, minlength=k))

        surf_descriptors = np.array(surf_descriptors, dtype=float)
        haralicks = np.array(haralicks)
        lbps = np.array(lbps)
        labels = np.array(labels)

        save_obj(train, 'surfdescriptors', surf_descriptors)
        save_obj(train, 'haralicks', haralicks)
        save_obj(train, 'lbps', lbps)
        save_obj(train, 'labels', labels)


    return haralicks, lbps, labels, surf_descriptors
Ejemplo n.º 23
0
    def apply_net(self, input_image, perform_downsample=False, perform_pad=False, perform_upsample=False, perform_blur=False, perform_offset=False):

        if perform_pad:
            input_image = np.pad(input_image, ((self.pad_by, self.pad_by), (self.pad_by, self.pad_by)), 'symmetric')

        if perform_downsample and self.downsample != 1:
            input_image = np.float32(mahotas.imresize(input_image, 1.0/self.downsample))

        nx = input_image.shape[0] - self.pad_by*2
        ny = input_image.shape[1] - self.pad_by*2
        nbatches = nx * ny


        input_batches = np.zeros((self.batch_size, self.layer0_channels, self.layer0_size, self.layer0_size), dtype=np.float32)

        output = np.zeros(nbatches, dtype=np.float32)

        #t_input_image = theano.shared(np.asarray(input_image,dtype=theano.config.floatX),borrow=True)

        eval_network = theano.function([self.x], [self.all_layers[-1].output])

        batch_count = 0
        batch_start = 0
        batchi = 0

        for xi in range(nx):
            for yi in range(ny):

                input_batches[batchi, :, :, :] = input_image[xi : xi + self.pad_by * 2 + 1, yi : yi + self.pad_by * 2 + 1]

                batchi += 1

                if batchi == self.batch_size:
                    # Classify and reset
                    output[batch_start:batch_start + self.batch_size] = eval_network(input_batches)[0][:,0]
                    batch_count += 1
                    batch_start += self.batch_size
                    batchi = 0

                    if batch_count % 100 == 0:
                        print "Batch {0} done. Up to {1}.".format(batch_count, (xi, yi))

        if batchi > 0:
            output[batch_start:batch_start + batchi] = eval_network(input_batches)[0][:batchi,0]

        output = output.reshape(nx, ny)

        if perform_upsample:
            output = np.float32(mahotas.imresize(output, self.downsample))

        if perform_blur and self.best_sigma != 0:
            output = scipy.ndimage.filters.gaussian_filter(output, self.best_sigma)

        if perform_offset:
            #Translate
            output = np.roll(output, self.best_offset[0], axis=0)
            output = np.roll(output, self.best_offset[1], axis=1)

        # Crop to valid size
        #output = output[self.pad_by:-self.pad_by,self.pad_by:-self.pad_by]

        return output
                randnonmem = random.randrange(len(nonmembrane_indices[0]))
                (samp_i, samp_j) = (nonmembrane_indices[0][randnonmem],
                                    nonmembrane_indices[1][randnonmem])
                membrane_type = "non-membrane"

            samp_vol = np.zeros((imgd, imgd), dtype=np.uint8)

            sample_img = input_vol[samp_i - min_border:samp_i + min_border,
                                   samp_j - min_border:samp_j + min_border]
            if np.random.uniform() > 0.5:
                sample_img = sample_img[::-1, ...]
            sample_img = np.rot90(sample_img, random.randrange(4))

            if DOWNSAMPLE_BY != 1:
                sample_img = np.uint8(
                    mahotas.imresize(sample_img, 1.0 / DOWNSAMPLE_BY))

            mid_pix = min_border / DOWNSAMPLE_BY

            if EVEN_OUTPUT:
                samp_vol[:, :] = sample_img[mid_pix - imgrad:mid_pix + imgrad,
                                            mid_pix - imgrad:mid_pix + imgrad]
            else:
                samp_vol[:, :] = sample_img[mid_pix - imgrad:mid_pix + imgrad +
                                            1, mid_pix - imgrad:mid_pix +
                                            imgrad + 1]

            if display_output:
                print 'Location ({0},{1},{2}).'.format(samp_i, samp_j, imgi)
                output = zeros((imgd, imgd), dtype=uint8)
                output[:, imgd] = samp_vol[:, :]
        normdata = (np.float32(data) - mindata) / (maxdata - mindata)
        mahotas.imsave(output_path + '/{0:04d}_classify_output_layer{1}_{2}.tif'.format(image_num, layer, index), np.uint16(normdata * 65535))

    # Main classification loop
    for image_index in range(classify_start, classify_start + classify_n):

        # Normalized training
        input_image, target_image = open_image_and_gold(image_index, crop_from, crop_size)

        # Direct pixel intensity training
        #input_image = np.float32(255-mahotas.imread(image_path_format_string.format(image_index))[crop_from[0]:crop_from[0]+crop_size,crop_from[1]:crop_from[1]+crop_size])

        downsample = 1
        if param_file.find('_ds2') != -1:
            downsample = 2
            input_image = np.float32(mahotas.imresize(input_image, 1.0/downsample))
        elif param_file.find('_ds4') != -1:
            downsample = 4
            input_image = np.float32(mahotas.imresize(input_image, 1.0/downsample))


        # Random rotate / mirror
        # mirror = np.random.choice(2)
        # rotate = np.random.choice(4)
        # input_image = rotmir(input_image, mirror, rotate)
        # target_image = rotmir(input_image, mirror, rotate)

        #Pad the image borders so we get a full image output and to avoid edge effects
        pad_image = np.pad(input_image, ((pad_by, pad_by), (pad_by, pad_by)), 'symmetric')
        layer0_in = pad_image.reshape(1, pad_image.shape[0], pad_image.shape[1])
Ejemplo n.º 26
0
        normdata = (np.float32(data) - mindata) / (maxdata - mindata)
        mahotas.imsave(output_path + '/{0:04d}_classify_output_layer{1}_{2}.tif'.format(image_num, layer, index), np.uint16(normdata * 65535))

    # Main classification loop
    for image_index in range(classify_start, classify_start + classify_n):

        # Normalized training
        input_image, target_image = open_image_and_gold(image_index, crop_from, crop_size)

        # Direct pixel intensity training
        #input_image = np.float32(255-mahotas.imread(image_path_format_string.format(image_index))[crop_from[0]:crop_from[0]+crop_size,crop_from[1]:crop_from[1]+crop_size])

        downsample = 1
        if param_file.find('_ds2') != -1:
            downsample = 2
            input_image = np.float32(mahotas.imresize(input_image, 0.5))

        # Random rotate / mirror
        # mirror = np.random.choice(2)
        # rotate = np.random.choice(4)
        # input_image = rotmir(input_image, mirror, rotate)
        # target_image = rotmir(input_image, mirror, rotate)

        #Pad the image borders so we get a full image output and to avoid edge effects
        pad_image = np.pad(input_image, ((pad_by, pad_by), (pad_by, pad_by)), 'symmetric')
        layer0_in = pad_image.reshape(1, pad_image.shape[0], pad_image.shape[1])

        start_time = time.clock()

        #Classify image
        layer_output = []
Ejemplo n.º 27
0
    def extract(self):
        '''Extracts morphology features to measure the size and shape of objects.

        Returns
        -------
        pandas.DataFrame
            extracted feature values for each object in `label_image`
        '''
        logger.info('extract morphology features')
        distances = ndi.morphology.distance_transform_edt(self.label_image)
        regionprops = measure.regionprops(label_image=self.label_image,
                                          intensity_image=distances)
        labels = []
        features = []
        for obj_props in regionprops:
            obj = obj_props.label
            mask = self.get_object_mask_image(obj)
            roundness = mh.features.roundness(mask)

            # calculate centroid, area and perimeter for selected object
            if 'centroid' in obj_props:  # skimage < 0.16
                local_centroid_y, local_centroid_x = obj_props.centroid
            elif 'centroidarray' in obj_props:  # skimage >= 0.16
                local_centroid_y, local_centroid_x = obj_props.centroidarray
            else:
                logger.error(
                    "No centroid coordinates computed for object with label %s"
                    " -- using `NaN` instead!", obj)
                local_centroid_x = np.NaN
                local_centroid_y = np.NaN
            area = obj_props.area
            perimeter = obj_props.perimeter
            extent = obj_props.extent

            # calculate circularity (a.k.a. form factor)
            if perimeter == 0:
                circularity = np.nan
            else:
                circularity = (4.0 * np.pi * area) / (perimeter**2)

            # calculate convexity (a.k.a solidity)
            area_convex_hull = obj_props.convex_area
            convexity = area / float(area_convex_hull)

            # calculate ellipse features
            eccentricity = obj_props.eccentricity
            equivalent_diameter = obj_props.equivalent_diameter
            major_axis = obj_props.major_axis_length
            minor_axis = obj_props.minor_axis_length
            if major_axis == 0:
                elongation = np.nan
            else:
                elongation = (major_axis - minor_axis) / major_axis

            # calculate "distance" features
            max_radius = obj_props.max_intensity
            mean_radius = obj_props.mean_intensity

            values = [
                local_centroid_x, local_centroid_y, area, perimeter,
                eccentricity, extent, convexity, circularity, roundness,
                elongation, equivalent_diameter, major_axis, minor_axis,
                max_radius, mean_radius
            ]
            if self.compute_zernike:
                logger.debug('extract Zernike moments for object #%d', obj)
                r = 100
                mask_rs = mh.imresize(mask, [r * 2, r * 2])
                zernike_values = mh.features.zernike_moments(
                    mask_rs, degree=self._degree, radius=r)
                values.extend(zernike_values)
            features.append(values)
            labels.append(obj)

        if len(set(labels)) != len(self.object_ids):
            logger.error(
                'Number of unique objects with measurements returned by'
                ' regionprops ({}) does not match the number'
                ' of labels ({})'.format(len(set(labels)),
                                         len(self.object_ids)))
            raise PipelineRunError()
        return pd.DataFrame(features, columns=self.names, index=labels)
Ejemplo n.º 28
0
def normalize_image(original_image, saturation_level=0.005, invert=True):
    sorted_image = np.sort( np.uint8(original_image).ravel() )
    minval = np.float32( sorted_image[ len(sorted_image) * ( saturation_level / 2 ) ] )
    maxval = np.float32( sorted_image[ len(sorted_image) * ( 1 - saturation_level / 2 ) ] )
    norm_image = np.float32(original_image - minval) * ( 255 / (maxval - minval))
    norm_image[norm_image < 0] = 0
    norm_image[norm_image > 255] = 255
    if invert:
        norm_image = 255 - norm_image
    return np.uint8(norm_image)

input_image = np.float32(normalize_image(mahotas.imread(input_image_file), invert=(not image_inverted)))
stump_image = np.float32(normalize_image(mahotas.imread(input_stump_file), invert=(not image_inverted)))

if image_downsample_factor != 1:
    input_image = mahotas.imresize(input_image, image_downsample_factor)
    stump_image = mahotas.imresize(stump_image, image_downsample_factor)

average_image = combo_net.apply_combo_net(input_image, stump_input=stump_image)

if image_downsample_factor != 1:
    average_image = np.float32(mahotas.imresize(average_image, 1.0 / image_downsample_factor))

temp_path = output_file + '_tmp'
out_hdf5 = h5py.File(temp_path, 'w')
# copy the probabilities for future use
probs_out = out_hdf5.create_dataset('probabilities',
                                    data = average_image,
                                    chunks = (64,64),
                                    compression = 'gzip')
out_hdf5.close()
Ejemplo n.º 29
0
                                     (1 - saturation_level / 2)])
    norm_image = np.float32(original_image - minval) * (255 /
                                                        (maxval - minval))
    norm_image[norm_image < 0] = 0
    norm_image[norm_image > 255] = 255
    if invert:
        norm_image = 255 - norm_image
    return np.uint8(norm_image)


input_image = np.float32(
    normalize_image(mahotas.imread(input_image_path),
                    invert=(not image_inverted)))

if image_downsample_factor != 1:
    input_image = mahotas.imresize(input_image, image_downsample_factor)

average_image = combo_net.apply_combo_net(input_image)


def write_image(output_path, data, image_num=0, downsample=1):
    if downsample != 1:
        data = np.float32(mahotas.imresize(data, downsample))
    maxdata = np.max(data)
    mindata = np.min(data)
    normdata = (np.float32(data) - mindata) / (maxdata - mindata)
    mahotas.imsave(output_path, np.uint16(normdata * 65535))


write_image(output_image_path, average_image)
Ejemplo n.º 30
0
from sklearn.svm import SVC
from sklearn.cross_validation import train_test_split
from sklearn.grid_search import GridSearchCV
from sklearn.metrics import classification_report

if __name__ == '__main__':
    X = []
    y = []
    for path, subdirs, files in os.walk('data/English/Img/GoodImg/Bmp/'):
        for filename in files:
            f = os.path.join(path, filename)
            target = filename[3:filename.index('-')]
            img = mh.imread(f, as_grey=True)
            if img.shape[0] <= 30 or img.shape[1] <= 30:
                continue
            img_resized = mh.imresize(img, (30, 30))
            if img_resized.shape != (30, 30):
                img_resized = mh.imresize(img_resized, (30, 30))
            X.append(img_resized.reshape((900, 1)))
            y.append(target)
    X = np.array(X)
    X = X.reshape(X.shape[:2])
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=.1)
    pipeline = Pipeline([('clf', SVC(kernel='rbf', gamma=0.01, C=100))])
    parameters = {
        'clf__gamma': (0.01, 0.03, 0.1, 0.3, 1),
        'clf__C': (0.1, 0.3, 1, 3, 10, 30),
    }
    grid_search = GridSearchCV(pipeline,
                               parameters,
                               n_jobs=3,
def normalize_image(original_image, saturation_level=0.005, invert=True):
    sorted_image = np.sort( np.uint8(original_image).ravel() )
    minval = np.float32( sorted_image[ len(sorted_image) * ( saturation_level / 2 ) ] )
    maxval = np.float32( sorted_image[ len(sorted_image) * ( 1 - saturation_level / 2 ) ] )
    norm_image = np.float32(original_image - minval) * ( 255 / (maxval - minval))
    norm_image[norm_image < 0] = 0
    norm_image[norm_image > 255] = 255
    if invert:
        norm_image = 255 - norm_image
    return np.uint8(norm_image)

input_image = np.float32(normalize_image(mahotas.imread(input_image_path), invert=(not image_inverted)))

if image_downsample_factor != 1:
    input_image = mahotas.imresize(input_image, image_downsample_factor)

average_image = combo_net.apply_combo_net(input_image)

def write_image (output_path, data, image_num=0, downsample=1):
    if downsample != 1:
        data = np.float32(mahotas.imresize(data, downsample))
    maxdata = np.max(data)
    mindata = np.min(data)
    normdata = (np.float32(data) - mindata) / (maxdata - mindata)
    mahotas.imsave(output_path, np.uint16(normdata * 65535))

write_image(output_image_path, average_image)

print 'Classification complete.'
Ejemplo n.º 32
0
def step(frame, file_time, output_file = None):
    global g_adjacency, g_max, g_prev, g_colors, g_feat, g_gray, g_rgb, g_rgbfeat, g_blobidx, g_blobidxset, g_myrect, g_myhist
    global current_frame_time, xml_root
    global g_last_depth, g_last_depth_change
    #global g_frameidx
    #ni.show_frame(frame)
    num_kinects = frame['num_kinects']
    if num_kinects == 0:
        return
    depth = numpy.array(frame['depths'][0])
    
    rgb = numpy.array(frame['images'][0])
    cvmod.set_depth(depth.ravel())
    g_sa = 0.
    if g_showall:
        g_sa = 1.
    var = numpy.array([gx1, gx2, gy1, gy2, gz1, gz2, gd1, gd2, g_sa], dtype=numpy.float)
    cvmod.set_vars(var)
    if not BATCH_MODE and not BATCH_BLOB_MODE:
        cvmod.output3d()
    cvmod.process_blob()
    
    adj = numpy.array(g_adjacency, dtype=numpy.uint8) * 255
    adj = cv2.erode(adj, None)
    adj = cv2.erode(adj, None)
    adj = cv2.erode(adj, None)
    
    
    if not BATCH_MODE and not BATCH_BLOB_MODE:
##        cv2.imshow('Depth', depth_change)
##        cv2.waitKey(10)
        cv2.imshow('ADJ', adj)
        cv2.waitKey(10)
    
    max_idx, img, rects = maskblob.createblobmask(adj)

    #print 'Rects',rects
    
    if g_prev == None:
        g_max, g_prev = max_idx, img
        for i in range(max_idx):
            g_colors.append(get_rcol())
        return
##    print "print", g_max, max_idx
    new_g_colors = [None]



    if not BATCH_MODE:
        if g_feat != None:
            nextPts, status, err = cv2.calcOpticalFlowPyrLK(g_gray, gorig, g_feat, None, winSize=(15, 15), maxLevel=5)
            for i in range(len(nextPts)):
                if status[i][0] == 1:
                    x1, y1 = g_feat[i][0]
                    #cv2.circle(cmask, (x1, y1), 4., (0, 0, 255), -1)
                    x2, y2 = nextPts[i][0]
                    dx = abs(x1-x2)
                    dy = abs(y1-y2)
                    #if dx*dx+dy*dy < 70*70:
                    #    cv2.line(cmask, (x1, y1), (x2, y2), (0, 255, 0), 3)


            ass_mat = numpy.zeros((max_idx, g_max), dtype=numpy.uint8)
            for idx1 in range(1, g_max+1):
                bmask = maskblob.getblob(g_prev, idx1)
                bmask = cv2.dilate(bmask, None)

                for i in range(len(nextPts)):
                    if status[i][0] == 1:
                        x1, y1 = g_feat[i][0]
                        if bmask[y1][x1] > 0:

                            for idx2 in range(1, max_idx+1):
                                dmask = maskblob.getblob(img, idx2)
                                dmask = cv2.dilate(dmask, None)
                                x2, y2 = nextPts[i][0]
                                dx = abs(x1-x2)
                                dy = abs(y1-y2)
                                if dx*dx+dy*dy < 70*70:
                                    if dmask[y2][x2] > 0:
                                        ass_mat[idx2-1][idx1-1] += 1
                                        cv2.line(cmask, (x1, y1), (x2, y2), (0, 255, 0), 1)
            if not BATCH_MODE and not BATCH_BLOB_MODE:
                print ass_mat

            for i in range(max_idx):
                k = ass_mat[i]
                if numpy.max(k) > 0:
                    p = numpy.argmax(k)
                    blobidxset[i] = g_blobidxset[p]

            if not BATCH_MODE and not BATCH_BLOB_MODE:
                print blobidxset


        # End Batch

    if len(rects) > 0:
        a0, b0, w0, h0 =  rects[0]
        a1 = a0+w0
        b1 = b0+h0
##        cv2.rectangle(cmask, (a0, b0), (a1, b1), (255, 255, 255), 5)
    else:
        a0 = 0
        b0 = 0
        a1 = 630
        b1 = 470
        
        
    if g_myrect == None:
        
        g_myrect = a0, b0, a1, b1
        g_myhist = get_histogram(rgb, a0, b0, a1, b1)
        #show_histogram(g_myhist)
    else:

        frame_element = xml.SubElement(xml_root,'frame')
        frame_element.attrib['timestamp'] = str(current_frame_time)
##        frame_element.attrib['confidence'] = str(numpy.std(prob_vector * hsv))

        skeleton_element = xml.SubElement(frame_element,'skeleton')
        if not (frame['skel'][0]['HEAD']['2d'][0] == 0 and
            frame['skel'][0]['HEAD']['2d'][1] == 0):
            xmlhelper.generate_skeleton_xml(frame, skeleton_element)
        
        blobs_element = xml.SubElement(frame_element,'blobs')        

        #print 'Max prob: ', numpy.max(prob)

        if output_file is not None:
            blue_channel = rgb[:,:,0]
            green_channel = rgb[:,:,1]
            red_channel = rgb[:,:,2]
            
            for blob_num in range(0, max_idx):
                
                blob_mask = maskblob.getblob(img,blob_num + 1)
                num_blob_pixels = numpy.sum(blob_mask, dtype=numpy.int32) / 255
                
                if num_blob_pixels <= 8:
                    continue

##                num_prob_pixels = numpy.sum(prob * blob_mask, dtype=numpy.int32) / 255
                
                x_1 = rects[blob_num][0]
                y_1 = rects[blob_num][1]
                x_2 = x_1 + rects[blob_num][2]
                y_2 = y_1 + rects[blob_num][3]

                
##                blob_prob = prob * (blob_mask / 255)
##                blob_attention = numpy.sum(blob_prob) / numpy.sum(prob)
                
                blob_red = red_channel & blob_mask
                red_value = numpy.sum(blob_red, dtype=numpy.int32) / num_blob_pixels
                red_variance = numpy.std(blob_red[blob_red.nonzero()])
                blob_red_rect = blob_red[x_1:x_2, y_1:y_2]

                blob_green = green_channel & blob_mask
                green_value = numpy.sum(blob_green, dtype=numpy.int32) / num_blob_pixels
                green_variance = numpy.std(blob_green[blob_green.nonzero()])
                blob_green_rect = blob_green[x_1:x_2, y_1:y_2]

                blob_blue = blue_channel & blob_mask
                blue_value = numpy.sum(blob_blue, dtype=numpy.int32) / num_blob_pixels
                blue_variance = numpy.std(blob_blue[blob_blue.nonzero()])
                blob_blue_rect = blob_blue[x_1:x_2, y_1:y_2]

                blob_depth = depth & blob_mask
                max_depth = numpy.max(blob_depth[blob_depth.nonzero()])
                min_depth = numpy.min(blob_depth[blob_depth.nonzero()])

                blob_color_rect = numpy.zeros((rects[blob_num][2], rects[blob_num][3], 3), numpy.uint8)

                moments = cv2.moments(blob_mask)
                hu_moments = cv2.HuMoments(moments)

                # Scale image for scale-invariant shape features
                resized_image = mahotas.imresize(blob_mask,
                                                 float(SHAPE_FEATURE_SIZE) / max(blob_mask.shape))

                z_moments = zernike_moments(resized_image, SHAPE_FEATURE_SIZE, degree = Z_ORDER)

                blob_element = xml.Element('blob')
                
                blobs_element.append(blob_element)

                blob_element.attrib['x'] = str(rects[blob_num][0])
                blob_element.attrib['y'] = str(rects[blob_num][1])
                blob_element.attrib['width'] = str(rects[blob_num][2])
                blob_element.attrib['height'] = str(rects[blob_num][3])
                rect_center = (rects[blob_num][0] + .5 * rects[blob_num][2],
                               rects[blob_num][1] + .5 * rects[blob_num][3])
                blob_element.attrib['head_dist'] = str(distance(rect_center,
                                                            frame['skel'][0]['HEAD']['2d']))
                blob_element.attrib['right_hand_dist'] = str(distance(rect_center,
                                                            frame['skel'][0]['RIGHT_HAND']['2d']))
                blob_element.attrib['left_hand_dist'] = str(distance(rect_center,
                                                            frame['skel'][0]['LEFT_HAND']['2d']))
                
                features_element = xml.Element('features')
                blob_element.append(features_element)

                size_element = xml.Element('size')
                xml.SubElement(size_element, 'pixels').text = str(num_blob_pixels)

                features_element.append(size_element)
                
                hu_element = xml.Element('hu')
                xml.SubElement(hu_element, 'i_1').text = str(hu_moments[0][0])
                xml.SubElement(hu_element, 'i_2').text = str(hu_moments[1][0])
                xml.SubElement(hu_element, 'i_3').text = str(hu_moments[2][0])
                xml.SubElement(hu_element, 'i_4').text = str(hu_moments[3][0])
                xml.SubElement(hu_element, 'i_5').text = str(hu_moments[4][0])
                xml.SubElement(hu_element, 'i_6').text = str(hu_moments[5][0])
                xml.SubElement(hu_element, 'i_7').text = str(hu_moments[6][0])

                features_element.append(hu_element)

                zernike_element = xml.Element('zernike')

                for i in xrange(Z_FEATURES):
                    xml.SubElement(zernike_element, 'a_' + str(i)).text = str(z_moments[i])

                features_element.append(zernike_element)
                


                rgb_element = xml.Element('rgb')
                xml.SubElement(rgb_element, 'r').text = str(red_value)
                xml.SubElement(rgb_element, 'g').text = str(green_value)
                xml.SubElement(rgb_element, 'b').text = str(blue_value)

                features_element.append(rgb_element)
                
                rgb_variance_element = xml.Element('rgb_var')
                xml.SubElement(rgb_variance_element, 'r').text = str(red_variance)
                xml.SubElement(rgb_variance_element, 'g').text = str(green_variance)
                xml.SubElement(rgb_variance_element, 'b').text = str(blue_variance)
                
                features_element.append(rgb_variance_element)
                


    if not BATCH_MODE:
        grayrgb = cv2.cvtColor(rgb, cv2.COLOR_BGR2GRAY)
        grayrgborig = cv2.cvtColor(rgb, cv2.COLOR_BGR2GRAY)
        grayrgbfeat = show_features(grayrgb, 500)

        
        if g_rgbfeat != None:
            nextPts, status, err = cv2.calcOpticalFlowPyrLK(g_rgb, grayrgborig, g_rgbfeat, None, winSize=(15, 15), maxLevel=5)
            for i in range(len(nextPts)):
                if status[i][0] == 1:
                    x1, y1 = g_rgbfeat[i][0]
                    #cv2.circle(rgb, (x1, y1), 4., (0, 0, 255), -1)
                    x2, y2 = nextPts[i][0]
                    dx = abs(x1-x2)
                    dy = abs(y1-y2)
                    if dx*dx+dy*dy < 30*30:
                        cv2.line(rgb, (x1, y1), (x2, y2), (0, 255, 0), 1)
            f = cv2.calcOpticalFlowFarneback(g_rgb, grayrgborig, None, .5, 3, 15, 3, 5, 1.2, 0)
            print f.shape

        g_rgb = grayrgborig
        g_rgbfeat = grayrgbfeat

        if not BATCH_BLOB_MODE:
##            cv2.imshow('MAS', cmask)
##            cv2.waitKey(10)
##            cv2.imshow('GAS', gmask)
##            cv2.waitKey(10)
            cv2.imshow('Gray',g_gray)
            cv2.waitKey(10)


            cv2.imshow('IMG', rgb)
            cv2.waitKey(10)

    if BATCH_MODE or BATCH_BLOB_MODE:
        print numpy.max(depth)
        print cv2.imwrite(file_time + 'rgb.jpg',rgb)
        print cv2.imwrite(file_time + 'd.jpg',numpy.divide(depth,12))
Ejemplo n.º 33
0
    def apply_net(
        self,
        input_image,
        perform_downsample=False,
        perform_pad=False,
        perform_upsample=False,
        perform_blur=False,
        perform_offset=False,
    ):

        if perform_pad:
            input_image = np.pad(input_image, ((self.pad_by, self.pad_by), (self.pad_by, self.pad_by)), "symmetric")

        if perform_downsample and self.downsample != 1:
            input_image = np.float32(mahotas.imresize(input_image, 1.0 / self.downsample))

        nx = input_image.shape[0] - self.all_layers[0].input_footprint + 1
        ny = input_image.shape[1] - self.all_layers[0].input_footprint + 1
        nbatches = nx * ny

        layer_temp = np.zeros(
            (nbatches, 1, self.all_layers[0].input_footprint, self.all_layers[0].input_footprint), dtype=np.float32
        )
        print layer_temp.shape

        batchi = 0
        for x in range(nx):
            for y in range(ny):
                # print (x,y)
                layer_temp[batchi, :, :, :] = input_image[
                    x : (x + self.all_layers[0].input_footprint), y : (y + self.all_layers[0].input_footprint)
                ]
                batchi += 1

        assert batchi == nbatches

        output = np.zeros(nbatches, dtype=np.float32)

        for block in range(nbatches / BLOCK_BATCHES + 1):

            block_from = block * BLOCK_BATCHES
            block_to = min((block + 1) * BLOCK_BATCHES, layer_temp.shape[0])
            nbatches = block_to - block_from

            block_temp = layer_temp[block_from:block_to, :, :, :]

            for layeri in range(len(self.all_layers)):
                print layeri
                start_time = time.clock()
                block_temp = self.all_layers[layeri].apply_layer(block_temp, nbatches)
                end_time = time.clock()
                print ("Layer time = %.2fm" % ((end_time - start_time) / 60.0))

            if isinstance(block_temp, gpuarray.GPUArray):
                block_temp = block_temp.get()

            output[block_from:block_to] = block_temp[:, 0, 0, 0]

        output = output.reshape(nx, ny)

        if perform_upsample:
            output = np.float32(mahotas.imresize(output, self.downsample))

        if perform_blur and self.best_sigma != 0:
            output = scipy.ndimage.filters.gaussian_filter(output, self.best_sigma)

        if perform_offset:
            # Translate
            output = np.roll(output, self.best_offset[0], axis=0)
            output = np.roll(output, self.best_offset[1], axis=1)

        # Crop to valid size
        # output = output[self.pad_by:-self.pad_by,self.pad_by:-self.pad_by]

        return output
Ejemplo n.º 34
0
            else:
                randnonmem = random.choice(len(nonmembrane_indices[0]))
                (samp_i, samp_j) = (nonmembrane_indices[0][randnonmem], nonmembrane_indices[1][randnonmem])
                membrane_type = "non-membrane"

            # rotate by a random amount (linear interpolation)
            rotation = random.random()*360

            samp_vol = zeros((imgd, imgd, zd), dtype=uint8)

            for zoffset in range(zd):
                sample_img = input_vol[samp_i-min_border:samp_i+min_border, samp_j-min_border:samp_j+min_border, zoffset]
                sample_img = scipy.misc.imrotate(sample_img, rotation)

                if DOWNSAMPLE_BY != 1:
                    sample_img = np.uint8(mahotas.imresize(sample_img, 1.0 / DOWNSAMPLE_BY))

                mid_pix = min_border / DOWNSAMPLE_BY

                if EVEN_OUTPUT:
                    samp_vol[:,:,zoffset] = sample_img[mid_pix-imgrad:mid_pix+imgrad, mid_pix-imgrad:mid_pix+imgrad]
                else:
                    samp_vol[:,:,zoffset] = sample_img[mid_pix-imgrad:mid_pix+imgrad+1, mid_pix-imgrad:mid_pix+imgrad+1]

            if display_output:
                print 'Location ({0},{1},{2}).'.format(samp_i, samp_j, imgi)
                output = zeros((imgd, imgd * zd), dtype=uint8)
                for out_z in range(zd):
                    output[:,out_z * imgd : (out_z + 1) * imgd] = samp_vol[:,:,out_z]
                figure(figsize=(5, 5 * zd))
                title(membrane_type)
Ejemplo n.º 35
0
    def apply_net(self,
                  input_image,
                  perform_downsample=False,
                  perform_pad=False,
                  perform_upsample=False,
                  perform_blur=False,
                  perform_offset=False):

        if perform_pad:
            input_image = np.pad(input_image, ((self.pad_by, self.pad_by),
                                               (self.pad_by, self.pad_by)),
                                 'symmetric')

        if perform_downsample and self.downsample != 1:
            input_image = np.float32(
                mahotas.imresize(input_image, 1.0 / self.downsample))

        nx = input_image.shape[0] - self.all_layers[0].input_footprint + 1
        ny = input_image.shape[1] - self.all_layers[0].input_footprint + 1
        nbatches = nx * ny

        layer_temp = np.zeros((nbatches, 1, self.all_layers[0].input_footprint,
                               self.all_layers[0].input_footprint),
                              dtype=np.float32)
        print layer_temp.shape

        batchi = 0
        for x in range(nx):
            for y in range(ny):
                #print (x,y)
                layer_temp[batchi, :, :, :] = input_image[x:(
                    x + self.all_layers[0].input_footprint), y:(
                        y + self.all_layers[0].input_footprint)]
                batchi += 1

        assert batchi == nbatches

        output = np.zeros(nbatches, dtype=np.float32)

        for block in range(nbatches / BLOCK_BATCHES + 1):

            block_from = block * BLOCK_BATCHES
            block_to = min((block + 1) * BLOCK_BATCHES, layer_temp.shape[0])
            nbatches = block_to - block_from

            block_temp = layer_temp[block_from:block_to, :, :, :]

            for layeri in range(len(self.all_layers)):
                print layeri
                start_time = time.clock()
                block_temp = self.all_layers[layeri].apply_layer(
                    block_temp, nbatches)
                end_time = time.clock()
                print('Layer time = %.2fm' % ((end_time - start_time) / 60.))

            if isinstance(block_temp, gpuarray.GPUArray):
                block_temp = block_temp.get()

            output[block_from:block_to] = block_temp[:, 0, 0, 0]

        output = output.reshape(nx, ny)

        if perform_upsample:
            output = np.float32(mahotas.imresize(output, self.downsample))

        if perform_blur and self.best_sigma != 0:
            output = scipy.ndimage.filters.gaussian_filter(
                output, self.best_sigma)

        if perform_offset:
            #Translate
            output = np.roll(output, self.best_offset[0], axis=0)
            output = np.roll(output, self.best_offset[1], axis=1)

        # Crop to valid size
        #output = output[self.pad_by:-self.pad_by,self.pad_by:-self.pad_by]

        return output
Ejemplo n.º 36
0
from sklearn.svm import SVC
from sklearn.cross_validation import train_test_split
from sklearn.grid_search import GridSearchCV
from sklearn.metrics import classification_report

if __name__ == '__main__':
    X = []
    y = []
    for path, subdirs, files in os.walk('data/English/Img/GoodImg/Bmp/'):
        for filename in files:
            f = os.path.join(path, filename)
            target = filename[3:filename.index('-')]
            img = mh.imread(f, as_grey=True)
            if img.shape[0] <= 30 or img.shape[1] <= 30:
                continue
            img_resized = mh.imresize(img, (30, 30))
            if img_resized.shape != (30, 30):
                img_resized = mh.imresize(img_resized, (30, 30))
            X.append(img_resized.reshape((900, 1)))
            y.append(target)
    X = np.array(X)
    X = X.reshape(X.shape[:2])
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=.1)
    pipeline = Pipeline([
        ('clf', SVC(kernel='rbf', gamma=0.01, C=100))
    ])
    parameters = {
        'clf__gamma': (0.01, 0.03, 0.1, 0.3, 1),
        'clf__C': (0.1, 0.3, 1, 3, 10, 30),
    }
    grid_search = GridSearchCV(pipeline, parameters, n_jobs=3, verbose=1, scoring='accuracy')
Ejemplo n.º 37
0
            Warp_strength= 12.14
            # Thresholding parameters (for post processing after the edge is computed)
            Threshold_min = -1
            Threshold_max = 0.0019
            # [] Choose to compute the analog or digital edge,
            Morph_flag =1 # [] To compute analog edge, set Morph_flag=0 and to compute digital edge, set Morph_flag=1


            [Edge, PST_Kernel]= PST(Image_orig_grey, LPF, Phase_strength, Warp_strength, Threshold_min, Threshold_max, Morph_flag)

            if Morph_flag ==0:
                Edge = (Edge/np.max(Edge))*3
            else:
                Overlay=mh.overlay(Image_orig_grey,Edge)
                image=Edge.astype(np.uint8)*255
                new_image=mh.imresize(image, (224,224))
          # convert from Numpy to a list of values
            swim.append(new_image)
            if count%50==0:
              print(count)
            count+=1
            if count>2000: #I added this limit because my comp/ google Colab couldn't handle more data than that
              break
          except:
            pass
  count+=1
  print(count)


#Save/ load the list to/from a file
os.chdir("/content/gdrive/My Drive/Colab Notebooks/Motion_Net")