3 Star 18 Fork 2

张鑫旭 / 图像相似度

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

图像相似度

介绍

判断图像,或者图像部分区域是不是在视觉上接近纯色。

使用说明

本JS的执行依赖于 color-thief.js,本项目中的 color-thief.js 做过一点自定义,使其支持部分区域取色的选择,使用原项目JS可能会没有效果。

使用示意:

<script src="./color-thief.js"></script>
<script src="./image-similarity.js"></script>

全局暴露了2个方法,一个是 imageSimilarityValue() 方法,语法如下:

imageSimilarityValue(src, bounding)

其中: src 是任意格式的图像地址。 bounding 是图像上局部区域的尺寸设置,格式是数组,需要4个数组项,都是数值,分别表示坐标和宽高,例如 [10, 10, 300, 100] 表示判断原始图像左上角坐标是 10,10,宽高是 300x100 的矩形区域的视觉色彩是否丰富。

返回值是个Promise,通常使用示意。

imageSimilarityValue(src, bounding).then(result => {
    // result
});

其中 result 是个对象,格式如下所示:

{
    colors: ['rgb(0,0,0)', ...],
    similarity: 0-255
}

colors 是图像限制在特定尺寸后选取的4个主要颜色,similarity是这些颜色的平均相似度值。

第二个方法是imageSimilarity()方法,语法如下:

imageSimilarity(src, bounding).then(similarity => {
    // similarity是数值
});

这里的 similarity 是整数值,范围从0-5,分别表示相似的程度,值越小则越相似。

这是当前JS项目内置的规则:

// 0 极度相似
// 1 相似
// 2 不太相似
// 3 不相似
// 4 差异较大

imageSimilarity() 方法底层依赖的就是 imageSimilarityValue() 方法,这里的 similarity 其实就是把 imageSimilarityValue() 方法中返回的 similarity 值和 50 相处取了个整。

为什么取50,全是作者自己的感觉,所以,这个相似度的阈值选择可能是不准确的,大家可以根据自己实际需求进行调整。

其他

MIT License Copyright (c) 2021 张鑫旭 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

判断图像,或者图像部分区域是不是在视觉上接近纯色,ES6+ 展开 收起
JavaScript
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
JavaScript
1
https://gitee.com/zhangxinxu/image-similarity.git
git@gitee.com:zhangxinxu/image-similarity.git
zhangxinxu
image-similarity
图像相似度
master

搜索帮助