Пример #1
0
def run_hessenberg(A, verbose = 0):
    if verbose > 1:
        print("original matrix (hessenberg):\n", A)

    n = A.rows

    Q, H = mp.hessenberg(A)

    if verbose > 1:
        print("Q:\n",Q)
        print("H:\n",H)

    B = Q * H * Q.transpose_conj()

    eps = mp.exp(0.8 * mp.log(mp.eps))

    err0 = 0
    for x in xrange(n):
        for y in xrange(n):
            err0 += abs(A[y,x] - B[y,x])
    err0 /= n * n

    err1 = 0
    for x in xrange(n):
        for y in xrange(x + 2, n):
            err1 += abs(H[y,x])

    if verbose > 0:
        print("difference (H):", err0, err1)

    if verbose > 1:
        print("B:\n", B)

    assert err0 < eps
    assert err1 == 0
Пример #2
0
def run_hessenberg(A, verbose=0):
    if verbose > 1:
        print("original matrix (hessenberg):\n", A)

    n = A.rows

    Q, H = mp.hessenberg(A)

    if verbose > 1:
        print("Q:\n", Q)
        print("H:\n", H)

    B = Q * H * Q.transpose_conj()

    eps = mp.exp(0.8 * mp.log(mp.eps))

    err0 = 0
    for x in xrange(n):
        for y in xrange(n):
            err0 += abs(A[y, x] - B[y, x])
    err0 /= n * n

    err1 = 0
    for x in xrange(n):
        for y in xrange(x + 2, n):
            err1 += abs(H[y, x])

    if verbose > 0:
        print("difference (H):", err0, err1)

    if verbose > 1:
        print("B:\n", B)

    assert err0 < eps
    assert err1 == 0
def hessenberg(blocks, dataWater):
    step = 0.034
    pixelWater = 64 * 64

    for h in range(0, pixelWater):
        block = mp.matrix(blocks[h])
        Q, H = mp.hessenberg(block)
        h11 = H[0, 0]
        if dataWater[h] == 255:
            M1 = 0.5 * step
            M2 = -1.5 * step
        elif dataWater[h] == 0:
            M1 = -0.5 * step
            M2 = 1.5 * step

        h11 = H[0, 0]
        k = math.floor((math.ceil(h11 / step)) / 2)
        T1 = 2 * k * step + M1
        T2 = 2 * k * step + M2
        if abs(h11 - T2) < abs(h11 - T1):
            h11new = T2
        else:
            h11new = T1
        newH = []
        newQ = []
        for i in range(0, 4):
            rowH = []
            rowQ = []
            for j in range(0, 4):
                x = round(H[i, j])
                rowH.append(x)
                x = Q[i, j]
                rowQ.append(x)
            newH.append(rowH)
            newQ.append(rowQ)
        newblock = np.dot(np.dot(np.array(newQ), np.array(newH)),
                          np.array(newQ).T).tolist()
        blocks[h] = newblock
    # print("=========")
    # print(blocks[0])
    return blocks
def extraction():
    # Kiem tra xem file co ton tai hay khong
    if not os.path.exists('output/watermarkResulted.png') or not os.path.exists('output/ImageResulted.png'):
        print("STOPPPPP: Image not exists")
        exit()

    # Doc anh thuy phan
    image_watermark = cv.imread('output/watermarkResulted.png', 2)
    # print(image_watermark[0][0])
    dataWater = []
    for i in range(0, 64):
        for j in range(0, 64):
            dataWater.append(image_watermark[i][j])

    # Tao 1 anh trang de dung trong qua trinh trich xuat
    blank_image = np.zeros(shape=[64, 64, 3], dtype=np.uint8)
    ret, imgExtrac = cv.threshold(blank_image, 127, 255, cv.THRESH_BINARY)

    # Doc anh host da duoc thuy van
    img = cv.imread('output/ImageResulted.png')
    r1 = []
    x = []
    for i in range(0, 1024):
        x = [img[i][j][0] for j in range(0, 1024)]
        r1.append(x)
        x = []

    #Thuc hien bien doi DWT 2 chieu 2 lan dua ve kich vo 256x256
    coeffs1 = pywt.dwt2(r1, wavelet= 'db1')
    LL1, (LH1, HL1, HH1) = coeffs1
    coeffs2 = pywt.dwt2(HH1, wavelet='db1')
    LL2, (LH2, HL2, HH2) = coeffs2

    #Thuc hien bien doi FWHT
    fwhtHH2 = fwht(HH2)

    #Chia thanh cac khoi 4x4. Tong cos 64x64 khoi
    blocks = h.divblock(fwhtHH2)


    # dataWater = []
    step = 0.034
    #Thuc hien trich xuat anh thuy van va hien len
    for i in range(0, 64*64):

        block = mp.matrix(blocks[i])
        # print(block)
        Q , H = mp.hessenberg(block)

        h11 = H[0, 0]
        # print(h11)
        if math.ceil(h11/step) % 2 == 0:
            pixelExtrac = 0
        elif math.ceil(h11/step) % 2 == 1:
            pixelExtrac = 255

        dataWater.append(pixelExtrac)

    for i in range(0,64):
        for j in range(0, 64):
            imgExtrac[i][j] = dataWater[i * 64 + j]
    print('Have showed watermark')
    cv.imshow("Extraction Image", imgExtrac)
    cv.waitKey(0)
    cv.destroyAllWindows()