Skip to content

Commit e6fde84

Browse files
committed
switch to histograms
1 parent 61394f1 commit e6fde84

1 file changed

Lines changed: 8 additions & 9 deletions

File tree

index.html

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -267,15 +267,15 @@
267267
reader.readAsDataURL(file);
268268
}
269269

270-
function getMeanGrayAt(x, y, radius) {
270+
function getGrayHist(x, y, radius) {
271271
x += 2
272272
y += 2
273273
const imgData = ctx.getImageData(x - radius, y - radius, radius * 2, radius * 2);
274274
const data = imgData.data;
275275

276276
let sum = 0;
277277
let count = 0;
278-
278+
let hist = [0,0,0,0,0,0,0,0,0,0];
279279
for (let j = 0; j < radius * 2; j++) {
280280
for (let i = 0; i < radius * 2; i++) {
281281
const dx = i - radius;
@@ -288,7 +288,9 @@
288288
const g = data[idx + 1];
289289
const b = data[idx + 2];
290290
const gray = 0.299 * r + 0.587 * g + 0.114 * b;
291+
const grayIdx = Math.floor(gray/25.5)
291292
sum += gray;
293+
hist[grayIdx]++;
292294
count++;
293295

294296
// Color this pixel red
@@ -303,7 +305,7 @@
303305
// Write modified image data back to canvas
304306
//ctx.putImageData(imgData, x - radius, y - radius);
305307

306-
return count > 0 ? +(sum / count).toFixed(1) : 0;
308+
return hist;
307309
}
308310

309311
function showGrayLevels() {
@@ -312,16 +314,13 @@
312314
let result = "[\n";
313315
let excelResult = ""
314316
for (let i = 0; i < m; i++) {
315-
result += " [";
316317
for (let j = 0; j < n; j++) {
317318
let x = circles[i][j].x;
318319
let y = circles[i][j].y;
319-
let gray = getMeanGrayAt(x, y, radius);
320-
result += `${gray}${j < n - 1 ? ", " : ""}`;
321-
excelResult += `${gray}${j < n - 1 ? "\t" : ""}`;
320+
let grayHist = getGrayHist(x, y, radius);
321+
result += grayHist.join(',')+'\n';
322+
excelResult += grayHist.join('\t')+'\n';
322323
}
323-
result += `]${i < m - 1 ? ",\n" : ""}`;
324-
excelResult += `${i < m - 1 ? "\n" : ""}`;
325324
}
326325
navigator.clipboard.writeText(excelResult).then(() => {
327326
alert('Table copied to clipboard! Paste into Excel.');

0 commit comments

Comments
 (0)