Skip to content

Commit

Permalink
more files
Browse files Browse the repository at this point in the history
  • Loading branch information
hearues-zueke-github committed Jun 18, 2018
1 parent c25d499 commit 70a926d
Show file tree
Hide file tree
Showing 7 changed files with 223 additions and 5 deletions.
14 changes: 14 additions & 0 deletions c_programs/test_currying.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <stdio.h>

int main(
int argc, char* argv[]) {
int (*f)(int a) {
int g(int b) {
return a+b;
}

return g;
}

return 0;
}
4 changes: 4 additions & 0 deletions cpp_programs/compile_programs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#! /bin/bash

g++ -std=c++11 -Wall test_currying.cpp -o test_currying
echo "Compiled 'test_currying.cpp'"
16 changes: 16 additions & 0 deletions cpp_programs/test_currying.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <iostream>

int main(int argc, char* argv[]) {
auto f = [](int a) {
auto g = [a](int b) {
return a+b;
};
return g;
};

auto g = f(4);

std::cout << "g(3): " << g(3) << std::endl;

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

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

import os

import numpy as np

from PIL import Image

if __name__ == "__main__":
from os.path import expanduser
home = expanduser("~")
print("home: {}".format(home))

path_images = home+"/Pictures/tiles_images/"

img = Image.open(path_images+"smm_smb3_tiles.png")
pix = np.array(img)

tw = 16 # tile width
th = 16 # tile height
tiles_x = 4
tiles_y = 3
pix_tiles = np.zeros((th*tiles_y, tw*tiles_x, 3), dtype=np.uint8)

# get all tiles per indices
choose_tiles = [[0, 1, 0, 0],
[0, 2, 0, 1],
[0, 4, 0, 2],
[5, 6, 0, 3],
[0, 17, 1, 0],
[0, 18, 1, 1],
[0, 20, 1, 2],
[5, 22, 1, 3],
[0, 33, 2, 0],
[0, 34, 2, 1],
[0, 36, 2, 2],
[5, 38, 2, 3]]

for ts_y, ts_x, t_y, t_x in choose_tiles:
pix_tiles[th*t_y:th*(t_y+1), tw*t_x:tw*(t_x+1)] = \
pix[(th+1)*ts_y+1:(th+1)*ts_y+th+1, (tw+1)*ts_x+1:(tw+1)*ts_x+tw+1]

img_tiles = Image.fromarray(pix_tiles)
img_tiles.show()

img_anime = Image.open(path_images+"anime_1.jpeg")
img_anime.show()

pix_anime = np.array(img_anime)
print("pix_anime.shape: {}".format(pix_anime.shape))
anim_h, anim_w, anim_c = pix_anime.shape

pix_anime = pix_anime[:(anim_h//th)*th, :(anim_w//tw)*tw]
print("pix_anime.shape: {}".format(pix_anime.shape))

# transform pix_tiles so that it will be like a
# (tiles_y, tiles_x, th, tw, 3) shape
x, y, z = pix_tiles.shape
print("pix_tiles.shape: {}".format(pix_tiles.shape))

pix_tiles_own = pix_tiles.transpose(0, 2, 1)
pix_tiles_own = pix_tiles_own.reshape((th, z*x//th, y))
pix_tiles_own = pix_tiles_own.transpose(0, 2, 1)
pix_tiles_own = pix_tiles_own.reshape((x*y//th//tw, th, tw, z))
pix_tiles_own = pix_tiles_own.transpose(0, 2, 1, 3)
pix_tiles_own = pix_tiles_own.reshape((x//th, y//tw, th, tw, z))

print("pix_tiles_own.shape: {}".format(pix_tiles_own.shape))
71 changes: 71 additions & 0 deletions puzzle_solver/Utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# bs...border_size
def get_derivatives_box(pix, pix_integral, bs, s=1):
# First do the x-derivative
# h...height
# w...width
h, w = pix.shape

deriv_x_right = pix_integral[bs-s :bs+h-s , bs+1 :bs+w+1] \
+pix_integral[bs+s+1:bs+h+s+1, bs+1+s:bs+w+1+s] \
-pix_integral[bs-s :bs+h-s , bs+1+s:bs+w+1+s] \
-pix_integral[bs+s+1:bs+h+s+1, bs+1 :bs+w+1]
deriv_x_left = pix_integral[bs-s :bs+h-s , bs-s :bs+w-s] \
+pix_integral[bs+s+1:bs+h+s+1, bs :bs+w] \
-pix_integral[bs-s :bs+h-s , bs :bs+w] \
-pix_integral[bs+s+1:bs+h+s+1, bs-s :bs+w-s]

deriv_y_right = pix_integral[bs+1 :bs+h+1 , bs-s :bs+w-s ] \
+pix_integral[bs+1+s:bs+h+1+s, bs+s+1:bs+w+s+1] \
-pix_integral[bs+1+s:bs+h+1+s, bs-s :bs+w-s ] \
-pix_integral[bs+1 :bs+h+1 , bs+s+1:bs+w+s+1]
deriv_y_left = pix_integral[bs-s :bs+h-s , bs-s :bs+w-s ] \
+pix_integral[bs :bs+h , bs+s+1:bs+w+s+1] \
-pix_integral[bs :bs+h , bs-s :bs+w-s ] \
-pix_integral[bs-s :bs+h-s , bs+s+1:bs+w+s+1]

deriv_x = deriv_x_right-deriv_x_left
deriv_y = deriv_y_right-deriv_y_left

print("Needed time for deriv other: {:.4f}".format(end_time-start_time))

return deriv_x, deriv_y

def get_derivatives_box_tiles(tile_h, tile_w, pix_integral, bs, s=1, offset=0):
# First do the x-derivative
# h...height
# w...width
h, w = tile_h, tile_w
# print("bs: {}".format(bs))
# print("pix.shape: {}".format(pix.shape))
# offset = 1
bs_l = bs-offset
bs_h = bs+offset

v_1 = pix_integral[:, bs_l-s :bs_h+h-s , bs_l+1 :bs_h+w+1]
v_2 = pix_integral[:, bs_l+s+1:bs_h+h+s+1, bs_l+1+s:bs_h+w+1+s]
v_3 = pix_integral[:, bs_l-s :bs_h+h-s , bs_l+1+s:bs_h+w+1+s]
v_4 = pix_integral[:, bs_l+s+1:bs_h+h+s+1, bs_l+1 :bs_h+w+1]
print("v_1.shape: {}".format(v_1.shape))
print("v_2.shape: {}".format(v_2.shape))
print("v_3.shape: {}".format(v_3.shape))
print("v_4.shape: {}".format(v_4.shape))
deriv_x_right = v_1+v_2-v_3-v_4
v_1 = pix_integral[:, bs_l-s :bs_h+h-s , bs_l-s :bs_h+w-s]
v_2 = pix_integral[:, bs_l+s+1:bs_h+h+s+1, bs_l :bs_h+w]
v_3 = pix_integral[:, bs_l-s :bs_h+h-s , bs_l :bs_h+w]
v_4 = pix_integral[:, bs_l+s+1:bs_h+h+s+1, bs_l-s :bs_h+w-s]
deriv_x_left = v_1+v_2-v_3-v_4

deriv_y_right = pix_integral[:, bs_l+1 :bs_h+h+1 , bs_l-s :bs_h+w-s ] \
+pix_integral[:, bs_l+1+s:bs_h+h+1+s, bs_l+s+1:bs_h+w+s+1] \
-pix_integral[:, bs_l+1+s:bs_h+h+1+s, bs_l-s :bs_h+w-s ] \
-pix_integral[:, bs_l+1 :bs_h+h+1 , bs_l+s+1:bs_h+w+s+1]
deriv_y_left = pix_integral[:, bs_l-s :bs_h+h-s , bs_l-s :bs_h+w-s ] \
+pix_integral[:, bs_l :bs_h+h , bs_l+s+1:bs_h+w+s+1] \
-pix_integral[:, bs_l :bs_h+h , bs_l-s :bs_h+w-s ] \
-pix_integral[:, bs_l-s :bs_h+h-s , bs_l+s+1:bs_h+w+s+1]

deriv_x = deriv_x_right-deriv_x_left
deriv_y = deriv_y_right-deriv_y_left

return deriv_x, deriv_y
38 changes: 33 additions & 5 deletions puzzle_solver/find_best_part_position_improved.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

from PIL import Image, ImageDraw, ImageFont

import Utils

img = Image.open("snow-mountains-1600x900-green-landscape-scenery-5k-5405.jpg")
img_resizes = img.resize((400, 225), Image.ANTIALIAS)
img_resizes.show()
Expand Down Expand Up @@ -65,18 +67,44 @@
pix_tiles_gray_int = pix_tiles_gray.copy().astype(np.uint8)
pix_tiles_gray_int[pix_tiles_gray >= 255.5] = 255

pix_tiles_integral = np.cumsum(np.cumsum(pix_tiles_gray_int.astype(np.float), axis=1), axis=2)
pix_tiles_integral = np.concatenate((np.zeros((x, 1, z+2*bs)).astype(np.float), pix_tiles_integral), axis=1)
pix_tiles_integral = np.concatenate((np.zeros((x, y+1+2*bs, 1)).astype(np.float), pix_tiles_integral), axis=2)
pix_tiles_integral_orig = np.cumsum(np.cumsum(pix_tiles_gray_int.astype(np.float), axis=1), axis=2)
pix_tiles_integral_orig = np.concatenate((np.zeros((x, 1, z+2*bs)).astype(np.float), pix_tiles_integral_orig), axis=1)
pix_tiles_integral_orig = np.concatenate((np.zeros((x, y+1+2*bs, 1)).astype(np.float), pix_tiles_integral_orig), axis=2)

pix_tiles_integral = pix_tiles_integral_orig.copy()
pix_tiles_integral -= np.min(pix_tiles_integral)
pix_tiles_integral /= np.max(pix_tiles_integral)
pix_tiles_integral *= 256
# pix_tiles_integral_int = pix_tiles_integral.copy().astype(np.uint8)
# pix_tiles_integral_int[pix_tiles_integral > 255.5] = 255
print("pix_tiles_integral[0, :10, :10]:\n{}".format(pix_tiles_integral[0, :10, :10]))
# print("pix_tiles_integral[0, :10, :10]:\n{}".format(pix_tiles_integral[0, :10, :10]))

# img_tiles_integral = Image.fromarray(pix_tiles_integral_int)
img_tiles_integral = Image.fromarray(pix_tiles_integral.reshape((x*(y+1+bs*2), z+1+bs*2)))
img_tiles_integral = Image.fromarray(pix_tiles_integral.reshape((x*(y+1+2*bs), z+1+2*bs)))
if img_tiles_integral.mode != 'RGB':
img_tiles_integral = img_tiles_integral.convert('RGB')
img_tiles_integral.save("tiles_integrals.png", "PNG")

# TODO: call the Utils get_derivatives_box but for many small pictures!
offset = 2
pix_tiles_deriv_x, pix_tiles_deriv_y = \
Utils.get_derivatives_box_tiles(tile_width, tile_width, pix_tiles_integral_orig, bs, s=1, offset=offset)
print("Got the derivatives with box tiles!")

def get_normalizes_uint8_pix(pix_orig):
pix = pix_orig.copy()
pix -= np.min(pix)
pix /= np.max(pix)
pix *= 255.99999
pix_int = pix.copy().astype(np.uint8)
return pix_int

img_tiles_deriv_x = Image.fromarray(get_normalizes_uint8_pix(pix_tiles_deriv_x.reshape((x*(y+offset*2), z+offset*2))))
img_tiles_deriv_y = Image.fromarray(get_normalizes_uint8_pix(pix_tiles_deriv_y.reshape((x*(y+offset*2), z+offset*2))))

img_tiles_deriv_x.save("tiles_deriv_x.png", "PNG")
img_tiles_deriv_y.save("tiles_deriv_y.png", "PNG")

# TODO: add 2nd derivative and get the keypoints for each tile!
# TODO: make a matrix with 2nd derive with shape (x, 3, tile, tile) for faster
# calculation of the Non-Maxima Supression
15 changes: 15 additions & 0 deletions test_programs/currying.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#! /usr/bin/python3.5

def f(a):
def g(b):
return a+b
return g

f1 = f(3)
f2 = f(6)

print("f1(4): {}".format(f1(4)))
print("f2(4): {}".format(f2(4)))

print("f(3)(4): {}".format(f(3)(4)))
print("f(6)(4): {}".format(f(6)(4)))

0 comments on commit 70a926d

Please sign in to comment.