This notebook uses the Accord library for math, statistics, vision, and machine learning. It should be split up into many notebooks, but this is currently for testing.

In [1]:
from System import Array

import Accord
import Accord.Math
import clr
clr.ImportExtensions(Accord.Math)

In [2]:
Accord.Math.Matrix.Indices(0, 10)

Out[2]:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
In [4]:
from Accord.Math import Matrix
v = Matrix.Identity(3)
v

Out[4]:
[[1, 0, 0],
[0, 1, 0],
[0, 0, 1]]
In [5]:
I = Array.CreateInstance(float, 2, 2)
I[0,0] = 1
I

Out[5]:
[[1, 0],
[0, 0]]
In [6]:
u = Array[float]([1, 6])
v = Array[float]([-2, 10])

In [7]:
u.InnerProduct(v)

Out[7]:
58
In [8]:
outer = u.OuterProduct(v)
outer

Out[8]:
[[-2, 10],
[-12, 60]]
In [9]:
I.Multiply(u)

Out[9]:
[1, 0]
In [10]:
I.Multiply(v)

Out[10]:
[-2, 0]
In [11]:
from Accord.Math.Decompositions import SingularValueDecomposition

In [12]:
i = SingularValueDecomposition(I)

i.Diagonal

Out[12]:
[1, 0]

# Principal Component Analysis Example¶

In [16]:
from Processing import *
size(500, 500)
background(0)
smooth()

In [17]:
points = []

In [18]:
def drawPoint():
fill(255, 0, 0)
noStroke()
points.append([mouseX(), mouseY()])
ellipse(mouseX(), mouseY(), 5, 5)
onMouseDragged += drawPoint
loop()

In [19]:
def clear():
global points
points = []
background(0)
onKeyPressed += clear

In [29]:
points

Out[29]:
[[95.0, 249.0], [95.0, 249.0], [99.0, 249.0], [99.0, 249.0], [104.0, 247.0], [104.0, 247.0], [111.0, 242.0], [111.0, 242.0], [116.0, 238.0], [116.0, 238.0], [121.0, 234.0], [121.0, 234.0], [125.0, 232.0], [125.0, 232.0], [130.0, 228.0], [130.0, 228.0], [137.0, 224.0], [137.0, 224.0], [146.0, 220.0], [146.0, 220.0], [151.0, 215.0], [151.0, 215.0], [156.0, 211.0], [156.0, 211.0], [162.0, 207.0], [162.0, 207.0], [173.0, 201.0], [173.0, 201.0], [186.0, 193.0], [186.0, 193.0], [195.0, 187.0], [195.0, 187.0], [203.0, 181.0], [203.0, 181.0], [213.0, 176.0], [213.0, 176.0], [224.0, 171.0], [224.0, 171.0], [237.0, 162.0], [237.0, 162.0], [251.0, 153.0], [251.0, 153.0], [261.0, 150.0], [261.0, 150.0], [269.0, 144.0], [269.0, 144.0], [277.0, 140.0], [277.0, 140.0], [287.0, 135.0], [287.0, 135.0], [297.0, 129.0], [297.0, 129.0], [304.0, 125.0], [304.0, 125.0], [308.0, 123.0], [308.0, 123.0], [318.0, 120.0], [318.0, 120.0], [326.0, 120.0], [326.0, 120.0], [339.0, 120.0], [339.0, 120.0], [347.0, 120.0], [347.0, 120.0], [353.0, 120.0], [353.0, 120.0], [356.0, 120.0], [356.0, 120.0], [358.0, 120.0], [358.0, 120.0], [361.0, 120.0], [361.0, 120.0], [363.0, 120.0], [363.0, 120.0], [363.0, 119.0], [363.0, 119.0], [178.0, 323.0], [178.0, 323.0], [180.0, 319.0], [180.0, 319.0], [185.0, 316.0], [185.0, 316.0], [190.0, 311.0], [190.0, 311.0], [195.0, 306.0], [195.0, 306.0], [202.0, 300.0], [202.0, 300.0], [212.0, 291.0], [212.0, 291.0], [222.0, 283.0], [222.0, 283.0], [236.0, 272.0], [236.0, 272.0], [248.0, 260.0], [248.0, 260.0], [257.0, 252.0], [257.0, 252.0], [267.0, 240.0], [267.0, 240.0], [283.0, 228.0], [283.0, 228.0], [299.0, 216.0], [299.0, 216.0], [309.0, 208.0], [309.0, 208.0], [318.0, 196.0], [318.0, 196.0], [325.0, 192.0], [325.0, 192.0], [331.0, 188.0], [331.0, 188.0], [336.0, 188.0], [336.0, 188.0], [341.0, 188.0], [341.0, 188.0], [344.0, 188.0], [344.0, 188.0], [348.0, 188.0], [348.0, 188.0], [353.0, 188.0], [353.0, 188.0], [357.0, 188.0], [357.0, 188.0], [361.0, 188.0], [361.0, 188.0], [361.0, 188.0], [361.0, 188.0], [359.0, 232.0], [359.0, 232.0], [359.0, 234.0], [359.0, 234.0], [359.0, 235.0], [359.0, 235.0], [360.0, 239.0], [360.0, 239.0], [363.0, 245.0], [363.0, 245.0], [365.0, 252.0], [365.0, 252.0], [368.0, 258.0], [368.0, 258.0], [370.0, 262.0], [370.0, 262.0], [373.0, 269.0], [373.0, 269.0], [375.0, 274.0], [375.0, 274.0], [375.0, 277.0], [375.0, 277.0], [376.0, 277.0], [376.0, 277.0], [376.0, 277.0], [376.0, 277.0], [379.0, 281.0], [379.0, 281.0], [381.0, 284.0], [381.0, 284.0], [383.0, 288.0], [383.0, 288.0], [383.0, 290.0], [383.0, 290.0], [384.0, 290.0], [384.0, 290.0], [385.0, 294.0], [385.0, 294.0], [385.0, 295.0], [385.0, 295.0], [385.0, 294.0], [385.0, 294.0], [385.0, 295.0], [385.0, 295.0], [402.0, 332.0], [402.0, 332.0], [404.0, 335.0], [404.0, 335.0], [405.0, 339.0], [405.0, 339.0], [407.0, 343.0], [407.0, 343.0], [411.0, 348.0], [411.0, 348.0], [416.0, 355.0], [416.0, 355.0], [419.0, 361.0], [419.0, 361.0], [423.0, 367.0], [423.0, 367.0], [427.0, 373.0], [427.0, 373.0], [429.0, 377.0], [429.0, 377.0], [431.0, 381.0], [431.0, 381.0], [433.0, 383.0], [433.0, 383.0], [433.0, 384.0], [433.0, 384.0], [433.0, 385.0], [433.0, 385.0], [434.0, 385.0], [434.0, 385.0], [434.0, 386.0], [434.0, 386.0], [434.0, 387.0], [434.0, 387.0], [435.0, 389.0], [435.0, 389.0], [435.0, 390.0], [435.0, 390.0], [435.0, 391.0], [435.0, 391.0], [435.0, 392.0], [435.0, 392.0], [461.0, 439.0], [461.0, 439.0], [461.0, 440.0], [461.0, 440.0], [461.0, 441.0], [461.0, 441.0], [462.0, 444.0], [462.0, 444.0], [465.0, 448.0], [465.0, 448.0], [466.0, 452.0], [466.0, 452.0], [468.0, 456.0], [468.0, 456.0], [471.0, 461.0], [471.0, 461.0], [474.0, 463.0], [474.0, 463.0], [476.0, 466.0], [476.0, 466.0], [478.0, 470.0], [478.0, 470.0], [480.0, 475.0], [480.0, 475.0], [482.0, 477.0], [482.0, 477.0], [482.0, 478.0], [482.0, 478.0], [482.0, 479.0], [482.0, 479.0], [482.0, 479.0], [482.0, 479.0]]
In [30]:
data = Array.CreateInstance(float, len(points), len(points[0]))
for i in range(len(points)):
for j in range(len(points[i])):
data[i,j] = points[i][j]
print("# of points: ", data.Length)

from Accord.Statistics.Analysis import *
analysis = DescriptiveAnalysis(data)
ux,uy = analysis.Means

pca = PrincipalComponentAnalysis(data, AnalysisMethod.Center)

# Compute the Principal Component Analysis
pca.Compute()

stroke(0, 255,0)
strokeWeight(5)
for pc in pca.Components:
x,y = pc.Eigenvector.Multiply(pc.Eigenvalue**0.5)
line(ux - x, uy - y, ux + x, uy + y)

# of points:  492

In [31]:
get()

Out[31]:
In [32]:
for v in analysis.CovarianceMatrix:
print(v)

11580.2068027
6510.53333333
6510.53333333
11027.7245728


# Accord Image Processing¶

In [2]:
from AForge.Imaging.Filters import *
from Accord.Imaging.Filters import *
from AForge.Imaging import Image
from Graphics import *

from System.Drawing import Bitmap
from System.Drawing.Imaging import PixelFormat

image = p.toBitmap() #Image.Clone(p.toBitmap(), PixelFormat.Format24bppRgb)
calico.display(p)
# Create a new Gabor filter
filter = GaborFilter()

# Apply the filter
output = filter.Apply(image)
calico.display(Picture(output))

In [3]:
filter = GaussianBlur( 4, 11 )
output = filter.Apply( image )
calico.display(Picture(image))
calico.display(Picture(output))