Guest User

Most possible combinations of disparity

a guest
May 19th, 2020
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.06 KB | None | 0 0
  1. import cv2
  2. import numpy as np
  3. import re
  4.  
  5. testMode = True
  6.  
  7. # Using http://vision.middlebury.edu/stereo/data/scenes2006/FullSize/zip-2views/Aloe-2views.zip
  8. # Using http://vision.middlebury.edu/stereo/data/scenes2006/FullSize/zip-2views/Bowling1-2views.zip
  9.  
  10. datasets = [
  11.     {
  12.         "leftStereoImg": "./Bowling1/view1.png",
  13.         "rightStereoImg": "./Bowling1/view5.png",
  14.         "leftDisparityMap": "./Bowling1/disp1.png",
  15.         "rightDisparityMap": "./Bowling1/disp5.png"
  16.     }
  17.     ,
  18.     {
  19.         "leftStereoImg": "./Aloe/view1.png",
  20.         "rightStereoImg": "./Aloe/view5.png",
  21.        "leftDisparityMap": "./Aloe/disp1.png",
  22.         "rightDisparityMap": "./Aloe/disp5.png"
  23.     }
  24. ]
  25.  
  26. datasetToUse = 0
  27. dataset = datasets[datasetToUse]
  28.  
  29. leftStereoImg = cv2.imread(dataset["leftStereoImg"])
  30. rightStereoImg = cv2.imread(dataset["rightStereoImg"])
  31.  
  32. [rows, columns, channels] = leftStereoImg.shape # TODO: better image shape comparison logic
  33.  
  34.  
  35. def parseInputToUnderstandShiftingLogic(shiftingDescription):
  36.     # example input:
  37.     #   "shift leftStereoImg to the right using leftDisparityMap to get rightStereoImg"
  38.     regexExpressions = {
  39.         "imageToShift": r"(?![(shift)\s])\w+(?=( to the))",
  40.         "action": r"(?![(to the)\s])\w+(?=( using))",
  41.         "disparityMapToUse": r"(?![(using)\s])\w+(?=( to get))",
  42.         "expectedResultingImage": r"(?![(to get)\s])\w+$"
  43.     }
  44.     imageToShift = re.search(regexExpressions["imageToShift"], shiftingDescription).group()
  45.     action = re.search(regexExpressions["action"], shiftingDescription).group()
  46.     expectedResultingImage = re.search(regexExpressions["expectedResultingImage"], shiftingDescription).group()
  47.     disparityMapToUse = re.search(regexExpressions["disparityMapToUse"], shiftingDescription).group()
  48.     return [imageToShift, action, expectedResultingImage, disparityMapToUse]
  49.  
  50. if testMode :
  51.     # test1
  52.     shiftingDescription = "shift leftStereoImg to the right using leftDisparityMap to get rightStereoImg"
  53.     [imageToShift, action, expectedResultingImage, disparityMapToUse] = parseInputToUnderstandShiftingLogic(shiftingDescription)
  54.     description = "Using " + disparityMapToUse + " to shift " + imageToShift + " to the " + action + " to get " + expectedResultingImage
  55.     assert description == "Using leftDisparityMap to shift leftStereoImg to the right to get rightStereoImg"
  56.     shiftingDescription = "shift leftStereoImg to the right using leftDisparityMap to get rightStereoImg"
  57.     # test2
  58.     shiftingDescription = "shift rightStereoImg to the left using leftDisparityMap to get leftStereoImg"
  59.     [imageToShift, action, expectedResultingImage, disparityMapToUse] = parseInputToUnderstandShiftingLogic(shiftingDescription)
  60.     description = "Using " + disparityMapToUse + " to shift " + imageToShift + " to the " + action + " to get " + expectedResultingImage
  61.     assert description == "Using leftDisparityMap to shift rightStereoImg to the left to get leftStereoImg"
  62.  
  63. def generateShiftedImage(shiftingDescription):
  64.     [imageToShift, action, expectedResultingImage, disparityMapToUse] = parseInputToUnderstandShiftingLogic(shiftingDescription)
  65.     description = "Using " + disparityMapToUse + " to shift " + imageToShift + " to the " + action + " to get " + expectedResultingImage
  66.     print(description)
  67.  
  68.     disparityMap = cv2.imread(dataset[disparityMapToUse], cv2.IMREAD_GRAYSCALE)
  69.  
  70.     shiftedImage = np.zeros([rows, columns, channels])
  71.    
  72.     imageToShiftSource = leftStereoImg if imageToShift == "leftStereoImg" else rightStereoImg
  73.     for row in range(rows):
  74.         for column in range(columns):
  75.             disparityMap_point = disparityMap[row, column]
  76.             if disparityMap_point != 0 :
  77.                 # Since we want to shift to the right, we need to move the pixels in the negative axis
  78.                 t = column - disparityMap[row,column] if action == "right" else column + disparityMap[row,column]
  79.                 if(t>=0 and t<columns):
  80.                     for ch in range(channels):
  81.                         shiftedImage[row,column,ch] = imageToShiftSource[row,t,ch]
  82.  
  83.  
  84.     cv2.imwrite(description + ".png", shiftedImage)
  85.  
  86. def main():
  87.     shiftingDescriptions = [
  88.         "shift leftStereoImg to the right using leftDisparityMap to get rightStereoImg",
  89.         "shift leftStereoImg to the right using rightDisparityMap to get rightStereoImg",
  90.         "shift leftStereoImg to the left using leftDisparityMap to get rightStereoImg",
  91.         "shift leftStereoImg to the left using rightDisparityMap to get rightStereoImg",
  92.         "shift rightStereoImg to the left using leftDisparityMap to get leftStereoImg",
  93.         "shift rightStereoImg to the left using rightDisparityMap to get leftStereoImg",
  94.         "shift rightStereoImg to the right using leftDisparityMap to get leftStereoImg",
  95.         "shift rightStereoImg to the right using rightDisparityMap to get leftStereoImg"
  96.     ]
  97.     for shiftingDescription in shiftingDescriptions:
  98.         generateShiftedImage(shiftingDescription)
  99.         print("done with " + shiftingDescription)
  100.  
  101. if __name__ == "__main__":
  102.     main()
Add Comment
Please, Sign In to add comment