如何锐化opencv python中的边缘

Tay*_*hra 4 python opencv image-processing python-3.x

我正在学习图像处理,其中我试图使用 python 中的 opencv 锐化图像的边缘,我已经尽可能地减少了噪声,但现在我想让图像的边缘更清晰,我已经尝试过了,cv2.Canny()但是但效果并不理想。

这是图像

在此输入图像描述

应用 c2.Canny() 后

在此输入图像描述

但我正在尝试使单词边框或边缘更清晰

这是我的代码

import cv2
import matplotlib.pyplot as plt
img_1 = cv2.imread('noise/1.png',cv2.IMREAD_GRAYSCALE)
edges = cv2.Canny(img_1,200,200)
plt.imshow(edges)
Run Code Online (Sandbox Code Playgroud)

fmw*_*w42 6

这是在 Python/OpenCV 中处理该问题的一种方法。

  • 将输入读取为灰度
  • 设置阈值以确保它是二进制的
  • 应用形态学关闭
  • 查找轮廓并通过在其上绘制黑色来删除输入中的所有小区域
  • 应用 Canny 边缘检测
  • 保存结果

输入:

在此输入图像描述

import cv2
import numpy as np

# read image as grayscale
img = cv2.imread('K.png', cv2.IMREAD_GRAYSCALE)

# threshold to binary
thresh = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY)[1]

# apply morphology
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5,5))
morph = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel)

# find contours - write black over all small contours
letter = morph.copy()
cntrs = cv2.findContours(morph, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cntrs = cntrs[0] if len(cntrs) == 2 else cntrs[1]
for c in cntrs:
    area = cv2.contourArea(c)
    if area < 100:
        cv2.drawContours(letter,[c],0,(0,0,0),-1)

# do canny edge detection
edges = cv2.Canny(letter, 200, 200)

# write results
cv2.imwrite("K_thresh.png", thresh)
cv2.imwrite("K_morph.png", morph)
cv2.imwrite("K_letter.png", letter)
cv2.imwrite("K_edges.png", edges)

# show results
cv2.imshow("K_thresh", thresh)
cv2.imshow("K_morph", morph)
cv2.imshow("K_letter", letter)
cv2.imshow("K_edges", edges)
cv2.waitKey(0)
Run Code Online (Sandbox Code Playgroud)


阈值图像:

在此输入图像描述

形态开放应用:

在此输入图像描述

删除小区域:

在此输入图像描述

精明的边缘:

在此输入图像描述