gpt4 book ai didi

javascript - 如何计算图像周围的计算透明区域?

转载 作者:行者123 更新时间:2023-11-30 19:37:46 26 4
gpt4 key购买 nike

image

就像这张图一样,红色边框表示图像的大小,绿色边框表示图像的可见大小,绿色边框之外是alpha像素。如何计算可见帧?

两种计算机语言都可以。

最佳答案

// This function will return object with properties like left, top position and width & height of green box.
//@imgObj : Image Object of target image (.png)

function getImageDetails(imgObj)
{
var cns = document.createElement("canvas");

cns.width = imgObj.width;
cns.height = imgObj.height;

var ctx = canvas.getContext("2d");
ctx.drawImage(imgObj,0,0,cns.width,cns.height);

var imgData = ctx.getImageData(0, 0, cns.width, cns.height).data;
var minCol,maxCol,curCol,
minRow,maxRow,curRow;

for (var i = 0; i < imgData.length; i += 4) {
var index = Math.floor(i/4);
curCol = index%cns.width;
curRow = Math.floor(index/cns.width);
if( imgData[i] != 0 ||
imgData[i + 1] != 0 ||
imgData[i + 2] != 0 ||
imgData[i + 3] != 0)
{
minCol = minCol == undefined || minCol > curCol ? curCol : minCol;
maxCol = maxCol == undefined || maxCol < curCol ? curCol : maxCol;
minRow = minRow == undefined || minRow > curRow ? curRow : minRow;
maxRow = maxRow == undefined || maxRow < curRow ? curRow : maxRow;
}
}

var details = new Object();

details.x = minCol;
details.y = minRow;
details.width = maxCol-minCol+1;
details.height = maxRow-minRow+1;

return details
}

关于javascript - 如何计算图像周围的计算透明区域?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39267185/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com