我正在尋找一種使用 OpenCV 去除影像邊框周圍黑點的方法。
圖片:

預期的解決方案:

import cv2
def get_img(img_name):
lower = (0, 0, 0)
upper = (75, 75, 75)
img = cv2.imread(img_name)
#print(img)
img_rgb_inrange = cv2.inRange(img, lower, upper)
neg_rgb_image = ~img_rgb_inrange
w = cv2.cvtColor(neg_rgb_image,cv2.COLOR_GRAY2RGB)
image3 = img-w
cv2.imwrite('img.png', image3)
get_img('address of the img')
我使用了在
最終輸出:

想知道,是否有任何動態方式(而不是初始化上限和下限)可以消除影像中的噪聲但仍保持前景和背景?
uj5u.com熱心網友回復:
import cv2
import matplotlib.pyplot as plt
image = cv2.imread('E3BbU.jpeg')
img_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(img_gray, 150, 255, cv2.THRESH_BINARY)
contours, hierarchy = cv2.findContours(image=thresh, mode=cv2.RETR_TREE, method=cv2.CHAIN_APPROX_NONE)
image2 = image.copy()
cv2.drawContours(image=image2, contours=sorted(contours, key=len)[:-1], contourIdx=-1, color=(255, 255, 255), thickness=2, lineType=cv2.LINE_AA)
fig, ax = plt.subplots(2, 1)
for i, img in enumerate([image, image2]):
ax[i].imshow(img);
ax[i].axes.get_xaxis().set_visible(False)
ax[i].axes.get_yaxis().set_visible(False)
plt.show()

轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/505707.html
上一篇:色彩空間縮減計算
