Skip to content

Instantly share code, notes, and snippets.

@Huud
Huud / Calculate_Mean_and_std_of_dataset.py
Created July 23, 2020 14:18
How to calculate the mean and standard deviation of an image dataset
# calculate mean and std deviation
from pathlib import Path
import cv2
imageFilesDir = Path(r'C:\your\dataset\dir\here\trainData')
files = list(imageFilesDir.rglob('*.png'))
# Since the std can't be calculated by simply finding it for each image and averaging like
# the mean can be, to get the std we first calculate the overall mean in a first run then
@Huud
Huud / Recompiler_Vs_Interpreter.cpp
Created July 19, 2020 13:34
An esample code that generates C++ code and compiles it to a DLL file then uses it.
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <fstream>
#include <random>
#include <cmath>
#include <string>
#include <chrono>
#include <vector>
#include <windows.h>
@Huud
Huud / Generate Normal Map From Height Map.py
Last active May 7, 2024 17:55
Generate Normal Map From Height Map
# A fast - numpy based - CPU functions that take a height map and generate a normal map from it
import numpy as np
import matplotlib.image as mpimg
# a function that takes a vector - three numbers - and normalize it, i.e make it's length = 1
def normalizeRGB(vec):
length = np.sqrt(vec[:,:,0]**2 + vec[:,:,1]**2 + vec[:,:,2]**2)
vec[:,:,0] = vec[:,:,0] / length
vec[:,:,1] = vec[:,:,1] / length
@Huud
Huud / "I Can't Sleep" Effect.py
Last active February 26, 2020 14:28
A Python script that takes a B&W image and generates an illusion where lines bending create that image, it's based on a viral image that reads "I Can't Sleep", examples: https://imgur.com/a/1NT0BIe
from PIL import *
# from IPython.display import display # uncomment if you are using Jupyter
im = Image.open(r"C:\Put_your_B&W_img_here.jpg")
outIm = Image.new('RGB', im.size, color = 'white')
draw = ImageDraw.Draw(outIm)
gridSpacing = 5
lineThickness = int(gridSpacing/2)
for i in range(0,im.size[0],gridSpacing):