Save frame from TangoService_connectOnFrameAvailable

前端 未结 2 1004
时光取名叫无心
时光取名叫无心 2021-01-29 04:51

How can I save a frame via TangoService_connectOnFrameAvailable() and display it correctly on my computer? As this reference page mentions, the pixels are stored in the HAL_PIXE

2条回答
  •  星月不相逢
    2021-01-29 05:20

    I think that there is a better way to do if you can to do it offline. The best way to save the image should be something like this (don't forgot to create the folder Pictures or you won't save anything)

    void onFrameAvailableRouter(void* context, TangoCameraId id, const TangoImageBuffer* buffer) {
      //To write the image in a txt file.
      std::stringstream name_stream;
      name_stream.setf(std::ios_base::fixed, std::ios_base::floatfield);
      name_stream.precision(3);
      name_stream << "/storage/emulated/0/Pictures/"
                    <data,size * sizeof(uint8_t));
      f.close();
    }
    

    Then to convert the .txt file to png you can do this

    inputFolder = "input"
    outputFolderRGB = "output/rgb"
    outputFolderGray = "output/gray"
    
    input_filename  = "timestamp.txt"
    output_filename = "rgb.png"
    allFile = listdir(inputFolder)
    numberOfFile = len(allFile)
    
    if "input" in glob.glob("*"):
        if  "output/rgb" in glob.glob("output/*"):
            print ""
        else:
            makedirs("output/rgb")
            if "output/gray" in glob.glob("output/*"):
                print ""
            else:
                makedirs("output/gray")
    
        #The output reportories are ready
        for file in allFile:
            count+=1
            print "current file : ",count,"/",numberOfFile
            input_filename = file
            output_filename = input_filename[0:(len(input_filename)-3)]+"png"
    
            # load file into buffer
            data = np.fromfile(inputFolder+"/"+input_filename, dtype=np.uint8)  
    
            #To get RGB image  
            # create yuv image
            yuv = np.ndarray((height + height / 2, width), dtype=np.uint8, buffer=data)    
            # create a height x width x channels matrix with the datatype uint8 for rgb image
            img = np.zeros((height, width, channels), dtype=np.uint8);    
            # convert yuv image to rgb image
            cv2.cvtColor(yuv, cv2.COLOR_YUV2BGRA_NV21, img, channels)
            cv2.imwrite(outputFolderRGB+"/"+output_filename, img)
    
            #If u saved the image in graysacale use this part instead
            #yuvReal = np.ndarray((height, width), dtype=np.uint8, buffer=data)
            #cv2.imwrite(outputFolderGray+"/"+output_filename, yuvReal)
    else:
        print "not any input"
    

    You just have to put your .txt in a folder input It's a python script but if you prefer a c++ version it's very close.

提交回复
热议问题