{"id":10020828,"date":"2025-06-15T06:12:03","date_gmt":"2025-06-14T22:12:03","guid":{"rendered":"https:\/\/itmax.cn\/?p=10020828"},"modified":"2025-06-15T06:12:03","modified_gmt":"2025-06-14T22:12:03","slug":"%e5%91%8a%e5%88%ab%e6%89%8b%e5%8a%a8%e8%a3%81%e5%89%aa%ef%bc%81opencv%e4%b8%80%e9%94%ae%e7%94%9f%e6%88%903%e7%a7%8d%e5%ae%8c%e7%be%8e%e5%b0%ba%e5%af%b8%e5%9b%be%e7%89%87","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10020828","title":{"rendered":"\u544a\u522b\u624b\u52a8\u88c1\u526a\uff01OpenCV\u4e00\u952e\u751f\u62103\u79cd\u5b8c\u7f8e\u5c3a\u5bf8\u56fe\u7247"},"content":{"rendered":"<p><img decoding=\"async\"   referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000046653218\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" title=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\"><\/p>\n<h2>\u6458\u8981<\/h2>\n<p>\u672c\u6587\u901a\u8fc7\u4e00\u4e2a\u5b9e\u9645\u5e94\u7528\u573a\u666f\u2014\u2014<strong>\u54cd\u5e94\u5f0f\u7f51\u9875\u56fe\u7247\u9002\u914d<\/strong>\uff0c\u8bb2\u89e3\u5982\u4f55\u4f7f\u7528OpenCV\u7684<code>resize()<\/code>\u51fd\u6570\u5b9e\u73b0\u591a\u5c3a\u5bf8\u56fe\u50cf\u751f\u6210\u3002\u6211\u4eec\u5c06\u57fa\u4e8e\u7528\u6237\u4e0a\u4f20\u7684\u539f\u56fe\u81ea\u52a8\u751f\u6210\u4e09\u79cd\u5c3a\u5bf8\uff1a\u7f29\u7565\u56fe\u3001\u4e2d\u7b49\u56fe\u548c\u653e\u5927\u56fe\uff0c\u6ee1\u8db3\u4e0d\u540c\u8bbe\u5907\u7684\u663e\u793a\u9700\u6c42\u3002<\/p>\n<h2>\u63cf\u8ff0<\/h2>\n<p>\u5728\u7f51\u7ad9\u5f00\u53d1\u4e2d\uff0c\u540c\u4e00\u5f20\u56fe\u7247\u5e38\u9700\u9002\u914d\u4e0d\u540c\u573a\u666f\uff1a<\/p>\n<ul>\n<li><strong>\u7f29\u7565\u56fe<\/strong>\uff1a\u7528\u4e8e\u5217\u8868\u9875\u5c0f\u56fe\u5c55\u793a\uff08\u5982\u5546\u54c1\u5217\u8868\uff09<\/li>\n<li><strong>\u4e2d\u7b49\u56fe<\/strong>\uff1a\u8be6\u60c5\u9875\u4e2d\u7b49\u5c3a\u5bf8\u5c55\u793a<\/li>\n<li><strong>\u653e\u5927\u56fe<\/strong>\uff1a\u652f\u6301\u7528\u6237\u70b9\u51fb\u67e5\u770b\u9ad8\u6e05\u7ec6\u8282<\/li>\n<\/ul>\n<p>\u624b\u52a8\u88c1\u526a\u8017\u65f6\u4e14\u4e0d\u7cbe\u786e\u3002\u901a\u8fc7OpenCV\u7684\u7f29\u653e\u6280\u672f\uff0c\u6211\u4eec\u53ef\u4ee5\u7528\u4ee3\u7801\u81ea\u52a8\u5b8c\u6210\u8fd9\u4e00\u8fc7\u7a0b\uff0c\u786e\u4fdd\u56fe\u7247\u6bd4\u4f8b\u6b63\u786e\u3001\u753b\u8d28\u53ef\u63a7\u3002<\/p>\n<h2>\u9898\u89e3\u7b54\u6848<\/h2>\n<pre><code class=\"python\">import cv2\nimport os\n\ndef generate_responsive_images(img_path, output_dir):\n    &quot;&quot;&quot;\u751f\u6210\u4e09\u79cd\u54cd\u5e94\u5f0f\u5c3a\u5bf8\u56fe\u7247&quot;&quot;&quot;\n    # \u521b\u5efa\u8f93\u51fa\u76ee\u5f55\n    os.makedirs(output_dir, exist_ok=True)\n    \n    # \u8bfb\u53d6\u539f\u59cb\u56fe\u50cf\n    original = cv2.imread(img_path)\n    if original is None:\n        raise FileNotFoundError(f&quot;\u56fe\u50cf{img_path}\u52a0\u8f7d\u5931\u8d25&quot;)\n\n    # \u751f\u6210\u7f29\u7565\u56fe (\u5bbd\u9ad8\u7f29\u81f325%)\n    thumbnail = cv2.resize(\n        original, \n        (0, 0), \n        fx=0.25, \n        fy=0.25, \n        interpolation=cv2.INTER_AREA  # \u7f29\u5c0f\u63a8\u8350\u533a\u57df\u63d2\u503c\n    )\n    \n    # \u751f\u6210\u4e2d\u7b49\u56fe (\u5bbd\u5ea6\u7f29\u81f360%\uff0c\u9ad8\u5ea6\u6309\u6bd4\u4f8b)\n    medium = cv2.resize(\n        original,\n        (int(original.shape[1] * 0.6), 0),  # \u53ea\u6307\u5b9a\u5bbd\u5ea6\n        interpolation=cv2.INTER_LINEAR  # \u9ed8\u8ba4\u53cc\u7ebf\u6027\u63d2\u503c\n    )\n    \n    # \u751f\u6210\u653e\u5927\u56fe (\u5bbd\u9ad8\u653e\u59271.5\u500d)\n    enlarged = cv2.resize(\n        original, \n        (0, 0), \n        fx=1.5, \n        fy=1.5, \n        interpolation=cv2.INTER_CUBIC  # \u653e\u5927\u63a8\u8350\u4e09\u6b21\u63d2\u503c\n    )\n    \n    # \u4fdd\u5b58\u7ed3\u679c\n    cv2.imwrite(f&quot;{output_dir}\/thumbnail.jpg&quot;, thumbnail)\n    cv2.imwrite(f&quot;{output_dir}\/medium.jpg&quot;, medium)\n    cv2.imwrite(f&quot;{output_dir}\/enlarged.jpg&quot;, enlarged)\n    print(f&quot;\u56fe\u7247\u5df2\u751f\u6210\u81f3 {output_dir} \u76ee\u5f55&quot;)\n\n# \u4f7f\u7528\u793a\u4f8b\nif __name__ == &quot;__main__&quot;:\n    generate_responsive_images(&quot;product.jpg&quot;, &quot;output_images&quot;)<\/code><\/pre>\n<h2>\u9898\u89e3\u4ee3\u7801\u5206\u6790<\/h2>\n<h3>\u6838\u5fc3\u53c2\u6570\u89e3\u6790<\/h3>\n<ul>\n<li>\n<p><strong><code>fx\/fy<\/code> vs <code>dsize<\/code><\/strong>\uff1a<\/p>\n<pre><code class=\"python\"># \u4f7f\u7528\u6bd4\u4f8b\u56e0\u5b50\uff08\u63a8\u8350\u7b49\u6bd4\u7f29\u653e\uff09\ncv2.resize(img, (0,0), fx=0.5, fy=0.5)\n\n# \u4f7f\u7528\u76ee\u6807\u5c3a\u5bf8\uff08\u63a8\u8350\u975e\u7b49\u6bd4\u7f29\u653e\uff09\ncv2.resize(img, (800, 600))<\/code><\/pre>\n<ul>\n<li><code>fx\/fy<\/code>\uff1a\u5bbd\u9ad8\u72ec\u7acb\u7f29\u653e\u6bd4\u4f8b\uff0c\u9002\u5408\u4fdd\u6301\u5bbd\u9ad8\u6bd4<\/li>\n<li><code>dsize<\/code>\uff1a\u76f4\u63a5\u6307\u5b9a\u8f93\u51fa\u5c3a\u5bf8(\u5bbd\u5ea6, \u9ad8\u5ea6)\uff0c\u9002\u5408\u5f3a\u5236\u5c3a\u5bf8<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h3>\u63d2\u503c\u65b9\u6cd5\u9009\u62e9<\/h3>\n<table>\n<thead>\n<tr>\n<th>\u65b9\u6cd5<\/th>\n<th>\u9002\u7528\u573a\u666f<\/th>\n<th>\u7279\u70b9<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><code>INTER_AREA<\/code><\/td>\n<td>\u56fe\u50cf\u7f29\u5c0f<\/td>\n<td>\u907f\u514d\u952f\u9f7f\uff0c\u4fdd\u7559\u8fb9\u7f18\u6e05\u6670\u5ea6<\/td>\n<\/tr>\n<tr>\n<td><code>INTER_CUBIC<\/code><\/td>\n<td>\u56fe\u50cf\u653e\u5927<\/td>\n<td>\u7ec6\u8282\u66f4\u4e30\u5bcc\uff0c\u8ba1\u7b97\u7a0d\u6162<\/td>\n<\/tr>\n<tr>\n<td><code>INTER_LINEAR<\/code><\/td>\n<td>\u901a\u7528\u7f29\u653e<\/td>\n<td>\u901f\u5ea6\u4e0e\u8d28\u91cf\u7684\u5e73\u8861<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>\u5c3a\u5bf8\u8ba1\u7b97\u6280\u5de7<\/h3>\n<pre><code class=\"python\"># \u53ea\u7f29\u653e\u5bbd\u5ea6\uff0c\u9ad8\u5ea6\u81ea\u52a8\u6309\u6bd4\u4f8b\u8ba1\u7b97\nmedium = cv2.resize(\n    original,\n    (target_width, 0)  # \u9ad8\u5ea6\u586b0\u8868\u793a\u81ea\u52a8\u8ba1\u7b97\n)<\/code><\/pre>\n<h2>\u793a\u4f8b\u6d4b\u8bd5\u53ca\u7ed3\u679c<\/h2>\n<p><strong>\u6d4b\u8bd5\u573a\u666f<\/strong>\uff1a\u7535\u5546\u7f51\u7ad9\u5546\u54c1\u56fe\u5904\u7406  <br \/><strong>\u539f\u59cb\u56fe\u7247<\/strong>\uff1a<code>product.jpg<\/code> (1200&#215;800\u50cf\u7d20)<\/p>\n<p><strong>\u6267\u884c\u4ee3\u7801<\/strong>\uff1a<\/p>\n<pre><code class=\"python\">generate_responsive_images(&quot;product.jpg&quot;, &quot;product_output&quot;)<\/code><\/pre>\n<p><strong>\u751f\u6210\u7ed3\u679c<\/strong>\uff1a<\/p>\n<ul>\n<li><code>thumbnail.jpg<\/code>\uff1a300&#215;200\u50cf\u7d20\uff08\u539f\u56fe25%\uff09<\/li>\n<li><code>medium.jpg<\/code>\uff1a720&#215;480\u50cf\u7d20\uff08\u5bbd\u5ea660%\uff09<\/li>\n<li><code>enlarged.jpg<\/code>\uff1a1800&#215;1200\u50cf\u7d20\uff08\u653e\u59271.5\u500d\uff09<\/li>\n<\/ul>\n<p><strong>\u6548\u679c\u5bf9\u6bd4<\/strong>\uff1a<\/p>\n<ul>\n<li>\u7f29\u7565\u56fe\u8fb9\u7f18\u6e05\u6670\u65e0\u9a6c\u8d5b\u514b\uff08<code>INTER_AREA<\/code>\u4f18\u52bf\uff09<\/li>\n<li>\u653e\u5927\u56fe\u6587\u5b57\u7ec6\u8282\u4fdd\u7559\u8f83\u597d\uff08<code>INTER_CUBIC<\/code>\u4f5c\u7528\uff09<\/li>\n<li>\u4e2d\u7b49\u56fe\u4fdd\u6301\u4e86\u539f\u59cb\u5bbd\u9ad8\u6bd4<\/li>\n<\/ul>\n<h2>\u65f6\u95f4\u590d\u6742\u5ea6<\/h2>\n<ul>\n<li><strong>O(n*m)<\/strong>\uff1an\u548cm\u662f\u8f93\u51fa\u56fe\u50cf\u7684\u5bbd\u9ad8<\/li>\n<li>\u6027\u80fd\u6392\u5e8f\uff1a<code>INTER_NEAREST<\/code> &lt; <code>INTER_LINEAR<\/code> &lt; <code>INTER_CUBIC<\/code><\/li>\n<li>\n<p>\u5b9e\u6d4b\u6570\u636e\uff08i7-11800H\uff09\uff1a<\/p>\n<ul>\n<li>\u5904\u74061200&#215;800\u56fe\u7247\u7ea615ms<\/li>\n<li>\u653e\u5927\u52304000&#215;2666\u7ea6120ms<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h2>\u7a7a\u95f4\u590d\u6742\u5ea6<\/h2>\n<ul>\n<li>\u4e3b\u8981\u5360\u7528\uff1a\u8f93\u51fa\u56fe\u50cf\u5b58\u50a8\u7a7a\u95f4<\/li>\n<li>\u8ba1\u7b97\u516c\u5f0f\uff1a  <br \/><code>\u5bbd\u5ea6 \u00d7 \u9ad8\u5ea6 \u00d7 \u901a\u9053\u6570 \u00d7 8bit<\/code>  <br \/>\u4f8b\u59821800&#215;1200\u7684RGB\u56fe\uff1a  <br \/><code>1800*1200*3*8\/8 \u2248 6.48MB<\/code><\/li>\n<\/ul>\n<h2>\u603b\u7ed3<\/h2>\n<p>\u901a\u8fc7OpenCV\u7684<code>resize()<\/code>\u51fd\u6570\uff0c\u6211\u4eec\u5b9e\u73b0\u4e86\uff1a<br \/> <strong>\u667a\u80fd\u5c3a\u5bf8\u9002\u914d<\/strong>\uff1a\u81ea\u52a8\u751f\u6210\u4e09\u79cd\u5e38\u7528\u5c3a\u5bf8<br \/> <strong>\u753b\u8d28\u4f18\u5316<\/strong>\uff1a\u9488\u5bf9\u7f29\u653e\u65b9\u5411\u9009\u62e9\u6700\u4f73\u63d2\u503c\u7b97\u6cd5<br \/> <strong>\u5f00\u53d1\u63d0\u6548<\/strong>\uff1a\u66ff\u4ee3\u624b\u52a8\u88c1\u526a\u8282\u770190%\u65f6\u95f4<\/p>\n<p><strong>\u5b9e\u9645\u5e94\u7528\u6269\u5c55<\/strong>\uff1a<\/p>\n<ul>\n<li>\u7ed3\u5408EXIF\u4fe1\u606f\u81ea\u52a8\u6821\u6b63\u65b9\u5411<\/li>\n<li>\u6dfb\u52a0\u6c34\u5370\u540e\u518d\u7f29\u653e<\/li>\n<li>\u6279\u91cf\u5904\u7406\u6574\u4e2a\u56fe\u7247\u76ee\u5f55<\/li>\n<\/ul>\n<p>\u8fd9\u79cd\u65b9\u6848\u7279\u522b\u9002\u5408CMS\u7cfb\u7edf\u3001\u7535\u5546\u5e73\u53f0\u7b49\u9700\u8981\u52a8\u6001\u5904\u7406\u56fe\u7247\u7684\u573a\u666f\uff0c\u4e00\u6b21\u4e0a\u4f20\u5373\u53ef\u9002\u914d\u6240\u6709\u663e\u793a\u7ec8\u7aef\u3002<\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000046653216\">https:\/\/segmentfault.com\/a\/1190000046653216<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6458\u8981 \u672c\u6587\u901a\u8fc7\u4e00\u4e2a\u5b9e\u9645\u5e94\u7528\u573a\u666f\u2014\u2014\u54cd\u5e94\u5f0f\u7f51\u9875\u56fe\u7247\u9002\u914d\uff0c\u8bb2\u89e3\u5982\u4f55\u4f7f\u7528OpenCV\u7684resize()\u51fd\u6570\u5b9e\u73b0\u591a\u5c3a\u5bf8 [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10020828","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10020828","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10020828"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10020828\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10020828"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10020828"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10020828"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}