Guest User

Most possible combinations of disparity (Updated)

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