Advertisement
OleksiiMatiash

Untitled

Nov 26th, 2023
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.37 KB | None | 0 0
  1. def processInlined(fileName: str, referenceFileName: str, exif: PyffyExif):
  2.     gaussianFilterSigma = 50.0
  3.     intensity = 1.0
  4.  
  5.     image = np.fromfile(fileName, dtype = np.uint16, count = exif.dataSizeInWords, offset = exif.dataOffset)
  6.     reference = np.fromfile(referenceFileName, dtype = np.uint16, count = exif.dataSizeInWords, offset = exif.dataOffset)
  7.     activeAreaImage = image.reshape(exif.imageHeight, exif.imageWidth)[exif.activeArea[0]:exif.activeArea[2], exif.activeArea[1]:exif.activeArea[3]]
  8.     activeAreaReference = reference.reshape(exif.imageHeight, exif.imageWidth)[exif.activeArea[0]:exif.activeArea[2], exif.activeArea[1]:exif.activeArea[3]]
  9.     activeAreaImageHeight = activeAreaImage.shape[0]
  10.     activeAreaImageWidth = activeAreaImage.shape[1]
  11.  
  12.     r1 = activeAreaImage[::2].reshape((-1))
  13.     r2 = activeAreaImage[1::2].reshape((-1))
  14.     channels = np.empty((4, activeAreaImage.size // 4), dtype = uint16)
  15.     channels[0] = r1[0::2]
  16.     channels[1] = r1[1::2]
  17.     channels[2] = r2[0::2]
  18.     channels[3] = r2[1::2]
  19.  
  20.     r1 = activeAreaReference[::2].reshape((-1))
  21.     r2 = activeAreaReference[1::2].reshape((-1))
  22.     referenceChannels = np.empty((4, activeAreaReference.size // 4), dtype = uint16)
  23.     referenceChannels[0] = r1[0::2]
  24.     referenceChannels[1] = r1[1::2]
  25.     referenceChannels[2] = r2[0::2]
  26.     referenceChannels[3] = r2[1::2]
  27.  
  28.     referenceChannels = referenceChannels.astype(float32)
  29.     result = np.empty_like(referenceChannels, dtype = float32)
  30.     for i in range(referenceChannels.shape[0]):
  31.         channel = referenceChannels[i].reshape(activeAreaImageHeight // 2, activeAreaImageWidth // 2)
  32.         channel = cv2.GaussianBlur(channel, (0, 0), gaussianFilterSigma, gaussianFilterSigma)
  33.         result[i] = channel.reshape(-1)
  34.  
  35.     result = np.empty_like(referenceChannels, dtype = float32)
  36.     for i in range(referenceChannels.shape[0]):
  37.         result[i] = referenceChannels[i] / np.max(referenceChannels[i])
  38.  
  39.     channels = channels.astype(float32)
  40.     averagedGreenReferenceChannels = None
  41.     greenChannelsCount = 0
  42.     for i in range(channels.shape[0]):
  43.         if exif.colorPattern[i] == 1:
  44.             greenChannelsCount += 1
  45.             if averagedGreenReferenceChannels is None:
  46.                 averagedGreenReferenceChannels = np.copy(referenceChannels[i])
  47.             else:
  48.                 averagedGreenReferenceChannels += referenceChannels[i]
  49.  
  50.     averagedGreenReferenceChannels = averagedGreenReferenceChannels / greenChannelsCount
  51.     for i in range(channels.shape[0]):
  52.         channels[i] = np.divide(channels[i], 1 - (1 - averagedGreenReferenceChannels[i] * intensity), out = np.zeros_like(channels[i], dtype = float32), where = averagedGreenReferenceChannels[i] != 0)
  53.  
  54.     for i in range(channels.shape[0]):
  55.         if exif.colorPattern[i] != 1:
  56.             referenceChannels[i] = np.divide(referenceChannels[i], 1 - (1 - averagedGreenReferenceChannels[i] * intensity), out = np.zeros_like(referenceChannels[i], dtype = float32), where = averagedGreenReferenceChannels[i] != 0)
  57.  
  58.     for i in range(channels.shape[0]):
  59.         if exif.colorPattern[i] != 1:
  60.             channels[i] = np.divide(channels[i], 1 - (1 - referenceChannels[i] * intensity), out = np.zeros_like(channels[i], dtype = float32), where = referenceChannels[i] != 0)
  61.  
  62.     for i in range(channels.shape[0]):
  63.         channels[i] += exif.blackLevels[i]
  64.         channels[i] = np.clip(channels[i], a_min = 0, a_max = exif.whiteLevels[i])
  65.  
  66.     channels = channels.astype(uint16)
  67.  
  68.     channels01 = np.row_stack((channels[0], channels[1]))
  69.     channels01 = np.reshape(channels01, (1, channels01[0].size * 2), "F")
  70.     channels01 = np.reshape(channels01, (activeAreaImageHeight // 2, activeAreaImageWidth))
  71.  
  72.     channels23 = np.row_stack((channels[2], channels[3]))
  73.     channels23 = np.reshape(channels23, (1, channels23[0].size * 2), "F")
  74.     channels23 = np.reshape(channels23, (activeAreaImageHeight // 2, activeAreaImageWidth))
  75.  
  76.     activeAreaImage = np.hstack((channels01, channels23)).reshape(activeAreaImageHeight, activeAreaImageWidth)
  77.  
  78.     image = image.reshape(exif.imageHeight, exif.imageWidth)
  79.     activeAreaImage = activeAreaImage.reshape(exif.activeArea[2] - exif.activeArea[0], exif.activeArea[3] - exif.activeArea[1])
  80.     image[exif.activeArea[0]:exif.activeArea[2], exif.activeArea[1]:exif.activeArea[3]] = activeAreaImage
  81.     image = image.reshape(-1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement