gpt4 book ai didi

python-3.x - OpenCV 通过矩阵比较值进行错误级别分析

转载 作者:行者123 更新时间:2023-12-02 16:08:05 25 4
gpt4 key购买 nike

我的目标是使用 opencv 和 python 生成一个错误级别分析工作流,我发现这个例子做了我需要的,但是在 C++ 中:Error level analysis in Image

虽然我设法转换了它的一部分,但由于我缺乏 C++ 知识,我确实停留在循环行部分,所以我可以在这里使用一些支持。

这是我设法转换的:

path=r'C:\****\training\pop_test'
image='source.jpg'

scale = 15
jpg_quality = 75

input_image= cv2.imread(os.path.join(path, image))
w,h,d = img.shape

# save tmp compressed file
cv2.imwrite(os.path.join(path, 'tmp.jpg'), input_image, [cv2.IMWRITE_JPEG_QUALITY, jpg_quality])
compressed_image= cv2.imread(os.path.join(path, 'tmp.jpg'))

# create matrice
output_image = np.zeros((w, h, d), dtype = "uint8")

这是我遇到困难的 C++:

for (int row = 0; row < input_image.rows; ++row)
{
const uchar* ptr_input = input_image.ptr<uchar>(row);
const uchar* ptr_compressed = compressed_image.ptr<uchar>(row);
uchar* ptr_out = output_image.ptr<uchar>(row);

for (int column = 0; column < input_image.cols; column++)
{
// Calc abs diff for each color channel multiplying by a scale factor
ptr_out[0] = abs(ptr_input[0] - ptr_compressed[0]) * scale;
ptr_out[1] = abs(ptr_input[1] - ptr_compressed[1]) * scale;
ptr_out[2] = abs(ptr_input[2] - ptr_compressed[2]) * scale;

ptr_input += 3;
ptr_compressed += 3;
ptr_out += 3;
}
}

谁能给我指出正确的方向?

最佳答案

我认为这就是您想要的 Python/OpenCV 中的 ELA(来自 http://www.hackerfactor.com/papers/bh-usa-07-krawetz-wp.pdf 的阅读信息

输入:

enter image description here

import cv2
import numpy as np

# read image
img1 = cv2.imread("lenna.png")

# set compression and scale
jpg_quality1 = 95
jpg_quality2 = 90
scale = 15

# write img1 at 95% jpg compression
cv2.imwrite("lenna_c95.jpg", img1, [cv2.IMWRITE_JPEG_QUALITY, jpg_quality1])

# read compressed image
img2 = cv2.imread("lenna_c95.jpg")

# get absolute difference between img1 and img2 and multiply by scale
diff1 = scale * cv2.absdiff(img1, img2)

# write img2 at 90% jpg compression
cv2.imwrite("lenna_c90.jpg", img2, [cv2.IMWRITE_JPEG_QUALITY, jpg_quality2])

# read compressed image
img3 = cv2.imread("lenna_c90.jpg")

# get absolute difference between img1 and img2 and multiply by scale
diff2 = scale * cv2.absdiff(img2, img3)

# write result to disk
cv2.imwrite("lenna_ela_95.jpg", diff1)
cv2.imwrite("lenna_ela_90.jpg", diff2)

# display it
cv2.imshow("ela95", diff1)
cv2.imshow("ela90", diff2)
cv2.waitKey(0)


ELA 结果为 95%:

enter image description here

90% 的 ELA 结果:

enter image description here

关于python-3.x - OpenCV 通过矩阵比较值进行错误级别分析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62009185/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com