You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
988 B

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import sys
import cv2
import numpy as np
import matplotlib.pyplot as plt
def Differentiation(img_path, T):
img = cv2.imread(img_path,0) # 读取图片
m, n = img.shape[0], img.shape[1]
ans = np.zeros((m, n), dtype=np.uint8)
kernelx = np.array([[-1, 0], [0, 1]], dtype=int)
kernely = np.array([[0, -1], [1, 0]], dtype=int)
x = cv2.filter2D(img, cv2.CV_16S, kernelx)
y = cv2.filter2D(img, cv2.CV_16S, kernely)
absX = cv2.convertScaleAbs(x)
absY = cv2.convertScaleAbs(y)
Roberts = cv2.addWeighted(absX, 0.5, absY, 0.5, 0)
# 背景保留
for i in range(m):
for j in range(n):
if Roberts[i, j] >= T:
ans[i, j] = Roberts[i, j]
else:
ans[i, j] = img[i, j]
cv2.imwrite('./output/Differentiation.jpg', ans)
'''
输入img_path:图片地址
T阈值
输出:微分法的结果
'''
if __name__ == '__main__':
Differentiation(sys.argv[1], eval(sys.argv[2]))