Skip to content

Commit

Permalink
解决了中文路径崩溃的问题,在GUI中添加了批处理的功能
Browse files Browse the repository at this point in the history
感谢各位网友对本项目的支持,让我有动力继续完善这个项目
  • Loading branch information
fire-keeper committed Dec 7, 2020
1 parent bc394d4 commit d519f11
Show file tree
Hide file tree
Showing 21 changed files with 297 additions and 115 deletions.
18 changes: 10 additions & 8 deletions BlindWatermark/BlindWatermark.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import cv2
from pywt import dwt2,idwt2
import os
from .tools import cv_imread,cv_imwrite



class watermark():
Expand Down Expand Up @@ -34,7 +36,7 @@ def init_block_add_index(self,img_shape):

def read_ori_img(self,filename):
#傻逼opencv因为数组类型不会变,输入是uint8输出也是uint8,而UV可以是负数且uint8会去掉小数部分
ori_img = cv2.imread(filename).astype(np.float32)
ori_img = cv_imread(filename).astype(np.float32)
self.ori_img_shape = ori_img.shape[:2]
if self.color_mod == 'RGB':
self.ori_img_YUV = ori_img
Expand Down Expand Up @@ -96,7 +98,7 @@ def read_ori_img(self,filename):


def read_wm(self,filename):
self.wm = cv2.imread(filename)[:,:,0]
self.wm = cv_imread(filename)[:,:,0]
self.wm_shape = self.wm.shape[:2]

#初始化块索引数组,因为需要验证块是否足够存储水印信息,所以才放在这儿
Expand Down Expand Up @@ -195,7 +197,7 @@ def embed(self,filename):
embed_img[embed_img>255]=255
embed_img[embed_img<0]=0

cv2.imwrite(filename,embed_img)
cv_imwrite(filename,embed_img)

def block_get_wm(self,block,index):
block_dct = cv2.dct(block)
Expand All @@ -220,7 +222,7 @@ def extract(self,filename,out_wm_name):
return 0

#读取图片
embed_img = cv2.imread(filename).astype(np.float32)
embed_img = cv_imread(filename).astype(np.float32)
if self.color_mod == 'RGB':
embed_img_YUV = embed_img
elif self.color_mod == 'YUV':
Expand Down Expand Up @@ -310,14 +312,14 @@ def extract(self,filename,out_wm_name):
extract_wm_Y[wm_index] = extract_wm_Y.copy()
extract_wm_U[wm_index] = extract_wm_U.copy()
extract_wm_V[wm_index] = extract_wm_V.copy()
cv2.imwrite(out_wm_name,extract_wm.reshape(self.wm_shape[0],self.wm_shape[1]))
cv_imwrite(out_wm_name,extract_wm.reshape(self.wm_shape[0],self.wm_shape[1]))

path,file_name = os.path.split(out_wm_name)
if not os.path.isdir(os.path.join(path,'Y_U_V')):
os.mkdir(os.path.join(path,'Y_U_V'))
cv2.imwrite(os.path.join(path,'Y_U_V','Y'+file_name),extract_wm_Y.reshape(self.wm_shape[0],self.wm_shape[1]))
cv2.imwrite(os.path.join(path,'Y_U_V','U'+file_name),extract_wm_U.reshape(self.wm_shape[0],self.wm_shape[1]))
cv2.imwrite(os.path.join(path,'Y_U_V','V'+file_name),extract_wm_V.reshape(self.wm_shape[0],self.wm_shape[1]))
cv_imwrite(os.path.join(path,'Y_U_V','Y'+file_name),extract_wm_Y.reshape(self.wm_shape[0],self.wm_shape[1]))
cv_imwrite(os.path.join(path,'Y_U_V','U'+file_name),extract_wm_U.reshape(self.wm_shape[0],self.wm_shape[1]))
cv_imwrite(os.path.join(path,'Y_U_V','V'+file_name),extract_wm_V.reshape(self.wm_shape[0],self.wm_shape[1]))


if __name__=="__main__":
Expand Down
2 changes: 1 addition & 1 deletion BlindWatermark/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .BlindWatermark import watermark
from .ncc import NCC
from .ncc import test_ncc
from .psnr import test_psnr
from .psnr import test_psnr
5 changes: 3 additions & 2 deletions BlindWatermark/ncc.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import numpy as np
import cv2
from .tools import cv_imread

def NCC(A,B):
cross_mul_sum=((A-A.mean())*(B-B.mean())).sum()
cross_square_sum = np.sqrt((np.square(A-(A.mean())).sum())*(np.square(B-(B.mean())).sum()))
return cross_mul_sum/cross_square_sum

def test_ncc(filename1,filename2):
a = cv2.imread(filename1)
b = cv2.imread(filename2)
a = cv_imread(filename1)
b = cv_imread(filename2)
for i in range(3):
print(NCC(a[:,:,i],b[:,:,i]))

Expand Down
6 changes: 3 additions & 3 deletions BlindWatermark/psnr.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import cv2
import numpy as np

from .tools import cv_imread

def PSNR(img1, img2):
mse = np.mean( (img1/255. - img2/255.) ** 2 )
Expand All @@ -10,7 +10,7 @@ def PSNR(img1, img2):
return 20 * np.log10(PIXEL_MAX / np.sqrt(mse))

def test_psnr(filename1,filename2):
a = cv2.imread(filename1)
b = cv2.imread(filename2)
a = cv_imread(filename1)
b = cv_imread(filename2)
for i in range(3):
print(PSNR(a[:,:,i],b[:,:,i]))
7 changes: 7 additions & 0 deletions BlindWatermark/tools.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import cv2
import numpy as np
import os

def cv_imread(file_path):
cv_img = cv2.imdecode(np.fromfile(file_path, dtype=np.uint8),cv2.IMREAD_COLOR)
return cv_img

def cv_imwrite(path,img):
suffix = os.path.splitext(path)[-1]
cv2.imencode(suffix, img)[1].tofile(path)

def recovery(ori_img,attacked_img,outfile_name = './recoveried.png',rate=0.7):
img = cv2.imread(ori_img)
Expand Down
6 changes: 6 additions & 0 deletions GUI/.eric6project/BlindWatermark.e4q
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE UserProject SYSTEM "UserProject-4.0.dtd">
<!-- eric6 user project file for project BlindWatermark -->
<!-- Saved: 2020-12-03, 20:13:02 -->
<!-- Copyright (C) 2020 , -->
<UserProject version="4.0"/>
16 changes: 16 additions & 0 deletions GUI/.eric6project/BlindWatermark.e6t
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Tasks SYSTEM "Tasks-6.0.dtd">
<!-- eric6 tasks file for project BlindWatermark -->
<!-- Saved: 2020-12-03, 20:13:02 -->
<Tasks version="6.0">
<ProjectScanFilter></ProjectScanFilter>
<Task priority="1" completed="False" type="1" uid="{ac0265be-54c7-4647-8602-4d7ea9dcdeec}">
<Summary>TODO: not implemented yet</Summary>
<Description></Description>
<Created>2020-12-03, 11:55:39</Created>
<Resource>
<Filename>main_win.py</Filename>
<Linenumber>422</Linenumber>
</Resource>
</Task>
</Tasks>
17 changes: 9 additions & 8 deletions GUI/BlindWatermark/BlindWatermark.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from pywt import dwt2,idwt2
import os
from PyQt5.QtCore import pyqtSignal, QThread
from .tools import cv_imread,cv_imwrite

class watermark(QThread):
#just for pyqt
Expand Down Expand Up @@ -38,7 +39,7 @@ def init_block_add_index(self,img_shape):

def read_ori_img(self,filename):
#傻逼opencv因为数组类型不会变,输入是uint8输出也是uint8,而UV可以是负数且uint8会去掉小数部分
ori_img = cv2.imread(filename).astype(np.float32)
ori_img = cv_imread(filename).astype(np.float32)
self.ori_img_shape = ori_img.shape[:2]
if self.color_mod == 'RGB':
self.ori_img_YUV = ori_img
Expand Down Expand Up @@ -100,7 +101,7 @@ def read_ori_img(self,filename):


def read_wm(self,filename):
self.wm = cv2.imread(filename)[:,:,0]
self.wm = cv_imread(filename)[:,:,0]
self.wm_shape = self.wm.shape[:2]

#初始化块索引数组,因为需要验证块是否足够存储水印信息,所以才放在这儿
Expand Down Expand Up @@ -210,7 +211,7 @@ def embed2array(self):
def embed(self,filename,write=True):
self.embed_img = self.embed2array()
if write:
cv2.imwrite(filename,self.embed_img)
cv_imwrite(filename,self.embed_img)


def block_get_wm(self,block,index):
Expand All @@ -236,7 +237,7 @@ def extract(self,filename,out_wm_name):
return 0

#读取图片
embed_img = cv2.imread(filename).astype(np.float32)
embed_img = cv_imread(filename).astype(np.float32)
if self.color_mod == 'RGB':
embed_img_YUV = embed_img
elif self.color_mod == 'YUV':
Expand Down Expand Up @@ -327,14 +328,14 @@ def extract(self,filename,out_wm_name):
extract_wm_Y[wm_index] = extract_wm_Y.copy()
extract_wm_U[wm_index] = extract_wm_U.copy()
extract_wm_V[wm_index] = extract_wm_V.copy()
cv2.imwrite(out_wm_name,extract_wm.reshape(self.wm_shape[0],self.wm_shape[1]))
cv_imwrite(out_wm_name,extract_wm.reshape(self.wm_shape[0],self.wm_shape[1]))

path,file_name = os.path.split(out_wm_name)
if not os.path.isdir(os.path.join(path,'Y_U_V')):
os.mkdir(os.path.join(path,'Y_U_V'))
cv2.imwrite(os.path.join(path,'Y_U_V','Y'+file_name),extract_wm_Y.reshape(self.wm_shape[0],self.wm_shape[1]))
cv2.imwrite(os.path.join(path,'Y_U_V','U'+file_name),extract_wm_U.reshape(self.wm_shape[0],self.wm_shape[1]))
cv2.imwrite(os.path.join(path,'Y_U_V','V'+file_name),extract_wm_V.reshape(self.wm_shape[0],self.wm_shape[1]))
cv_imwrite(os.path.join(path,'Y_U_V','Y'+file_name),extract_wm_Y.reshape(self.wm_shape[0],self.wm_shape[1]))
cv_imwrite(os.path.join(path,'Y_U_V','U'+file_name),extract_wm_U.reshape(self.wm_shape[0],self.wm_shape[1]))
cv_imwrite(os.path.join(path,'Y_U_V','V'+file_name),extract_wm_V.reshape(self.wm_shape[0],self.wm_shape[1]))
self.valueChanged.emit(100)

if __name__=="__main__":
Expand Down
Binary file modified GUI/BlindWatermark/__pycache__/BlindWatermark.cpython-36.pyc
Binary file not shown.
Binary file modified GUI/BlindWatermark/__pycache__/__init__.cpython-36.pyc
Binary file not shown.
Binary file modified GUI/BlindWatermark/__pycache__/ncc.cpython-36.pyc
Binary file not shown.
Binary file modified GUI/BlindWatermark/__pycache__/psnr.cpython-36.pyc
Binary file not shown.
Binary file modified GUI/BlindWatermark/__pycache__/tools.cpython-36.pyc
Binary file not shown.
19 changes: 14 additions & 5 deletions GUI/BlindWatermark/ncc.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
import numpy as np
import cv2
from .tools import cv_imread

def NCC(A,B):
cross_mul_sum=((A-A.mean())*(B-B.mean())).sum()
cross_square_sum = np.sqrt((np.square(A-(A.mean())).sum())*(np.square(B-(B.mean())).sum()))
return cross_mul_sum/cross_square_sum

def average_ncc(filename1,filename2):
a = cv2.imread(filename1)
b = cv2.imread(filename2)
if isinstance(filename1,list):
filename1 = filename1[0]
if isinstance(filename2,list):
filename2 = filename2[0]
a = cv_imread(filename1)
b = cv_imread(filename2)
out = 0
for i in range(3):
out+=NCC(a[:,:,i],b[:,:,i])
return out/3
def test_ncc(filename1,filename2):
a = cv2.imread(filename1)
b = cv2.imread(filename2)
if isinstance(filename1,list):
filename1 = filename1[0]
if isinstance(filename2,list):
filename2 = filename2[0]
a = cv_imread(filename1)
b = cv_imread(filename2)
for i in range(3):
print(NCC(a[:,:,i],b[:,:,i]))

Expand All @@ -24,6 +33,6 @@ def test_ncc(filename1,filename2):
P=[1,1,4,2,1,1,4,2,4,4,4,4,1,1,4,2]
Q=[2,2,6,2,2,2,6,2,6,6,6,6,2,2,6,2]

NCC2()

print(NCC(np.array(A),np.array(P)))
print(NCC(np.array(A),np.array(Q)))
18 changes: 13 additions & 5 deletions GUI/BlindWatermark/psnr.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import cv2
import numpy as np

from .tools import cv_imread

def PSNR(img1, img2):
mse = np.mean( (img1/255. - img2/255.) ** 2 )
Expand All @@ -10,15 +10,23 @@ def PSNR(img1, img2):
return 20 * np.log10(PIXEL_MAX / np.sqrt(mse))

def average_psnr(filename1,filename2):
a = cv2.imread(filename1)
b = cv2.imread(filename2)
if isinstance(filename1,list):
filename1 = filename1[0]
if isinstance(filename2,list):
filename2 = filename2[0]
a = cv_imread(filename1)
b = cv_imread(filename2)
out=0
for i in range(3):
out+=PSNR(a[:,:,i],b[:,:,i])
return out/3

def test_psnr(filename1,filename2):
a = cv2.imread(filename1)
b = cv2.imread(filename2)
if isinstance(filename1,list):
filename1 = filename1[0]
if isinstance(filename2,list):
filename2 = filename2[0]
a = cv_imread(filename1)
b = cv_imread(filename2)
for i in range(3):
print(PSNR(a[:,:,i],b[:,:,i]))
7 changes: 7 additions & 0 deletions GUI/BlindWatermark/tools.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import cv2
import numpy as np
from PyQt5.QtCore import pyqtSignal, QThread
import os

def cv_imread(file_path):
cv_img = cv2.imdecode(np.fromfile(file_path, dtype=np.uint8),cv2.IMREAD_COLOR)
return cv_img

def cv_imwrite(path,img):
suffix = os.path.splitext(path)[-1]
cv2.imencode(suffix, img)[1].tofile(path)

class recovery(QThread):
num_of_good=pyqtSignal(int,str)
Expand Down
26 changes: 16 additions & 10 deletions GUI/Ui_main_win.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'E:\python\PYQT5\main_win.ui'
# Form implementation generated from reading ui file 'E:\github\BlindWatermark\GUI\main_win.ui'
#
# Created by: PyQt5 UI code generator 5.12.1
#
Expand All @@ -12,7 +12,7 @@
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(925, 785)
MainWindow.resize(1044, 845)
self.centralWidget = QtWidgets.QWidget(MainWindow)
self.centralWidget.setObjectName("centralWidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.centralWidget)
Expand All @@ -39,6 +39,7 @@ def setupUi(self, MainWindow):
self.gridLayout.addWidget(self.label_2, 2, 0, 1, 1)
self.doubleSpinBox = QtWidgets.QDoubleSpinBox(self.frame_3)
self.doubleSpinBox.setMaximum(399.99)
self.doubleSpinBox.setProperty("value", 1.0)
self.doubleSpinBox.setObjectName("doubleSpinBox")
self.gridLayout.addWidget(self.doubleSpinBox, 2, 1, 1, 1)
self.doubleSpinBox_2 = QtWidgets.QDoubleSpinBox(self.frame_3)
Expand All @@ -59,23 +60,26 @@ def setupUi(self, MainWindow):
self.frame_2.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame_2.setObjectName("frame_2")
self.gridLayout_2 = QtWidgets.QGridLayout(self.frame_2)
self.gridLayout_2.setSizeConstraint(QtWidgets.QLayout.SetDefaultConstraint)
self.gridLayout_2.setObjectName("gridLayout_2")
self.pushButton = QtWidgets.QPushButton(self.frame_2)
self.pushButton.setObjectName("pushButton")
self.gridLayout_2.addWidget(self.pushButton, 0, 0, 1, 1)
self.pushButton_2 = QtWidgets.QPushButton(self.frame_2)
self.pushButton_2.setObjectName("pushButton_2")
self.gridLayout_2.addWidget(self.pushButton_2, 1, 0, 1, 1)
self.gridLayout_2.addWidget(self.pushButton_2, 2, 0, 1, 1)
self.label_5 = QtWidgets.QLabel(self.frame_2)
self.label_5.setText("")
self.label_5.setScaledContents(True)
self.label_5.setObjectName("label_5")
self.gridLayout_2.addWidget(self.label_5, 1, 1, 1, 1)
self.gridLayout_2.addWidget(self.label_5, 2, 1, 1, 1)
self.label_4 = QtWidgets.QLabel(self.frame_2)
self.label_4.setText("")
self.label_4.setWordWrap(True)
self.label_4.setObjectName("label_4")
self.gridLayout_2.addWidget(self.label_4, 0, 1, 1, 1)
self.gridLayout_2.addWidget(self.label_4, 1, 1, 1, 1)
self.pushButton = QtWidgets.QPushButton(self.frame_2)
self.pushButton.setObjectName("pushButton")
self.gridLayout_2.addWidget(self.pushButton, 1, 0, 1, 1)
self.gridLayout_2.setColumnStretch(0, 1)
self.gridLayout_2.setColumnStretch(1, 1)
self.gridLayout_4.addWidget(self.frame_2, 5, 1, 1, 1)
self.frame = QtWidgets.QFrame(self.tab)
self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
Expand Down Expand Up @@ -220,6 +224,8 @@ def setupUi(self, MainWindow):
self.line_5.setFrameShadow(QtWidgets.QFrame.Sunken)
self.line_5.setObjectName("line_5")
self.gridLayout_4.addWidget(self.line_5, 8, 1, 1, 1)
self.gridLayout_4.setColumnStretch(0, 2)
self.gridLayout_4.setColumnStretch(1, 1)
self.tabWidget.addTab(self.tab, "")
self.tab_4 = QtWidgets.QWidget()
self.tab_4.setObjectName("tab_4")
Expand Down Expand Up @@ -303,7 +309,7 @@ def setupUi(self, MainWindow):
self.verticalLayout.addWidget(self.tabWidget)
MainWindow.setCentralWidget(self.centralWidget)
self.menuBar = QtWidgets.QMenuBar(MainWindow)
self.menuBar.setGeometry(QtCore.QRect(0, 0, 925, 26))
self.menuBar.setGeometry(QtCore.QRect(0, 0, 1044, 26))
self.menuBar.setObjectName("menuBar")
self.menu = QtWidgets.QMenu(self.menuBar)
self.menu.setObjectName("menu")
Expand Down Expand Up @@ -341,8 +347,8 @@ def retranslateUi(self, MainWindow):
self.label.setToolTip(_translate("MainWindow", "两个随机的整数即可"))
self.label.setText(_translate("MainWindow", "随机种子"))
self.frame_2.setToolTip(_translate("MainWindow", "从剪切板导入密钥, 同时修改下面的参数. 注, 点击左侧栏中的密钥可以将密钥放到剪切板中"))
self.pushButton.setText(_translate("MainWindow", "导入图片"))
self.pushButton_2.setText(_translate("MainWindow", "导入水印"))
self.pushButton.setText(_translate("MainWindow", "导入图片"))
self.radioButton.setToolTip(_translate("MainWindow", "提取模式 , 只需在 导入图片 导入嵌入水印后的图"))
self.radioButton.setText(_translate("MainWindow", "提取"))
self.radioButton_2.setToolTip(_translate("MainWindow", "嵌入模式"))
Expand Down
Binary file modified GUI/img/__pycache__/about_sources_rc.cpython-36.pyc
Binary file not shown.
Binary file modified GUI/img/__pycache__/sources_rc.cpython-36.pyc
Binary file not shown.
Loading

0 comments on commit d519f11

Please sign in to comment.