gpt4 book ai didi

Python:如何删除像素注释?

转载 作者:行者123 更新时间:2023-12-02 15:57:56 25 4
gpt4 key购买 nike

我有一个像下面这样的图像文件...

enter image description here

并且我想删除该图像文件中所有可用的注释。目前尚不清楚这必须如何在 Python 中完成。这就是我所做的;

import cv2 as cv
import numpy as np

img = cv.imread("/path/to/image/1.png")
image_contours = np.zeros((img.shape[1], img.shape[0], 1), np.uint8)

image_binary = np.zeros((img.shape[1], img.shape[0], 1), np.uint8)

for channel in range(img.shape[2]):
ret, image_thresh = cv.threshold(img[:, :, channel], 127, 255, cv.THRESH_BINARY)
contours = cv.findContours(image_thresh, 1, 1)[0]
cv.drawContours(image_contours, contours, -1, (255,255,255), 3)

contours = cv.findContours(image_contours, cv.RETR_LIST,
cv.CHAIN_APPROX_SIMPLE)[0]

cv.drawContours(image_binary, [max(contours, key = cv.contourArea)],
-1, (255, 255, 255), -1)

cv.imwrite("/path/to/save/save.png", image_binary)

我没有从注释中删除原始图像。但相反,我得到了下面的那个......我缺少什么?

enter image description here

最佳答案

这是在 Python/OpenCV 中使用掩码中值滤波的一种方法。

  • 阅读输入
  • 转换为灰色
  • 作为掩码的阈值
  • 将形态学应用到蒙版以扩大白色字母
  • 反转面具
  • 制作mask和inverted mask 3 channel 图像
  • 对图像进行中值滤波
  • 对中值滤波图像应用蒙版
  • 对输入图像应用反转掩码
  • 将两者相加
  • 保存结果

输入:

enter image description here

import cv2
import numpy as np
import math

# read image
img = cv2.imread('xray.png')

# convert to gray
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# threshold
mask = cv2.threshold(gray,220,255,cv2.THRESH_BINARY)[1]

# open and then dilate mask to make white regions slightly larger
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3,3))
mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel)
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (25,25))
mask = cv2.morphologyEx(mask, cv2.MORPH_DILATE, kernel)

# invert mask
mask_inv = 255 - mask

# make mask and inverted mask 3 channel
mask = cv2.merge([mask,mask,mask])
mask_inv = cv2.merge([mask_inv,mask_inv,mask_inv])

# median filter input image
median = cv2.medianBlur(img, 55)

# apply inverted mask to image
img_masked = cv2.bitwise_and(img, mask_inv)

# apply mask to median
median_masked = cv2.bitwise_and(median, mask)

# add together
result = cv2.add(img_masked,median_masked)

# save results
cv2.imwrite('xray_mask.png', mask)
cv2.imwrite('xray_mask_inv.png', mask_inv)
cv2.imwrite('xray_median.png', median)
cv2.imwrite('xray_masked.png', img_masked)
cv2.imwrite('xray_median_masked.png', median_masked)
cv2.imwrite('xray_removed.png', result)

cv2.imshow('gray', gray)
cv2.imshow('mask', mask)
cv2.imshow('mask_inv', mask_inv )
cv2.imshow('median', median)
cv2.imshow('img_masked', img_masked)
cv2.imshow('median_masked', median_masked)
cv2.imshow('result', result)
cv2.waitKey(0)
cv2.destroyAllWindows()


面具:

enter image description here

反掩码:

enter image description here

中值滤波图像:

enter image description here

屏蔽中位数:

enter image description here

反向屏蔽输入:

enter image description here

结果:

enter image description here

关于Python:如何删除像素注释?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61809204/

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