Python 中的连续图像掩蔽
Consecutive Image Masking in Python
我正在进行一个项目,在不同位置遮盖连续图像以研究一只苍蝇的运动。最终图像应如下所示:correct masking of image. However, as the code continues to run, the images start to turn out like this: Incorrect image masking,这不是我想要的,因为我只希望每个图像有一个可见区域(包含一只苍蝇的圆圈)而不是三个。我需要对我的代码做些什么才能使每个图像中的不同位置只出现一个圆圈,而不是图像中有多个圆圈?
`List = [
[180, 353, 85, str("/media/pi/T3 1TB 2Ch xFer/Ramzy Masked /Arena2")],
[180, 542, 85, str("/media/pi/T3 1TB 2Ch xFer/Ramzy Masked /Arena3")],
[180, 728, 85, str("/media/pi/T3 1TB 2Ch xFer/Ramzy Masked /Arena4")],
]
def load_images_from_folder(folder):
images = []
for filename in os.listdir(folder):
img = cv2.imread(os.path.join(folder,filename))
mask = np.zeros(img.shape[:2], dtype="uint8")
for row in List:
cv2.circle(mask, (row[0], row[1]), row[2], 255, -1)
masked = cv2.bitwise_and(img, img, mask=mask)
cv2.imwrite(os.path.join(row[3], f'{filename}'), masked)
load_images_from_folder("/media/pi/T3 1TB 2Ch xFer/Ramzy Research
Photos/8.17.21")
cv2.waitKey(0)
cv2.destroyAllWindows()
`
我对 cv2 库不是很熟悉,但我相信这一行:
cv2.circle(mask, (row[0], row[1]), row[2], 255, -1)
在每次循环时在您的蒙版上添加一个新圆圈。有很多方法可以解决此问题,但您可以先将此行移动到循环中 cv2.circle 调用的正上方,如下所示:
def load_images_from_folder(folder):
images = []
for filename in os.listdir(folder):
img = cv2.imread(os.path.join(folder,filename))
for row in List:
mask = np.zeros(img.shape[:2], dtype="uint8")
cv2.circle(mask, (row[0], row[1]), row[2], 255, -1)
masked = cv2.bitwise_and(img, img, mask=mask)
cv2.imwrite(os.path.join(row[3], f'{filename}'), masked)
这将在每次循环迭代时创建一个全新的掩码。
我正在进行一个项目,在不同位置遮盖连续图像以研究一只苍蝇的运动。最终图像应如下所示:correct masking of image. However, as the code continues to run, the images start to turn out like this: Incorrect image masking,这不是我想要的,因为我只希望每个图像有一个可见区域(包含一只苍蝇的圆圈)而不是三个。我需要对我的代码做些什么才能使每个图像中的不同位置只出现一个圆圈,而不是图像中有多个圆圈?
`List = [
[180, 353, 85, str("/media/pi/T3 1TB 2Ch xFer/Ramzy Masked /Arena2")],
[180, 542, 85, str("/media/pi/T3 1TB 2Ch xFer/Ramzy Masked /Arena3")],
[180, 728, 85, str("/media/pi/T3 1TB 2Ch xFer/Ramzy Masked /Arena4")],
]
def load_images_from_folder(folder):
images = []
for filename in os.listdir(folder):
img = cv2.imread(os.path.join(folder,filename))
mask = np.zeros(img.shape[:2], dtype="uint8")
for row in List:
cv2.circle(mask, (row[0], row[1]), row[2], 255, -1)
masked = cv2.bitwise_and(img, img, mask=mask)
cv2.imwrite(os.path.join(row[3], f'{filename}'), masked)
load_images_from_folder("/media/pi/T3 1TB 2Ch xFer/Ramzy Research
Photos/8.17.21")
cv2.waitKey(0)
cv2.destroyAllWindows()
`
我对 cv2 库不是很熟悉,但我相信这一行:
cv2.circle(mask, (row[0], row[1]), row[2], 255, -1)
在每次循环时在您的蒙版上添加一个新圆圈。有很多方法可以解决此问题,但您可以先将此行移动到循环中 cv2.circle 调用的正上方,如下所示:
def load_images_from_folder(folder):
images = []
for filename in os.listdir(folder):
img = cv2.imread(os.path.join(folder,filename))
for row in List:
mask = np.zeros(img.shape[:2], dtype="uint8")
cv2.circle(mask, (row[0], row[1]), row[2], 255, -1)
masked = cv2.bitwise_and(img, img, mask=mask)
cv2.imwrite(os.path.join(row[3], f'{filename}'), masked)
这将在每次循环迭代时创建一个全新的掩码。