创建一个目录以存储输出掩码,以进行图像分割任务

发布于 2025-02-03 08:20:28 字数 1001 浏览 4 评论 0原文

遵循教程 kaggle Notebook尝试创建一个可以将预测掩码存储在文件夹中的函数。尝试以下代码时,我会收到错误'内置函数imread返回null而不设置错误' “在此处输入图像说明”

请建议对潜在解决方案进行修改或重定向。

import cv2
import os

image_dir = "/content/sample_data/Output"

def pred_images(sample_image, sample_mask, path):
    pred_mask = model.predict(sample_image[tf.newaxis, ...])
    print(pred_mask.shape)
    pred_mask = pred_mask.reshape(img_size[0],img_size[1],1)
    img= cv2.imread(pred_mask, 1)
    cv2.imwrite(os.path.join(path, '*.png'), img)

for i in range(len(train_dataset)):
  for image, mask in TRAIN.take(i):
    sample_image, sample_mask= image, mask
    pred_images(sample_image, sample_mask, {image_dir})

following the tutorial Kaggle Notebook for Unet, I am trying to create a function that could store the predicted mask in a folder. While trying below codes, I am getting error 'built-in function imread returned NULL without setting an error' enter image description here

Please suggest a modification or redirect for potential solutions.

import cv2
import os

image_dir = "/content/sample_data/Output"

def pred_images(sample_image, sample_mask, path):
    pred_mask = model.predict(sample_image[tf.newaxis, ...])
    print(pred_mask.shape)
    pred_mask = pred_mask.reshape(img_size[0],img_size[1],1)
    img= cv2.imread(pred_mask, 1)
    cv2.imwrite(os.path.join(path, '*.png'), img)

for i in range(len(train_dataset)):
  for image, mask in TRAIN.take(i):
    sample_image, sample_mask= image, mask
    pred_images(sample_image, sample_mask, {image_dir})

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

三五鸿雁 2025-02-10 08:20:28

您的代码中有几个错误:

  1. 您正在使用 imread到底是什么? cv2.imread图像文件并将图像读取为变量。也就是说,用实际的像素值“转换” a string 将fileName带入矩阵
    但是,您将imread应用于矩阵mask_pred - 您在那里做什么?这没有任何意义,因此您收到的错误消息。


  2. 您正在尝试您的图像,img到文件名'/contery/pontent/sample_data/output/*。png' - - 这不是有效的文件名。您不允许您使用'*'(还有一些其他特殊字符)名称。

  3. 此外,您的路径参数设置为{image_dir} - 也就是说,您正在制作 set 带有一个元素(image_dir),然后尝试尝试使用此 set 作为<代码>路径文件。

You have several errors in your code:

  1. You are using imread for what exactly? cv2.imread ismeant to open an image file and read the image into a variable. That is, to "convert" a string with filename into a matrix with the actual pixel values.
    However, you are applying imread to a matrix mask_pred -- what are you doing there? This makes no sense, thus the error message you got.

  2. You are trying to write your image, img to a file name '/content/sample_data/Output/*.png' -- this is NOT a valid file name. You are not allowed to use '*' (and a few other special characters) in file names.

  3. Moreover, your path argument is set to {image_dir} -- that is, you are making a set with one element (image_dir) and then try and use this set as the path for the file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文