Strawberry Bubblegum
2020-06-23 14:32
采纳率: 60%
浏览 247

基于Python Opencv 更改指定矩阵数组

如何利用奇偶量化进行图像水印
比如想要更改图片【1】【2】矩阵中的数字,将所有的1换为0,将所有的0换成1或者-1
如何在下面调取出来这个【1】【2】矩阵

import numpy as np
from scipy import ndimage
import cv2
import random
import os
#量子化テーブル
Q = np.array(((16, 11, 10, 16, 24, 40, 51, 61),
                (12, 12, 14, 19, 26, 58, 60, 55),
                (14, 13, 16, 24, 40, 57, 69, 56),
                (14, 17, 22, 29, 51, 87, 80, 62),
                (18, 22, 37, 56, 68, 109, 103, 77),
                (24, 35, 55, 64, 81, 104, 113, 92),
                (49, 64, 78, 87, 103, 121, 120, 101),
                (72, 92, 95, 98, 112, 100, 103, 99)), dtype=np.float32)

y = cv2.imread(r'C:\Users\Owner\Desktop\so\sample.jpg', 0)
def psnr1(img1, img2):
   mse = np.mean((img1/1.0 - img2/1.0) ** 2 )
   if mse < 1.0e-10:
      return 100
   return 10 * math.log10(255.0**2/mse)

def get_FileSize(filePath):

    fsize = os.path.getsize(filePath)
    fsize = fsize/float(1024 * 1024)

    return round(fsize, 2)

y1 = y.astype(np.float32)
# print(y1.dtype)
m, n = y1.shape
hdata = np.vsplit(y1,n/8) # 縦方向から8個にする
for i in range(0, n//8):
        blockdata = np.hsplit(hdata[i],m/8) 
     #水平方向にも8にする
        for j in range(0, m//8):
            block = blockdata[j]
            #print("block[{},{}] data \n{}".format(i,j,blockdata[j]))
            Yb = cv2.dct(block.astype(np.float))
            F1 = Yb * Q
            F = F1 // Q 
            #print("block[{},{}] data\n{}".format(i,j,F))
            iblock = cv2.idct(Yb)
            #print(iblock)
Y = cv2.dct(y1)

print(Y.shape)
cv2.imshow("Dct",Y)



y2 = cv2.idct(Y)
print(psnr1(y,y2))
size1 = get_FileSize(r"C:\Users\Owner\Desktop\so\sample.jpg")
print("文件大小:%.2f MB"%(size1))


size = get_FileSize(r"C:\Users\Owner\Desktop\so\sample1.jpg")
print("文件大小:%.2f MB"%(size))

print(size/size1 - 1)
cv2.imshow("iDCT",y2.astype(np.uint8))
cv2.waitKey(0)
cv2.imwrite(r'C:\Users\Owner\Desktop\so\sample1.jpg', y2)
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题