From 453503656ef2ae353d5e8c34e22782276c130226 Mon Sep 17 00:00:00 2001 From: TaiYama Date: Thu, 14 Sep 2017 09:43:35 +0900 Subject: [PATCH 1/2] fix a channel input --- cgi-bin/paint_x2_unet/img2imgDataset.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/cgi-bin/paint_x2_unet/img2imgDataset.py b/cgi-bin/paint_x2_unet/img2imgDataset.py index 86d0247..cc521de 100755 --- a/cgi-bin/paint_x2_unet/img2imgDataset.py +++ b/cgi-bin/paint_x2_unet/img2imgDataset.py @@ -21,6 +21,22 @@ def cvt2YUV(img): img = cv2.cvtColor( img, cv2.COLOR_BGR2YUV ) return img +def cvt2GRAY(img): + if len(img.shape) == 2: + # Grayscale image + return img + width, height, color = img.shape + if color == 4: + # RGBA image + r, g, b, a = cv2.split(img) + white = (255 - a).repeat(3).reshape((width, height, 3)) + img2 = cv2.merge((r, g, b)).astype(np.uint32) + img2 += white + img2 = img2.clip(0, 255).astype(np.uint8) + return cv2.cvtColor(img2, cv2.COLOR_RGB2GRAY) + else: + # RGB image + class ImageAndRefDataset(chainer.dataset.DatasetMixin): def __init__(self, paths, root1='./input', root2='./ref', dtype=np.float32): @@ -39,7 +55,10 @@ def get_example(self, i, minimize=False, blur=0, s_size=128): path1 = os.path.join(self._root1, self._paths[i]) #image1 = ImageDataset._read_image_as_array(path1, self._dtype) - image1 = cv2.imread(path1, cv2.IMREAD_GRAYSCALE) + #image1 = cv2.imread(path1, cv2.IMREAD_GRAYSCALE) + image1 = cv2.imread(path1, cv2.IMREAD_UNCHANGED ) + image1 = cvt2GRAY(image1) + print("load:" + path1, os.path.isfile(path1), image1 is None) image1 = np.asarray(image1, self._dtype) From a96f059753f1f5c722999d256b9c2b6f4c231bf8 Mon Sep 17 00:00:00 2001 From: whughw Date: Sun, 30 Dec 2018 12:09:51 +0800 Subject: [PATCH 2/2] Update img2imgDataset.py Fix the bug in function cvt2GRAY: it couldn't handle with RGB image because of a missing line of code, which resulted in a runtime error and caused an exception. --- cgi-bin/paint_x2_unet/img2imgDataset.py | 1 + 1 file changed, 1 insertion(+) diff --git a/cgi-bin/paint_x2_unet/img2imgDataset.py b/cgi-bin/paint_x2_unet/img2imgDataset.py index cc521de..3d424a7 100755 --- a/cgi-bin/paint_x2_unet/img2imgDataset.py +++ b/cgi-bin/paint_x2_unet/img2imgDataset.py @@ -36,6 +36,7 @@ def cvt2GRAY(img): return cv2.cvtColor(img2, cv2.COLOR_RGB2GRAY) else: # RGB image + return cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) class ImageAndRefDataset(chainer.dataset.DatasetMixin):