簡體   English   中英

移除邊界框輪廓

[英]Remove bounding box outline

我有這張圖片

在此處輸入圖片說明

當我申請

from skimage import filters
result_sobel = filters.sobel(image)

圖像是

在此處輸入圖片說明

如何刪除邊界框輪廓以使其與背景融合?

理想情況下,輸出將是黑色背景,中間是紅色,沒有輪廓的邊界框。

您可以在 skimage.filters.sobel 中使用掩碼:

import skimage

img = skimage.io.imread('N35nj.png', as_gray=True)   
mask = img > skimage.filters.threshold_otsu(img)
edges = skimage.filters.sobel(img, mask=mask)

讓我們繪制結果:

import matplotlib.pyplot as plt

fig, ax = plt.subplots(nrows=1, ncols=2, figsize=(10,5))
ax[0].imshow(img, cmap='gray')
ax[0].set_title('Original image')

ax[1].imshow(edges, cmap='magma')
ax[1].set_title('Sobel edges')

for a in ax.ravel():
    a.axis('off')

plt.tight_layout()
plt.show()

在此處輸入圖片說明

這是 Python/OpenCV 中的一種方式。 只需從原始灰度圖像中獲取輪廓即可。 然后在紅色輪廓圖像上以黑色繪制 3 像素厚(Sobel 邊緣厚度)。 我注意到您的兩個圖像大小不同,並且輪廓相對於灰色框偏移。 這是為什么?

灰色原件:

在此處輸入圖片說明

索貝爾紅邊:

在此處輸入圖片說明

import cv2
import numpy as np

# read original image as grayscale 
img = cv2.imread('gray_rectangle.png', cv2.IMREAD_GRAYSCALE)
hi, wi = img.shape[:2]

# read edge image
edges = cv2.imread('red_edges.png')

# edges image is larger than original and shifted, so crop it to same size
edges2 = edges[3:hi+3, 3:wi+3]

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

# get contours and draw them as black on edges image
result = edges2.copy()
contours = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours = contours[0] if len(contours) == 2 else contours[1]
cv2.drawContours(result, contours, -1, (0,0,0), 3)

# write result to disk
cv2.imwrite("red_edges_removed.png", result)

# display it
cv2.imshow("ORIG", img)
cv2.imshow("EDGES", edges)
cv2.imshow("THRESH", thresh)
cv2.imshow("RESULT", result)
cv2.waitKey(0)


結果:

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM