假设我有这样的图像:
为此我想删除边框并得到这个:
完全相同的图像,没有边框。
我找到了一种“hacky”方法来做到这一点,它找到外部轮廓并在其上画一条线...说实话,这不是最好的方法,因为我需要调整线的“厚度”,使其足够粗覆盖边框,但不要太厚,这样就不会覆盖任何圆圈。
该image
变量是您在上面看到的图像(已经灰度化、阈值化)。
cnts = cv2.findContours(image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if imutils.is_cv2() else cnts[1]
cv2.drawContours(image, cnts, -1, 0, 15) # 15 is the right thickness for this image, but might not be for other ones...
Run Code Online (Sandbox Code Playgroud)
结果就是上面第二张图。效果很好,但它不适用于所有图像(因为厚度不同)。有一个更好的方法吗?
这就是我在评论中的意思...填充所有黑色的东西,比如左上角,并用白色连接到它,这样你想要的部分现在完全被白色包围,一直到边缘。然后用黑色填充所有白色并连接到左上角的所有内容。
#!/usr/local/bin/python3
import numpy as np
import cv2
from PIL import Image
# Load image and greyscale it
im = np.array(Image.open("framedcircles.jpg").convert('L'))
# Normalize and threshold image
im = cv2.normalize(im, None, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX)
res, im = cv2.threshold(im, 64, 255, cv2.THRESH_BINARY)
# Fill everything that is the same colour (black) as top-left corner with white
cv2.floodFill(im, None, (0,0), 255)
# Fill everything that is the same colour (white) as top-left corner with black
cv2.floodFill(im, None, (0,0), 0)
# Save result
Image.fromarray(im).save("result.png")
Run Code Online (Sandbox Code Playgroud)
结果:
归档时间: |
|
查看次数: |
16079 次 |
最近记录: |