-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add options to mean, median and variance (#471)
- Loading branch information
1 parent
ad9f91d
commit 37db7e3
Showing
8 changed files
with
337 additions
and
28 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 |
---|---|---|
@@ -1,19 +1,48 @@ | ||
import { Image } from '../Image'; | ||
import { Point } from '../geometry'; | ||
|
||
export interface MeanOptions { | ||
/** | ||
* Points to calculate mean from. | ||
*/ | ||
points: Point[]; | ||
} | ||
|
||
/** | ||
* Compute the mean of an image. The mean can be either computed on each channel | ||
* individually or on the whole image. | ||
* @param image - Image to process. | ||
* @param options - Mean options. | ||
* @returns The mean pixel. | ||
*/ | ||
export function mean(image: Image): number[] { | ||
const pixel = new Array<number>(image.channels).fill(0); | ||
for (let row = 0; row < image.height; row++) { | ||
for (let column = 0; column < image.width; column++) { | ||
export function mean(image: Image, options?: MeanOptions): number[] { | ||
const pixelSum = new Array<number>(image.channels).fill(0); | ||
const nbValues = options ? options.points.length : image.size; | ||
if (nbValues === 0) throw new RangeError('Array of coordinates is empty.'); | ||
if (options) { | ||
for (const point of options.points) { | ||
for (let channel = 0; channel < image.channels; channel++) { | ||
pixel[channel] += image.getValue(column, row, channel); | ||
if ( | ||
point.column < 0 || | ||
point.column >= image.width || | ||
point.row < 0 || | ||
point.row >= image.height | ||
) { | ||
throw new RangeError( | ||
`Invalid coordinate: {column: ${point.column}, row: ${point.row}}.`, | ||
); | ||
} | ||
pixelSum[channel] += image.getValueByPoint(point, channel); | ||
} | ||
} | ||
} else { | ||
for (let row = 0; row < image.height; row++) { | ||
for (let column = 0; column < image.width; column++) { | ||
for (let channel = 0; channel < image.channels; channel++) { | ||
pixelSum[channel] += image.getValue(column, row, channel); | ||
} | ||
} | ||
} | ||
} | ||
return pixel.map((channel) => channel / image.size); | ||
return pixelSum.map((channelSum) => channelSum / nbValues); | ||
} |
Oops, something went wrong.