import os, sys, cv2, re, getopt, shutil
import numpy as np
import matplotlib.pyplot as plt
def showBin(srcdict):
dirnm, fnm = os.path.split(os.path.abspath(srcdict["fileName"]))
raw = np.fromfile(os.path.join(dirnm, fnm), dtype="uint"+str(srcdict["bitwidth"]))
if "littile" != srcdict["byorder"]:
raw = raw.newbyteorder()
if "bitwidth" in srcdict and "bitdepth" in srcdict \
and srcdict["bitwidth"] > srcdict["bitdepth"]:
raw = raw >> (srcdict["bitwidth"] - srcdict["bitdepth"])
raw = np.reshape(raw >> (srcdict["bitwidth"] - srcdict["bitdepth"]), (srcdict["high"], srcdict["width"]))
plt.figure(dpi=900)
plt.subplot(1, 2, 1), plt.title('Gray')
img_scaled = cv2.normalize(raw, dst=None, alpha=0, beta = 2**srcdict["bitdepth"] - 1, norm_type=cv2.NORM_MINMAX)
plt.imshow(img_scaled, cmap = "gray", vmin = 0, vmax = 2 ** srcdict["bitdepth"] - 1), plt.axis('off')
plt.subplot(1, 2, 2), plt.title(srcdict["firpattern"])
raw = cv2.normalize(raw, dst=None, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX)
rgbImg = cv2.cvtColor(raw.astype("uint8"), cfalist[srcdict["firpattern"]])
plt.imshow(rgbImg.astype("uint8")), plt.axis('off')
plt.subplots_adjust(left=0.01, bottom=0.01, right=0.99, top=0.99, wspace=0.01, hspace=0.01)
plt.savefig(os.path.join(dirnm, os.path.splitext(fnm)[0]+'.png'), bbox_inches='tight')
plt.get_current_fig_manager().window.state('zoomed')
plt.show()
def showRaw(srcdict):
dirnm, fnm = os.path.split(os.path.abspath(srcdict["fileName"]))
srcdict["fileName"] = os.path.join(dirnm, os.path.splitext(fnm)[0] + '.bin')
shutil.copyfile(os.path.join(dirnm, fnm), srcdict["fileName"])
showBin(srcdict)
def showHex(srcdict):
dirnm, fnm = os.path.split(os.path.abspath(srcdict["fileName"]))
srcdict["fileName"] = os.path.join(dirnm, os.path.splitext(fnm)[0]+'.bin')
with open(os.path.join(dirnm, fnm), 'r', encoding="utf-8") as rfd:
with open(srcdict["fileName"], 'wb') as wfd:
for line in rfd.readlines():
val = line.strip().split()[-1]
wfd.write(bytes.fromhex(val))
showBin(srcdict)
pass
cfalist = {
"RG": cv2.COLOR_BayerRG2RGB,
"GR": cv2.COLOR_BayerGR2RGB,
"GB": cv2.COLOR_BayerGB2RGB,
"BG": cv2.COLOR_BayerBG2RGB
}
funclist = {
".bin": showBin,
".raw": showRaw,
".hex": showHex
}
if __name__ == '__main__':
srcdict = {
"byorder": "littile",
"firpattern": "RG"
}
opts, args = getopt.getopt(sys.argv[1:],
'-f:-w:-h:-d:-t:-o:-p:',
['filename=', 'width=', "high=", "bitdepth=", "bitwidth=", "byorder=", "firpattern="])
for opt_name, opt_value in opts:
if opt_name in ('-f', '--filename'):
srcdict["fileName"] = opt_value
elif opt_name in ('-w', '--width'):
srcdict["width"] = int(opt_value)
elif opt_name in ('-h', '--high'):
srcdict["high"] = int(opt_value)
elif opt_name in ('-d', '--bitdepth'):
srcdict["bitdepth"] = int(opt_value)
elif opt_name in ('-t', '--bitwidth'):
srcdict["bitwidth"] = int(opt_value)
elif opt_name in ('-o', '--byorder'):
srcdict["byorder"] = opt_value
elif opt_name in ('-p', '--firpattern'):
srcdict["firpattern"] = opt_value.upper()
pass
funclist[os.path.splitext(srcdict["fileName"])[-1]](srcdict)