我想检测图标的宽度(右手指针) 在下图中。
我认为可能的解决方案可能涉及color removal illustrated in this approach,在这种情况下可用于删除浅蓝色背景颜色。但我不确定去除颜色后的下一步。
答案 0 :(得分:1)
我想出了什么!
以下是我的表现:
<img>
标记(通过普通的ole-html或js)将图像添加到文档中context.drawImage
context.getImageData
获取画布中像素的颜色colorToFilter
minX
,minY
,maxX
和maxY
变量,以跟踪不属于colorToFilter
pixelMatrix
中的每个像素,检查当前row
或column
是否小于或大于colorToFilter
<的颜色/ LI>
maxX - minX
和maxY - minY
的差异以及您的尺寸!仅供参考:我因为跨域问题而使用数据uri作为图片src
。
如果您需要更多解释,请与我们联系。这个解决方案可能不是最有效的,但它可以工作!
/**
* converts a number from 0-255 to a hex number padded with a zero if necessary
* @param {number} number
*/
function toHex(number) {
const numberAsString = Number(number).toString(16);
return numberAsString.length === 1 ? '0' + numberAsString : numberAsString;
}
/**
* Grabs the pixel colors from an image and stores it in a matrix
* @param {HTMLImageElement} img
*/
function convertImageToPixelData(img) {
// credit for getting the color of pixel comes from here:
// https://stackoverflow.com/questions/8751020/how-to-get-a-pixels-x-y-coordinate-color-from-an-image
const canvas = document.createElement('canvas');
const imgWidth = img.clientWidth;
const imgHeight = img.clientHeight;
canvas.width = imgWidth;
canvas.height = imgHeight;
const context = canvas.getContext('2d');
context.drawImage(img, 0, 0, imgWidth, imgHeight);
const rawPixelData = context.getImageData(0, 0, imgWidth, imgHeight).data;
// convert to hex string
const pixelColors = rawPixelData.reduce((groups, pixelColor, index) => {
if (index % 4 === 3) { // skip the alpha channel
return groups;
}
if (groups[groups.length - 1].length === 6) {
groups.push('');
}
groups[groups.length - 1] += toHex(pixelColor);
return groups;
}, ['']);
// convert to matrix
/**
* @type {string[][]}
*/
const pixelMatrix = pixelColors.reduce((matrix, pixel) => {
const currentRow = matrix[matrix.length - 1];
if (currentRow.length < imgWidth) {
currentRow.push(pixel);
} else {
matrix.push([pixel]);
}
return matrix;
}, [[]]);
return pixelMatrix;
}
/**
* finds the dimensions of an icon inside a single colored image
* @param {HTMLImageElement} img
*/
function findBox(img) {
const pixelMatrix = convertImageToPixelData(img);
const colorToFilter = pixelMatrix[0][0];
const imgWidth = img.clientWidth;
const imgHeight = img.clientHeight;
let maxX = 0;
let minX = imgWidth;
let maxY = 0;
let minY = imgHeight;
// for each pixel calculate if the index of the pixel is greater than the current max or min
for (let row = 0; row < imgHeight; row += 1) {
for (let column = 0; column < imgWidth; column += 1) {
const pixel = pixelMatrix[row][column];
if (pixel !== colorToFilter) {
if (column < minX) {
minX = column;
}
if (column > maxX) {
maxX = column;
}
if (row < minY) {
minY = row;
}
if (row > maxY) {
maxY = row;
}
}
}
}
const width = maxX - minX + 1;
const height = maxY - minY + 1;
return { width, height };
}
const img = document.querySelector('#my-image');
const box = findBox(img);
console.log(box);
&#13;
<img id="my-image" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAJYAAABkCAIAAADrOV6nAAAFUklEQVR42u3caVPaXByG8X7/D+AuakXBBQVFgiRhJyAgCpElICSAJkFQcFdQLKCt6/NYW8LWe+Z6pTOO429O+J+TxG+6hIj6um/4E4AQgRCBEIT4K4AQgRCBEIQIhAiECIQgRCBEIEQgBCECIQIhAiEIEQgRCBEIQYhA2DvlyQglOqclSiV5LKE4CPuntVggtbUkUyMiMSSaJ4u+leskUdv3JTgQ9nLxfVfYnvcuyNRoU44Yke3q8o7xTqAfsk9VMwkQ9thSC5hEz4LsmJbJR7YhyTJ55NJUwut3/LPcc/weCHskjvPOSubGRXJUts+WGO1pSHcRJWpp6gO2F92kQNgLRd2ifay55kj1WfITs1cJ+mIgDsIuT5juHZ3cWHzEsOxcvhXor/gZK475HItxppvTSjTjUUnNCXOiFDLVs1/wqyd0JXpY8mytYlPRtcklapXo1g6Bnjv/ysXznls7dasan5qS3ezDvrBbF89AUNu6eI4UmNXq71w8+c0bVldmZgqPY6p5QmR8loH262HC+HbeOd5imDqJmP//4lkXTJd+TdE2JrV2Fw1yyarJMh579AAHbN3JtL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