示例#1
0
    def DetectGrasps(self, cloud, viewPoints, viewPointIndices, nSamples,
                     scoreThresh, minWidth, maxWidth, tablePosition,
                     tableUpAxis, tableFingerLength, offsets):
        '''Calls the DetectGrasps Matlab script.'''

        # short circuit the process if there are no points in the cloud
        if cloud.shape[0] == 0:
            return []

        mCloud = matlab.double(cloud.T.tolist())
        mViewPoints = matlab.double(viewPoints.T.tolist())
        mViewPointIndices = matlab.int32(viewPointIndices.tolist(),
                                         size=(len(viewPointIndices), 1))
        mTablePosition = matlab.double(tablePosition.tolist(), size=(3, 1))
        plotBitmap = matlab.logical([False, False, False, False, False])

        mGrasps = self.eng.DetectGrasps(mCloud, mViewPoints, mViewPointIndices,
                                        nSamples, scoreThresh, minWidth,
                                        maxWidth, mTablePosition, tableUpAxis,
                                        tableFingerLength, plotBitmap)

        return self.UnpackGrasps(mGrasps, offsets)
def _convert_dict_dtypes(dictionary):
    """
    takes in dictionary of numpy/python dtypes, and returns 
    dictionary of nearly equivalent MATLAB dtypes.

    type 'list' is currently unsupported if they are mixed dtype.
    (though this could be easily implemented using MATLAB cell,
     it would not work well in the context of FEM_VGPy)
    """
    dict_out = {}
    
    for key in dictionary.keys():
        # walk through all keys, checking the type
        value = dictionary[key]

        # first if-elif ladder (initial checks)
        if value is None:
            #don't add it to the output
            continue
        elif type(value) is str:
            # matlab can automatically convert str to char
            dict_out[key] = value
            continue
        elif type(value) is list:
            # attempt to convert this to a tuple. element
            # dtypes will be taken care of in 2nd ladder
            value = tuple(value)
        
        # second if-elif ladder (convert dtypes)
        if type(value) is tuple:
            #iterable type; convert to array
            if type(value[0]) is int:
                dict_out[key] = matlab.int32(value)
            elif type(value[0]) is float:
                dict_out[key] = matlab.double(value)

        elif type(value) is numpy.ndarray:
            # we want to convert to an equivalent matlab matrix...

            #convert data to equivalent list
            data_list = value.tolist()
            
            #change dtype to matlab double and save
            dict_out[key] = matlab.double(data_list)
            
        elif type(value) is float:
            # matlab considers floats to be matrices as well.
            dict_out[key] = matlab.double([value])

        elif type(value) is int:
            # matlab considers ints to be matrices as well.
            dict_out[key] = matlab.int32([value])

        elif type(value) is dict:
            # use recursion to take care of this
            dict_out[key] = _convert_dict_dtypes(value)
            
        else:
            # undefined. alert user, then save as-is.
            # if there is a problem, matlab engine will
            # throw the proper exceptions
            print "\n!!! undefined type ",
            print type(value),
            print "... saving anyway\n"
            dict_out[key] = value
    
    return dict_out
示例#3
0
def plot_test(eng):
    print "Plot Testing Begin"
    eng.workspace['data'] =  \
      eng.randi(matlab.double([1,100]),matlab.double([30,2]))
    eng.eval("plot(data(:,1),'ro-')")
    eng.hold('on', nargout=0)
    eng.eval("plot(data(:,2),'bx--')")
    print "Plot testing end"


if __name__ == '__main__':
    # basic_test(eng)
    I = eng.imread('rice.png')
    mapping = eng.getmapping(8, 'riu2')

    lbpMap = eng.lbp(I, 2, 8, 0, 0)

    H1 = eng.lbp(I, 1, 8, mapping, 'h')
    print H1
    eng.subplot(2, 1, 1)
    eng.stem(H1)
    H2 = eng.lbp(I)
    eng.subplot(2, 1, 2)
    eng.stem(H2)
    # a = matlab.double([1,4,9,16,25],[3,4,5,6,7,8])
    SP = matlab.int32([[-1, -1], [-1, 0], [-1, 1], [0, -1], [-0, 1], [1, -1],
                       [1, 0], [1, 1]])
    I2 = eng.lbp(I, SP, 0, 'i')

    eng.quit()
示例#4
0
        # correct the values of updated input image
        input_img.data.clamp_(0, 1)

    def unload(tensor):
        # Convert an image from tensor to PIL image
        image = tensor.cpu().clone()
        image = image.squeeze(0)
        image_pil = t_func.to_pil_image(image)
        return image_pil

    output = unload(input_img)
    save_path = args.output
    plt.imsave(save_path, output)
    print("Save image to {}".format(save_path))

    print("Post processing")
    inimg = np.array(unload(content_img))
    inimg_mat = matlab.int32(inimg.tolist())
    outimg = np.array(output)
    outimg_mat = matlab.int32(outimg.tolist())

    eng = start_matlab()
    processed_img = inimg - np.asarray(eng.RF(inimg_mat, args.post_s, args.post_r, args.post_it, inimg_mat)) + \
            np.asarray(eng.RF(outimg_mat, args.post_s, args.post_r, args.post_it, inimg_mat))
    processed_img = np.uint8(np.clip(processed_img, 0, 255))

    output_rt, output_ext = os.path.splitext(args.output)
    save_path = output_rt + "_post" + output_ext
    print("Save post processed image to {}".format(save_path))
    plt.imsave(save_path, Image.fromarray(processed_img))
        partial_index_1,
        partial_index_2,
        inlier_index,
        descriptor_categories=descriptor_categories)
    # inlier未更新的版本
    # descriptor_final = create_descriptor.create_descriptor(np.array(n_pre_matches_r), np.array(n_pre_matches_s), des_1, des_2, partial_index_1, partial_index_2,
    #     descriptor_categories=descriptor_categories)
    print("KNN结束")
    # network筛选出可靠的inlier
    predict = ornet.predict(descriptor_final)
    inlier_index = np.where(np.argmax(predict, axis=1) == 0)[0]
    print("inlier数量:", len(inlier_index))
    # 转换类型以便于喂入matlab +1 是因为matalb从1开始索引
    n_pre_matches_s, param = eng.featureTransform(n_pre_matches_s,
                                                  n_pre_matches_r,
                                                  matlab.int32((inlier_index +
                                                                1).tolist()),
                                                  nargout=2)

img_1 = img_1[:, :, [2, 1, 0]]
img_2 = img_2[:, :, [2, 1, 0]]
img_1 = np.array(img_1) / 255.0
img_2 = np.array(img_2) / 255.0
img_1 = matlab.double(img_1.tolist())
img_2 = matlab.double(img_2.tolist())
pre_matches_r = matlab.double(pre_matches_r.tolist())
pre_matches_s = matlab.double(pre_matches_s.tolist())
eng.demonstration(img_2,
                  img_1,
                  pre_matches_s,
                  pre_matches_r,
                  matlab.int32((inlier_index + 1).tolist()),
示例#6
0
  print "Basic Testing End"

def plot_test(eng):
  print "Plot Testing Begin"
  eng.workspace['data'] =  \
    eng.randi(matlab.double([1,100]),matlab.double([30,2]))
  eng.eval("plot(data(:,1),'ro-')")
  eng.hold('on',nargout=0)
  eng.eval("plot(data(:,2),'bx--')")
  print "Plot testing end"


if __name__=='__main__':
    # basic_test(eng)
    I=eng.imread('rice.png')
    mapping=eng.getmapping(8,'riu2')

    lbpMap = eng.lbp(I, 2, 8, 0, 0)

    H1=eng.lbp(I,1,8,mapping,'h')
    print H1
    eng.subplot(2,1,1)
    eng.stem(H1)
    H2=eng.lbp(I)
    eng.subplot(2,1,2)
    eng.stem(H2)
    # a = matlab.double([1,4,9,16,25],[3,4,5,6,7,8])
    SP=matlab.int32([[-1,-1],[-1,0],[-1,1],[0,-1],[-0,1],[1,-1],[1,0],[1,1]])
    I2=eng.lbp(I,SP,0,'i')

    eng.quit()
    def _convert_dict_dtypes(cls, dictionary):
        """
        takes in dictionary of numpy/python dtypes, and returns 
        dictionary of nearly equivalent MATLAB dtypes.

        type 'list' is currently unsupported if they are mixed dtype.
        (though this could be easily implemented using MATLAB cell,
         it would not work well in the context of FEM_VGPy)
        """
        dict_out = {}
        
        for key in dictionary.keys():
            # walk through all keys
            value = dictionary[key]

            # first if-elif ladder (initial checks)
            if '__' in (key[0:2], key[-2:]):
                # this is a python construct,
                # don't add it to the output.
                continue
            elif value is None:
                # this is a python construct,
                # don't add it to the output.
                continue
            elif type(value) is str:
                # matlab can automatically convert str to char
                dict_out[key] = value
                continue
            elif type(value) is bool:
                # matlab can automatically handle bool
                dict_out[key] = value
                continue
            elif type(value) is list:
                # attempt to convert this to a tuple. element
                # dtypes will be taken care of in 2nd ladder
                value = tuple(value)
            
            # second if-elif ladder (convert dtypes)
            if type(value) is tuple:
                # iterable type; convert to array
                if (type(value[0]) is int) or (type(value[0]) is numpy.int_):
                    dict_out[key] = matlab.int32(value)
                elif (type(value[0]) is float) or (type(value[0]) is numpy.float_):
                    dict_out[key] = matlab.double(value)

            elif type(value) is numpy.ndarray:
                # we want to convert to an equivalent matlab matrix...

                # convert data to equivalent list
                data_list = value.tolist()
               
                # change to matlab equivalent and save
                if (value.dtype == 'int32') or (value.dtype == 'int64') or (value.dtype == 'int'):
                    # for python 2.7, int64 is always treated as int32 in matlab.
                    dict_out[key] = matlab.int32(data_list)
                else:
                    dict_out[key] = matlab.double(data_list)
                
            elif type(value) is float:
                # matlab considers floats to be matrices as well.
                dict_out[key] = matlab.double([value])

            elif type(value) is int:
                # matlab considers ints to be matrices as well.
                dict_out[key] = matlab.int32([value])

            elif type(value) is dict:
                # use recursion to take care of this
                dict_out[key] = cls._convert_dict_dtypes(value)
                
            elif type(value) is unicode:
                # convert this to a string representation
                dict_out[key] = str(value)
                
            else:
                # undefined. alert user, then save as-is.
                # if there is a problem, matlab engine will
                # throw the proper exceptions
                print "\n!!! undefined type ",
                print type(value),
                print "... saving anyway\n"
                dict_out[key] = value
        
        return dict_out