Пример #1
0
def vxltoPng(filename):
    """Converts VXL file to a PNG using amplitude data. Uses open cv libraries"""
    camsys = Voxel.CameraSystem()
    r = Voxel.FrameStreamReader(filename, camsys)
    bool1, cols = r.getStreamParamu("frameWidth")
    bool2, rows = r.getStreamParamu("frameHeight")
    if not bool1 or not bool2:
        print("Cannot read the stream")
    if not r.isStreamGood():
        print("Stream is not good: " + filename)
    numFrames = r.size()
    meanAmplitudes = np.zeros((rows, cols), dtype='float')
    for i in (range(numFrames)):
        if not r.readNext():
            print("Failed to read frame %d" % i)
            break
        tofFrame = Voxel.ToF1608Frame.typeCast(
            r.frames[Voxel.DepthCamera.FRAME_RAW_FRAME_PROCESSED])
        meanAmplitudes += np.array(tofFrame._amplitude, copy=True).reshape(
            (rows, cols))
    r.close()
    meanAmplitudes = meanAmplitudes / np.max(meanAmplitudes) * 255

    outFileName = os.path.splitext(filename)[0] + '.png'
    cv2.imwrite(outFileName, meanAmplitudes.astype(np.uint8))
Пример #2
0
def commonPhaseOffset(file1,
                      distance,
                      modFreq1,
                      cx,
                      cy,
                      file2=None,
                      modFreq2=None,
                      window=4,
                      chipset='ti.tinitn'):
    c = 299792458  # m/s
    camsys = Voxel.CameraSystem()
    averagePhase1, rows, cols = computeAveragePhases(
        camsys, file1, cx, cy, window)  #default window size = 4
    ds1 = c / (2 * modFreq1 * 1E6)
    phaseActual1 = distance / ds1 * 4096
    phaseAverage1 = averagePhase1
    phaseCorr = wrapPhaseToSignedInteger(int(phaseAverage1 - phaseActual1))
    if file2 and modFreq2:
        averagePhase2, rows, cols = computeAveragePhases(
            camsys, file2, cx, cy, window)
        ds2 = c / (2 * modFreq2 * 1E6)
        phaseActual2 = distance / ds2 * 4096
        phaseAverage2 = averagePhase2
        phaseCorr2 = wrapPhaseToSignedInteger(int(phaseAverage2 -
                                                  phaseActual2))
    else:
        phaseCorr2 = 0
    if chipset == 'ti.calculus':  #additive phase
        phaseCorr = -phaseCorr
        phaseCorr2 = -phaseCorr2
    return True, phaseCorr, phaseCorr2, rows, cols
Пример #3
0
 def read(vxlFile, cameraInfo):
     reader = Voxel.FrameStreamReader(vxlFile, cameraInfo.system)
     if not reader.isStreamGood():
         raise ValueError("Stream is not good: " + vxlFile)
     frames = list()
     for i in range(reader.size()):
         if not reader.readNext():
             raise ValueError("Failed to read frame number " + str(i))
         rawProcessedFrame = Voxel.ToF1608Frame.typeCast(
             reader.frames[Voxel.DepthCamera.FRAME_RAW_FRAME_PROCESSED])
         depthFrame = Voxel.DepthFrame.typeCast(
             reader.frames[Voxel.DepthCamera.FRAME_DEPTH_FRAME])
         attributes = {
             "ambient":
             np.array(rawProcessedFrame._ambient,
                      copy=True).reshape(cameraInfo.resolution),
             "amplitude":
             np.array(rawProcessedFrame._amplitude,
                      copy=True).reshape(cameraInfo.resolution),
             "phase":
             np.array(rawProcessedFrame._phase,
                      copy=True).reshape(cameraInfo.resolution),
             "depth":
             np.array(depthFrame.depth,
                      copy=True).reshape(cameraInfo.resolution)
         }
         frames.append(VxlFrame(attributes))
     reader.close()
     return VxlVideo(frames)
Пример #4
0
def Getraws(npyDir):
    # Read the images and concatenate
    images = []
    filenames = []
    for dirName, subdirList, fileList in os.walk(npyDir):
        # Sort the tif file numerically
        fileList = natsort.natsorted(fileList)

        for f in fileList:
            if f.endswith(".bin"):
                filename, file_extension = os.path.splitext(f)
                fullpath = os.path.join(npyDir, f)

                print filename

                # check!
                pVoxel = vx.PyVoxel()
                #pVoxel.ReadFromRaw(fullpath)
                pVoxel.ReadFromBin(fullpath)
                #pVoxel.Normalize()

                images.append(pVoxel.m_Voxel)
                filenames.append(filename)

    return images, filenames
def computeAveragePhases(camsys, fileName, window=4, cx=0, cy=0):
    """ Computes the average phases for a given file. Uses simple average for getting phase value """
    r = Voxel.FrameStreamReader(fileName, camsys)
    if not cx:
        bool3, cx = r.getStreamParamf("cx")
        if not bool3:
            cx = 0
    if not cy:
        bool4, cy = r.getStreamParamf("cy")
        if not bool4:
            cy = 0

    cx = int(cx)
    cy = int(cy)

    phases, amplitudes, frames, rows, cols = extractPhasesandAmplitudes(
        fileName, camsys)
    if cx == 0:
        cx = rows / 2
    if cy == 0:
        cy = cols / 2
    centerShape = [
        cx - window / 2, cx + window / 2, cy - window / 2, cy + window / 2
    ]
    averagePhases = np.zeros([window, window], dtype='float')
    for val in np.arange(frames):
        phaseWindow = phases[val][centerShape[0]:centerShape[1],
                                  centerShape[2]:centerShape[3]]
        averagePhases += phaseWindow
    averagePhases = averagePhases / frames
    averagePhases = np.mean(averagePhases)

    return averagePhases, rows, cols
    def __init__(self, filename, cameraSystem):
        super(FileStreamSource, self).__init__(os.path.basename(filename))
        self.filename = filename
        self.runThread = None
        self.running = False
        self.cameraSystem = cameraSystem

        self.frameStream = Voxel.FrameStreamReader(str(filename), cameraSystem)
        self.frameStreamHandler = self.frameStream
Пример #7
0
def computeAveragePhases(camsys, filename, window=0):
    """Returns the average phases for all pixels for a given file"""
    r = Voxel.FrameStreamReader(filename, camsys)
    bool1, cols = r.getStreamParamu("frameWidth")
    bool2, rows = r.getStreamParamu("frameHeight")
    _, cx = r.getStreamParamf('cx')
    _, cy = r.getStreamParamf('cy')
    _, fx = r.getStreamParamf('fx')
    _, fy = r.getStreamParamf('fy')
    _, k1 = r.getStreamParamf('k1')
    _, k2 = r.getStreamParamf('k2')
    _, k3 = r.getStreamParamf('k3')
    _, p1 = r.getStreamParamf('p1')
    _, p2 = r.getStreamParamf('p2')
    cx = int(cx)
    cy = int(cy)
    if cx == 0:
        cx = rows / 2
    if cy == 0:
        cy = cols / 2
    if window:
        centerShape = [
            cx - window / 2, cx + window / 2, cy - window / 2, cy + window / 2
        ]
    else:
        centerShape = [0, rows, 0, cols]
    if not r.isStreamGood() or not bool1 or not bool2:
        print("Stream is not good: " + filename)
    numFrames = r.size()
    if window:
        averagePhase = np.zeros((window, window), dtype='complex')
    else:
        averagePhase = np.zeros((rows, cols), dtype='complex')

    for i in np.arange(numFrames):
        if not r.readNext():
            print("Failed to read frame %d" % i)
            break
        tofFrame = Voxel.ToF1608Frame.typeCast(
            r.frames[Voxel.DepthCamera.FRAME_RAW_FRAME_PROCESSED])
        phase = np.array(tofFrame._phase, copy=True).reshape((rows, cols))\
        [centerShape[0]:centerShape[1], centerShape[2]:centerShape[3]]*2*np.pi/4096
        amplitude = np.array(tofFrame._amplitude, copy = True).reshape((rows,cols))\
        [centerShape[0]:centerShape[1], centerShape[2]:centerShape[3]]
        averagePhase += amplitude * (np.cos(phase) + 1j * np.sin(phase))
    averagePhase /= numFrames
    if window:
        averagePhase = np.sum(averagePhase) / (window * window)
        if averagePhase < 0:
            averagePhase += 4096
    averagePhase = np.angle(averagePhase) * 4096 / (2 * np.pi)

    r.close()
    dist = np.array([k1, k2, k3, p1, p2])
    mtx = np.array([[fx, 0, cx], [0, fy, cy], [0, 0, 1]])
    return averagePhase, rows, cols, mtx, dist
Пример #8
0
 def selectConfFile(self):
     self.confFile, _ = QtGui.QFileDialog.getOpenFileName(
         self, "Select Conf File", filter='Conf files (*.conf)')
     if self.confFile:
         config = Voxel.ConfigurationFile()
         ret = config.read(str(self.confFile))
         if ret:
             self.calibrationWizard.previousConfiguration = config
             self.complete = True
             self.completeChanged.emit()
Пример #9
0
    def setPreviousConfiguration(self, id):
        c = self.depthCamera.configFile.getCameraProfile(id)

        if not c:
            QtGui.QMessageBox.critical(
                self, 'Calibration Wizard',
                'Could not get configuration for camera profile "' +
                self.depthCamera.getCurrentCameraProfileID() + '"')
            return

        self.previousConfiguration = Voxel.ConfigurationFile(c)
        self.previousProfileID = self.depthCamera.getCurrentCameraProfileID()
Пример #10
0
    def __init__(self):
        super(CalibrationWizard, self).__init__()
        #         self.depthCamera = Voxel.DepthCamera()
        self.cameraSystem = Voxel.CameraSystem()
        self.devices = self.cameraSystem.scan()
        if self.devices:
            self.depthCamera = self.cameraSystem.connect(self.devices[0])
        else:
            self.depthCamera = None
        ret, self.profilePath = Voxel.Configuration().getLocalPath('profiles')

        self.configPath = os.getcwd() + os.sep + '..'
        #         self.editIndex = editIndex
        self.setMinimumHeight(600)
        self.setMinimumWidth(400)
        self.setWizardStyle(QtGui.QWizard.ModernStyle)
        self.setWindowTitle('Calibration Wizard')
        self.addPage(CalibrationInitPage(self))
        self.addPage(CalibrationSelectPage(self))
        self.profileName = None
        self.calibs = OrderedDict()
        self.paths = {}
        self.dataCapture = []
        self.pages = {}
        self.calibParams = {}
        self.chipset = None

        self.previousConfiguration = []
        self.previousConfigurations = {}
        self.calibPages = [('lens', CalibrationLensPage(self)),
                           ('capture', CalibrationDataCapturePage(self)),
                           ('nonLinearity', CalibrationNonLinearityPage(self)),
                           ('temperature', CalibrationTemperaturePage(self)),
                           ('commonPhase', CalibrationCommonPhasePage(self)),
                           ('hdrCommonPhase',
                            CalibrationHDRCommonPhasePage(self)),
                           ('perPixel', CalibrationPerPixelOffsetPage(self))]
        for name, page in self.calibPages:
            self.addCalibrationPage(name, page)
        self.addPage(CalibrationConclusionPage(self))
Пример #11
0
def computeAveragePhases(camsys, filename, cx=0, cy=0, window=4):
    """ Computes the average phases for a given file. Uses the complex average for getting phase value """
    r = Voxel.FrameStreamReader(filename, camsys)
    bool1, cols = r.getStreamParamu("frameWidth")
    bool2, rows = r.getStreamParamu("frameHeight")
    if not cx:
        bool3, cx = r.getStreamParamf("cx")
        if not bool3:
            cx = 0
    if not cy:
        bool4, cy = r.getStreamParamf("cy")
        if not bool4:
            cy = 0

    cx = int(cx)
    cy = int(cy)
    if cx == 0:
        cx = rows / 2
    if cy == 0:
        cy = cols / 2
    if window:
        centerShape = [
            cx - window / 2, cx + window / 2, cy - window / 2, cy + window / 2
        ]
    else:
        centerShape = [0, rows, 0, cols]
    if not r.isStreamGood() or not bool1 or not bool2:
        print("Stream is not good: " + filename)
    numFrames = r.size()
    if window:
        averagePhase = np.zeros((window, window), dtype='complex')
    else:
        averagePhase = np.zeros((rows, cols), dtype='complex')

    for i in np.arange(numFrames):
        if not r.readNext():
            print("Failed to read frame %d" % i)
            break
        tofFrame = Voxel.ToF1608Frame.typeCast(
            r.frames[Voxel.DepthCamera.FRAME_RAW_FRAME_PROCESSED])
        phase = np.array(tofFrame._phase, copy=True).reshape((rows, cols))\
        [centerShape[0]:centerShape[1], centerShape[2]:centerShape[3]]*2*np.pi/4096
        amplitude = np.array(tofFrame._amplitude, copy = True).reshape((rows,cols))\
        [centerShape[0]:centerShape[1], centerShape[2]:centerShape[3]]
        averagePhase += amplitude * (np.cos(phase) + 1j * np.sin(phase))
    averagePhase /= numFrames
    if window:
        averagePhase = np.sum(averagePhase) / (window * window)
    averagePhase = np.angle(averagePhase) * 4096 / (2 * np.pi)

    r.close()
    return averagePhase, rows, cols
Пример #12
0
 def __init__(self, chipset, profile):
     self.config = None
     self.chipset = chipset
     super (WriteConfigFile, self). __init__()
     self.chipsetName = chipset + 'CDKCamera'
     self.profile = profile + '.conf'
     c = Voxel.Configuration()
     ret, path = c.getLocalConfPath()
     self.confFile = path  + os.sep + self.chipsetName + self.profile
     if not os.path.isfile(self.confFile):
         self.configBase()
     else:
         self.config = ConfigParser()
         self.config.read(self.confFile)    
Пример #13
0
 def __init__(self, cameraSystem):
     print("MainWindow init")
     self.depthCamera = cameraSystem.connect(devices[0])
     rate = Voxel.FrameRate()
     rate.numerator = 5
     rate.denominator = 1
     self.depthCamera.setFrameRate(rate)
     if self.depthCamera:
         self.depthCamera.clearAllCallbacks()
         self.depthCamera.registerCallback(
             Voxel.DepthCamera.FRAME_DEPTH_FRAME, self.callbackInternal)
         if not self.depthCamera.start():
             print(" start fail")
         else:
             print(" start ok")
Пример #14
0
def perPixelOffset(fileName, pathToSave=None, profileName=None):
    """Computes pixelwise phase offset for all pixels. Returns a numpy file containing the pixelwise offsets as well as the bin file
    
    .. note: Copy the bin file to the /path/to/.voxel/conf folder path before using it in the conf file
    """
    camsys = Voxel.CameraSystem()
    if not profileName:
        profileName = os.path.basename(fileName).split('.')[0]
    ret = computeAveragePhases(camsys, fileName, 0)
    if not ret:
        print "Can't calculate the phase offsets. Check file"
        sys.exit()
    else:
        phases, rows, cols, mtx, dist = ret
    if pathToSave is None:
        newFile = os.path.splitext(fileName)[0] + PHASE_FILENAME
    else:
        newFile = pathToSave + profileName + PHASE_FILENAME
    np.save(newFile, phases.T)
    cGrid = np.mgrid[0:rows, 0:cols].astype(np.float32)
    cGrid1D = np.reshape(np.transpose(cGrid, (1, 2, 0)),
                         (rows * cols, 1, 2)).astype(np.float32)
    cGridCorrected1D = cv2.undistortPoints(cGrid1D, mtx, dist)
    cGridCorrected = np.transpose(
        np.reshape(cGridCorrected1D, (rows, cols, 2)), (2, 0, 1))
    ry = cGridCorrected[0]
    rx = cGridCorrected[1]
    # Uncomment to get undistortion mapping
    #with open('temp2.txt', 'w') as f:
    #for r in range(0, self.rows):
    #for c in range(0, self.columns):
    #f.write('(%d, %d) -> (%.2f, %.2f)\n'%(self.cGrid[1, r, c], self.cGrid[0, r, c], rx[r, c], ry[r, c]))

    rad2DSquare = ((rx**2) + (ry**2))

    cosA = 1 / ((rad2DSquare + 1)**0.5)

    deltaPhase = phases - phases[int(mtx[0, 2]), int(mtx[1, 2])] / cosA
    if pathToSave is None:
        phaseOffsetFileName = os.path.splitext(fileName)[0] + PHASE_OFFSET_FILE
    else:
        phaseOffsetFileName = pathToSave + os.sep + profileName + PHASE_OFFSET_FILE
    with open(phaseOffsetFileName, 'wb') as f:
        f.write(struct.pack('H', rows))
        f.write(struct.pack('H', cols))
        f.write(struct.pack('H', 0))
        np.reshape(deltaPhase, rows * cols).astype(np.short).tofile(f)
    return True, phaseOffsetFileName, rows, cols
Пример #15
0
def commonPhaseOffset(file1,
                      distance,
                      modFreq1,
                      cx,
                      cy,
                      file2=None,
                      modFreq2=None,
                      window=4,
                      chipset='ti.tinitn'):
    """ Computes the common phase offsets. 
    
    This function calculates the common phase offsets for a given file, taking VXL as input. 
    
    **Parameters**::
    
        - file1, file2: File(s) for computing common phase offsets. These should be vxl files of a flat wall. The camera should be almost parallel to the wall.
        - modFreq1, modFreq2 : Modulation Frequency (ies) when the data is captured. If dealiasing is enabled, two modulation frequencies are required
        - cx, cy: The coordinates of the center pixel, found during lens calibration. If cx and cy are not available, the central value (120,160) is taken.
        - window: The window size. By default, a window of 4x4 is required. 
        - chipset: The chipset being used. 
        
    **Returns**::
    
        - phase_Corr1: Phase offset value for the first modulation frequency
        - phase_Corr2: Phase offset value for the second modulation frequency
        """
    c = 299792458  # m/s
    camsys = Voxel.CameraSystem()
    averagePhase1, rows, cols = computeAveragePhases(
        camsys, file1, cx, cy, window)  #default window size = 4
    ds1 = c / (2 * modFreq1 * 1E6)
    phaseActual1 = distance / ds1 * 4096
    phaseAverage1 = averagePhase1
    phaseCorr = wrapPhaseToSignedInteger(int(phaseAverage1 - phaseActual1))
    if file2 and modFreq2:
        averagePhase2, rows, cols = computeAveragePhases(
            camsys, file2, cx, cy, window)
        ds2 = c / (2 * modFreq2 * 1E6)
        phaseActual2 = distance / ds2 * 4096
        phaseAverage2 = averagePhase2
        phaseCorr2 = wrapPhaseToSignedInteger(int(phaseAverage2 -
                                                  phaseActual2))
    else:
        phaseCorr2 = 0
    if chipset == 'ti.calculus':  #additive phase
        phaseCorr = -phaseCorr
        phaseCorr2 = -phaseCorr2
    return True, phaseCorr, phaseCorr2, rows, cols
def extractRawdataFromVXL(filename, camsys):
    """Takes raw data from vxl file"""
    r = Voxel.FrameStreamReader(filename, camsys)
    if not r.isStreamGood():
        print("Stream is not good: " + filename)
    bool1, cols = r.getStreamParamu("frameWidth")
    bool2, rows = r.getStreamParamu("frameHeight")
    numFrames = r.size()
    rawFrames = np.zeros((numFrames, rows * cols * 4), dtype='uint8')
    for i in (range(numFrames)):
        if not r.readNext():
            print("Failed to read frame %d" % i)
            break
        rawFrame = Voxel.RawDataFrame.typeCast(
            r.frames[Voxel.DepthCamera.FRAME_RAW_FRAME_UNPROCESSED])
        rawFrames[i] = np.array(rawFrame.data, copy=True)[:-255]
    return rawFrames, rows, cols
Пример #17
0
    def validatePage(self):
        createNew = False
        if self.calibrationWizard.depthCamera:
            if self.profiles.currentIndex() == 0:
                createNew = True
                profileName = str(self.calibrationWizard.profileName)
            if createNew:
                print profileName
                id = self.depthCamera.addCameraProfile(
                    profileName, self.depthCamera.getCurrentCameraProfileID())

                if id < 0:
                    QtGui.QMessageBox.critical(
                        'Failed to create a new profile "' + profileName +
                        '". See logs for more details.')
                    return False
                self.calibrationWizard.currentProfileID = id
                self.calibrationWizard.currentConfiguration = self.depthCamera.configFile.getCameraProfile(
                    id)
                self.calibrationWizard.currentProfileName = profileName

            else:
                self.calibrationWizard.currentConfiguration = self.calibrationWizard.previousConfiguration
                self.calibrationWizard.currentProfileName = self.profileNames[
                    self.profiles.currentIndex() - 1]
                self.calibrationWizard.currentProfileID = self.depthCamera.getCurrentCameraProfileID(
                )
            return True
        else:
            if self.profileSelectButtons.checkedId() == 1:
                self.calibrationWizard.currentConfiguration = self.calibrationWizard.previousConfiguration
                self.calibrationWizard.profileName = self.calibrationWizard.currentConfiguration.getProfileName(
                )

            else:
                self.calibrationWizard.currentConfiguration = Voxel.ConfigurationFile(
                )
                self.calibrationWizard.currentConfiguration.setParentID(
                    self.calibrationWizard.previousConfiguration.getID())
                self.calibrationWizard.profileName = str(
                    self.calibrationWizard.profileName)
                self.calibrationWizard.currentConfiguration.setProfileName(
                    (self.calibrationWizard.profileName))
                self.calibrationWizard.currentConfiguration.setID(
                    150)  #change this id in the actual conf file
            return True
Пример #18
0
def hdrCommonPhaseOffset(fileName1,
                         distance,
                         modFreq1,
                         cx=0,
                         cy=0,
                         fileName2=None,
                         modFreq2=None,
                         window=4,
                         chipset='TintinCDKCamera'):
    """ Calculates HDR phase offsets for ti chipsets. 
    
    .. note: Make sure that the hdr flag is on for the file before capturing the file"""
    c = 299792458.  # m/s
    camsys = Voxel.CameraSystem()
    ret1 = computeHDRAveragePhases(camsys, fileName1, cx, cy, window, chipset)
    if not ret1:
        return False
    ret1, averagePhase1, rows, cols = ret1
    ds1 = c / (2 * modFreq1 * 1E6)
    phaseActual1 = distance / ds1 * 4096
    hdrphaseCorr = wrapPhaseToSignedInteger(int(averagePhase1 - phaseActual1))
    if chipset == 'TintinCDKCamera':
        if fileName2 and modFreq2:
            ret2, averagePhase2, _, _ = computeHDRAveragePhases(
                camsys, fileName2, cx, cy, window)
            if not ret2:
                return True, hdrphaseCorr, 0
            ds2 = c / (2 * modFreq2 * 1E6)
            phaseActual2 = distance / ds2 * 4096
            hdrphaseCorr2 = wrapPhaseToSignedInteger(
                int(averagePhase2 - phaseActual2))
        else:
            hdrphaseCorr2 = 0
    elif chipset == 'CalculusCDKCamera':
        hdrphaseCorr = -hdrphaseCorr
        hdrphaseCorr2 = 0
    return True, hdrphaseCorr, hdrphaseCorr2
Пример #19
0
        ###  point.i is intensity, which come from amplitude
        ###  point.z is distance, which come from depth

        # for index in range(pointCloudFrame.size()):
        #     point = pointCloudFrame.points[index]
        #     print("current point : index %s  [ x : %s , y: %s ,z : %s ,i : %s]" %(index, point.x,point.y,point.z,point.i))

    def stop(self):
        if self.depthCamera:
            self.depthCamera.stop()
            self.depthCamera.clearAllCallbacks()
            del self.depthCamera
            self.depthCamera = None


cameraSystem = Voxel.CameraSystem()

devices = cameraSystem.scan()

if len(devices) == 1:
    print(" Find one device.")
    window = MainWindow(cameraSystem)
    key = raw_input("Input enter key to quit.")
    print(" Quit now.")
    window.stop()
else:
    print(" No device found.")

del cameraSystem
Пример #20
0
from Hand import *

window.fps_counter.enabled = True
window.exit_button.visible = False

noise = PerlinNoise(octaves=2, seed=round(time.time()))

map_size = 8 * 3
lower_bound = -10

for z in range(map_size):
    for x in range(map_size):
        y = round(noise([x / map_size, z / map_size]) * 5)
        for i in range(lower_bound, y + 1):
            voxel = Voxel(type='grass' if i == y else
                          ('dirt' if i > y - 3 else 'stone'),
                          position=(x - map_size / 2, i, z - map_size / 2))

hand = Hand()
sky = mySky()
player = FirstPersonController()


def update():
    if held_keys['escape']:
        application.quit()
    if held_keys['1']:
        param.block_pick = 'stone'
    if held_keys['2']:
        param.block_pick = 'grass'
    if held_keys['3']:
Пример #21
0
 def __init__(self, device):
     self.system = Voxel.CameraSystem()
     self.resolution = VxlCameraInfo.deviceResolution[device]
Пример #22
0
             self.modFreq2,
             chipset=self.depthCamera.chipset())
     except Exception, e:
         print(e)
         ret = False
     if ret:
         self.paramsText += "phase_corr_1 = %d\n phase_corr_2 = %d\n" % (
             phaseCorr1, phaseCorr2)
         self.calibrationWizard.calibParams['phase_corr1'] = phaseCorr1
         self.calibrationWizard.calibParams['phase_corr2'] = phaseCorr2
     else:
         QtGui.QMessageBox.critical(self, "Check Data",
                                    "Can't get the coefficients")
 if self.perPixelCaptured:
     try:
         c = Voxel.Configuration()
         r, path = c.getLocalConfPath()
         ret, phaseOffsetFileName, _, _ = perPixelOffset(
             self.perPixelFileName,
             pathToSave=path,
             profileName=self.calibrationWizard.profileName)
     except Exception, e:
         ret = False
         print e
     if ret:
         self.paramsText += "phaseOffsetFileName: %s" % (
             phaseOffsetFileName)
         self.calibrationWizard.calibParams[
             'phasecorrection'] = 'file:' + os.path.basename(
                 phaseOffsetFileName)
     else:
Пример #23
0
def computeHDRAveragePhases(camsys,
                            filename,
                            cx=0,
                            cy=0,
                            window=4,
                            chipset='TintinCDKCamera'):
    """ Computes average phases for the HDR frame"""
    r = Voxel.FrameStreamReader(filename, camsys)
    _, cols = r.getStreamParamu("frameWidth")
    _, rows = r.getStreamParamu("frameHeight")
    if not cx:
        _, cx = r.getStreamParamf('cx')
    if not cy:
        _, cy = r.getStreamParamf('cy')
    cx = int(cx)
    cy = int(cy)
    if cx == 0:
        cx = rows / 2
    if cy == 0:
        cy = cols / 2
    if window:
        centerShape = [
            cx - window / 2, cx + window / 2, cy - window / 2, cy + window / 2
        ]
        averagePhase = np.zeros((window, window), dtype='complex')
    else:
        centerShape = [0, rows, 0, cols]
        averagePhase = np.zeros((rows, cols), dtype='complex')
    if not r.isStreamGood():
        print("Stream is not good: " + filename)

    numFrames = r.size()
    if chipset == 'TintinCDKCamera':
        for i in np.arange(numFrames):
            if not r.readNext():
                print("Failed to read frame %d" % i)
                break
            tofFrame = Voxel.ToF1608Frame.typeCast(
                r.frames[Voxel.DepthCamera.FRAME_RAW_FRAME_PROCESSED])
            flag = np.array(tofFrame._flags, copy=True)[0]
            if flag & 0x04 == 0x04:
                phase = np.array(tofFrame._phase, copy=True).reshape((rows, cols))\
                [centerShape[0]:centerShape[1], centerShape[2]:centerShape[3]]*2*np.pi/4096
                averagePhase += np.array(tofFrame._amplitude, copy = True).reshape((rows,cols))\
                [centerShape[0]:centerShape[1], centerShape[2]:centerShape[3]]*(np.cos(phase)+ 1j*np.sin(phase))
        if np.sum(np.absolute(averagePhase)) == 0:
            print("no HDR frames received")
            return False, 0, 0, 0
        averagePhase /= numFrames
        if window:
            averagePhase = np.sum(averagePhase) / (window * window)
        averagePhase = np.angle(averagePhase) * 4096 / (2 * np.pi)
        r.close()
    if chipset == 'CalculusCDKCamera':
        tofFrame = Voxel.ToF1608Frame.typeCast(
            r.frames[Voxel.DepthCamera.FRAME_RAW_FRAME_PROCESSED])
        flagFirst = np.array(tofFrame._flags, copy=True)[0]
        amplitudeFirst = np.array(tofFrame._amplitude, copy=True)
        if not r.readNext():
            print("Failed to read frame 2")
        flagSecond = np.array(tofFrame._flags, copy=True)[0]
        amplitudeSecond = np.array(tofFrame._amplitude, copy=True)
        if np.mean(amplitudeFirst) > np.mean(amplitudeSecond):
            flagVal = flagSecond
        else:
            flagVal = flagFirst
        if flagVal == 0x02 or flagVal == 0x03:
            flagVal = flagVal - 2
        for i in np.arange(numFrames):
            if not r.readNext():
                print("Failed to read frame %d" % i)
                break
            tofFrame = Voxel.ToF1608Frame.typeCast(
                r.frames[Voxel.DepthCamera.FRAME_RAW_FRAME_PROCESSED])
            flag = np.array(tofFrame._flags, copy=True)[0]
            if flag == flagVal or flag == flagVal + 2:
                phase = np.array(tofFrame._phase, copy=True).reshape((rows, cols))\
                [centerShape[0]:centerShape[1], centerShape[2]:centerShape[3]]*2*np.pi/4096
                averagePhase += np.array(tofFrame._amplitude, copy = True).reshape((rows,cols))\
                [centerShape[0]:centerShape[1], centerShape[2]:centerShape[3]]*(np.cos(phase)+ 1j*np.sin(phase))
        if np.sum(np.absolute(averagePhase)) == 0:
            print("no HDR frames received")
            return False, 0, 0, 0
        averagePhase /= numFrames
        if window:
            averagePhase = np.sum(averagePhase) / (window * window)
        averagePhase = np.angle(averagePhase) * 4096 / (2 * np.pi)
        r.close()
    return True, averagePhase, rows, cols
Пример #24
0
#!/usr/bin/env python2.7
#
# TI Voxel Lib component.
#
# Copyright (c) 2014 Texas Instruments Inc.
#

import Voxel

sys = Voxel.CameraSystem()

devices = sys.scan()
print devices

print "Got ", len(devices), " devices"

count = 0
count = count + 1
print count

gdepthCamera = None

pcframe = None

gframe = None


def callback(depthCamera, frame, type):
    print 'Hello'
    print depthCamera
    print frame
Пример #25
0
import Voxel as vox

import LineUtils as lin
import TrackFit as tf
import LSQ3D as lsq3d

points = []
fin = open('Niffte-event.dat')
for line in fin:
    data = (line.strip()).split()
    volume = int(data[0])
    row = int(data[1])
    column = int(data[2])
    bucket = int(data[3])
    adc = int(data[4])
    voxel = vox.Voxel(volume,row,column,bucket,adc)
    points.append(ngeo.MapVoxeltoXYZ(voxel))

#sort them in order of farthest from the origin (0,0,0) to closest
points.sort(key=attrgetter('mag'),reverse=True)

print "Finding best fit line...\n"
track = lsq3d.LSQ3D(points)

print track
#newe = (track.start).add((track.dir).scale(track.length))
#print newe

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
fig = plt.figure()
Пример #26
0
def predict():
	# Load the model
	model = get_model()

	#model.load("Unet-softmax/model_ACDC")
	model.load("Model_StairNet_IVUS")

	Images, Filenames = Getnpys()

	numImage = len(Images)

	#for i in images
	for i in range(numImage):

		# get a pair of images
		img = Images[i]
		img = np.expand_dims(img, axis=3)

		print(Filenames[i])
		print(img.shape)

		# Deploy the model on img
		y_pred = model.predict(img.astype(np.float32))
		y_pred = np.array(y_pred).astype(np.float32)

		y_pred[y_pred < 0.5] = 0
		#y_pred = np.clip(y_pred, 0, 1)
		y_pred = np.argmax(y_pred, axis=3)
		shape = y_pred.shape
		#print(y_pred.shape)
		#y_pred = y_pred.reshape(shape[0], shape[1], shape[2])
	
		# y_pred = np.round(y_pred)
		# y_pred = np.clip(y_pred, 0, 1)
		# y_pred = np.reshape(y_pred, (-1, 256, 256))
		# y_pred = y_pred.astype(np.uint8, copy=False)
		print(y_pred.shape)

		# Save the prediction
		# idx_aorta = y_pred > 0
		# y_pred[idx_aorta] = 255
		# skimage.io.imsave(SaveDir + Filenames[i] + "_pred.tif", y_pred)

		##################################################################################
		# Make the overlay
		# Convert img from gray to 3 channels image and overlay the mask
		# img = np.squeeze(img)
		# rgbImg = np.zeros([img.shape[0],256,256,3], dtype=np.uint8)
		# rgbImg[:,:,:,0] = img
		# rgbImg[:,:,:,1] = img
		# rgbImg[:,:,:,2] = img

		# rgbImg[idx_aorta,0] = (rgbImg[idx_aorta,0] + 255) / 2
		# rgbImg[idx_aorta,1] = rgbImg[idx_aorta,1] / 2
		# rgbImg[idx_aorta,2] = rgbImg[idx_aorta,2] / 2
		
		# skimage.io.imsave(SaveDir + Filenames[i] + "_overlay.tif", rgbImg)

		np.save(SaveDir + Filenames[i] + "_pred.npy", y_pred)
		pVoxel = vx.PyVoxel()
		pVoxel.NumpyArraytoVoxel(y_pred)
		pVoxel.WriteToBin(SaveDir + Filenames[i] + "_pred.bin")
Пример #27
0
import matplotlib.pyplot as plt

import numpy as np

parser = argparse.ArgumentParser(
    formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument("-f",
                    "--file",
                    type=str,
                    help="Voxel file (.vxl)",
                    required=True)

args = parser.parse_args()

camsys = Voxel.CameraSystem()

r = Voxel.FrameStreamReader(args.file, camsys)

iAverage = None
qAverage = None
frameCount = 0

if not r.isStreamGood():
    print 'Could not open stream'
    sys.exit(1)

print 'Stream contains ', r.size(), ' frames'

count = r.size()
Пример #28
0
def flatWallLensCalibration(fileName1, distance1, fileName2, distance2, cx= 0 , cy = 0):
    camsys = Voxel.CameraSystem()
    nearPhase, _, _ = computeAveragePhases(camsys, fileName1, cx, cy,  window= 0)
    farPhase, _, _ = computeAveragePhases(camsys, fileName2, cx, cy, window = 0)
    deltaPhase = farPhase - nearPhase
    deltaPhaseSmooth = snd.uniform_filter (deltaPhase, 5, mode = 'nearest')
 # 1. Compute normal pixel
    ny, nx = np.unravel_index(np.argmin(deltaPhaseSmooth), deltaPhase.shape)
    rows, columns = deltaPhase.shape
    # 2. Construct a set of all possible distances from the Normal Pixel
     # a is the set of all squared distances from the normal pixel
    y = np.outer(np.linspace(0, rows - 1, rows), np.ones((columns,)))
    x = np.outer(np.ones((rows,)), np.linspace(0, columns - 1, columns))
    a = np.square(y - ny) + np.square(x - nx)
    
    # a1 is the set of all unique distances from the normal pixel
    (a1, inverse) = np.unique(a.reshape(-1), return_inverse=True)
    # a_root is the matrix of all pixel distances from the normal pixel
    a1Root = np.sqrt(a1)
    # inverse1 is the matrix with the index in a1 of a pixel's distance from the normal pixel
    inverse1 = inverse.reshape(rows, -1)
    
    # 3. For every distance, take the set of pixels at the same distance from the normal pixel
    # and take the mean of the corresponding cosThetas at each distance
    cosThetas = []
    cosThetaMatrix = np.zeros(inverse1.shape)
    for index, value in enumerate(a1):
      i = inverse1 == index
      co = deltaPhaseSmooth[ny, nx]/np.mean(deltaPhaseSmooth[i])
      cosThetas.append(co)
      cosThetaMatrix[i] = co
    
    cosThetas = np.array(cosThetas)
    # This array is now the set of pixel distances vs cosThetas
    # We can use this directly to compute per pixel offsets if we like
    
    # Note: there might be some cosThetas > 1. Clamping it for now
    cosThetas = np.clip(cosThetas, 0, 1)
    cosThetaMatrix = np.clip(cosThetaMatrix, 0, 1)
    
    # 4. Note: before computing tanThetas, we need to translate these to Cx, Cy - see step 4 in the PPT
    cosThetas = cosThetas*deltaPhaseSmooth[ny, nx]/deltaPhaseSmooth[cy, cx]
    cosThetaMatrix = cosThetaMatrix*deltaPhaseSmooth[ny, nx]/deltaPhaseSmooth[cy, cx]
    
    tanThetas = np.tan(np.arccos(cosThetas))
    
    # 5. Use either scipy.optimize.curve_fit or np.linalg.lstsq to fit the tanTheta function
    # x is pixel distance (a1_root)
    # and tanThetas is the measured tanTheta
    # Note: should we skip a few distances at the beginning?
    popt, pcov = sopt.curve_fit(lambda x, f, k1, k2, k3:  f*x*(1 + x*x*(k1 + x*x*(k2 + x*x*k3))), tanThetas, a1Root)
    #popt[] should now be f, k1, k2, k3
    # And can be used for distortion matrix
    # p1, p2 are assumed to be 0 (no tangential distortion) and fx, fy are assumed to be equal to f
    if popt and pcov:
        mtx = np.array([[popt[0], 0, cx], [0, popt[1], cy], [0 ,0 , 1]])
        dist = np.array([popt[2], popt[3], 0., 0, popt[3]])
        
        return True, mtx, dist
    else:
        return False
Пример #29
0
        phase = np.array(tofFrame._phase, copy=True).reshape((rows, cols))\
        [centerShape[0]:centerShape[1], centerShape[2]:centerShape[3]]*2*np.pi/4096
        amplitude = np.array(tofFrame._amplitude, copy = True).reshape((rows,cols))\
        [centerShape[0]:centerShape[1], centerShape[2]:centerShape[3]]
        averagePhase += amplitude * (np.cos(phase) + 1j * np.sin(phase))
    averagePhase /= numFrames
    if window:
        averagePhase = np.sum(averagePhase) / (window * window)
        if averagePhase < 0:
            averagePhase += 4096
    averagePhase = np.angle(averagePhase) * 4096 / (2 * np.pi)

    r.close()
    return averagePhase, rows, cols


if __name__ == "__main__":
    camsys = Voxel.CameraSystem()
    if len(sys.argv) != 5:
        print("Usage: computeAveragePhases.py inFile.vxl cx cy, window")
        sys.exit()

    inFileName = sys.argv[1]
    cx = float(sys.argv[2])
    cy = int(sys.argv[3])
    window = int(sys.argv[4])

    averagePhase, _, _ = computeAveragePhases(camsys, inFileName, cx, cy,
                                              window)
    print(averagePhase)