Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
cuttlefish
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
squidworks
cuttlefish
Commits
2a9d5c26
Commit
2a9d5c26
authored
5 years ago
by
Jake Read
Browse files
Options
Downloads
Patches
Plain Diff
rudimentary
parent
d8d28dc0
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
hunks/adhoc/correlate.js
+88
-27
88 additions, 27 deletions
hunks/adhoc/correlate.js
with
88 additions
and
27 deletions
hunks/adhoc/correlate.js
+
88
−
27
View file @
2a9d5c26
...
...
@@ -15,22 +15,47 @@ let computedResult = null
let
running
=
false
let
correlate
=
(
a
,
b
,
x
,
y
)
=>
{
// score a against b,
// score a against b, starting b from x, y
// a, b are float64 arrays, pixel-wise
let
sumA
=
0
let
sumB
=
0
let
sumDot
=
0
let
bi
=
0
// i will be index within the flat-af array under a,
for
(
let
i
=
0
;
i
<
a
.
length
;
i
++
){
for
(
let
j
=
0
;
j
<
a
[
0
].
length
;
j
++
){
sumA
+=
a
[
i
][
j
]
sumB
+=
b
[
i
+
x
][
j
+
y
]
sumDot
+=
a
[
i
][
j
]
*
b
[
i
+
x
][
j
+
y
]
}
}
return
sumDot
/
Math
.
sqrt
(
sumA
*
sumA
*
sumB
*
sumB
)
/*
for(let i = 0; i < a.data.length; i ++){
sumA += a.data[i]
// something is up w/ this feer sure
bi
=
(
y
*
b
.
width
+
x
)
*
4
bi
+=
Math
.
floor
(
i
/
(
a
.
width
*
4
))
*
(
b
.
width
*
4
)
+
i
// to find the reciprocal pixel in b,
// we can start at the top-left corner: here is the 'i_th' element
// within b, correspondign to the XY of the top-left of a,
bi = (y * b.width * 4) + x * 4
// to get down from here, the row of a this i is in:
let rca = Math.floor(i / a.width * 4)
bi += rca * b.width * 4
// and the increment,
bi += (i % (b.width * 4))
// ok ?
if(bi >= b.data.length){
bi = b.data.length
// happens all the time, go home do this tomorrow
console.log('yep', bi, rca, i, x, y)
//throw new Error('halting')
}
sumB += b.data[bi]
sumDot += a.data[i] * b.data[bi]
}
return
(
sumDot
/
Math
.
sqrt
(
sumA
*
sumA
+
sumB
*
sumB
))
*/
//return 55 //(sumDot / Math.sqrt(sumA*sumA + sumB*sumB))
// a, b are str8 up matricies
//console.log(b.width)
// ok, now we can do the maths on a and b, thx
...
...
@@ -44,6 +69,23 @@ let delay = (time) => {
})
}
let
packAndGrayscaleImage
=
(
imgD
)
=>
{
// make an md array shaped like[x][y]
let
arr
=
[]
for
(
let
x
=
0
;
x
<
imgD
.
width
;
x
++
){
arr
.
push
(
new
Float64Array
(
imgD
.
height
))
}
let
p
=
0
for
(
let
y
=
0
;
y
<
imgD
.
height
;
y
++
){
for
(
let
x
=
0
;
x
<
imgD
.
width
;
x
++
){
// grayscale all values as we load them up:
arr
[
x
][
y
]
=
(
imgD
.
data
[
p
]
+
imgD
.
data
[
p
+
1
]
+
imgD
.
data
[
p
+
2
]
+
imgD
.
data
[
p
+
3
])
/
4
;
p
+=
4
;
}
}
return
arr
}
async
function
run
(
a
,
b
){
running
=
true
// a is img to search for, b is img to search within. both are ImageData types
...
...
@@ -51,35 +93,54 @@ async function run(a, b){
let
resY
=
b
.
height
-
a
.
height
let
numruns
=
resX
*
resY
console
.
log
(
'
numruns
'
,
numruns
)
// the move now is to make an md array of these values,
let
bArr
=
packAndGrayscaleImage
(
b
)
let
aArr
=
packAndGrayscaleImage
(
a
)
// ok, results array like[x][y]
let
result
=
[]
// so, let's see about this ... we
for
(
let
x
=
0
;
x
<
b
.
width
-
a
.
width
;
x
++
){
for
(
let
y
=
0
;
y
<
b
.
height
-
a
.
height
;
y
++
){
// best to use b in-place,
let
corr
=
correlate
(
a
,
b
,
x
,
y
)
// four channels ...
result
.
push
(
corr
,
corr
,
corr
,
corr
)
}
// not blocking ...
await
delay
(
0
)
}
console
.
log
(
'
complete
'
)
for
(
let
x
=
0
;
x
<
resX
;
x
++
){
result
.
push
(
new
Float64Array
(
resY
))
}
// now fill,
for
(
let
x
=
0
;
x
<
resX
;
x
++
){
for
(
let
y
=
0
;
y
<
resY
;
y
++
){
result
[
x
][
y
]
=
correlate
(
aArr
,
bArr
,
x
,
y
)
}
await
delay
(
0
)
// to avoid blocking,
}
console
.
log
(
'
run complete
'
)
// make image from the result,
let
max
=
-
Infinity
let
min
=
Infinity
for
(
let
i
=
0
;
i
<
result
.
length
;
i
++
){
if
(
result
[
i
]
>
max
)
max
=
result
[
i
];
if
(
result
[
i
]
<
min
)
min
=
result
[
i
];
let
mp
=
{
x
:
0
,
y
:
0
}
for
(
let
x
=
0
;
x
<
resX
;
x
++
){
for
(
let
y
=
0
;
y
<
resY
;
y
++
){
if
(
result
[
x
][
y
]
>
max
){
max
=
result
[
x
][
y
]
mp
.
x
=
x
mp
.
y
=
y
}
if
(
result
[
x
][
y
]
<
min
)
min
=
result
[
x
][
y
]
}
for
(
let
i
=
0
;
i
<
result
.
length
;
i
++
){
result
[
i
]
=
(
result
[
i
]
-
min
)
*
(
255
/
(
max
-
min
))
}
console
.
log
(
'
max, min
'
,
max
,
min
)
console
.
log
(
'
result
'
,
result
)
let
u8
=
Uint8ClampedArray
.
from
(
result
)
console
.
log
(
'
u8
'
,
u8
)
console
.
log
(
'
resX, resY
'
,
resX
,
resY
)
let
imgRes
=
new
ImageData
(
u8
,
resX
,
resY
)
console
.
log
(
'
mp
'
,
mp
)
// now we want to unwrap this into an imagedata type,
// filling back in grayscale type
let
imdBuffer
=
new
Uint8ClampedArray
(
resX
*
resY
*
4
)
let
lc
=
0
let
dt
=
0
for
(
let
y
=
0
;
y
<
resY
;
y
++
){
for
(
let
x
=
0
;
x
<
resX
;
x
++
){
// stretch to full range,
dt
=
(
result
[
x
][
y
]
-
min
)
*
(
255
/
(
max
-
min
))
imdBuffer
[
lc
++
]
=
dt
imdBuffer
[
lc
++
]
=
dt
imdBuffer
[
lc
++
]
=
dt
imdBuffer
[
lc
++
]
=
dt
}
}
let
imgRes
=
new
ImageData
(
imdBuffer
,
resX
,
resY
)
console
.
log
(
'
imgRes
'
,
imgRes
)
running
=
false
computedResult
=
imgRes
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment