你好,stackoverflow people:(我正试图从两个不同的路径屏蔽许多图像,但我不知道该怎么做。这是一个只有两个图像的例子,以及我到目前为止所做的
image = cv.imread('Dataset/IDRiD_02.jpg', cv.IMREAD_COLOR)
od = cv.imread('od/IDRiD_02_OD.jpg', cv.IMREAD_GRAYSCALE)
mask = od
other = cv.bitwise_not(mask)
masking = cv.bitwise_and(image, image, mask=other)
cv.imwrite('Output/masking/' + 'masking.jpg', masking)
输入为IDRiD_02.jpg和IDRiD_02_OD.jpg,然后输出为masking.jpg
然后我想做同样的事情,但有很多图像
import cv2 as cv
import numpy as np
import os
import glob
import os.path
od_images = []
for directory_path in glob.glob("od/"):
for mask_path in glob.glob(os.path.join(directory_path, "*.jpg")):
mask = cv.imread(mask_path, cv.IMREAD_GRAYSCALE)
od_images.append(mask)
od_images = np.array(od_images)
path = "Dataset/*.jpg"
for file in glob.glob(path):
#read image
image = cv.imread(file, cv.IMREAD_COLOR)
# e.g. MyPhoto.jpg
basename = os.path.basename(file)
# e.g. MyPhoto
name = os.path.splitext(basename)[0]
mask = cv.bitwise_not(od_images)
masking = cv.bitwise_and(image, image, mask = mask)
cv.imwrite('Output/masking/' + name + '_masking.jpg', masking)
但在我运行代码后,我得到了以下错误消息
masking = cv.bitwise_and(image, image, mask = mask)
error: OpenCV(4.5.5) D:aopencv-pythonopencv-pythonopencvmodulescoresrcarithm.cpp:230: error: (-215:Assertion failed) (mtype == CV_8U || mtype == CV_8S) && _mask.sameSize(*psrc1) in function 'cv::binary_op'
有人能理解并帮助我吗?谢谢你之前:(
希望它能为您服务!
import cv2 as cv
import os
img_path = r"image_folder_path"
od_images = r"od_img_folder_path"
for img,od in zip(os.listdir(img_path), os.listdir(od_images)):
image = cv.imread(img_path+"\"+img, cv.IMREAD_COLOR)
od = cv.imread(od_images+"\"+od, cv.IMREAD_GRAYSCALE)
other = cv.bitwise_not(od)
res = cv.bitwise_and(image, image, mask=other)
cv.imwrite('Output/masking/' +img+ '_masking.jpg', res)