|
267 | 267 | reader.readAsDataURL(file); |
268 | 268 | } |
269 | 269 |
|
270 | | -function getMeanGrayAt(x, y, radius) { |
| 270 | +function getGrayHist(x, y, radius) { |
271 | 271 | x += 2 |
272 | 272 | y += 2 |
273 | 273 | const imgData = ctx.getImageData(x - radius, y - radius, radius * 2, radius * 2); |
274 | 274 | const data = imgData.data; |
275 | 275 |
|
276 | 276 | let sum = 0; |
277 | 277 | let count = 0; |
278 | | - |
| 278 | + let hist = [0,0,0,0,0,0,0,0,0,0]; |
279 | 279 | for (let j = 0; j < radius * 2; j++) { |
280 | 280 | for (let i = 0; i < radius * 2; i++) { |
281 | 281 | const dx = i - radius; |
|
288 | 288 | const g = data[idx + 1]; |
289 | 289 | const b = data[idx + 2]; |
290 | 290 | const gray = 0.299 * r + 0.587 * g + 0.114 * b; |
| 291 | + const grayIdx = Math.floor(gray/25.5) |
291 | 292 | sum += gray; |
| 293 | + hist[grayIdx]++; |
292 | 294 | count++; |
293 | 295 |
|
294 | 296 | // Color this pixel red |
|
303 | 305 | // Write modified image data back to canvas |
304 | 306 | //ctx.putImageData(imgData, x - radius, y - radius); |
305 | 307 |
|
306 | | - return count > 0 ? +(sum / count).toFixed(1) : 0; |
| 308 | + return hist; |
307 | 309 | } |
308 | 310 |
|
309 | 311 | function showGrayLevels() { |
|
312 | 314 | let result = "[\n"; |
313 | 315 | let excelResult = "" |
314 | 316 | for (let i = 0; i < m; i++) { |
315 | | - result += " ["; |
316 | 317 | for (let j = 0; j < n; j++) { |
317 | 318 | let x = circles[i][j].x; |
318 | 319 | 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'; |
322 | 323 | } |
323 | | - result += `]${i < m - 1 ? ",\n" : ""}`; |
324 | | - excelResult += `${i < m - 1 ? "\n" : ""}`; |
325 | 324 | } |
326 | 325 | navigator.clipboard.writeText(excelResult).then(() => { |
327 | 326 | alert('Table copied to clipboard! Paste into Excel.'); |
|
0 commit comments