Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import cv2
- import numpy as np
- import re
- testMode = True
- # Using http://vision.middlebury.edu/stereo/data/scenes2006/FullSize/zip-2views/Aloe-2views.zip
- # Using http://vision.middlebury.edu/stereo/data/scenes2006/FullSize/zip-2views/Bowling1-2views.zip
- datasets = [
- {
- "leftStereoImg": "./Bowling1/view1.png",
- "rightStereoImg": "./Bowling1/view5.png",
- "leftDisparityMap": "./Bowling1/disp1.png",
- "rightDisparityMap": "./Bowling1/disp5.png"
- }
- ,
- {
- "leftStereoImg": "./Aloe/view1.png",
- "rightStereoImg": "./Aloe/view5.png",
- "leftDisparityMap": "./Aloe/disp1.png",
- "rightDisparityMap": "./Aloe/disp5.png"
- }
- ]
- datasetToUse = 0
- dataset = datasets[datasetToUse]
- leftStereoImg = cv2.imread(dataset["leftStereoImg"])
- rightStereoImg = cv2.imread(dataset["rightStereoImg"])
- [rows, columns, channels] = leftStereoImg.shape # TODO: better image shape comparison logic
- def parseInputToUnderstandShiftingLogic(shiftingDescription):
- # example input:
- # "shift leftStereoImg to the right using leftDisparityMap to get rightStereoImg"
- regexExpressions = {
- "imageToShift": r"(?![(shift)\s])\w+(?=( to the))",
- "action": r"(?![(to the)\s])\w+(?=( using))",
- "disparityMapToUse": r"(?![(using)\s])\w+(?=( to get))",
- "expectedResultingImage": r"(?![(to get)\s])\w+$"
- }
- imageToShift = re.search(regexExpressions["imageToShift"], shiftingDescription).group()
- action = re.search(regexExpressions["action"], shiftingDescription).group()
- expectedResultingImage = re.search(regexExpressions["expectedResultingImage"], shiftingDescription).group()
- disparityMapToUse = re.search(regexExpressions["disparityMapToUse"], shiftingDescription).group()
- return [imageToShift, action, expectedResultingImage, disparityMapToUse]
- if testMode :
- # test1
- shiftingDescription = "shift leftStereoImg to the right using leftDisparityMap to get rightStereoImg"
- [imageToShift, action, expectedResultingImage, disparityMapToUse] = parseInputToUnderstandShiftingLogic(shiftingDescription)
- description = "Using " + disparityMapToUse + " to shift " + imageToShift + " to the " + action + " to get " + expectedResultingImage
- assert description == "Using leftDisparityMap to shift leftStereoImg to the right to get rightStereoImg"
- shiftingDescription = "shift leftStereoImg to the right using leftDisparityMap to get rightStereoImg"
- # test2
- shiftingDescription = "shift rightStereoImg to the left using leftDisparityMap to get leftStereoImg"
- [imageToShift, action, expectedResultingImage, disparityMapToUse] = parseInputToUnderstandShiftingLogic(shiftingDescription)
- description = "Using " + disparityMapToUse + " to shift " + imageToShift + " to the " + action + " to get " + expectedResultingImage
- assert description == "Using leftDisparityMap to shift rightStereoImg to the left to get leftStereoImg"
- def generateShiftedImage(shiftingDescription):
- [imageToShift, action, expectedResultingImage, disparityMapToUse] = parseInputToUnderstandShiftingLogic(shiftingDescription)
- description = "Using " + disparityMapToUse + " to shift " + imageToShift + " to the " + action + " to get " + expectedResultingImage
- print(description)
- disparityMap = cv2.imread(dataset[disparityMapToUse], cv2.IMREAD_GRAYSCALE)
- shiftedImage = np.zeros([rows, columns, channels])
- imageToShiftSource = leftStereoImg if imageToShift == "leftStereoImg" else rightStereoImg
- for row in range(rows):
- for column in range(columns):
- disparityMap_point = disparityMap[row, column]
- if disparityMap_point != 0 :
- # Since we want to shift to the right, we need to move the pixels in the negative axis
- t = column - disparityMap[row,column] if action == "right" else column + disparityMap[row,column]
- if(t>=0 and t<columns):
- for ch in range(channels):
- shiftedImage[row,column,ch] = imageToShiftSource[row,t,ch]
- cv2.imwrite(description + ".png", shiftedImage)
- def main():
- shiftingDescriptions = [
- "shift leftStereoImg to the right using leftDisparityMap to get rightStereoImg",
- "shift leftStereoImg to the right using rightDisparityMap to get rightStereoImg",
- "shift leftStereoImg to the left using leftDisparityMap to get rightStereoImg",
- "shift leftStereoImg to the left using rightDisparityMap to get rightStereoImg",
- "shift rightStereoImg to the left using leftDisparityMap to get leftStereoImg",
- "shift rightStereoImg to the left using rightDisparityMap to get leftStereoImg",
- "shift rightStereoImg to the right using leftDisparityMap to get leftStereoImg",
- "shift rightStereoImg to the right using rightDisparityMap to get leftStereoImg"
- ]
- for shiftingDescription in shiftingDescriptions:
- generateShiftedImage(shiftingDescription)
- print("done with " + shiftingDescription)
- if __name__ == "__main__":
- main()
Add Comment
Please, Sign In to add comment