如何在没有for循环的情况下填充具有给定颜色的图像阵列?

时间:2020-02-02 18:49:48

标签: python python-3.x numpy cv2

我有一个numpy图片数组,其中包含numpy图片数组,并且我尝试使用给定的颜色填充该数组中的每个图片,而不使用for循环。换句话说,我希望一切都矢量化。

要产生错误:取消注释行并注释其下面的行,您将得到ValueError:操作数不能与重新映射的形状一起广播[original-> remapped]:(3,)和要求的形状(4 ,2)

您可以使用此图像进行测试。

Tiger

这是我所做的:

import cv2
import numpy as np
import matplotlib.pyplot as plt


def pad_image(image, pad_width, values, mode='img_arr'):
    """
    Add a pad(border) of a given width and value to the image.
    Args:
        image: Image array or a single image.
        pad_width: The width of the pad.
        values: Value of the pad layer.
        mode: A string representation of the input
            'img_arr': Array of images.
            'img': A single image.

    Return:
        numpy array of padded images or a padded image.
    """
    if mode == 'img_arr':
        # The following commented line will give an error if constant_values is an RGB tuple.
        # return np.pad(
        #     image, ((0, 0), (pad_width, pad_width), (pad_width, pad_width), (0, 0)), constant_values=values)
        return np.array([cv2.copyMakeBorder(
            img, pad_width, pad_width, pad_width, pad_width, cv2.BORDER_CONSTANT, value=values)
            for img in image])
    if mode == 'img':
        return cv2.copyMakeBorder(
            image, pad_width, pad_width, pad_width, pad_width, cv2.BORDER_CONSTANT, value=values)


if __name__ == '__main__':
    tiger = cv2.imread('tiger.jpeg')
    tiger_arr_test = np.array([tiger for i in range(1000)])
    pad_width = 5
    color = (255, 0, 0)
    padded_arr = pad_image(tiger_arr_test, pad_width, color)
    plt.imshow(padded_arr[0])
    plt.title(f'First padded image of the array of images.')
    plt.show()
    padded_single = pad_image(tiger, pad_width, color, 'img')
    print(padded_single.shape)
    plt.imshow(padded_single)
    plt.title('Padded single image.')
    plt.show()

1 个答案:

答案 0 :(得分:1)

这是一种无需使用CrimeListFragment

的方法
import React, { Component } from 'react';
import { TouchableOpacity,StyleSheet, Text, View, Image } from 'react-native';
import { withNavigation } from 'react-navigation';


 class HomeScreen extends React.Component {    
 render() {                  
    const {navigation} = this.props;
  return (          
      <View style={styles.MasterContainer}>
          <NavBar navigation/>
          <UserBar/>
          <View style={{height: 40,}}></View>
          <ButtonTab/>
          <Admob/>
          <TapBar/>
      </View>
  );
 }
 }

 class NavBar extends React.Component {
 render(){                
    return(
        <View style={styles.NavBar}>
            <TouchableOpacity>
                <Text style={{fontWeight: 'bold',fontSize: 18, color: 'white'}}     
 onPress={() => navigation.navigate('NavPg')}>더보기</Text>
            </TouchableOpacity>
        </View>
    )
 }
}

使用原始形状制作边框颜色数组

class CustomEditText(context: Context, attrs: AttributeSet) : AppCompatEditText(context, attrs) {
...
}

然后只需将原始数组分配到新数组的中间

numpy.pad

使用import cv2 import numpy as np tiger = cv2.imread('tiger.jpg') tigers = np.array((tiger,tiger,tiger,tiger,tiger)) color = np.array((255, 0, 0)) pad = 5 sub = slice(pad,-pad) -用零填充,然后将颜色分配给四个边缘

z,a,b,c = tigers.shape
newshape = (z, pad+a+pad, pad+b+pad, c)
w = np.zeros(newshape,dtype=np.uint8) + color

w[:,sub,sub,:] = tigers plt.imshow(w[3]) plt.show() plt.close() 并使用布尔掩码分配颜色

numpy.pad
相关问题