批量把图片进行重采样
import matplotlib
import numpy
import cv2 as cv
import os
i = 1
dir = r"E:/Multimedia Security/num1" #原图路径
outdir = r"E:/Multimedia Security/chongcaiyang2"#保存重采样图路径
imlist = os.listdir(dir)
for img in imlist:
img1 = cv.imread(dir + '/' + img)#读取图片
img2=cv.resize(img1,(0,0),fx=0.5,fy=0.5,interpolation=cv.INTER_LINEAR)#进行重采样 模式为双线性插值
outdir1=outdir+'/'+str(i)+'.tif'#输出路径
cv.imwrite(outdir1, img2)#保存图片
i = i + 1
进行图像残差计算得到p-map图
import matplotlib.pyplot as plt
import matplotlib
import numpy as np
import cv2
import os
import matplotlib.image as
mping
import shutil
from PIL import Image
from scipy import ndimage
a = np.array([[-0.25, 0.5, -0.25], [0.5, 0, 0.5]
, [-0.25, 0.5, -0.25]])
r = 1
t = 2
K = 1
o = 1
dst = mping.imread(r"C:\Users\chengyuxiao\Desktop\1111.tif") # 读取重采样图片
dst = dst[:, :,0]
h, w = np.shape(dst)
h2 = int(np.ceil(h / 2))
padimg = np.pad(dst, ((1, 1), (1, 1)), 'constant', constant_values=(0, 0)).astype(np.float32)
e = padimg - cv2.filter2D(padimg, -1, a)
e1 = padimg - cv2.filter2D(padimg, -1, a, borderType=cv2.BORDER_CONSTANT) # 对残差图像做对比度增强
e = e[1:-1, 1:-1]
e = np.exp(-e ** 2)
p = r * np.exp(-(abs(e) ** 2) / o)
cv2.imwrite("4444.tif", p)#保存P-map图
下一步对p-map图做傅里叶变换,进行滤波和对比度增强。
V = np.fft.fft2(p)
V = np.fft.fftshift(V)
mask = np.ones(dst.shape, np.uint8)
win2 = 40 / 256 * h2
mask[int(h / 2 - win2):int(h / 2 + win2), int(w / 2 - win2):int(w / 2 + win2)] = 0
P0 = abs(V) * mask
P2 = ((P0 / np.max(np.abs(P0))) ** 4) * (np.max(np.abs(P0)))
print(np.where(P2 == P2.max()))
plt.imshow(P2)
plt.show()
得到重采样图的FTT图
计算重采样的特征值
计算特征值的函数:
def getcha(P, h):
P0 = P[:int(np.ceil(h / 2)), int(np.ceil(h / 2)):]
l, m = np.where(P0 == np.max(P0))
m = m[0] + 1
if m < h / 2:
P1 = P0[:int(np.ceil(h / 2)), :]
else:
P1 = P0[:, int(np.ceil(h / 2)):]
C = np.array([0] * int(np.ceil(h / 2))).astype(np.float32)
x = (np.sum(abs(P1) ** 2)) ** (-1)
for j in range(0, int(np.ceil(h / 2))):
C[j] = np.sum(abs(P1[int(np.ceil(h / 2)) - 1 - j:-1, 0:1 + j]) ** 2) * x
# 分类
C = abs(C)
C = C / np.max(C)
cha = C[:-1] - C[1:]
return cha
主函数
a = np.array([[-0.25, 0.5, -0.25], [0.5, 0, 0.5]
, [-0.25, 0.5, -0.25]])
r = 1
t = 2
K = 1
o = 1
dst = mping.imread(r"C:\Users\chengyuxiao\Desktop\1111.tif") # 图片读取函数
dst = dst[:, :,0]
h, w = np.shape(dst)
h2 = int(np.ceil(h / 2))
padimg = np.pad(dst, ((1, 1), (1, 1)), 'constant', constant_values=(0, 0)).astype(np.float32)
e = padimg - cv2.filter2D(padimg, -1, a)
e1 = padimg - cv2.filter2D(padimg, -1, a, borderType=cv2.BORDER_CONSTANT) # 对残差图像做对比度增强
e = e[1:-1, 1:-1]
e = np.exp(-e ** 2)
p = r * np.exp(-(abs(e) ** 2) / o)
V = np.fft.fft2(p)
V = np.fft.fftshift(V)
mask = np.ones(dst.shape, np.uint8)
win2 = 40 / 256 * h2
mask[int(h / 2 - win2):int(h / 2 + win2), int(w / 2 - win2):int(w / 2 + win2)] = 0
P0 = abs(V) * mask
P2 = ((P0 / np.max(np.abs(P0))) ** 4) * (np.max(np.abs(P0)))
cha1=getcha(P2,h)
cha1=abs(cha1)
dele=max(cha1)
print(dele)
mask
P2 = ((P0 / np.max(np.abs(P0))) ** 4) * (np.max(np.abs(P0)))
cha1=getcha(P2,h)
cha1=abs(cha1)
dele=max(cha1)
print(dele)