Skip to content

Commit

Permalink
add a derivation function in python
Browse files Browse the repository at this point in the history
  • Loading branch information
hearues-zueke-github committed Jun 6, 2018
1 parent 84ea065 commit 1ebaab9
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 10 deletions.
21 changes: 11 additions & 10 deletions c_programs/test_global_local_variables.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,26 @@

unsigned char l[3] = {1, 2, 3};

void g() {
printf("outside g: l: %d, %d, %d\n", l[0], l[1], l[2]);
void g0() {
printf("g0: outside l: %d, %d, %d\n", l[0], l[1], l[2]);
unsigned char l[3] = {2, 3, 4};
void g1() {
printf("outside g1: l: %d, %d, %d\n", l[0], l[1], l[2]);
unsigned char l[3] = {2, 3, 4};
printf("g1: outside l: %d, %d, %d\n", l[0], l[1], l[2]);
unsigned char l[3] = {3, 4, 6};
void g2() {
printf("outside g2: l: %d, %d, %d\n", l[0], l[1], l[2]);
unsigned char l[3] = {3, 4, 5};
printf("inside g2: l: %d, %d, %d\n", l[0], l[1], l[2]);
printf("g2: outside l: %d, %d, %d\n", l[0], l[1], l[2]);
unsigned char l[3] = {4, 5, 8};
printf("g2: inside l: %d, %d, %d\n", l[0], l[1], l[2]);
}
g2();
printf("inside g1: l: %d, %d, %d\n", l[0], l[1], l[2]);
printf("g1: inside l: %d, %d, %d\n", l[0], l[1], l[2]);
}
g1();
printf("inside g: l: %d, %d, %d\n", l[0], l[1], l[2]);
printf("g0: inside l: %d, %d, %d\n", l[0], l[1], l[2]);
}

int main(int argc, char* argv[]) {
g();
g0();

return 0;
}
61 changes: 61 additions & 0 deletions picture_manipulation/picture_derivations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#! /usr/bin/python3.5

import numpy as np

from PIL import Image

def padding_one_pixel(pix):
shape = pix
pix_1 = np.vstack((pix[0], pix, pix[-1])).T
pix_2 = np.vstack((pix_1[0], pix_1, pix_1[-1])).T
return pix_2

shape = (5, 6)
pix = np.random.randint(0, 10, shape)

print("pix:\n{}".format(pix))

part = pix[0:3, 0:3]
print("part:\n{}".format(part))

pix_pad = padding_one_pixel(pix)
print("pix_pad:\n{}".format(pix_pad))

idx_y, idx_x = np.array([(j, i) for j in range(0, 3) for i in range(0, 3)]).T

idx_y_row = np.zeros((0, 9)).astype(np.int)
idx_x_row = np.zeros((0, 9)).astype(np.int)
for j in range(0, shape[0]):
for i in range(0, shape[1]):
idx_y_row = np.vstack((idx_y_row, idx_y+j))
idx_x_row = np.vstack((idx_x_row, idx_x+i))

idx_y_row = idx_y_row.reshape((-1, ))
idx_x_row = idx_x_row.reshape((-1, ))

print("idx_y: {}".format(idx_y))
print("idx_x: {}".format(idx_x))
print("idx_y_row:\n{}".format(idx_y_row))
print("idx_x_row:\n{}".format(idx_x_row))
print("pix_pad[idx_y, idx_x]: {}".format(pix_pad[idx_y, idx_x]))
print("pix_pad[idx_y_row, idx_x_row]:\n{}".format(pix_pad[idx_y_row, idx_x_row]))
pix_deriv_table = pix_pad[idx_y_row, idx_x_row].reshape(shape[0]*shape[1], 3*3)
print("pix_deriv_table:\n{}".format(pix_deriv_table))

mask_sobel = np.zeros((3, 3)).astype(np.int)
mask_sobel[0] = -1
mask_sobel[2] = 1
mask_x = mask_sobel.copy().reshape((-1, ))
mask_y = mask_sobel.T.copy().reshape((-1, ))

print("mask_x: {}".format(mask_x))
print("mask_y: {}".format(mask_y))

pix_deriv_x = np.sum(pix_deriv_table*mask_x, axis=1).reshape(shape)
pix_deriv_y = np.sum(pix_deriv_table*mask_y, axis=1).reshape(shape)

print("pix_deriv_x: {}".format(pix_deriv_x))
print("pix_deriv_y: {}".format(pix_deriv_y))

if __name__ == "__main__":
print("Hello World!")
26 changes: 26 additions & 0 deletions test_programs/test_global_local_variables.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#! /usr/bin/python3.5

# -*- coding: utf-8 -*-

l = [1, 2, 3]
# globals()["l"] = [1, 2, 3]
def g0():
global l
print("g0: outside l: {}".format(l))
l = [2, 3, 4]
def g1():
global l
print("g1: outside l: {}".format(l))
l = [3, 4, 6]
def g2():
global l
print("g2: outside l: {}".format(l))
l = [4, 5, 8]
print("g2: inside l: {}".format(l))
g2()
print("g1: inside l: {}".format(l))
g1()
print("g0: inside l: {}".format(l))

if __name__ == "__main__":
g0()

0 comments on commit 1ebaab9

Please sign in to comment.