[Python] 纯文本查看 复制代码 import cv2
import numpy as np
def detect_edges(image):
# 转换为灰度图
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# 应用高斯模糊
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
# 使用Canny算法检测边缘
edges = cv2.Canny(blurred, 30, 150)
return edges
def analyze_edges(edges):
# 这里可以添加更复杂的分析,比如边缘的连续性、密度等
# 简单的示例:计算边缘像素的比例
total_pixels = edges.shape[0] * edges.shape[1]
edge_pixels = np.sum(edges > 0)
edge_ratio = edge_pixels / total_pixels
return edge_ratio
def detect_splicing(image_path):
# 读取图片
image = cv2.imread(image_path)
if image is None:
print("Error: Image not found.")
return
# 检测边缘
edges = detect_edges(image)
# 显示边缘图像(可选)
cv2.imshow('Edges', edges)
cv2.waitKey(0)
cv2.destroyAllWindows()
# 分析边缘
edge_ratio = analyze_edges(edges)
print(f"Edge Ratio: {edge_ratio:.4f}")
# 假设边缘比例超过某个阈值(这里设为0.05)则认为图片可能被拼接
if edge_ratio > 0.05:
print("Warning: Image may be spliced.")
else:
print("Image seems to be normal.")
# 使用示例
detect_splicing('path_to_your_image.jpg') |