vieyahn2017 / pypy

python trial collections
1 stars 1 forks source link

图片修复之FMM #11

Open vieyahn2017 opened 1 year ago

vieyahn2017 commented 1 year ago

cv2.inpaint(src, mask, 3, cv2.INPAINT_TELEA) # Telea的基于FMM的图像修复算法

vieyahn2017 commented 1 year ago

import cv2
import numpy as np

def show_image(image, title="img"):
    # cv2.namedWindow(title, 0)
    cv2.imshow(title, image)
    # cv2.waitKey(0)

def cal_line_and_to_p2(x1, y1, x2, y2):
    p1 = [x1, y1]
    p2 = [x2, y2]
    if x2 == x1:
        slope = 0
    else:
        slope = (y2-y1)/(x2-x1)
    # slope, _ = np.polyfit(p1, p2, 1)
    # print(("===", p1, p2, slope))
    return p1, p2, slope

def line_detect_possible_demo(image):
    mask_rgb = (0, 255, 0)
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    edges = cv2.Canny(gray, 50, 150, apertureSize=3)  # apertureSize,Canny边缘检测梯度那一步,窗口大小是3
    lines = cv2.HoughLinesP(edges, 1, np.pi / 180, 100, minLineLength=50, maxLineGap=10)  #函数将通过步长为1的半径和步长为π/180的角来搜索所有可能的直线
    #minLineLength-线的最短长度,比这个线短的都会被忽略
    #maxLineGap-两条线之间的最大间隔,如果小于此值,这两条线就会被看成一条线。
    for line in lines:
        # print(type(lines)) # <class 'numpy.ndarray'>
        # print(line)
        x1, y1, x2, y2 = line[0]
        p1, p2, slope = cal_line_and_to_p2(x1, y1, x2, y2)
        # if slope > 5 and slope < 7: # 斜率控制删除点
        cv2.line(image, p1, p2, mask_rgb, 2)
        print(p1, p2, slope)

    # 手动加入点
    # cv2.imshow("line_detect_possible_demo", image)
    show_image(image, "line_detect_possible_demo")
    lower = mask_rgb  # lower bound for each channel
    upper = mask_rgb  # upper bound for each channel
    mask = cv2.inRange(image, lower, upper)
    show_image(mask, "mask")
    return mask

BASEDIR = 'D:\\javaway\\viey2017\\Image-inpainting-by-FMM-and-criminisi-master\\py\\'
src = cv2.imread(BASEDIR + '2.jpg')

def test():
    mask = line_detect_possible_demo(src)
    #cv2.waitKey(0)
    #cv2.destroyAllWindows()
    cv2.imwrite(BASEDIR + '2-mask.jpg', mask)

    dst = cv2.inpaint(src, mask, 3, cv2.INPAINT_TELEA)  # Telea的基于FMM的图像修复算法
    cv2.imshow('dst',dst)
    cv2.imwrite(BASEDIR + '2-repair-1.jpg', dst)
    cv2.waitKey(0)

# test()

def repair():
    mask = cv2.imread(BASEDIR + '2-mask.jpg', cv2.IMREAD_GRAYSCALE)
    dst = cv2.inpaint(src, mask, 3, cv2.INPAINT_TELEA)  # Telea的基于FMM的图像修复算法
    cv2.imshow('src', src)
    cv2.imshow('mask', mask)
    cv2.imshow('dst',dst)
    cv2.imwrite(BASEDIR + '2-repair.jpg', dst)
    cv2.waitKey(0)

repair()
vieyahn2017 commented 1 year ago

dst = cv2.inpaint(src, mask, 3, cv2.INPAINT_NS) # Navier-Stokes 图像修复算法 dst = cv2.inpaint(src, mask, 3, cv2.INPAINT_TELEA) # Telea的基于FMM的图像修复算法

vieyahn2017 commented 1 year ago

dst = cv2.inpaint(src, mask, 3, cv2.INPAINT_NS) # Navier-Stokes 图像修复算法 Fluid Dynamics Method 流体力学算法 dst = cv2.inpaint(src, mask, 3, cv2.INPAINT_TELEA) # Telea的基于FMM的图像修复算法 Fast Marching Method 快速行进算法

vieyahn2017 commented 1 year ago

去除水印的案例 https://gitee.com/lvyingde51/OpenCV_PY/blob/master/lab/lab4.md