1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
| """ @author: Dragon Liu Operating environment: Python 3.7.1 lib: opencv-python Date: 2020/3/22 BUG: 时间复杂度O(m*n*div*r*r),不可行,另外存在img[]访问问题 """
import math import cv2 import numpy as np import matplotlib.pyplot as plt
def gr(px, py, qx, qy, dr):
nut = math.exp( - ( pow((qx - px), 2) + pow((qy - py), 2) ) ) det = 2 * pow(dr, 2) result = nut / det return result
def gzeta(guide_img, div, px, py, qx, qy, dzeta): guide_img = guide_img nut = math.exp( - ( pow( ( guide_img[px, py, div] - guide_img[qx, qy, div] ), 2 ) ) ) det = 2 * pow(dzeta, 2) result = nut / det return result
def GS(image, r, dr, px, py, div):
Upsilon = 0 output = 0 for i in range(r, -(r+1), -1): for j in range(r, -(r+1), -1): Upsilon = Upsilon + gr(px, py, px + i, py + j, dr) output = output + gr(px, py, px + i, py + j, dr) * image[px + i,py + j,div] output = output / Upsilon return output
def medbox(img, x, y, div, length, width): nums = [] length = width = 3 for i in range(math.floor(length/2), -math.floor(length/2)-1, -1): for j in range(math.floor(width/2), -math.floor(width/2)-1, -1): nums.append( img[x+i, y+j, div])
return np.median(nums)
def WGGF(guide_img,source,r,dzeta,px,py,div, lam): guide_img = guide_img source = source Upsilon = 0 output = 0 flag = 0 for i in range(r, -(r+1), -1): for j in range(r, -(r+1), -1): temp = abs( guide_img[px + i, py + 1, div] - guide_img[px, py, div] ) if temp <= lam: flag = flag + 1 Upsilon = Upsilon + gzeta(guide_img, div, px, py, px+i, py+j, dzeta) output = output + guide_img[px + i,py + j,div] * gzeta(guide_img, div, px, py, px+i, py+j, dzeta)
if flag == 1 or Upsilon == 0: output = medbox( source, px, py, j, 3, 3 ) else: output = output / Upsilon return output
def main(): img = cv2.imread('02.png', 1) source = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) source = source / 255.0 guide_img = source print(1.666) [m ,n, div] = np.shape(source) r = 5 dr = 0.5 guide_img = cv2.GaussianBlur(source, (r,r), dr) print(2.666) target = guide_img r = 5 dzeta = 0.1 lam = 0.12 num = 0 for k in range(div): for i in range(m): for j in range(n): if i <= r or i >= m - r or j <= r or j >= n - r: continue else: target[i, j, k] = WGGF(guide_img, source, r, dzeta, i, j, k, lam) num = num + 1 print(num) print(3.666) titles = ['Source Image', 'WGGF Image'] images = [source*255.0, target*255.0] for i in range(2): plt.subplot(1, 2, i+1), plt.imshow(images[i], 'gray') plt.title(titles[i]) plt.xticks([]),plt.yticks([]) plt.show()
if __name__ == '__main__': main()
|