umautobots / GTAVisionExport

Code to export full segmentations from GTA
MIT License
126 stars 42 forks source link

Stencil and depth data #41

Closed RicoMontulet closed 5 years ago

RicoMontulet commented 5 years ago

Hey, I tried reading the RGB images from the .tiffs generated by the managed code. They look fine but the stencil looks weird. I'm reading it using the code provided in the generate_bboxes.py and get the following result if I display the image. (I looked at ImageUtils.WriteToTiff() and it just writes byte[], so should be straight forward to read. I logged the sizes of these byte arrays and got the following

Color[0].length: 8294400, depth.length: 3686400, stencil.length: 2073600, screen: 1920x1080

The stencil is an exact 1080 by 1920, but when I read it in python using the code below I get the following image. It seems to be shifted/read in sideways? somehow but the color images read perfectly fine.

stencil

What did I mess up? Can you help me? Code used:

    imgpath = data_dir + filename + ".tiff"
    tiffimg = TIFF.open(imgpath)
    img = Image.open(imgpath)
    w = img.width
    h = img.height
    del img
    image = np.empty((h, w, 4), dtype=np.uint8)
    depth = np.empty((h, w), dtype=np.float32)
    stencil = np.empty((h, w), dtype=np.uint8)
    TIFF.setdirectory(tiffimg, 0)
    TIFF.readencodedstrip(tiffimg, 0, image.ctypes.data, -1)
    lastdir = num_directories(tiffimg) - 1
    TIFF.setdirectory(tiffimg, lastdir - 1)
    TIFF.readencodedstrip(tiffimg, 0, depth.ctypes.data, -1)
    TIFF.setdirectory(tiffimg, lastdir)
    TIFF.readencodedstrip(tiffimg, 0, stencil.ctypes.data, -1)
    cv2.imshow("mask", stencil)
    cv2.waitKey(0)

edit: Also my depth data is messed up, I get the following warnings when trying to load them in python:

TIFFReadDirectory: Warning, Sum of Photometric type-related color channels and ExtraSamples doesn't match SamplesPerPixel. Defining non-color channels as ExtraSamples..
TIFFReadDirectory: Warning, Sum of Photometric type-related color channels and ExtraSamples doesn't match SamplesPerPixel. Defining non-color channels as ExtraSamples..
LZWDecode: Not enough data at scanline 0 (short 4608000 bytes).
depth.min(): nan depth.max(): nan

I split the color from the depth and stencil image to see if that made a difference but no luck so far...

        var colorTiffs = Tiff.Open(Path.Combine(dataPath, "gtav" + i.ToString() + ".tiff"), "w");
        var depthTiff = Tiff.Open(Path.Combine(dataPath, "gtav" + i.ToString() + "-depth.tiff"), "w");
        var stencilTiff = Tiff.Open(Path.Combine(dataPath, "gtav" + i.ToString() + "-stencil.tiff"), "w");
        var pages = colors.Count;
        var page = 0;
        foreach (var color in colors)
        {
            colorTiffs.CreateDirectory();
            colorTiffs.SetField(TiffTag.IMAGEWIDTH, width);
            colorTiffs.SetField(TiffTag.IMAGELENGTH, height);
            colorTiffs.SetField(TiffTag.PLANARCONFIG, PlanarConfig.CONTIG);
            colorTiffs.SetField(TiffTag.SAMPLESPERPIXEL, 4);
            colorTiffs.SetField(TiffTag.ROWSPERSTRIP, height);
            colorTiffs.SetField(TiffTag.BITSPERSAMPLE, 8);
            colorTiffs.SetField(TiffTag.SUBFILETYPE, FileType.PAGE);
            colorTiffs.SetField(TiffTag.PHOTOMETRIC, Photometric.RGB);
            colorTiffs.SetField(TiffTag.COMPRESSION, Compression.JPEG);
            colorTiffs.SetField(TiffTag.JPEGQUALITY, 95);
            colorTiffs.SetField(TiffTag.PREDICTOR, Predictor.HORIZONTAL);
            colorTiffs.SetField(TiffTag.SAMPLEFORMAT, SampleFormat.UINT);
            colorTiffs.SetField(TiffTag.PAGENUMBER, page, pages);
            colorTiffs.WriteEncodedStrip(0, color, color.Length);
            colorTiffs.WriteDirectory();
            page++;
        }
        colorTiffs.Flush();
        colorTiffs.Close();
        depthTiff.CreateDirectory();
        depthTiff.SetField(TiffTag.IMAGEWIDTH, width);
        depthTiff.SetField(TiffTag.IMAGELENGTH, height);
        depthTiff.SetField(TiffTag.ROWSPERSTRIP, height);
        depthTiff.SetField(TiffTag.PLANARCONFIG, PlanarConfig.CONTIG);
        depthTiff.SetField(TiffTag.SAMPLESPERPIXEL, 1);
        depthTiff.SetField(TiffTag.BITSPERSAMPLE, 32);
        depthTiff.SetField(TiffTag.SUBFILETYPE, FileType.PAGE);
        depthTiff.SetField(TiffTag.PHOTOMETRIC, Photometric.MINISBLACK);
        depthTiff.SetField(TiffTag.COMPRESSION, Compression.LZW);
        depthTiff.SetField(TiffTag.PREDICTOR, Predictor.FLOATINGPOINT);
        depthTiff.SetField(TiffTag.SAMPLEFORMAT, SampleFormat.IEEEFP);
        depthTiff.SetField(TiffTag.PAGENUMBER, 0, 1);
        depthTiff.WriteEncodedStrip(0, depth, depth.Length);
        depthTiff.WriteDirectory();
        depthTiff.Flush();
        depthTiff.Close();
        stencilTiff.CreateDirectory();
        stencilTiff.SetField(TiffTag.IMAGEWIDTH, width);
        stencilTiff.SetField(TiffTag.IMAGELENGTH, height);
        stencilTiff.SetField(TiffTag.ROWSPERSTRIP, height);
        stencilTiff.SetField(TiffTag.PLANARCONFIG, PlanarConfig.CONTIG);
        stencilTiff.SetField(TiffTag.SAMPLESPERPIXEL, 1);
        stencilTiff.SetField(TiffTag.BITSPERSAMPLE, 8);
        stencilTiff.SetField(TiffTag.SUBFILETYPE, FileType.PAGE);
        stencilTiff.SetField(TiffTag.PHOTOMETRIC, Photometric.MINISBLACK);
        stencilTiff.SetField(TiffTag.COMPRESSION, Compression.LZW);
        stencilTiff.SetField(TiffTag.PREDICTOR, Predictor.HORIZONTAL);
        stencilTiff.SetField(TiffTag.SAMPLEFORMAT, SampleFormat.UINT);
        stencilTiff.SetField(TiffTag.PAGENUMBER, 0, 1);
        stencilTiff.WriteEncodedStrip(0, stencil, stencil.Length);
        stencilTiff.WriteDirectory();
        stencilTiff.Flush();
        stencilTiff.Close();

SOLVED: Enabled MSAA + VSYNC, enabled mod and disable MSAA and vsync again. Now everything looks fine! Thanks to Cuky88