gpt4 book ai didi

python - 如何锐化opencv python中的边缘

转载 作者:行者123 更新时间:2023-12-02 16:14:45 26 4
gpt4 key购买 nike

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

这是图片

enter image description here

应用 c2.Canny() 后

enter image description here

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

这是我的代码

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)

最佳答案

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

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

输入:

enter image description here

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)


阈值图像:

enter image description here

形态学开放应用:

enter image description here

移除的小区域:

enter image description here

精明的边缘:

enter image description here

关于python - 如何锐化opencv python中的边缘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60894593/

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