Я пытаюсь использовать пользовательскую функцию предварительной обработки, которая использует 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'