資源簡介
python實現(xiàn)yuv轉(zhuǎn)RGB圖片程序,更多說明訪問我的博客https://blog.csdn.net/bvngh3247

代碼片段和文件信息
“““
Paper:?“Fast?and?Accurate?Image?Super?Resolution?by?Deep?CNN?with?Skip?Connection?and?Network?in?Network“
utility?functions
“““
import?os
import?numpy?as?np
from?scipy?import?misc
from?PIL?import?Image
class?LoadError(Exception):
def?__init__(self?message):
self.message?=?message
def?load_image(filename?width=0?height=0?channels=0?alignment=0?print_console=True):
if?not?os.path.isfile(filename):
raise?LoadError(“File?not?found?[%s]“?%?filename)
image?=?misc.imread(filename)
if?len(image.shape)?==?2:
image?=?image.reshape(image.shape[0]?image.shape[1]?1)
if?(width?!=?0?and?image.shape[1]?!=?width)?or?(height?!=?0?and?image.shape[0]?!=?height):
raise?LoadError(“Attributes?mismatch“)
if?channels?!=?0?and?image.shape[2]?!=?channels:
raise?LoadError(“Attributes?mismatch“)
if?alignment?!=?0?and?((width?%?alignment)?!=?0?or?(height?%?alignment)?!=?0):
raise?LoadError(“Attributes?mismatch“)
if?print_console:
print(“Loaded?[%s]:?%d?x?%d?x?%d“?%?(filename?image.shape[1]?image.shape[0]?image.shape[2]))
return?image
def?save_image(filename?image?print_console=True):
if?len(image.shape)?>=?3?and?image.shape[2]?==?1:
image?=?image.reshape(image.shape[0]?image.shape[1])
directory?=?os.path.dirname(filename)
if?directory?!=?““?and?not?os.path.exists(directory):
os.makedirs(directory)
image?=?misc.toimage(image?cmin=0?cmax=255)??#?to?avoid?range?rescaling
misc.imsave(filename?image)
if?print_console:
print(“Saved?[%s]“?%?filename)
def?convert_rgb_to_ycbcr(image?jpeg_mode=True?max_value=255):
if?len(image.shape)?2?or?image.shape[2]?==?1:
return?image
if?jpeg_mode:
xform?=?np.array([[0.299?0.587?0.114]?[-0.169?-?0.331?0.500]?[0.500?-?0.419?-?0.081]])
ycbcr_image?=?image.dot(xform.T)
ycbcr_image[:?:?[1?2]]?+=?max_value?/?2
else:
xform?=?np.array(
[[65.481?/?256.0?128.553?/?256.0?24.966?/?256.0]?[-?37.945?/?256.0?-?74.494?/?256.0?112.439?/?256.0]
?[112.439?/?256.0?-?94.154?/?256.0?-?18.285?/?256.0]])
ycbcr_image?=?image.dot(xform.T)
ycbcr_image[:?:?0]?+=?(16.0?*?max_value?/?256.0)
ycbcr_image[:?:?[1?2]]?+=?(128.0?*?max_value?/?256.0)
return?ycbcr_image
def?convert_rgb_to_y(image?jpeg_mode=True?max_value=255.0):
if?len(image.shape)?<=?2?or?image.shape[2]?==?1:
return?image
if?jpeg_mode:
xform?=?np.array([[0.299?0.587?0.114]])
y_image?=?image.dot(xform.T)
else:
xform?=?np.array([[65.481?/?256.0?128.553?/?256.0?24.966?/?256.0]])
y_image?=?image.dot(xform.T)?+?(16.0?*?max_value?/?256.0)
return?y_image
def?read_yuv420_file(r_file?y?cb?cr?w?h?frame_num):
????????my_file?=?open(r_file‘rb‘)
????????my_file.read((frame_num-1)*int(w*h*3/2))
????????for?num?in?range(0?1):
????????????print?(‘frame?=?‘?+?str(frame_num))
????????????for?i?in?range(0?h):
????????????????for?j?in?range(0?w):
????????????????????data?=?my_file.read(1)
????????????????????data?=?ord(data)
????????????????????y[i?j]?=?data
????????????for?y?in?
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????6597??2018-11-08?16:27??utilty.py
?????文件???????1499??2018-11-09?14:40??yuv_rgb.py
-----------?---------??----------?-----??----
?????????????????8096????????????????????2
評論
共有 條評論