简体   繁体   English

如何在python中使用opencv读取图像的遮罩

[英]How to read the mask of an image using opencv in python

I am working on this challenge called Carvana Segmentation in kaggle. 我正在kaggle中解决这个称为Carvana Segmentation的挑战。 The dataset consists of 5088 images, for each image there is a mask. 数据集包含5088张图像,每个图像都有一个遮罩。 For eg, the below is a single image (.jpg file) and its corresponding mask (.gif file). 例如,以下是单个图像(.jpg文件)及其对应的蒙版(.gif文件)。

在此处输入图片说明在此处输入图片说明

I was able to read .jpg files using cv2, but not the .gif files. 我能够使用cv2读取.jpg文件,但无法读取.gif文件。 The syntax i used to read .gif file is 我用来读取.gif文件的语法是

 >>> image = cv2.imread('filename.gif',cv2.IMREAD_GRAYSCALE)

When I try to print the image, returns None 当我尝试打印图像时,返回None

 >>> print(image) -> None

Can someone suggest any other method, please 有人可以建议其他方法吗

Following this repo: https://github.com/asharma327/Read_Gif_OpenCV_Python/blob/master/gif_to_pic.py 遵循此回购协议: https : //github.com/asharma327/Read_Gif_OpenCV_Python/blob/master/gif_to_pic.py

you can do the following to read the image 您可以执行以下操作来读取图像

import cv2
import os


def convert_gif_to_frames(gif):

    # Initialize the frame number and create empty frame list
    frame_num = 0
    frame_list = []

    # Loop until there are frames left
    while True:
        try:
            # Try to read a frame. Okay is a BOOL if there are frames or not
            okay, frame = gif.read()
            # Append to empty frame list
            frame_list.append(frame)
            # Break if there are no other frames to read
            if not okay:
                break
            # Increment value of the frame number by 1
            frame_num += 1
        except KeyboardInterrupt:  # press ^C to quit
            break

    return frame_list


def output_frames_as_pics(frame_list):

    # Reduce the list of frames by half to make the list more managable
    frame_list_reduce = frame_list[0::2]
    # Get the path of the current working directory
    path = os.getcwd()
    # Set then name of your folder
    '''Replace this name with what you want your folder name to be'''
    folder_name = 'Picturebook_Pics_Kiss'
    # If the folder does not exist, then make it
    if not os.path.exists(path + '/' + folder_name):
        os.makedirs(path + '/' + folder_name)

    for frames_idx in range(len(frame_list_reduce)):
        cv2.imwrite(os.path.join(path + '/' + folder_name, str(frames_idx+1) + '.png'), frame_list_reduce[frames_idx])

    return


gif = cv2.VideoCapture('/home/ahmedramzy/Documents/gif/giphy.gif')
# here you can get the frames and work on it
xx = convert_gif_to_frames(gif_kiss)
# here if you want to write it on hard disk using imwrite 
output_frames_as_pics(xx)

imageio allows to read gifs like this: imageio允许读取这样的gif:

import imageio

img = imageio.imread('filename.gif')

You can't use imread(), there's no codec for that builtin (still a license problem)[ https://answers.opencv.org/question/185929/how-to-read-gif-in-python/] 您不能使用imread(),没有针对该内置代码的编解码器(仍然是许可证问题)[ https://answers.opencv.org/question/185929/how-to-read-gif-in-python/]

Since you are interested in python, you may use PIL library as mentioned here . 由于您对python感兴趣,因此可以使用此处提到的PIL库。

from PIL import Image
im = Image.open("animation.gif")

# To iterate through the entire gif
try:
    while 1:
        im.seek(im.tell()+1)
        # do something to im
except EOFError:
    pass # end of sequence

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM