Yaohong

为了真相不惜被羞辱

How does warp Perspective work?

How does warp Perspective work?

1.warp perspective with cv2

if __name__ == "__main__":
    # coordinate: (y,x), left_top, right_rop, left_bottom, right_bottom
    src = np.float32([[20.0, 0.0], [20.0 ,315.0], [186.0, 17.2], [181.0, 299.0]])
    dst = np.float32([[0.0, 0.0],  [0.0,  315.0], [202.0, 7.0], [200.0, 306.0]])

    # load image
    warp_img = cv2.imread("./my_wide_angle_orig.jpg")
    warp_img = cv2.cvtColor(warp_img, cv2.COLOR_BGR2RGB)
    print("warp_img: ",warp_img.shape) # (638, 958, 3)
    width = int(warp_img.shape[1]/3)
    height = int(warp_img.shape[0]/3)
    warp_img  = cv2.resize(warp_img, (width,height), interpolation=cv2.INTER_LINEAR)
    print("warp_img.shape:",warp_img.shape) # (212, 319, 3)

    ## orig image
    plt.subplot(121),
    plt.title("warp_img")
    plt.imshow(warp_img)

    # cv2 warp perspective
    cv2_matrix = cv2.getPerspectiveTransform(src, dst)
    print("cv2_matrix:\n",cv2_matrix)
    cv2_fix_img = cv2.warpPerspective(warp_img, cv2_matrix, (width,height))
    plt.subplot(122),
    plt.title('cv2_fix_img')
    plt.imshow(cv2_fix_img) 
    plt.show()

2.Implement it in our way

Step1 calculate warp matrix:

my_warp_matrix reshape:
 [[ 1.13729359e+00 -8.24289989e-18 -2.27458717e+01]
 [-6.10786436e-02  9.69843448e-01  1.22157287e+00]
 [-3.44743207e-04 -7.38466280e-05  1.00000000e+00]]

Step2. use the warp matrix to warp perspective

codes are:


How to Read a Image in Python

How to Read a Image in Python

There are three ways to read a image, the codes is showing below.


image_path = "fasterRCNN/faster-rcnn-keras-master/img/street.jpg";

from PIL import Image
import numpy as np
image = Image.open(image_path) # RGB
tmp_image = np.array(image)
print("PIL open shape:",tmp_image.shape, tmp_image)


# load with cv2
import cv2
image = cv2.imread(image_path)  # mode: BGR
print("cv2 imread shape:", image.shape) # (width,height,channel)


# load with matplotlib
import matplotlib.image as mpimg
image = mpimg.imread(image_path) #  mode: RGB
print("matplotlib imread shape:",image.shape, image) # ( height, width,channel(RGB) )

If you use cv2 to load a image , then show it with matplotlib, you should convert the image to RGB mode, this is because cv2 read a image in BGR mode, while matplotlib presents the image in RGB mode;


Differences On Numpyp.Floor And Python Int method.md

Differences On Numpyp.Floor And Python Int method.md

numpy.floor Return the floor of the input.

The floor of the scalar x is the largest integer i, such that i <= x;

np.floor() will not change its data type;

Example:

import numpy as np

tmp_list = list([-3.3, -2.22, -1.56, -0.56, 0.56, 1.56, 2.22, 3.33])

tmp_list = np.array(tmp_list)
print("type(tmp_list[0]):", type(tmp_list[0]) )
print("type(np.floor(tmp_list)[0]):",  type(np.floor(tmp_list)[0]) )
print("np.floor(tmp_list):", np.floor(tmp_list))

# Output:
# type(tmp_list[0]): <class 'numpy.float64'>
# type(np.floor(tmp_list)[0]): <class 'numpy.float64'>
# np.floor(tmp_list): [-4. -3. -2. -1.  0.  1.  2.  3.]


for x in tmp_list:
	print("x:",x,",int:",int(x)) 

# output:
# x: -3.3 ,int: -3
# x: -2.22 ,int: -2
# x: -1.56 ,int: -1
# x: -0.56 ,int: 0
# x: 0.56 ,int: 0
# x: 1.56 ,int: 1
# x: 2.22 ,int: 2
# x: 3.33 ,int: 3

For floating point numbers, int() will truncates toward zero, so -0.1 will by truncate to 0, -1.8 to -1; And its data type will be changed to <class 'int'>.


Averaging histograms

Averaging histograms

An image histogram is the number of each pixel value, which is displayed in the graph.

x axis of the graph is pixel value, range from 0 to 255;

y axis of the graph is the number of this pixel value;

An example of an standard image together with its luminance and RGB histograms

1.How to averaging histograms?

Our goal is to generate a new image with a more even histogram distribution.


How does numpy add two arrays with different shapes?

How does numpy add two arrays with different shapes?

Numpy has a add method which add two numpy array.

Arithmetic operation + does the same thing as Numpy.add;

1.Add a same shapes array

Let’s see a example.

import numpy as np

list1 = np.array([1, 2, 3]);
list2= np.array([10, 20, 30]);

print("list1:",list1,"list2:",list2);
# Print: list1: [1 2 3] list2: [10 20 30]

added_list = list1 + list2;
print("added_list.shape:",added_list.shape,"\nadded_list:",added_list);
# Print:
# added_list.shape: (3,) 
# added_list: [11 22 33]

added_list = np.add(list1, list2);
print("added_list.shape:",added_list.shape,"\nadded_list:",added_list);
# Print:
# added_list.shape: (3,) 
# added_list: [11 22 33]

2.Add a different shape array

But what happen if two array have different shapes?


Understanding Transpose

Understanding Numpy Transpose

1.Transpose is to switch the row and column indices of the matrix A;

x = np.arange(8).reshape((4,2))
print(x)
print(x.T)
# output:
# [[0 1] # x
#  [2 3]
#  [4 5]
#  [6 7]]
# [[0 2 4 6] # x.T
#  [1 3 5 7]]

x = np.arange(9).reshape((3,3))
print(x)
print(x.T)
print(x.shape, x.T.shape)
# output:
# [[0 1 2]
#  [3 4 5]
#  [6 7 8]]
# [[0 3 6]
#  [1 4 7]
#  [2 5 8]]

x = np.arange(8).reshape((2,4))
print(x)
print(x.T)
# output:
# [[0 1 2 3]
#  [4 5 6 7]]
# [[0 4]
#  [1 5]
#  [2 6]
#  [3 7]]

If the array has only one dimension, the transpose of the array will not change;


What do `*args` and `**kwargs` mean in python function?

What do *args and **kwargs mean in python function?

*args means we can pass an arbitrary number of arguments to the function;

Similarly, **kwargs allow we pass many key=value argument to the function;

*args

*args iterable:

def my_sum(*args):
    result = 0
    # Iterating over the Python args tuple
    for x in args:
        result += x
    return result

print(my_sum(1, 2, 3))
# output: 6

The * is a unpacking operator;

def print_three_things(a, b, c):
    print( 'a = {0}, b = {1}, c = {2}'.format(a,b,c))

mylist = ['aardvark', 'baboon', 'cat']
print_three_things(*mylist)
# output: a = aardvark, b = baboon, c = cat

**kwargs example

**kwargs iterable:


How Keras add two layers?

How Keras add two layers?

tf.keras.layers.add() method can add two layer?

What it do is sum the values of corresponding positions in two layers.

For example:


input_shape = (1,2,3)
import tensorflow as tf
tf.enable_eager_execution()

print("----------x1 tensor-----------")
x1 = tf.random.uniform(input_shape, maxval=10, dtype=tf.dtypes.int32)
tf.print(x1);
print("----------x2 tensor-----------")
x2 = tf.random.uniform(input_shape, maxval=10, dtype=tf.dtypes.int32)
tf.print(x2);
print("----------add 2 tensors-----------")
y = tf.keras.layers.add([x1,x2])
tf.print(y);

Output: ———-x1 tensor———– [[[7 6 1] [5 7 2]]] ———-x2 tensor———– [[[0 7 8] [2 9 6]]] ———-add 2 tensors———– [[[7 13 9] [7 16 8]]]


Understanding Numpy expand_dims

Inserting 1 into the shape brackets base on the axis value

Understanding Numpy expand_dims

Shape (n,)(n is a number) means it has only one dimension.

The number of values is shape brackets represents the number of dimensions.

import numpy as np
arr = np.array([1,2,3,4,5]);
print("arr shape: ",arr.shape)
print("arr shape: ",arr)
arr2 = np.expand_dims(arr, 0);
print("expand_dims axis=0, shape:",arr2.shape)
print("expand_dims axis=0, arr2:",arr2)
arr2 = np.expand_dims(arr, 1);
print("expand_dims axis=1, shape:",arr2.shape)
print("expand_dims axis=1, arr2:",arr2)

output:

arr shape:  (5,) 
arr shape:  [1 2 3 4 5]
expand_dims axis=0, shape: (1, 5)
expand_dims axis=0, arr2: [[1 2 3 4 5]]
expand_dims axis=1, shape: (5, 1)
expand_dims axis=1, arr2: [[1]
 [2]
 [3]
 [4]
 [5]]

numpy.expand_dims

expand_dims looks like inserting 1 into the shape brackets base on the axis value;


Sed

Sed

sed is a steam editor. sed treats multiple input files as one long stream.

The full format for invoking sed is:

sed OPTIONS... [SCRIPT] [INPUTFILE...]

Some common OPTIONS:

-n to suppress output, sed -n '45p' file.txt this command prints only line 45 of input file;

-e options are used to specify a script expression, such as sed -e 's/hello/world/' input.txt > output.txt;

-f specify a script file, such as sed -f myscript.sed input.txt > output.txt