-
Notifications
You must be signed in to change notification settings - Fork 123
add primitives for vertical- and horizontal-symmetry #1862
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
schanzer
wants to merge
9
commits into
horizon
Choose a base branch
from
image-symmetry
base: horizon
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
9a467e4
add primitives for vertical- and horizontal-symmetry (needed in the l…
schanzer 81cb2ab
expose new primitives
schanzer d67b439
Revert "add primitives for vertical- and horizontal-symmetry (needed …
schanzer a33f7c7
re-do the changes without all the whitespace bullshit
schanzer df7657c
rename primitives
schanzer 912d8a6
fix function names for test suite
schanzer a8e058d
integrate improvements from @blerner
schanzer 616f18c
attempting to implement @blerner s suggestion
schanzer 65d19a8
Emmanuel learns how the annotation system works
schanzer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1759,6 +1759,52 @@ | |
|
|
||
| LineImage.prototype = heir(BaseImage.prototype); | ||
|
|
||
| var verticalSymmetry = function(img) { | ||
| var width = img.getWidth(); | ||
| var height = img.getHeight(); | ||
| var halfW = Math.floor(width / 2); | ||
| if (halfW === 0 || height === 0) { return 1; } | ||
| var canvas = makeCanvas(width, height); | ||
| img.render(canvas.getContext("2d")); | ||
| var data = canvas.getContext("2d").getImageData(0, 0, width, height).data; | ||
| var sumSq = 0, count = 0; | ||
| for (var y = 0; y < height; y++) { | ||
| for (var x = 0; x < halfW; x++) { | ||
| var i1 = (y * width + x) * 4; | ||
| var i2 = (y * width + (width - 1 - x)) * 4; | ||
| for (var ch = 0; ch < 4; ch++) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the actual similarity formula being used in these two functions? I think, if I'm understanding correctly, that it's the root mean-squared error over every pixel and every color channel? Probably worth a clarifying comment somewhere... |
||
| var d = data[i1 + ch] - data[i2 + ch]; | ||
| sumSq += d * d; | ||
| } | ||
| count += 4; | ||
| } | ||
| } | ||
| return 1 - Math.sqrt(sumSq / count) / 255; | ||
| }; | ||
|
|
||
| var horizontalSymmetry = function(img) { | ||
| var width = img.getWidth(); | ||
| var height = img.getHeight(); | ||
| var halfH = Math.floor(height / 2); | ||
| if (width === 0 || halfH === 0) { return 1; } | ||
| var canvas = makeCanvas(width, height); | ||
| img.render(canvas.getContext("2d")); | ||
| var data = canvas.getContext("2d").getImageData(0, 0, width, height).data; | ||
| var sumSq = 0, count = 0; | ||
| for (var y = 0; y < halfH; y++) { | ||
| for (var x = 0; x < width; x++) { | ||
| var i1 = (y * width + x) * 4; | ||
| var i2 = ((height - 1 - y) * width + x) * 4; | ||
| for (var ch = 0; ch < 4; ch++) { | ||
| var d = data[i1 + ch] - data[i2 + ch]; | ||
| sumSq += d * d; | ||
| } | ||
| count += 4; | ||
| } | ||
| } | ||
| return 1 - Math.sqrt(sumSq / count) / 255; | ||
| }; | ||
|
|
||
| var colorAtPosition = function(img, x, y) { | ||
| var width = img.getWidth(), | ||
| height = img.getHeight(), | ||
|
|
@@ -2038,6 +2084,9 @@ | |
| colorBlue: colorBlue, | ||
| colorAlpha: colorAlpha, | ||
| colorString: colorString, | ||
|
|
||
| verticalSymmetry: verticalSymmetry, | ||
| horizontalSymmetry: horizontalSymmetry, | ||
| } | ||
| ); | ||
| return RUNTIME.makeJSModuleReturn(); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I hate magic formulas like these :( Though I do understand it, I kinda wish we had
And then you'd have
var d = getPixel(data, x, y, ch) - getPixel(data, width - 1 - x, y, ch), which to me reads a lot more cleanly. OTOH, you're writing this in raw JS because of performance, so you could doand then have
which saves two whole multiplications per loop ;-)