图像锐化,阈值

设置阈值,大于T为梯度,否则不变。即:
在这里插入图片描述


import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt

def gradient_T(img, T):
	result = np.array(img)
	for i in range(img.shape[0]-1):
		for j in range(img.shape[1]-1):
			g = np.abs(int(img[i][j]) - int(img[i+1][j])) + np.abs(int(img[i][j]) - int(img[i][j+1]))
			if img[i][j] > T:
				result[i][j] = g
	return result


img = cv.imread('lena512color.tiff', 0)
result1 = gradient_T(img, 20)
result2 = gradient_T(img, 80)
result3 = gradient_T(img, 150)


res1 = np.hstack((img, result1))
res2 = np.hstack((result2, result3))
cv.imshow('img and result1', res1)
cv.imshow('result2 and result3', res2)

cv.waitKey(0)

第一张是原图,第二张是T=20时的梯度图像,第三张是T=80时的梯度图像,最后一张是T=150时的梯度图像,由图可以看出,当阈值越大,轮廓越不明显。

在这里插入图片描述
在这里插入图片描述

为边缘处为一个特定的灰度级。即
在这里插入图片描述

import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt

def gradient_T(img, T):
	result = np.array(img)
	for i in range(img.shape[0]-1):
		for j in range(img.shape[1]-1):
			g = np.abs(int(img[i][j]) - int(img[i+1][j])) + np.abs(int(img[i][j]) - int(img[i][j+1]))
			if g > T:
				result[i][j] = 80
	return result


img = cv.imread('lena512color.tiff', 0)
result1 = gradient_T(img, 20)


res1 = np.hstack((img, result1))
cv.imshow('img and result1', res1)

cv.waitKey(0)

在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值