我正在處理一個包含 72 個影像和 72 個掩碼的資料集。我將影像附加到一個numpy ndarray我希望 cv2 從對應于numpy ndarray.
這是影像和蒙版的路徑:
images_dir = '/content/drive/MyDrive/dataset/images'
masks_dir = '/content/drive/MyDrive/dataset/masks'
#將圖片添加到 numpy ndarray
file_names = np.sort(os.listdir(images_dir))
file_names = np.char.split(file_names, '.')
filenames = np.array([])
for i in range(len(file_names)):
filenames = np.append(filenames, file_names[i][0])
這是我希望打開 cv 讀取每個影像然后從相應路徑中屏蔽的函式:
def augment_dataset(count):
'''Function for data augmentation
Input:
count - total no. of images after augmentation = initial no. of images * count
Output:
writes augmented images (input images & segmentation masks) to the working directory
'''
transform_1 = augment(512, 512)
transform_2 = augment(480, 480)
transform_3 = augment(512, 512)
transform_4 = augment(800, 800)
transform_5 = augment(1024, 1024)
transform_6 = augment(800, 800)
transform_7 = augment(1600, 1600)
transform_8 = augment(1920, 1280)
i = 0
for i in range(count):
for file in filenames:
tile = file.split('_')[1]
img = cv2.imread(images_dir file '.jpg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
mask = cv2.imread(masks_dir file '.png')
mask = cv2.cvtColor(mask, cv2.COLOR_BGR2RGB)
當我運行代碼時:
augment_dataset(8)
出現此錯誤:
---------------------------------------------------------------------------
error Traceback (most recent call last)
<ipython-input-112-fae4beb79e15> in <module>()
----> 1 augment_dataset(8)
<ipython-input-111-121d55acd3fc> in augment_dataset(count)
20 tile = file.split('_')[1]
21 img = cv2.imread(images_dir file '.jpg')
---> 22 img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
23 mask = cv2.imread(masks_dir file '.png')
24 mask = cv2.cvtColor(mask, cv2.COLOR_BGR2RGB)
error: OpenCV(4.1.2) /io/opencv/modules/imgproc/src/color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cvtColor'
我知道這是因為 OpenCV 沒有讀取檔案。那么如何讓 openCV 讀取檔案呢?
uj5u.com熱心網友回復:
在這些情況下,最好print(path/to/directory)查看目錄是否正確。在這種情況下,我們可以看到我/在目錄路徑中遺漏了一個。所以 Python 無法決議資料。此外,您可以使用ose.path.exists(path/to/directory)查看路徑是否存在。如果回傳值為False,則必須檢查指定路徑是否有錯誤
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/365642.html
上一篇:從相機Pyqt5調整直播幀大小
