{"id":11084,"date":"2023-12-26T00:04:31","date_gmt":"2023-12-25T16:04:31","guid":{"rendered":"https:\/\/www.zhangxinxu.com\/wordpress\/?p=11084"},"modified":"2023-12-26T00:04:31","modified_gmt":"2023-12-25T16:04:31","slug":"js-img-image-similarity","status":"publish","type":"post","link":"https:\/\/www.zhangxinxu.com\/wordpress\/2023\/12\/js-img-image-similarity\/","title":{"rendered":"JS\u5b9e\u73b0\u56fe\u7247\u76f8\u4f3c\u5ea6\u7684\u5224\u65ad"},"content":{"rendered":"<p>by <a href=\"https:\/\/www.zhangxinxu.com\/\">zhangxinxu<\/a> from <a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=11084\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=11084<\/a> \u946b\u7a7a\u95f4-\u946b\u751f\u6d3b<br \/>\n\u672c\u6587\u6b22\u8fce\u5206\u4eab\u4e0e\u805a\u5408\uff0c\u5168\u6587\u8f6c\u8f7d\u5c31\u4e0d\u5fc5\u4e86\uff0c\u5c0a\u91cd\u7248\u6743\uff0c\u5708\u5b50\u5c31\u8fd9\u4e48\u5927\uff0c\u82e5\u6025\u7528\u53ef\u4ee5\u8054\u7cfb\u6388\u6743\u3002<\/p>\n<p><script>window.wxShareImgUrl = 'https:\/\/www.zhangxinxu.com\/study\/image\/share\/202312-img-compare.png';<\/script><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202312\/image-compare-cover.png\" width=\"294\" height=\"181\" alt=\"\u5bf9\u6bd4\u5c01\u9762\u793a\u610f\u56fe\" class=\"alignright clip size-medium\" \/><\/p>\n<h3>\u4e00\u3001\u56fe\u7247\u8fd1\u4f3c\u5ea6\u5bf9\u6bd4<\/h3>\n<p>\u5f00\u95e8\u89c1\u5c71\uff0c\u4e0d\u7ed5\u5f2f\u8def\u3002<\/p>\n<p>\u7528\u7684\u662f\u8fd9\u4e2aGithub\u9879\u76ee\uff1a<a href=\"https:\/\/github.com\/obartra\/ssim\" rel=\"noopener\" target=\"_blank\">https:\/\/github.com\/obartra\/ssim<\/a><\/p>\n<p>\u4e4b\u6240\u4ee5\u9009\u8fd9\u4e2a\uff0c\u662f\u6211\u770b\u8fd9\u4e2a\u9879\u76ee\u63d0\u4ea4\u6b21\u6570\u6bd4\u8f83\u591a\uff0c\u6587\u4ef6\u7248\u672c\u4e5f\u6bd4\u8f83\u9ad8\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202312\/2023-12-25_232903.png\" width=\"608\" height=\"415\" alt=\"\u9009\u62e9\u7684\u7406\u7531\" class=\"alignnone size-medium\" \/><\/p>\n<h4>\u6848\u4f8b\u5148\u884c<\/h4>\n<p>\u4e0d\u7ba1\u600e\u6837\uff0c\u5148\u770b\u5bf9\u6bd4\u6548\u679c\u3002<\/p>\n<p>\u60a8\u53ef\u4ee5\u72e0\u72e0\u5730\u70b9\u51fb\u8fd9\u91cc\uff1a<a href=\"https:\/\/www.zhangxinxu.com\/study\/202312\/js-image-similar-demo.php\" rel=\"noopener\" target=\"_blank\">JS\u5b9e\u73b0\u56fe\u7247\u5bf9\u6bd4\u5dee\u5f02\u53c2\u6570demo<\/a><\/p>\n<p>\u9996\u5148\uff0c\u9009\u4e24\u4e2a\u8fd1\u4f3c\u7684\u56fe\uff0c\u4f8b\u5982\u9009\u62e9\u6211\u7684CSS\u4e66\u7c4d\u7684\u4e66\u5c01\uff0c\u70b9\u51fb\u5bf9\u6bd4\uff0c\u5219\u4f1a\u6709\u4e0b\u56fe\u6240\u793a\u7684\u7ed3\u679c\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202312\/2023-12-25_233248.png\" width=\"398\" height=\"254\" alt=\"\u76f8\u4f3c\u56fe\u7247\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u76f8\u4f3c\u5ea6\u57280.51\u5de6\u53f3\uff0c\u8fd9\u4e24\u4e2a\u56fe\u7ed3\u6784\u7c7b\u4f3c\uff0c\u5c3a\u5bf8\u6709\u4e9b\u8bb8\u5dee\u522b\uff0c\u989c\u8272\u4e5f\u6709\u4e9b\u4e0d\u540c\uff0c\u6587\u6848\u4e5f\u6709\u6240\u4e0d\u540c\uff0c50%\u5de6\u53f3\u7684\u5dee\u5f02\u7b26\u5408\u9884\u671f\u3002<\/p>\n<p>\u7136\u540e\uff0c\u9009\u62e9\u4e24\u4e2a\u957f\u5f97\u5b8c\u5168\u5927\u76f8\u5f84\u5ead\u7684\u56fe\u7247\uff0c\u4f8b\u5982\u4f7f\u7528\u6211\u7684\u5e05\u7167 ?:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202312\/2023-12-25_233459.png\" width=\"386\" height=\"218\" alt=\"\u5b8c\u5168\u4e0d\u540c\u98ce\u683c\u56fe\u7247\u76f8\u4f3c\u5ea6\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u53ef\u4ee5\u770b\u5230\u5bf9\u6bd4\u7684\u7ed3\u679c\u63a5\u8fd1\u4e8e0\uff0c\u4e5f\u5c31\u662f\u5dee\u522b\u5de8\u5927\uff0c\u5b8c\u5168\u6ca1\u6709\u53ef\u6bd4\u6027\u3002<\/p>\n<p>\u6700\u540e\uff0c\u6f14\u793a\u9875\u9762\u8fd8\u652f\u6301\u9009\u62e9\u672c\u5730\u56fe\u7247\u8fdb\u884c\u76f8\u4f3c\u5ea6\u5bf9\u6bd4\uff0c\u5927\u5bb6\u53ef\u4ee5\u9009\u62e9\u672c\u5730\u7684\u56fe\u7247\u7d20\u6750\uff0c\u770b\u770b\u7ed3\u679c\u5982\u4f55\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u6211\u9009\u62e9\u4e86\u5982\u4e0b\u56fe\u6240\u793a\u7684\u4e24\u5f20\u56fe\uff0c\u5f97\u5230\u4e86\u76f8\u4f3c\u503c\u633a\u9ad8\u7684\u5bf9\u6bd4\u7ed3\u679c\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202312\/2023-12-25_233732.png\" width=\"387\" height=\"364\" alt=\"\u76f8\u4f3c\u5ea6\u9ad8\u793a\u610f\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u9ad8\u8fbe0.83\uff0c\u8fd9\u662f\u4e00\u4e2a\u6bd4\u8f83\u9ad8\u7684\u5bf9\u6bd4\u503c\u4e86\u3002<\/p>\n<h4>\u4f7f\u7528\u603b\u7ed3<\/h4>\n<p>\u6839\u636e\u6211\u7684\u4f7f\u7528\u611f\u53d7\uff0c\u5bf9\u6bd4\u503c\u5c0f\u4e8e0.4\u7684\uff0c\u5c31\u53ef\u4ee5\u8ba4\u4e3a\u6709\u660e\u663e\u5dee\u5f02\uff0c\u6570\u503c\u8d8a\u5c0f\uff0c\u81ea\u7136\u5dee\u5f02\u5c31\u8d8a\u5927\u4e86\u3002<\/p>\n<h3>\u4e8c\u3001\u4f7f\u7528\u8bf4\u660e<\/h3>\n<p>\u5b98\u65b9\u793a\u610f\u7684\u4ee3\u7801\u662fnpm\u5305\u5f15\u5165\uff0c\u5982\u4e0b\uff1a<\/p>\n<pre>npm install ssim.js<\/pre>\n<pre>import ssim from \"ssim.js\";\r\n\r\nconst img1 = loadImage(\".\/img1.jpg\");\r\nconst img2 = loadImage(\".\/img2.jpg\");\r\n\r\nconst { mssim, performance } = ssim(img1, img2);\r\n\r\nconsole.log(`SSIM: ${mssim} (${performance}ms)`);<\/pre>\n<p>\u800c\u4e0a\u9762\u51fa\u73b0\u7684loadImage\u65b9\u6cd5\uff0c\u5176\u5b9e\u662f\u4e0d\u5b58\u5728\u7684\uff0c\u6211\u641c\u7d22\u4e86ssim\u7684JS\u6e90\u7801\uff0c\u6839\u672c\u5c31\u6ca1\u6709loadImage\u8fd9\u4e2a\u5173\u952e\u5b57\u3002<\/p>\n<p>\u800c\u8fd9\u91cc\u7684img1\u548cimg2\u5e76\u975e\u56fe\u7247\u5143\u7d20\uff0c\u800c\u662fImageData\u5bf9\u8c61\uff0c\u53ef\u4ee5\u901a\u8fc7canvas\u5143\u7d20\u83b7\u53d6\uff0c<code>context.getImageData()<\/code>\u65b9\u6cd5\u3002<\/p>\n<p>\u4ee3\u7801\u793a\u610f\uff1a<\/p>\n<pre>\/\/ \u56fe\u7247\u8f6cimageData\u9700\u8981\u7684canvas\r\n\/\/ \u4e3a\u4e86\u8282\u7701\u5f00\u9500\uff0c\u5c31\u5728\u5916\u9762\u5b9a\u4e49\uff0c\u53ea\u7528\u8fd9\u4e00\u4e2a\u73a9\u800d\u4e86\r\nconst canvas = document.createElement('canvas');\r\nconst ctx = canvas.getContext('2d', { \r\n    willReadFrequently: true \r\n});\r\n\/\/ image \u8f6c imagedata\u7684\u65b9\u6cd5\r\nconst image2data = (img, flag) =&gt; {\r\n    const { width, height } = canvas;\r\n    ctx.clearRect(0, 0, width, height);\r\n    ctx.drawImage(img, 0, 0, width, height);\r\n    return ctx.getImageData(0, 0, width, height);\r\n};<\/pre>\n<p>\u5728\u4f7f\u7528image2data\u65b9\u6cd5\u4e4b\u524d\uff0c\u9700\u8981\u5148\u7ed9canvas\u5143\u7d20\u8bbe\u7f6e\u9ad8\u5bbd\u5c5e\u6027\u3002<\/p>\n<h4>\u5982\u679c\u662fWeb\u76f4\u8fde\u5f15\u7528<\/h4>\n<p>\u76f4\u8fde\u5f15\u7528\u7684\u8bdd\uff0c\u4f7f\u7528\u4e0b\u9762\u793a\u610f\u7684\u4ee3\u7801\uff1a<\/p>\n<pre>&lt;script src=\".\/ssim.web.js\"&gt;&lt;\/script&gt;\r\n&lt;script&gt;\r\nconst { mssim, performance } = ssim.ssim(imgData1, imgData2);\r\nconsole.log(`\u76f8\u4f3c\u5ea6: ${mssim} (${performance}ms)`);\r\n&lt;\/script&gt;<\/pre>\n<p>\u524d\u9762\u63d0\u4f9b\u7684\u6f14\u793a\u9875\u9762\u5c31\u662f\u4f7f\u7528\u76f4\u8fde\u5f15\u7528\u7684\u65b9\u5f0f\u5b9e\u73b0\u7684\u3002<\/p>\n<p>\u66f4\u5177\u4f53\u7684\u4f7f\u7528\u7ec6\u8282\u76f4\u63a5\u53c2\u9605demo\u6f14\u793a\u9875\u9762\u7684\u6e90\u7801\u5373\u53ef\u3002<\/p>\n<h3>\u4e09\u3001\u5e94\u7528\u573a\u666f\uff0c\u7ed3\u675f\u8bed<\/h3>\n<p>\u56fe\u7247\u76f8\u4f3c\u5ea6\u7684\u5e94\u7528\u901a\u5e38\u90fd\u4e0e\u56fe\u7247\u5e8f\u5217\u76f8\u5173\u3002<\/p>\n<p>\u4f8b\u5982\uff0cGIF\u52a8\u56fe\u9700\u8981\u8fdb\u884c\u538b\u7f29\uff0c\u5219\u53ef\u4ee5\u5220\u9664\u76f8\u4f3c\u5ea6\u6781\u4e3a\u7c7b\u4f3c\u7684\u56fe\u7247\u5e8f\u5217\u5e27\uff0c\u53ea\u4fdd\u7559\u7b2c\u4e00\u4e2a\uff0c\u540c\u65f6\u589e\u52a0\u8fd9\u4e00\u5e27\u7684\u5ef6\u65f6\u65f6\u95f4\u3002<\/p>\n<p>\u76ee\u524d\u7684Web\u6280\u672f\u5bf9\u6b64\u9700\u6c42\u7684\u5b9e\u73b0\u5df2\u7ecf\u975e\u5e38\u6210\u719f\uff0c\u5c24\u5176\u662f\u6709\u4e2aWebCodecs API\uff0c\u53ef\u4ee5\u8f7b\u677e\u5bf9GIF\u56fe\u7247\u8fdb\u884c\u89e3\u7801\u3002<\/p>\n<p>\u53c8\u4f8b\u5982\u5bf9\u8bc6\u522b\u89c6\u9891\u7d20\u6750\u7684\u8f6c\u573a\u753b\u9762\u7b49\u3002<\/p>\n<p>\u53e6\u5916\uff0c\u7d20\u6750\u7684\u5927\u5c0f\u5e76\u4e0d\u4f1a\u5f71\u54cd\u8bc6\u522b\u7684\u51c6\u786e\u6027\uff0c\u4f46\u662f\u4f1a\u5f71\u54cd\u8bc6\u522b\u7684\u6027\u80fd\u3002<\/p>\n<p>\u4e5f\u5c31\u662f1024*1024\u7684\u56fe\u7247\u5bf9\u6bd4\u7ed3\u679c\uff0c\u548c128*128\u7684\u56fe\u7247\u5bf9\u6bd4\u7ed3\u679c\u76f8\u5dee\u4e0d\u5927\uff0c\u4f46\u662f\u6027\u80fd\u5dee\u5f02\u90a3\u5c31\u975e\u5e38\u660e\u663e\u4e86\u3002<\/p>\n<p>\u6240\u4ee5\uff0c\u8fd9\u91cc\u6709\u4e2a\u5c0ftips\uff0c\u5728\u83b7\u53d6\u56fe\u7247\u6570\u636e\u7684\u65f6\u5019\uff0c\u8bb0\u5f97\u7ed8\u5236\u5728\u5408\u9002\u5927\u5c0f\u7684Canvas\u4e0a\uff0c\u6ca1\u6709\u5fc5\u8981\u57fa\u4e8e\u539f\u56fe\u5c3a\u5bf8\u7ed8\u5236\uff0c\u9664\u975e\u4f60\u8fd9\u4e2a\u76f8\u4f3c\u5ea6\u5bf9\u6bd4\u5c31\u662f\u4e00\u9524\u5b50\u4e70\u5356\uff0c\u800c\u4e0d\u662f\u6279\u91cf\u5316\u7684\u64cd\u4f5c\u3002<\/p>\n<p>OK\uff0c\u5c31\u8bf4\u8fd9\u4e48\u591a\u5427\uff0c\u4e00\u4e2a\u5c0f\u5c0f\u7684\u6280\u672f\u79ef\u7d2f\uff0c\u8bf4\u4e0d\u5b9a\u4ec0\u4e48\u65f6\u5019\u5c31\u4f1a\u7528\u5230\u5462\u3002<\/p>\n<p>\u611f\u8c22\u9605\u8bfb\uff0c\u6b22\u8fce<button onclick=\"shareWeixin.click()\" class=\"entry-share-btn\">\u5206\u4eab<\/button>\uff0c\u4ee5\u53ca\u5723\u8bde\u8282\u5feb\u4e50\uff01<\/p>\n<p>\u2603\ufe0f\u26c4\ufe0f??\u2764\ufe0f<\/p>\n<p>\u672c\u6587\u4e3a\u539f\u521b\u6587\u7ae0\uff0c\u6b22\u8fce\u5206\u4eab\uff0c\u52ff\u5168\u6587\u8f6c\u8f7d\uff0c\u5982\u679c\u5b9e\u5728\u559c\u6b22\uff0c\u53ef\u6536\u85cf\uff0c\u6c38\u4e0d\u8fc7\u671f\uff0c\u4e14\u4f1a\u53ca\u65f6\u66f4\u65b0\u77e5\u8bc6\u70b9\u53ca\u4fee\u6b63\u9519\u8bef\uff0c\u9605\u8bfb\u4f53\u9a8c\u4e5f\u66f4\u597d\u3002<br \/>\n\u672c\u6587\u5730\u5740\uff1a<a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=11084\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=11084<\/a><\/p>\n<p>\uff08\u672c\u7bc7\u5b8c\uff09<\/p>\n","protected":false},"excerpt":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202312\/image-compare-cover.png\" width=\"160\" height=\"99\" alt=\"\u5bf9\u6bd4\u5c01\u9762\u793a\u610f\u56fe\" class=\"alignright size-medium\" loading=\"lazy\" \/><\/p>\n<p>\u7ed9\u5927\u5bb6\u6f14\u793a\u4e0b\u5982\u4f55\u5bf9\u6bd4\u4e24\u4e2aIMG\u56fe\u7247\u7684\u76f8\u4f3c\u5ea6\uff0c\u6027\u80fd\u548c\u51c6\u786e\u5ea6\u5747\u4e0d\u9519\uff0c\u7136\u540e\u2026\u2026\u5723\u8bde\u8282\u5feb\u4e50\uff01?<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[180],"tags":[53,888,168,1917,75,1244,1764],"_links":{"self":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/11084"}],"collection":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/comments?post=11084"}],"version-history":[{"count":0,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/11084\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/media?parent=11084"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/categories?post=11084"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/tags?post=11084"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}