Monday, 12 April 2021

opencv 29 super resolution

original image, butterfly is region of interest

zoomed in 4 times, butterfly is pixelated

render with super resolution, image is still sharp



#super_resolution.py
import cv2
import argparse
import os
import time
from cv2 import dnn_superres

# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True,
help="path to input image we want to increase resolution of")
args = vars(ap.parse_args())

orignal_iamge = cv2.imread(args["image"])


def super_resolution(model_path, image):
# extract the model name and model scale from the file path
modelName = model_path.split(os.path.sep)[-1].split("_")[0].lower()
modelScale = model_path.split("_x")[-1]
modelScale = int(modelScale[:modelScale.find(".")])

# initialize OpenCV's super resolution DNN object, load the super
# resolution model from disk, and set the model name and scale
print("[INFO] loading super resolution model: {}".format(
model_path))
print("[INFO] model name: {}".format(modelName))
print("[INFO] model scale: {}".format(modelScale))
sr = dnn_superres.DnnSuperResImpl_create()
sr.readModel(model_path)
sr.setModel(modelName, modelScale)

# load the input image from disk and display its spatial dimensions
print("[INFO] w: {}, h: {}".format(image.shape[1], image.shape[0]))

# use the super resolution model to upscale the image, timing how
# long it takes
start = time.time()
upscaled = sr.upsample(image)
end = time.time()
print("[INFO] super resolution took {:.6f} seconds".format(
end - start))

# show the spatial dimensions of the super resolution image
print("[INFO] w: {}, h: {}".format(upscaled.shape[1],
   upscaled.shape[0]))

cv2.imshow(modelName, upscaled)


xl, yl, xr, yr = (0, 0, 0, 0)
btn_down = False
img = orignal_iamge.copy()

def drag_event(event, x, y, flags, param):
global btn_down, xl, yl, xr, yr, img

if event == cv2.EVENT_LBUTTONUP and btn_down:
btn_down = False

cv2.rectangle(img, (xl - 2, yl - 2), (xr + 2, yr + 2), (0, 0, 255), 2)
cv2.imshow('original', img)

roi = orignal_iamge[yl:yr, xl:xr]

zoom_img = cv2.resize(roi, (0, 0), fx=4, fy=4)
cv2.imshow('zoom in', zoom_img)

super_resolution("models\ESPCN_x4.pb", roi)
super_resolution("models\FSRCNN_x4.pb", roi)
super_resolution("models\LapSRN_x4.pb", roi)


elif event == cv2.EVENT_MOUSEMOVE and btn_down:
xr, yr = (x, y)
cv2.rectangle(img, (xl - 2, yl - 2), (xr + 2, yr + 2), (0, 0, 255), 2)
cv2.imshow('original', img)
img = orignal_iamge.copy()

elif event == cv2.EVENT_LBUTTONDOWN:
btn_down = True
xl, yl = (x, y)

cv2.imshow("Image", orignal_iamge)
cv2.setMouseCallback('Image', drag_event)

cv2.waitKey(0)

--------------------
#logs
(venv) C:\Users\zchen\PycharmProjects\opencv>python super_resolution.py --image assets/butterfly.jpeg
[INFO] loading super resolution model: models\ESPCN_x4.pb
[INFO] model name: espcn
[INFO] model scale: 4
[INFO] w: 94, h: 73
[INFO] super resolution took 0.004987 seconds
[INFO] w: 376, h: 292
[INFO] loading super resolution model: models\FSRCNN_x4.pb
[INFO] model name: fsrcnn
[INFO] model scale: 4
[INFO] w: 94, h: 73
[INFO] super resolution took 0.006982 seconds
[INFO] w: 376, h: 292
[INFO] loading super resolution model: models\LapSRN_x4.pb
[INFO] model name: lapsrn
[INFO] model scale: 4
[INFO] w: 94, h: 73
[INFO] super resolution took 0.077327 seconds
[INFO] w: 376, h: 292

reference:

mouse drag event

super resolution model download

No comments:

Post a Comment