- 图像增强——去除遮挡物功能
- 图像增强——图片无损放大功能
- 完成上述功能的UI界面
一、图片无损放大功能
imgfile = instance.root_path+'/part4/images_mag/test.jpg'
imgfile = instance.magnify_original[0][0]
print(instance.magnify_original[0][0])
f = open(imgfile, 'rb') # 二进制方式打开图片文件
img = base64.b64encode(f.read())
params = {"image": img}
access_token = token
request_url = request_url + "?access_token=" + access_token
headers = {'content-type': 'application/x-www-form-urlencoded'}
response = requests.post(request_url, data=params, headers=headers)
if response:
print(response.json())
img = base64.b64decode(response.json()['image'])
img_out= instance.root_path + '/part4/images_mag/result.jpg'
file = open(img_out, 'wb')
file.write(img)
res=img
# res=np.hstack([img_ori,img])
# img_array = np.frombuffer(img, np.uint8)
# 结果图转换成opencv可用格式
res_array=np.frombuffer(res, np.uint8)
# 原图转换
res=cv2.imdecode(res_array, cv2.COLOR_RGB2BGR)
cv2.imshow("Magnify_Result", res)
cv2.waitKey(0)
file.close
结果展示:
原图:
放大后:
二、去除遮挡物功能
(一)通过两种算法设计了两种去除画笔、水印等遮挡物的功能,用户可以使用画笔在原图上填涂需要去除的地方。
cv2.INPAINT_TELEA 简称Telea,基于快速行进方法(Fast Marching
Method,简称FMM)考虑图像中要修复的区域。算法从该区域的边界开始,并进入该区域内部,然后逐渐填充边界中的所有内容。在要修复的邻域上的像素周围需要一个小的邻域。用附近所有已知像素的归一化加权总和替换该像素。权重的选择很重要。那些位于该点附近,边界法线附近的像素和那些位于边界轮廓线上的像素将获得更大的权重。修复像素后,将使用快速行进方法将其移动到下一个最近的像素。
FMM确保首先修复已知像素附近的那些像素,以便像手动启发式操作一样工作。
cv2.INPAINT_NS: 基于流体动力学(fluid dynamics)并利用偏微分方程(partial differential
equations)。基本原理是启发式的。它首先沿着边缘从已知区域移动到未知区域(因为边缘是连续的)。它延续了等距线(isophotes)(线条连接具有相同强度的点,就像轮廓线连接具有相同高程的点一样),同时在修复区域的边界匹配梯度矢量(gradient
vectors)。为此使用了一些流体动力学方法。获得它们后,将填充颜色以减少该区域的最小差异。
在使用OpenCV应用修复时,我们需要提供两个图像, output = cv2.inpaint(image, mask,
radius,flags) 返回是修复后的图像
image:我们希望修复和恢复的输入图像。该图像以某种方式被“损坏”,我们需要应用修复算法对其进行修复。
maks:遮罩图像,高亮出了图像中被损坏的区域。该图像应具有与输入图像相同的空间尺寸(宽度和高度)。非零像素对应于应该修复(即固定)的区域,而零像素被认为是“正常”并且不需要修复;
radius:修复半径以像素为单位(算法考虑的每个修补点的圆形邻域) flags:修复的算法(cv2.INPAINT_TELEA or
cv2.INPAINT_NS)
参考链接:https://blog.csdn.net/qq_40985985/article/details/106210010
import numpy as np
import cv2 as cv
import sys
#Use time to compare NS and TELEA
import time
def removal_fmm(instance):
instance.fmmvalue = 1
print('removal_fmm')
#opencv Class for Mouse (Version for python) setmouse...
class Sketcher:
def __init__(self, windowname, dests, colors_func):
self.prev_point = None
self.windowname = windowname
# dests is a set of images: copy & mask
self.dests = dests
self.colors_func = colors_func
self.dirty = False
self.show()
cv.setMouseCallback(self.windowname, self.on_mouse)
def show(self):
cv.imshow(self.windowname, self.dests[0])
cv.imshow(self.windowname + ":mask", self.dests[1])
# on mouse function
def on_mouse(self, event, x, y, flags, param):
# point store the current position of the mouse
point = (x, y)
if event == cv.EVENT_LBUTTONDOWN:
# assignment of previous point
self.prev_point = point
elif event == cv.EVENT_LBUTTONUP:
self.prev_point = None
# cv.EVENT_FLAG_LBUTTON & flags 代表按住左键拖拽
if self.prev_point and flags & cv.EVENT_FLAG_LBUTTON:
# zip 把前后参数打包为元组
for dst, color in zip(self.dests, self.colors_func()):
cv.line(dst, self.prev_point, point, color, 5)
# Record this dirt
self.dirty = True
self.prev_point = point
self.show()
def removal_function(instance):
# print("Keys: ")
# print("t - inpaint using FMM") # Fast Marching method
# print("n - inpaint using NS technique")
# print("r - reset the inpainting mask")
# print("ESC - exit")
# Read image in color mode
imgfile = instance.root_path+'/part4/images_rem/test1.png'
imgfile = instance.removal_cv_original[0][0]
print(imgfile)
print(instance.removal_cv_original[0][0])
img = cv.imread(imgfile, cv.IMREAD_COLOR)
print(type(img))
# Return error if failed to read the image
if img is None:
print("Failed to read the image")
return
# Create the copy of the original image
img_mask = img.copy()
# Create a black mask of the image
inpaintMask = np.zeros(img.shape[:2], np.uint8)
# Create a Sketch
# dests= img_mask, inpaintMask
# color_func is a tuple : white with BGR and white on gray
sketch = Sketcher('image', [img_mask, inpaintMask], lambda: ((255, 255, 255), 255))
instance.fmmvalue=0
while True: # 后期可以添加快捷键?
ch = cv.waitKey()
fmm = instance.fmmvalue
# Esc
if ch == 27 or fmm == 1: # 设计按键和按钮同时使用的方法
break
if ch == ord('t') or fmm==1:
print('fmm',fmm)
t1 = time.time()
res = cv.inpaint(src=img_mask, inpaintMask=inpaintMask, inpaintRadius=3, flags=cv.INPAINT_TELEA)
res = np.hstack((img, res))
t2 = time.time()
print("Time: FMM = {} ms".format((t2 - t1) * 1000))
cv.imshow('Inpaint with FMM', res)
cv.imwrite("FMM-eye.png", res)
if ch == ord('n'):
t1 = time.time()
res = cv.inpaint(src=img_mask, inpaintMask=inpaintMask, inpaintRadius=3, flags=cv.INPAINT_NS)
res = np.hstack((img, res))
t2 = time.time()
cv.imshow('Inpaint Output using NS Technique', res)
cv.imwrite("NS-eye.png", res)
# type r to reset the image
if ch == ord('r'):
# The reason for which we copied image
img_mask[:] = img
inpaintMask[:] = 0
sketch.show()
print('Completed')
(二)结果展示
(三)待改进:需要通过按键进行算法选择,还没有完成按键和按钮统一的功能。