def vilization_and_show():
model = model_EES16()
model.load_weights("EES_check.h5")
IMG_NAME = "comic.bmp"
INPUT_NAME = "input.jpg"
img = cv2.imread(IMG_NAME)
shape = img.shape
img = cv2.resize(img, (shape[1] / 2, shape[0] / 2), cv2.INTER_CUBIC)
cv2.imwrite(INPUT_NAME, img)
img = cv2.cvtColor(img, cv2.COLOR_BGR2YCrCb)
Y = numpy.zeros((1, img.shape[0], img.shape[1], 1))
Y[0, :, :, 0] = img[:, :, 0]
feature_map_visilization(model, Y)
python类COLOR_BGR2YCrCb()的实例源码
def histogram_equalization(image, tile):
if (tile < 0):
tile = 0
elif (tile > 100):
tile = 100
tile = int(tile / 10)
img_yuv = cv2.cvtColor(image, cv2.COLOR_BGR2YCrCb)
clahe = cv2.createCLAHE(clipLimit=1.0, tileGridSize=(2 ** tile, 2 ** tile))
img_yuv[:, :, 0] = clahe.apply(img_yuv[:, :, 0])
img_out = cv2.cvtColor(img_yuv, cv2.COLOR_YCrCb2BGR)
img = exposure.rescale_intensity(img_out)
return img
def prepare_training_data():
names = os.listdir(DATA_PATH)
names = sorted(names)
nums = names.__len__()
data = numpy.zeros((nums * Random_Crop, 1, Patch_size, Patch_size), dtype=numpy.double)
label = numpy.zeros((nums * Random_Crop, 1, label_size, label_size), dtype=numpy.double)
for i in range(nums):
name = DATA_PATH + names[i]
hr_img = cv2.imread(name, cv2.IMREAD_COLOR)
shape = hr_img.shape
hr_img = cv2.cvtColor(hr_img, cv2.COLOR_BGR2YCrCb)
hr_img = hr_img[:, :, 0]
# produce Random_Crop random coordinate to crop training img
if(min(shape[0], shape[1]) - label_size < 0):
continue
Points_x = numpy.random.randint(0, min(shape[0], shape[1]) - label_size, Random_Crop)
Points_y = numpy.random.randint(0, min(shape[0], shape[1]) - label_size, Random_Crop)
for j in range(Random_Crop):
hr_patch = hr_img[Points_x[j]: Points_x[j] + label_size, Points_y[j]: Points_y[j] + label_size]
lr_patch = cv2.resize(hr_patch, (label_size / scale, label_size / scale), cv2.INTER_CUBIC)
lr_patch = lr_patch.astype(float) / 255.
hr_patch = hr_patch.astype(float) / 255.
data[i * Random_Crop + j, 0, :, :] = lr_patch
label[i * Random_Crop + j, 0, :, :] = hr_patch
# cv2.imshow("lr", lr_patch)
# cv2.imshow("hr", hr_patch)
# cv2.waitKey(0)
return data, label
def EEDS_predict():
EEDS = model_EEDS(input_col=128, input_row=128)
EEDS.load_weights("EEDS8_model_adam100.h5")
IMG_NAME = "butterfly_GT.bmp"
INPUT_NAME = "input.jpg"
OUTPUT_NAME = "EEDS8_adam100.jpg"
import cv2
img = cv2.imread(IMG_NAME)
# img = img[:96, :96, :]
shape = img.shape
img = cv2.resize(img, (shape[1] / 2, shape[0] / 2), cv2.INTER_CUBIC)
cv2.imwrite(INPUT_NAME, img)
img = cv2.cvtColor(img, cv2.COLOR_BGR2YCrCb)
Y = numpy.zeros((1, img.shape[0], img.shape[1], 1))
Y[0, :, :, 0] = img[:, :, 0]
img = cv2.resize(img, (shape[1], shape[0]), cv2.INTER_CUBIC)
pre = EEDS.predict(Y, batch_size=1)
pre[pre[:] > 255] = 255
pre[pre[:] < 0] = 0
pre = pre.astype(numpy.uint8)
img[:, :, 0] = pre[0, :, :, 0]
img = cv2.cvtColor(img, cv2.COLOR_YCrCb2BGR)
cv2.imwrite(OUTPUT_NAME, img)
# psnr calculation:
im1 = cv2.imread(IMG_NAME, cv2.IMREAD_COLOR)
im1 = cv2.cvtColor(im1, cv2.COLOR_BGR2YCrCb)
im2 = cv2.imread(INPUT_NAME, cv2.IMREAD_COLOR)
im2 = cv2.cvtColor(im2, cv2.COLOR_BGR2YCrCb)
im2 = cv2.resize(im2, (img.shape[1], img.shape[0]))
im3 = cv2.imread(OUTPUT_NAME, cv2.IMREAD_COLOR)
im3 = cv2.cvtColor(im3, cv2.COLOR_BGR2YCrCb)
print "Bicubic:"
print cv2.PSNR(im1, im2) # [:, :, 0]
print "EEDS:"
print cv2.PSNR(im1, im3)
def EES_predict():
EES = model_EES(input_col=128, input_row=128)
EES.load_weights("EES_model_adam200.h5")
IMG_NAME = "butterfly_GT.bmp"
INPUT_NAME = "input.jpg"
OUTPUT_NAME = "EES_pre_adam200.jpg"
import cv2
img = cv2.imread(IMG_NAME)
# img = img[:96, :96, :]
shape = img.shape
img = cv2.resize(img, (shape[1] / 2, shape[0] / 2), cv2.INTER_CUBIC)
cv2.imwrite(INPUT_NAME, img)
img = cv2.cvtColor(img, cv2.COLOR_BGR2YCrCb)
Y = numpy.zeros((1, img.shape[0], img.shape[1], 1))
Y[0, :, :, 0] = img[:, :, 0]
img = cv2.resize(img, (shape[1], shape[0]), cv2.INTER_CUBIC)
pre = EES.predict(Y, batch_size=1)
pre[pre[:] > 255] = 255
pre[pre[:] < 0] = 0
pre = pre.astype(numpy.uint8)
img[:, :, 0] = pre[0, :, :, 0]
img = cv2.cvtColor(img, cv2.COLOR_YCrCb2BGR)
cv2.imwrite(OUTPUT_NAME, img)
# psnr calculation:
im1 = cv2.imread(IMG_NAME, cv2.IMREAD_COLOR)
# im1 = cv2.cvtColor(im1, cv2.COLOR_BGR2YCrCb)
im2 = cv2.imread(INPUT_NAME, cv2.IMREAD_COLOR)
# im2 = cv2.cvtColor(im2, cv2.COLOR_BGR2YCrCb)
im2 = cv2.resize(im2, (img.shape[1], img.shape[0]))
im3 = cv2.imread(OUTPUT_NAME, cv2.IMREAD_COLOR)
# im3 = cv2.cvtColor(im3, cv2.COLOR_BGR2YCrCb)
print "Bicubic:"
print cv2.PSNR(im1, im2)
print "EES:"
print cv2.PSNR(im1, im3)
def EED_predict():
EED = model_EED()
EED.load_weights("EED_model_adam100.h5")
IMG_NAME = "butterfly_GT.bmp"
INPUT_NAME = "input.jpg"
OUTPUT_NAME = "EED_pre_adam100.jpg"
import cv2
img = cv2.imread(IMG_NAME)
shape = img.shape
img = cv2.resize(img, (shape[1] / 2, shape[0] / 2), cv2.INTER_CUBIC)
cv2.imwrite(INPUT_NAME, img)
img = cv2.cvtColor(img, cv2.COLOR_BGR2YCrCb)
Y = numpy.zeros((1, img.shape[0], img.shape[1], 1))
Y[0, :, :, 0] = img[:, :, 0]
img = cv2.resize(img, (shape[1], shape[0]), cv2.INTER_CUBIC)
pre = EED.predict(Y, batch_size=1)
pre[pre[:] > 255] = 255
pre = pre.astype(numpy.uint8)
img[:, :, 0] = pre[0, :, :, 0]
img = cv2.cvtColor(img, cv2.COLOR_YCrCb2BGR)
cv2.imwrite(OUTPUT_NAME, img)
# psnr calculation:
im1 = cv2.imread(IMG_NAME, cv2.IMREAD_COLOR)
im1 = cv2.cvtColor(im1, cv2.COLOR_BGR2YCrCb)
im2 = cv2.imread(INPUT_NAME, cv2.IMREAD_COLOR)
im2 = cv2.cvtColor(im2, cv2.COLOR_BGR2YCrCb)
im2 = cv2.resize(im2, (img.shape[1], img.shape[0]))
im3 = cv2.imread(OUTPUT_NAME, cv2.IMREAD_COLOR)
im3 = cv2.cvtColor(im3, cv2.COLOR_BGR2YCrCb)
print "Bicubic:"
print cv2.PSNR(im1, im2)
print "EES:"
print cv2.PSNR(im1, im3)
def prepare_data(_path):
names = os.listdir(_path)
names = sorted(names)
nums = names.__len__()
data = numpy.zeros((nums * Random_Crop, 1, Patch_size, Patch_size), dtype=numpy.double)
label = numpy.zeros((nums * Random_Crop, 1, label_size, label_size), dtype=numpy.double)
for i in range(nums):
name = _path + names[i]
hr_img = cv2.imread(name, cv2.IMREAD_COLOR)
shape = hr_img.shape
hr_img = cv2.cvtColor(hr_img, cv2.COLOR_BGR2YCrCb)
hr_img = hr_img[:, :, 0]
# two resize operation to produce training data and labels
lr_img = cv2.resize(hr_img, (shape[1] / scale, shape[0] / scale))
lr_img = cv2.resize(lr_img, (shape[1], shape[0]))
# produce Random_Crop random coordinate to crop training img
Points_x = numpy.random.randint(0, min(shape[0], shape[1]) - Patch_size, Random_Crop)
Points_y = numpy.random.randint(0, min(shape[0], shape[1]) - Patch_size, Random_Crop)
for j in range(Random_Crop):
lr_patch = lr_img[Points_x[j]: Points_x[j] + Patch_size, Points_y[j]: Points_y[j] + Patch_size]
hr_patch = hr_img[Points_x[j]: Points_x[j] + Patch_size, Points_y[j]: Points_y[j] + Patch_size]
lr_patch = lr_patch.astype(float) / 255.
hr_patch = hr_patch.astype(float) / 255.
data[i * Random_Crop + j, 0, :, :] = lr_patch
label[i * Random_Crop + j, 0, :, :] = hr_patch[conv_side: -conv_side, conv_side: -conv_side]
# cv2.imshow("lr", lr_patch)
# cv2.imshow("hr", hr_patch)
# cv2.waitKey(0)
return data, label
# BORDER_CUT = 8
def predict():
srcnn_model = predict_model()
srcnn_model.load_weights("3051crop_weight_200.h5")
IMG_NAME = "/home/mark/Engineer/SR/data/Set14/flowers.bmp"
INPUT_NAME = "input2.jpg"
OUTPUT_NAME = "pre2.jpg"
import cv2
img = cv2.imread(IMG_NAME, cv2.IMREAD_COLOR)
img = cv2.cvtColor(img, cv2.COLOR_BGR2YCrCb)
shape = img.shape
Y_img = cv2.resize(img[:, :, 0], (shape[1] / 2, shape[0] / 2), cv2.INTER_CUBIC)
Y_img = cv2.resize(Y_img, (shape[1], shape[0]), cv2.INTER_CUBIC)
img[:, :, 0] = Y_img
img = cv2.cvtColor(img, cv2.COLOR_YCrCb2BGR)
cv2.imwrite(INPUT_NAME, img)
Y = numpy.zeros((1, img.shape[0], img.shape[1], 1), dtype=float)
Y[0, :, :, 0] = Y_img.astype(float) / 255.
pre = srcnn_model.predict(Y, batch_size=1) * 255.
pre[pre[:] > 255] = 255
pre[pre[:] < 0] = 0
pre = pre.astype(numpy.uint8)
img = cv2.cvtColor(img, cv2.COLOR_BGR2YCrCb)
img[6: -6, 6: -6, 0] = pre[0, :, :, 0]
img = cv2.cvtColor(img, cv2.COLOR_YCrCb2BGR)
cv2.imwrite(OUTPUT_NAME, img)
# psnr calculation:
im1 = cv2.imread(IMG_NAME, cv2.IMREAD_COLOR)
im1 = cv2.cvtColor(im1, cv2.COLOR_BGR2YCrCb)[6: -6, 6: -6, 0]
im2 = cv2.imread(INPUT_NAME, cv2.IMREAD_COLOR)
im2 = cv2.cvtColor(im2, cv2.COLOR_BGR2YCrCb)[6: -6, 6: -6, 0]
im3 = cv2.imread(OUTPUT_NAME, cv2.IMREAD_COLOR)
im3 = cv2.cvtColor(im3, cv2.COLOR_BGR2YCrCb)[6: -6, 6: -6, 0]
print "bicubic:"
print cv2.PSNR(im1, im2)
print "SRCNN:"
print cv2.PSNR(im1, im3)
def EES_predict():
IMG_NAME = "./butterfly_GT.bmp"
INPUT_NAME = "input.jpg"
OUTPUT_NAME = "EES_pre.jpg"
label = cv2.imread(IMG_NAME)
shape = label.shape
img = cv2.resize(label, (shape[1] / scale, shape[0] / scale), cv2.INTER_CUBIC)
cv2.imwrite(INPUT_NAME, img)
EES = model_EES16()
EES.load_weights("EES_check.h5")
img = cv2.cvtColor(img, cv2.COLOR_BGR2YCrCb)
Y = numpy.zeros((1, img.shape[0], img.shape[1], 1))
Y[0, :, :, 0] = img[:, :, 0].astype(float) / 255.
img = cv2.cvtColor(label, cv2.COLOR_BGR2YCrCb)
pre = EES.predict(Y, batch_size=1) * 255.
pre[pre[:] > 255] = 255
pre = numpy.uint8(pre)
img[:, :, 0] = pre[0, :, :, 0]
img = cv2.cvtColor(img, cv2.COLOR_YCrCb2BGR)
cv2.imwrite(OUTPUT_NAME, img)
# psnr calculation:
im1 = cv2.imread(IMG_NAME, cv2.IMREAD_COLOR)
im1 = cv2.cvtColor(im1, cv2.COLOR_BGR2YCrCb)
im2 = cv2.imread(INPUT_NAME, cv2.IMREAD_COLOR)
im2 = cv2.cvtColor(im2, cv2.COLOR_BGR2YCrCb)
im2 = cv2.resize(im2, (img.shape[1], img.shape[0]))
cv2.imwrite("Bicubic.jpg", cv2.cvtColor(im2, cv2.COLOR_YCrCb2BGR))
im3 = cv2.imread(OUTPUT_NAME, cv2.IMREAD_COLOR)
im3 = cv2.cvtColor(im3, cv2.COLOR_BGR2YCrCb)
print "Bicubic:"
print cv2.PSNR(im1[:, :, 0], im2[:, :, 0])
print "EES:"
print cv2.PSNR(im1[:, :, 0], im3[:, :, 0])
##*******************************************************************************************************************//
def prepare_crop_data(_path):
names = os.listdir(_path)
names = sorted(names)
nums = names.__len__()
data = []
label = []
for i in range(nums):
name = _path + names[i]
hr_img = cv2.imread(name, cv2.IMREAD_COLOR)
hr_img = cv2.cvtColor(hr_img, cv2.COLOR_BGR2YCrCb)
hr_img = hr_img[:, :, 0]
shape = hr_img.shape
# two resize operation to produce training data and labels
lr_img = cv2.resize(hr_img, (shape[1] / scale, shape[0] / scale))
lr_img = cv2.resize(lr_img, (shape[1], shape[0]))
width_num = (shape[0] - (BLOCK_SIZE - BLOCK_STEP) * 2) / BLOCK_STEP
height_num = (shape[1] - (BLOCK_SIZE - BLOCK_STEP) * 2) / BLOCK_STEP
for k in range(width_num):
for j in range(height_num):
x = k * BLOCK_STEP
y = j * BLOCK_STEP
hr_patch = hr_img[x: x + BLOCK_SIZE, y: y + BLOCK_SIZE]
lr_patch = lr_img[x: x + BLOCK_SIZE, y: y + BLOCK_SIZE]
lr_patch = lr_patch.astype(float) / 255.
hr_patch = hr_patch.astype(float) / 255.
lr = numpy.zeros((1, Patch_size, Patch_size), dtype=numpy.double)
hr = numpy.zeros((1, label_size, label_size), dtype=numpy.double)
lr[0, :, :] = lr_patch
hr[0, :, :] = hr_patch[conv_side: -conv_side, conv_side: -conv_side]
data.append(lr)
label.append(hr)
data = numpy.array(data, dtype=float)
label = numpy.array(label, dtype=float)
return data, label