Skip to content
Snippets Groups Projects
Commit ca44fec2 authored by Olivier Bertrand's avatar Olivier Bertrand
Browse files

Move scripts in a separated folder for clarity

parent 20d18048
No related branches found
No related tags found
No related merge requests found
"""
Tools to generate patterns
"""
import numpy as np
def generate_1overf_noise(img_size, beta, noise=None):
"""
Generates the discrete fourier transformation of noise and weights it with weights<0.
Where the smallest weights are in the middle columns of the returned noise array and the biggest in the outer columns.
First generates the noise and applies the following formula:
.. math::
DFT(\\frac{1}{f^{\\beta}} * DFT(noise))
The noise must be a 2d array.
:params img_size: size of the output noise dimensions \
(array dimension e.g. (4,4))
:params beta: used to calculate the weights of the noise, \
the bigger beta the smaller the weights, can have an arbitrary value \
(weights are always between 0 and 1)
:params noise: array with same size as img_size, that is used as \
the noise (default: random generated noise)
"""
# Calculate image size -> next biggest power of two,
# in which the image fits (s>=max(img_size))
s = 2**(np.ceil(np.log2(np.max(img_size)))).astype(int)
if noise is None:
# generate white noise
noise = np.random.randn(s, s)
# calculate filter
fx = np.arange(s/2)+1
# index from 0-s/2,s/2-0 -> middle highest index
fx = np.hstack([fx, fx[-1::-1]])
fx = fx[:, np.newaxis]
# copy first row s times -> fx=[[0..0],[1..],..,[s/2,..s/2],..[0..0]
# fx is a symmetric matrix
fx = fx.repeat(s, axis=1)
fy = fx.transpose()
f = np.sqrt(fx**2 + fy**2) # Euclidian norm
f = f**(-beta)
# apply filter in frequency domain
# calculate disrete furier transformations
fnoise = np.fft.ifft2(np.fft.fft2(noise)*f)
# trim to image size
fnoise = fnoise[:img_size[0], :img_size[1]]
fnoise = np.real(fnoise)
return fnoise
def gray2red(img):
""" convert a gray image to a red image (black -> red)
Many bees and flies are not sensitive to red light, and
can be better observed on red background than on a black one
due to their dark colour.
:params img: Gray image to be converted into red-white one
"""
img = img[..., np.newaxis]
img = img.repeat(3, axis=2)
maxval = np.max(img)
img[:, :, 1] = maxval-img[:, :, 0]
img[:, :, 2] = maxval-img[:, :, 0]
img[:, :, 0] = maxval
return img
def norm_img(img):
""" Normalise an 8bit image between 0 and 255
:params img: Image to be normalised
"""
img = img.astype(np.float)
img -= img.min()
img /= img.max()
img *= 255
return img.astype(np.uint8)
def rectangular_pattern(width, length, beta=1.4, pixel_per_mm=1):
"""generate a rectangular pattern
:param width: width of the pattern in mm
:param length: length of the pattern in mm
:param beta: beta coef for generating a 1/(f^beta) pattern
:param pixel_per_mm: number of pixel per mm
:returns: a rectangular random image
:rtype: np.ndarray
"""
corridor = np.array([width, length])
corridor_px = corridor*pixel_per_mm # in px
return generate_1overf_noise(corridor_px, beta)
......@@ -84,7 +84,7 @@ def matrix(quaternion):
[1.0 - q[2, 2] - q[3, 3], q[1, 2] - q[3, 0], q[1, 3] + q[2, 0], 0.0],
[ q[1, 2] + q[3, 0], 1.0 - q[1, 1] - q[3, 3], q[2, 3] - q[1, 0], 0.0],
[ q[1, 3] - q[2, 0], q[2, 3] + q[1, 0], 1.0 - q[1, 1] - q[2, 2], 0.0],
[ 0.0, 0.0, 0.0, 1.0]])
[0.0, 0.0, 0.0, 1.0]])
def from_matrix(matrix, isprecise=False):
......
File moved
File moved
File moved
......@@ -15,6 +15,7 @@ import pandas as pd
import yaml # Used to load config files
import pkg_resources
from navipy.maths.homogeneous_transformations import compose_matrix
from navipy.maths.quaternion import matrix as quatmatrix
import navipy.maths.constants as constants
from navipy.tools.trajectory import Trajectory
from PIL import Image
......@@ -208,6 +209,7 @@ class BlenderRender(AbstractRender):
..todo check that TemporaryDirectory is writtable and readable
"""
super(BlenderRender, self).__init__()
self._renderaxis = '+x'
# Rendering engine needs to be Cycles to support panoramic
# equirectangular camera
bpy.context.scene.render.engine = 'CYCLES'
......@@ -546,10 +548,14 @@ class BlenderRender(AbstractRender):
raise TypeError(
'posorient must be of type array, list, or pandas Series')
# Render
self.camera.matrix_world = compose_matrix(
angles=posorient.loc[convention],
rotmat = compose_matrix(
angles=[0, 0, 0], # posorient.loc[convention],
translate=posorient.loc['location'],
axes=convention)
if self._renderaxis == '+x':
initrot = quatmatrix([0.5, 0.5, -0.5, -0.5])
rotmat[:3, :3] = rotmat[:3, :3].dot(initrot[:3, :3])
self.camera.matrix_world = rotmat
bpy.ops.render.render()
def scene(self, posorient):
......
......@@ -57,11 +57,11 @@ setup_dict = {'name': 'navipy',
'include_package_data': True,
'entry_points': {
'console_scripts': [
'blendnavipy=navipy.sensors.blendnavipy:main',
'blendunittest=navipy.sensors.blendunittest:main',
'blendongrid=navipy.sensors.blend_ongrid:main',
'blendoverlaytraj=navipy.sensors.blend_overlaytraj:main',
'blendalongtraj=navipy.sensors.blend_alongtraj:main'
'blendnavipy=navipy.scripts.blendnavipy:main',
'blendunittest=navipy.scripts.blendunittest:main',
'blendongrid=navipy.scripts.blend_ongrid:main',
'blendoverlaytraj=navipy.scripts.blend_overlaytraj:main',
'blendalongtraj=navipy.scripts.blend_alongtraj:main'
]},
}
......
{
"cells": [],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 2
}
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment