翻转图像会改变图像尺寸

时间:2019-05-28 10:30:03

标签: python opencv

我有一张图像及其面具是从kaggle举办的比赛中挑选的。图像的形状为(512,512,3),蒙版为(512,512,1)。在图像上应用function(flipping)之后,形状保持不变。但是,在尝试访问(print mask[:,:,0])之类的掩码时,在应用操作之前,我得到了一个矩阵

  [[0. 0. 0. ... 0. 0. 0.]
   [0. 0. 0. ... 0. 0. 0.]
   [0. 0. 0. ... 0. 0. 0.]
   ...
   [0. 0. 0. ... 0. 0. 0.]
   [0. 0. 0. ... 0. 0. 0.]
   [0. 0. 0. ... 0. 0. 0.]]

但是在应用操作之后,尝试访问掩码(print mask[:,:,0]),时出现以下错误

 Traceback (most recent call last):
 File "Augmentation.py", line 94, in <module>
 plot_img_and_mask_transformed(img,mask,img_flip,mask_flip)
 File "Augmentation.py", line 36, in plot_img_and_mask_transformed
   print(mask_tr[:,:,0])
 IndexError: too many indices for array

我应用的功能是

 def random_flip(img,mask,u=0.5):

   if np.random.random() < u :
      img = cv.flip(img,0)
      mask = cv.flip(mask,0)
   return img, mask

img, mask = get_image_and_mask(img_id)
img_tr,mask_tr = random_flip(img,mask)
plot(img,mask,img_tr,mask_tr)

翻转前图像的形状和遮罩

  ((512, 512, 3), (512, 512, 1))

翻转后图像的形状和遮罩

  ((512, 512, 3), (512, 512))

有人可以帮助我了解幕后发生的事情吗?

CODE

def get_image_and_mask(img_id):
  img = image.load_img(join(data_dir,'train','%s.jpg' % img_id),target_size=(input_size,input_size))
  img = image.img_to_array(img)
  mask = image.load_img(join(data_dir,'train_masks','%s_mask.gif' % img_id), grayscale=True,target_size=(input_size,input_size))
  mask = image.img_to_array(mask)
  img,mask = img / 255., mask/ 255.
  return img, mask

def plot_img_and_mask(img,mask):
  fig, axs = plt.subplots(ncols=2, figsize=(10,5),sharex=True,sharey=True)
  axs[0].imshow(img)
  axs[1].imshow(mask[:,:,0])
  for ax in axs:
     ax.set_xlim(0,input_size)
     ax.axis('off')
  fig.tight_layout()
  plt.show()

def plot_img_and_mask_transformed(img, mask, img_tr, mask_tr):
 fig, axs=plt.subplots(ncols=4,figsize=(16,4),sharex=True,sharey=True)
 axs[0].imshow(img)

 axs[1].imshow(mask[:,:,0])
 print(mask[:,:,0])
 print(mask_tr[:,:,0])
 axs[2].imshow(img_tr)
 axs[3].imshow(mask_tr)

 for ax in axs:
    ax.set_xlim(0,input_size)
    ax.axis('off')

 fig.tight_layout()
 plt.show()

def random_flip(img,mask,u=0.5):
  # Why do we have to check less than u
  if np.random.random() < u :
     img = cv.flip(img,0)
     mask = cv.flip(mask,0)
  return img, mask


def rotate(x,theta,row_axis=0,col_axis=1,channel_axis=2,fill_mode='nearest',cval=0):
 rotation_matrix = np.array([
  [np.cos(theta),-np.sin(theta),0],
  [np.sin(theta),np.cos(theta),0],
  [0,0,1]
 ])

 h, w = x.shape[row_axis], x.shape[col_axis]
 transform_matrix = image.transform_matrix_offset_center(rotation_matrix,h,w)
 x = image.apply_transform(x,transform_matrix,channel_axis,fill_mode,cval)
 return x

def random_rotate(img, mask, rotate_limit=(-20,20), u=0.5):
   if np.random.random() <  u:
     theta = np.pi/ 180 * np.random.uniform(rotate_limit[0], rotate_limit[1])
     img = rotate(img,theta)
     mask = rotate(mask,theta)
   return img, mask

if __name__== '__main__':

  input_size = 512
  data_dir = '../data/carvana-image-masking-challenge'
  np.random.seed(1987)

  df_train = pd.read_csv(join(data_dir,'train_masks.csv'),usecols=['img'])
  df_train['img_id']=df_train['img'].map(lambda s:s.split('.')[0])
  df_train.head(3)


 img_ids=df_train['img_id'].values
 np.random.shuffle(img_ids)
 img_id=img_ids[0]
 img,mask=get_image_and_mask(img_id)
 print((img.shape,mask.shape))
 plot_img_and_mask(img,mask)

 img_flip,mask_flip = random_flip(img,mask,u=1)
 print((img_flip.shape,mask_flip.shape))
 plot_img_and_mask_transformed(img,mask,img_flip,mask_flip)

输出

    Using TensorFlow backend.
    C:\Users\JamesJohnson\AppData\Local\Programs\Python\Python35\lib\site- packages\keras_preprocessing\image.py:492: UserWarning: grayscale is deprecated. Please use color_mode = "grayscale"
   warnings.warn('grayscale is deprecated. Please use '
  > ((512, 512, 3), (512, 512, 1))
  > ((512, 512, 3), (512, 512))
  [[0. 0. 0. ... 0. 0. 0.]
   [0. 0. 0. ... 0. 0. 0.]
   [0. 0. 0. ... 0. 0. 0.]
   ...
   [0. 0. 0. ... 0. 0. 0.]
   [0. 0. 0. ... 0. 0. 0.]
   [0. 0. 0. ... 0. 0. 0.]]
 Traceback (most recent call last):
 File "Augmentation.py", line 94, in <module>
 plot_img_and_mask_transformed(img,mask,img_flip,mask_flip)
 File "Augmentation.py", line 36, in plot_img_and_mask_transformed
 print(mask_tr[:,:,0])
 IndexError: too many indices for array

1 个答案:

答案 0 :(得分:2)

当您翻转遮罩时,看起来OpenCV会转储单例尺寸。翻转后需要重新引入。

mask_flip = mask_flip[..., None]

一种更方便的方法是修改方法,以便在翻转后将蒙版与单例尺寸一起返回,以防丢失。这样,您不必在每次翻转时都执行此操作,而方法会解决此问题。

def random_flip(img,mask,u=0.5):
    # Why do we have to check less than u
    if np.random.random() < u: 
        img = cv.flip(img,0)
        mask = cv.flip(mask,0)
        if len(mask.shape) == 2:
            mask = mask[..., None]
        return img, mask

顺便说一句,作为补充,您有一条注释,询问为什么在该方法中必须检查少于u的内容。请记住,np.random.random方法会统一生成一个介于0和1之间的值。假设您选择了u = 0.3。这意味着您有30%的机会选择0到0.3之间的值,而70%的机会则选择0.3到1之间的值。这很松散,这意味着如果u = 0.3,则存在if条件运行的可能性为30%,因此您会翻转图像和遮罩。因此,u控制着图像和遮罩发生翻转的可能性。