Last active
November 14, 2018 14:28
-
-
Save dadeba/17fd396216ad82f21442c3195751fdfe to your computer and use it in GitHub Desktop.
YUYV422 to PNG using OpenCV
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
from __future__ import print_function | |
import cv2 | |
import numpy as np | |
import sys | |
import os.path | |
def c255(x): | |
return min(max(int(x),0),255) | |
fd = open(sys.argv[1], 'rb') | |
img_str = fd.read() | |
fd.close() | |
img_array = np.asarray(bytearray(img_str), dtype=np.uint8) | |
height = 1080 | |
width = 1920 | |
im = np.zeros((height,width,3), np.uint8) | |
c = 0 | |
for j in range(0,height-1): | |
for i in range(0,width-1,2): | |
ind = c * 4 | |
c = c + 1 | |
p0 = img_array[ind+0] | |
p1 = img_array[ind+1] | |
p2 = img_array[ind+2] | |
p3 = img_array[ind+3] | |
y1 = 255/219.0*(p0 - 16) | |
u = 127/112.0*(p1 - 128) | |
y2 = 255/219.0*(p2 - 16) | |
v = 127/112.0*(p3 - 128) | |
r1 = c255(y1 + 1.402 * v) | |
g1 = c255(y1 - 0.344 * u - 0.714*v) | |
b1 = c255(y1 + 1.772 * u) | |
r2 = c255(y2 + 1.402 * v) | |
g2 = c255(y2 - 0.344 * u - 0.714*v) | |
b2 = c255(y2 + 1.772 * u) | |
im[j,i,0] = b1 | |
im[j,i,1] = g1 | |
im[j,i,2] = r1 | |
im[j,i+1,0] = b2 | |
im[j,i+1,1] = g2 | |
im[j,i+1,2] = r2 | |
outname,ext = os.path.splitext(sys.argv[1]) | |
cv2.imwrite(outname+'.png', im) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment