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

问题描述 投票:0回答:1

我正在尝试使用使用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

和DataGenerator:

    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'
opencv image-processing keras deep-learning mismatch
1个回答
0
投票

cv2图像不过是一个numpy数组。

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

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

© www.soinside.com 2019 - 2024. All rights reserved.