There are N kinds of encodings for images, such as JPG, PNG, BMP, etc. What's worse, the image data also has N kinds of encodings, RGB, RGBA, ARGB, BGRA, BGR, YUV, HSV, etc. YUV alone has multiple encodings. Format, such as NV21, NV12, SP420, sp422, etc. The most common RGBA requires different display solutions because of the different order of alpha channel, B channel and R channel. This brings a lot of inconvenience to our research on image processing, image conversion, and image display. Especially for cross-platform image transmission and streaming, if you don't pay attention, you will get a bunch of garbled characters or color errors.
Image encoding and image data encoding are different. For example, JPG is an image encoding. After decoding, you will get an image data. This data is generally an array of color information. On some systems, this array can be decoded to get GBR. Some of the data obtained are RGB data. Both are color information, but the order in the memory is different. The R channel and B channel are reversed on the display.
Most of the time, there is actually no need to transcode image data, and there are more image format conversions. In some systems that are very sensitive to processing speed, such as AI recognition, we need to do some data transcoding according to the needs of the system. In order to ensure the consistency and integrity of the data during the conversion process, we need to save the data during the debugging phase. What is saved is naked data such as RGB, BGR, and YUV. These naked data cannot be displayed intuitively. I did some research and used Opencv to display it. Used to check the integrity and consistency during data forwarding.
YUV420 image display
from PIL import Image
import sys
from struct import *
import array
if len(sys.argv) != 4:
print "***** Usage syntax Error!!!! *****\n"
print "Usage:"
print "python <script> <.yuv file yuv420p> <width>> <height> "
sys.exit(1) # exit
else:
pass
image_name = sys.argv[1]
width = int(sys.argv[2])
height = int(sys.argv[3])
y = array.array('B')
u = array.array('B')
v = array.array('B')
f_y = open(image_name, "rb")
f_uv = open(image_name, "rb")
f_uv.seek(width*height, 1)
image_out = Image.new("RGB", (width, height))
pix = image_out.load()
print "width=", width, "height=", height
for i in range(0, height/2):
for j in range(0, width/2):
u.append(ord(f_uv.read(1)));
for i in range(0, height/2):
for j in range(0, width/2):
v.append(ord(f_uv.read(1)));
for i in range(0,height):
for j in range(0, width):
y.append(ord(f_y.read(1)));
#print "i=", i, "j=", j , (i*width), ((i*width) +j)
#pix[j, i] = y[(i*width) +j], y[(i*width) +j], y[(i*width) +j]
Y_val = y[(i*width)+j]
U_val = u[((i/2)*(width/2))+(j/2)]
V_val = v[((i/2)*(width/2))+(j/2)]
B = 1.164 * (Y_val-16) + 2.018 * (U_val - 128)
G = 1.164 * (Y_val-16) - 0.813 * (V_val - 128) - 0.391 * (U_val - 128)
R = 1.164 * (Y_val-16) + 1.596*(V_val - 128)
pix[j, i] = int(R), int(G), int(B)
######################################################
# B = 1.164(Y - 16) + 2.018(U - 128)
# G = 1.164(Y - 16) - 0.813(V - 128) - 0.391(U - 128)
# R = 1.164(Y - 16) + 1.596(V - 128)
######################################################
#image_out.save("out.bmp")
image_out.show()
YUV422 image display
from PIL import Image
import sys
from struct import *
import array
if len(sys.argv) != 4:
print "***** Usage syntax Error!!!! *****\n"
print "Usage:"
print "python <script> <.yuv file uyvy422> <width>> <height> "
sys.exit(1) # exit
else:
pass
image_name = sys.argv[1]
width = int(sys.argv[2])
height = int(sys.argv[3])
y = array.array('B')
u = array.array('B')
v = array.array('B')
f_uyvy = open(image_name, "rb")
f_uv = open(image_name, "rb")
f_uv.seek(width*height, 1)
image_out = Image.new("RGB", (width, height))
pix = image_out.load()
print "width=", width, "height=", height
for i in range(0,height):
for j in range(0, width/2):
u = ord(f_uyvy.read(1));
y1 = ord(f_uyvy.read(1));
v = ord(f_uyvy.read(1));
y2 = ord(f_uyvy.read(1));
B = 1.164 * (y1-16) + 2.018 * (u - 128)
G = 1.164 * (y1-16) - 0.813 * (v - 128) - 0.391 * (u - 128)
R = 1.164 * (y1-16) + 1.596*(v - 128)
pix[j*2, i] = int(R), int(G), int(B)
B = 1.164 * (y2-16) + 2.018 * (u - 128)
G = 1.164 * (y2-16) - 0.813 * (v - 128) - 0.391 * (u - 128)
R = 1.164 * (y2-16) + 1.596*(v - 128)
pix[j*2+1, i] = int(R), int(G), int(B)
######################################################
# B = 1.164(Y - 16) + 2.018(U - 128)
# G = 1.164(Y - 16) - 0.813(V - 128) - 0.391(U - 128)
# R = 1.164(Y - 16) + 1.596(V - 128)
######################################################
#image_out.save("out.bmp")
image_out.show()
BGR image display
from PIL import Image
import sys
from struct import *
import array
if len(sys.argv) != 4:
print "***** Usage syntax Error!!!! *****\n"
print "Usage:"
print "python <script> <.bgr file bgr> <width>> <height> "
sys.exit(1) # exit
else:
pass
image_name = sys.argv[1]
width = int(sys.argv[2])
height = int(sys.argv[3])
y = array.array('B')
f_y = open(image_name, "rb")
image_out = Image.new("RGB", (width, height))
pix = image_out.load()
print "width=", width, "height=", height
for i in range(0, height):
for j in range(0, width*3):
y.append(ord(f_y.read(1)));
for i in range(0,height):
for j in range(0, width):
B = y[(i*width*3)+j*3]
G = y[(i*width*3)+j*3+1]
R = y[(i*width*3)+j*3+2]
pix[j, i] = int(R), int(G), int(B)
image_out.save("out.bmp")
image_out.show()
RGB is simple. The above program can display RGB without exchanging the positions of R and B.