0
我正在使用像这样的词典中的图像定义: 我想摆脱邻居条目(顶部和机器人)的那些小元素,如果他们触摸图像的上部或机器人边界和延伸不超过从它20个像素(不包括任何实际的字母触摸顶部或机器人),因为这表示图像(红色):
如何修剪匹配给定范围的数组数组中的值对?
的方式我试图做它是: 1.加载的图像灰度 2.使用cv2.findContours
获取图像的轮廓3.查找从x = 0开始但不超过x = 20的轮廓 4. F ind轮廓从高度1开始并以高度结束-21 5.用白色画这些轮廓
问题是cv2.findContours
返回坐标对数组数组列表。虽然我能够删除某些坐标对,但我在这里很难应用。
我尝试了许多方法,目前我坚持这一点:
import cv2
import os
def def_trimmer(img):
height, width = img.shape
img_rev = cv2.bitwise_not(img)
_, contours, _ = cv2.findContours(img_rev,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
# contours = np.concatenate(contours, axis = 0)
# contours = contours[((contours<[20-1, width])|(contours>[height-20-1, -1])).all(axis=(1,2))]
for outer in contours:
# for outer2 in outer1:
oldlen = len(outer)
outer = outer[(((outer<[20-1, width])|(outer>[height-20-1, -1])).all(axis=(1, 2)))]
newlen = len(outer)
print((oldlen, newlen))
cv2.drawContours(img,contours,-1,(255,255,255),-1)
return(img)
img = cv2.imread("img.png")
img_out = def_trimmer(img)
cv2.imshow("out", img_out)
感谢您的建议。这可能确实更快,更容易。我会尽快试一试:) – MrVocabulary