def ReadImages(ImageSize, DataPath):
    """
    Inputs: 
    ImageSize - Size of the Image
    DataPath - Paths of all images where testing will be run on
    Outputs:
    I1Combined - I1 image after any standardization and/or cropping/resizing to ImageSize
    I1 - Original I1 image for visualization purposes only
    """

    ImageName = DataPath
    Image = cv2.imread(ImageName)
    Gray = cv2.cvtColor(Image, cv2.COLOR_BGR2GRAY)

    if (Image is None):
        # OpenCV returns empty list if image is not read!
        print('ERROR: Image cannot be read')
        sys.exit()

    Tau = min(ImageSize[0], ImageSize[1]) / 3
    # generate label
    Label = np.random.randint(2 * Tau, size=8) - Tau

    # get corner correspondences
    Corners, Warped = iu.getCorrespondence(Image.shape, Label, ImageSize)

    # get forward and backward homographies
    CroppedPatch, WarpedPatch = iu.getWarpingPair(Gray, ImageSize, Corners,
                                                  Warped)
    I1 = np.float32(np.stack([CroppedPatch, WarpedPatch], -1))

    #cv2.imshow("Image", Image)
    #cv2.imshow("Warped Patch", WarpedPatch)
    #cv2.imshow("Cropped Patch", CroppedPatch)

    I1S = iu.StandardizeInputs(np.float32(I1))

    I1Combined = np.expand_dims(I1S, axis=0)

    return I1Combined, Image, Label, Corners
예제 #2
0
def GenerateBatch(BasePath, DirNamesTrain, ImageSize, LargeImgSize,
                  MiniBatchSize):
    """
    Inputs: 
    BasePath - Path to COCO folder without "/" at the end
    DirNamesTrain - Variable with Subfolder paths to train files
    NOTE that Train can be replaced by Val/Test for generating batch corresponding to validation (held-out testing in this case)/testing
    NOTE that TrainLabels can be replaced by Val/TestLabels for generating batch corresponding to validation (held-out testing in this case)/testing
    ImageSize - Size of the Image
    MiniBatchSize is the size of the MiniBatch
    Outputs:
    I1Batch - Batch of images
    LabelBatch - Batch of one-hot encoded labels 
    """
    Tau = min(ImageSize[0], ImageSize[1]) / 3

    # Share the random crop across the batch
    x, y = iu.randomCrop((LargeImgSize[0], LargeImgSize[1]), ImageSize)
    Corners = np.array([(x, y), (x + ImageSize[1], y),
                        (x + ImageSize[1], y + ImageSize[0]),
                        (x, y + ImageSize[0])],
                       dtype=np.float32)

    # Get indices for the patch
    Indices = np.arange(0, LargeImgSize[0] * LargeImgSize[1])
    Indices = Indices.reshape(LargeImgSize[0], LargeImgSize[1])
    Indices = Indices[y:y + ImageSize[0], x:x + ImageSize[1]]

    I1Batch = []
    ImgOrgBatch = []
    LabelBatch = []
    CornerBatch = []

    ImageNum = 0
    while ImageNum < MiniBatchSize:
        # Generate random image
        RandIdx = random.randint(0, len(DirNamesTrain) - 1)

        RandImageName = BasePath + "/Data" + os.sep + DirNamesTrain[
            RandIdx] + '.jpg'
        ImageNum += 1
        Image = cv2.imread(RandImageName, 0)
        Image = cv2.resize(Image, (LargeImgSize[1], LargeImgSize[0]))

        Label = np.random.randint(2 * Tau, size=8) - Tau

        # distination (mapped) corners with (x, y) format
        Warped = np.array(
            [(Corners[0, 0] + Label[0], Corners[0, 1] + Label[1]),
             (Corners[1, 0] + Label[2], Corners[1, 1] + Label[3]),
             (Corners[2, 0] + Label[4], Corners[2, 1] + Label[5]),
             (Corners[3, 0] + Label[6], Corners[3, 1] + Label[7])],
            dtype=np.float32)

        CroppedPatch, WarpedPatch = iu.getWarpingPair(Image, ImageSize,
                                                      Corners, Warped)

        #print('c', CroppedPatch.shape)
        #print('w', WarpedPatch.shape)

        #cv2.imshow("Image", Image)
        #cv2.imshow("Warped Patch", WarpedPatch)
        #cv2.imshow("Cropped Patch", CroppedPatch)

        I1 = np.float32(np.stack([CroppedPatch, WarpedPatch], -1))

        # Append All Images and Mask
        I1Batch.append(I1)
        ImgOrgBatch.append(Image[..., None])
        LabelBatch.append(Label)
        CornerBatch.append(Corners.reshape(8))

        #cv2.waitKey()

    return I1Batch, ImgOrgBatch, LabelBatch, CornerBatch, Indices