Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def processInlined(fileName: str, referenceFileName: str, exif: PyffyExif):
- gaussianFilterSigma = 50.0
- intensity = 1.0
- image = np.fromfile(fileName, dtype = np.uint16, count = exif.dataSizeInWords, offset = exif.dataOffset)
- reference = np.fromfile(referenceFileName, dtype = np.uint16, count = exif.dataSizeInWords, offset = exif.dataOffset)
- activeAreaImage = image.reshape(exif.imageHeight, exif.imageWidth)[exif.activeArea[0]:exif.activeArea[2], exif.activeArea[1]:exif.activeArea[3]]
- activeAreaReference = reference.reshape(exif.imageHeight, exif.imageWidth)[exif.activeArea[0]:exif.activeArea[2], exif.activeArea[1]:exif.activeArea[3]]
- activeAreaImageHeight = activeAreaImage.shape[0]
- activeAreaImageWidth = activeAreaImage.shape[1]
- r1 = activeAreaImage[::2].reshape((-1))
- r2 = activeAreaImage[1::2].reshape((-1))
- channels = np.empty((4, activeAreaImage.size // 4), dtype = uint16)
- channels[0] = r1[0::2]
- channels[1] = r1[1::2]
- channels[2] = r2[0::2]
- channels[3] = r2[1::2]
- r1 = activeAreaReference[::2].reshape((-1))
- r2 = activeAreaReference[1::2].reshape((-1))
- referenceChannels = np.empty((4, activeAreaReference.size // 4), dtype = uint16)
- referenceChannels[0] = r1[0::2]
- referenceChannels[1] = r1[1::2]
- referenceChannels[2] = r2[0::2]
- referenceChannels[3] = r2[1::2]
- referenceChannels = referenceChannels.astype(float32)
- result = np.empty_like(referenceChannels, dtype = float32)
- for i in range(referenceChannels.shape[0]):
- channel = referenceChannels[i].reshape(activeAreaImageHeight // 2, activeAreaImageWidth // 2)
- channel = cv2.GaussianBlur(channel, (0, 0), gaussianFilterSigma, gaussianFilterSigma)
- result[i] = channel.reshape(-1)
- result = np.empty_like(referenceChannels, dtype = float32)
- for i in range(referenceChannels.shape[0]):
- result[i] = referenceChannels[i] / np.max(referenceChannels[i])
- channels = channels.astype(float32)
- averagedGreenReferenceChannels = None
- greenChannelsCount = 0
- for i in range(channels.shape[0]):
- if exif.colorPattern[i] == 1:
- greenChannelsCount += 1
- if averagedGreenReferenceChannels is None:
- averagedGreenReferenceChannels = np.copy(referenceChannels[i])
- else:
- averagedGreenReferenceChannels += referenceChannels[i]
- averagedGreenReferenceChannels = averagedGreenReferenceChannels / greenChannelsCount
- for i in range(channels.shape[0]):
- channels[i] = np.divide(channels[i], 1 - (1 - averagedGreenReferenceChannels[i] * intensity), out = np.zeros_like(channels[i], dtype = float32), where = averagedGreenReferenceChannels[i] != 0)
- for i in range(channels.shape[0]):
- if exif.colorPattern[i] != 1:
- referenceChannels[i] = np.divide(referenceChannels[i], 1 - (1 - averagedGreenReferenceChannels[i] * intensity), out = np.zeros_like(referenceChannels[i], dtype = float32), where = averagedGreenReferenceChannels[i] != 0)
- for i in range(channels.shape[0]):
- if exif.colorPattern[i] != 1:
- channels[i] = np.divide(channels[i], 1 - (1 - referenceChannels[i] * intensity), out = np.zeros_like(channels[i], dtype = float32), where = referenceChannels[i] != 0)
- for i in range(channels.shape[0]):
- channels[i] += exif.blackLevels[i]
- channels[i] = np.clip(channels[i], a_min = 0, a_max = exif.whiteLevels[i])
- channels = channels.astype(uint16)
- channels01 = np.row_stack((channels[0], channels[1]))
- channels01 = np.reshape(channels01, (1, channels01[0].size * 2), "F")
- channels01 = np.reshape(channels01, (activeAreaImageHeight // 2, activeAreaImageWidth))
- channels23 = np.row_stack((channels[2], channels[3]))
- channels23 = np.reshape(channels23, (1, channels23[0].size * 2), "F")
- channels23 = np.reshape(channels23, (activeAreaImageHeight // 2, activeAreaImageWidth))
- activeAreaImage = np.hstack((channels01, channels23)).reshape(activeAreaImageHeight, activeAreaImageWidth)
- image = image.reshape(exif.imageHeight, exif.imageWidth)
- activeAreaImage = activeAreaImage.reshape(exif.activeArea[2] - exif.activeArea[0], exif.activeArea[3] - exif.activeArea[1])
- image[exif.activeArea[0]:exif.activeArea[2], exif.activeArea[1]:exif.activeArea[3]] = activeAreaImage
- image = image.reshape(-1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement