Python – Simulate talking enums and bitsflags


“id” of entry is rising 1 by 1 (seen at

def enum(*sequential):
 enums = dict(zip(sequential, range(len(sequential)) ))
return type('Enum', (), enums)


“id” of entry is rising exponentialto power of 2 to allow check for multiple bits

def bitflag(*sequential):
 bitflags = dict(zip(sequential, [2**i for i in range(len(sequential))] ))
 return type('Bitflag', (), bitflags)

python numpy quickies

Generate a 2 x 4 array of ints between 0 and 4, inclusive:

>>> np.random.randint(5, size=(2, 4))
array([[4, 0, 2, 1],
[3, 2, 2, 0]])


Evenly spaced values for an interval

Specfiy number of segments

numpy.linspace(startstopnum=50endpoint=Trueretstep=Falsenumpy.linspace reference

Return evenly spaced numbers over a specified interval.

May include stop value (endpoint = True)

Specify step interval size

numpy.arange([start], stop[, step], dtype=Nonenumpy.arange reference

Return evenly spaced values within a given interval.



Create Sample Positions for pixels

def genPixelSamples(width, height, numWidth, numHeight, midPoint = True):
“if midPoint = true, sample is positioned at pixel center, else in the corner”
“output with y in 0 and x in 1 of 3rd dimension”
output = np.zeros([numHeight, numWidth, 2])
output[:,:,0] = np.outer( np.linspace(0, height, numHeight, False), np.ones([numWidth]) );
output[:,:,1] = np.outer( np.ones([numHeight]), np.linspace(0, width, numWidth, False) );

if midPoint:
output[:,:] += 0.5 * output[1,1]

return output


Find object in image using the histograms

You want to detect some object (image B) in image A.

Therefore you can use the histogram, that is the color/grayscale distribution of the object’s image. Shorthand said, you have a look onto which values are characteristic for the object, which means, they are more probable to appear in the object’s image than in the rest of the image. Then you select the region in Image A which has most of these characteristic pixel values.

This is done the following way.
Calculate both histograms for image A and B.
By dividing the histogram of image B by the histogram of image A element wise, you get high values for those bins, that have a relative high count for image B in comparison to image A. Note that values, that do not appear in image A lead to infinity, but later on, these bins wont be used. But be careful, if you do more operations on the histogram, like smoothing.

The new “histogram” now contains something like likelihood values telling how probable an intensity value belongs to the object.
Now we map each pixel of image A to its likelihood and take the average over a region with size of image B.

High values in the modified image indicate a high probability that this region contains the object.

Here some Octave code:

function [idy, idx] = histMatch(A, B)
%HISTMATCH find Pattern B in A using Histogram

% hit: how to read images into matrices
%A = imread("img1.png");
%B = imread("imgobject.png");

%calculate histograms
hA = histc(A(:), [0:1:255]);
hB = histc(B(:), [0:1:255]);

%calculate division (max 1)
hC = hB./hA;
%set maximum 1 (not needed, are not accessed anyway)
hC = min(hC, 1);

%map values from A to hC
A2 = hC(A);

%create mask and convolute
mask = ones(size(B));
A3 = conv2(A2, mask, "same");

%determine coordinates of maximum 
%maximum per column with rowIndex
[colMax, rowIndices] = max(A3);
%maximum over the maxima
[max, colIndex] = max(colMax);

idx = colIndex;
idy = rowIndices(idx);