opencv - 如何为 DataGenerator keras 使用自定义图像加载器?

标签 opencv image-processing keras deep-learning mismatch

我正在尝试使用使用 OpenCV 的自定义预处理函数,但 DataGenerator 加载的图像与 CV2 默认类型不匹配。
是否可以指定使用哪个函数来加载图像?

这是我的代码。

def read_and_process_image(im,im_size):
     #read image from file 
    #im=cv2.imread(im)

    gray = cv2.cvtColor(im,cv2.COLOR_RGB2GRAY) # convert 2 grayscale
    im_pil = Image.fromarray(gray)
    _,thresh = cv2.threshold(gray,10,255,cv2.THRESH_BINARY) # turn it into a binary image
    contours,hierarchy = cv2.findContours(thresh,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE) # find contours

    if len(contours) != 0:
        print("contour")
        #find the biggest area
        cnt = max(contours, key = cv2.contourArea)

        #find the bounding rect
        x,y,w,h = cv2.boundingRect(cnt)                  

        r=int(w*0.12)
        crop = im[y+r:y+h-r,x+r:x+w-r]# crop image
        crop=cv2.flip(crop,40)
        #crop1=cv2.resize(crop,(im_size,im_size))
         # resize to im_size X im_size size
        #crop1 = cv2.convertScaleAbs(crop, alpha=1, beta=0.0001)
        crop1=normalize_histograms(crop)
        #clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))
        #crop1 = clahe.apply(crop1)
        return crop1
    else:
        return( normalize_histograms(cv2.resize(im,(im_size,im_size))) )  

要调用的预处理函数:
IM_SIZE=256
def preprocessing_image(image):
    global IM_SIZE
    image=read_and_process_image(image,IM_SIZE)
    return image

和数据生成器:
    train_datagen = keras.preprocessing.image.ImageDataGenerator(rescale=1./255,
                                                                 featurewise_center=True,
                                                                 featurewise_std_normalization=True,
                                                                preprocessing_function=preprocessing_image)
val_gen = train_datagen.flow_from_dataframe(dataframe=val_data, 
                                        directory="D:/PROJECTS/MLPC2019/dataset/train/train", 
                                        x_col="filename", 
                                        y_col="label",
                                        class_mode="categorical",
                                        shuffle=False,
                                        target_size=(IMAGE_SIZE,IMAGE_SIZE), 
                                        batch_size=BATCH_SIZE)
plt.imshow(val_gen[0])

我收到以下错误:
---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
<ipython-input-130-c8fee3202272> in <module>
----> 1 plt.imshow(val_gen[0])

~\Anaconda3\lib\site-packages\keras_preprocessing\image\iterator.py in __getitem__(self, idx)
     63         index_array = self.index_array[self.batch_size * idx:
     64                                        self.batch_size * (idx + 1)]
---> 65         return self._get_batches_of_transformed_samples(index_array)
     66 
     67     def __len__(self):

~\Anaconda3\lib\site-packages\keras_preprocessing\image\iterator.py in _get_batches_of_transformed_samples(self, index_array)
    237                 params = self.image_data_generator.get_random_transform(x.shape)
    238                 x = self.image_data_generator.apply_transform(x, params)
--> 239                 x = self.image_data_generator.standardize(x)
    240             batch_x[i] = x
    241         # optionally save augmented images to disk for debugging purposes

~\Anaconda3\lib\site-packages\keras_preprocessing\image\image_data_generator.py in standardize(self, x)
    702         """
    703         if self.preprocessing_function:
--> 704             x = self.preprocessing_function(x)
    705         if self.rescale:
    706             x *= self.rescale

<ipython-input-101-3a910a8620ec> in preprocessing_image(image)
     15     """
     16     # TODO: augment more here
---> 17     image=read_and_process_image(image,IM_SIZE)
     18     return image

<ipython-input-128-aa711687f072> in read_and_process_image(im, im_size)
      8         im_pil = Image.fromarray(gray)
      9         _,thresh = cv2.threshold(gray,10,255,cv2.THRESH_BINARY) # turn it into a binary image
---> 10         contours,hierarchy = cv2.findContours(thresh,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE) # find contours
     11 
     12         if len(contours) != 0:

error: OpenCV(4.1.2) C:\projects\opencv-python\opencv\modules\imgproc\src\contours.cpp:197: error: (-210:Unsupported format or combination of formats) [Start]FindContours supports only CV_8UC1 images when mode != CV_RETR_FLOODFILL otherwise supports CV_32SC1 images only in function 'cvStartFindContours_Impl'

最佳答案

一个 cv2 image 只不过是一个 numpy 数组。

您可以轻松地将 PIL 图像 (Keras) 转换为 cv2只需调用 cv2_image = np.array(pil_image) .

由于cv2使用 BGR 而不是 RGB,您可以调用 cv2_image = np.flip(cv2_image, axis=-1) (如果有 3 个 channel )

关于opencv - 如何为 DataGenerator keras 使用自定义图像加载器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59984134/

相关文章:

tensorflow - 在 keras 中使用带有 LSTM nn 的 Gensim Fasttext 模型

opencv - 如何在 C++ 中使用 opencv 检测瞳孔和凝视?

java - 无法使用 OpenCV 打开/查看/抓取/读取 IP 摄像头

visual-c++ - 用户输入显示在OpenCV中的错误坐标处

python - 使用具有不同参数的 cv2.ellipse 时椭圆拟合异常?

python - 使用 pip3 安装 Keras,但出现 "No Module Named keras"错误

python - 如何使用包含文件名和标签的数据帧将带有类标签的图像从单个目录加载到 Keras 模型中?

opencv - 这种时差的原因是什么?

opencv - 要跟踪的好功能?增强现实无人机 2

MATLAB - 合并子矩阵