{"id":994918,"date":"2024-12-27T08:59:37","date_gmt":"2024-12-27T00:59:37","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/994918.html"},"modified":"2024-12-27T08:59:40","modified_gmt":"2024-12-27T00:59:40","slug":"python-cv%e5%ba%93-%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/994918.html","title":{"rendered":"python cv\u5e93 \u5982\u4f55\u4f7f\u7528"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25071700\/b9e78936-d1e5-4c1c-95f7-b66d0d8d54be.webp\" alt=\"python cv\u5e93 \u5982\u4f55\u4f7f\u7528\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u7684OpenCV\u5e93\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u7528\u4e8e\u56fe\u50cf\u5904\u7406\u3001\u8ba1\u7b97\u673a\u89c6\u89c9\u548c<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u3002\u8981\u4f7f\u7528OpenCV\u5e93\uff0c\u4f60\u9700\u8981\u5b89\u88c5\u5b83\u3001\u5bfc\u5165\u5e93\u3001\u52a0\u8f7d\u56fe\u50cf\u6216\u89c6\u9891\u3001\u5e94\u7528\u5404\u79cd\u56fe\u50cf\u5904\u7406\u64cd\u4f5c\u3001\u5e76\u53ef\u89c6\u5316\u7ed3\u679c\u3002<\/strong>\u5176\u4e2d\uff0c\u5b89\u88c5OpenCV\u5e93\u662f\u6700\u57fa\u672c\u7684\u6b65\u9aa4\uff0c\u901a\u5e38\u901a\u8fc7pip\u5b89\u88c5\u547d\u4ee4\u6765\u5b8c\u6210\u3002\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7import cv2\u547d\u4ee4\u5728Python\u811a\u672c\u4e2d\u4f7f\u7528\u5b83\u3002\u63a5\u4e0b\u6765\u5c31\u662f\u52a0\u8f7d\u56fe\u50cf\u6216\u89c6\u9891\u6587\u4ef6\u8fdb\u884c\u5904\u7406\uff0cOpenCV\u63d0\u4f9b\u4e86\u8bb8\u591a\u5f3a\u5927\u7684\u51fd\u6570\u6765\u8fdb\u884c\u56fe\u50cf\u7684\u8bfb\u53d6\u3001\u663e\u793a\u3001\u4fdd\u5b58\u548c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u5b89\u88c5\u548c\u5bfc\u5165OpenCV\u5e93<\/p>\n<\/p>\n<p><p>\u5728\u4f7f\u7528OpenCV\u4e4b\u524d\uff0c\u9996\u5148\u8981\u786e\u4fdd\u5df2\u5b89\u88c5\u8be5\u5e93\u3002\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u901a\u8fc7Python\u7684\u5305\u7ba1\u7406\u5de5\u5177pip\u6765\u5b89\u88c5OpenCV\u5e93\u3002\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u5373\u53ef\u5b8c\u6210\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code>pip install opencv-python<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u4f60\u53ef\u4ee5\u5728Python\u811a\u672c\u6216\u4ea4\u4e92\u5f0f\u89e3\u91ca\u5668\u4e2d\u5bfc\u5165OpenCV\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u786e\u4fdd\u5b89\u88c5\u6b63\u786e\u540e\uff0c\u4f60\u53ef\u4ee5\u5f00\u59cb\u4f7f\u7528OpenCV\u7684\u5404\u79cd\u529f\u80fd\u8fdb\u884c\u56fe\u50cf\u5904\u7406\u548c\u8ba1\u7b97\u673a\u89c6\u89c9\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u52a0\u8f7d\u548c\u663e\u793a\u56fe\u50cf<\/p>\n<\/p>\n<p><p>\u5728\u4f7f\u7528OpenCV\u8fdb\u884c\u56fe\u50cf\u5904\u7406\u65f6\uff0c\u9996\u5148\u9700\u8981\u52a0\u8f7d\u56fe\u50cf\u3002OpenCV\u63d0\u4f9b\u4e86cv2.imread()\u51fd\u6570\u7528\u4e8e\u8bfb\u53d6\u56fe\u50cf\u6587\u4ef6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8bfb\u53d6\u56fe\u50cf<\/p>\n<p>image = cv2.imread(&#39;path_to_image.jpg&#39;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u50cf<\/strong><\/h2>\n<p>cv2.imshow(&#39;Image&#39;, image)<\/p>\n<p>cv2.w<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>tKey(0)<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>cv2.imread()\u51fd\u6570\u7528\u4e8e\u4ece\u6307\u5b9a\u8def\u5f84\u52a0\u8f7d\u56fe\u50cf\uff0c\u5e76\u5c06\u5176\u5b58\u50a8\u4e3aNumPy\u6570\u7ec4\u3002cv2.imshow()\u51fd\u6570\u7528\u4e8e\u663e\u793a\u56fe\u50cf\uff0ccv2.waitKey()\u51fd\u6570\u7528\u4e8e\u7b49\u5f85\u6309\u952e\u8f93\u5165\uff0ccv2.destroyAllWindows()\u51fd\u6570\u7528\u4e8e\u5173\u95ed\u6240\u6709\u7a97\u53e3\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u56fe\u50cf\u5904\u7406\u57fa\u7840\u64cd\u4f5c<\/p>\n<\/p>\n<p><p>OpenCV\u63d0\u4f9b\u4e86\u8bb8\u591a\u57fa\u7840\u7684\u56fe\u50cf\u5904\u7406\u64cd\u4f5c\uff0c\u5982\u56fe\u50cf\u7f29\u653e\u3001\u65cb\u8f6c\u3001\u88c1\u526a\u548c\u989c\u8272\u8f6c\u6362\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u56fe\u50cf\u5904\u7406\u64cd\u4f5c\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u56fe\u50cf\u7f29\u653e<\/strong><\/li>\n<\/ol>\n<p><p>\u53ef\u4ee5\u4f7f\u7528cv2.resize()\u51fd\u6570\u8c03\u6574\u56fe\u50cf\u5927\u5c0f\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u7f29\u653e\u56fe\u50cf<\/p>\n<p>resized_image = cv2.resize(image, (width, height))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u56fe\u50cf\u65cb\u8f6c<\/strong><\/li>\n<\/ol>\n<p><p>\u53ef\u4ee5\u4f7f\u7528cv2.getRotationMatrix2D()\u548ccv2.warpAffine()\u51fd\u6570\u65cb\u8f6c\u56fe\u50cf\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u83b7\u53d6\u65cb\u8f6c\u77e9\u9635<\/p>\n<p>rotation_matrix = cv2.getRotationMatrix2D(center, angle, scale)<\/p>\n<h2><strong>\u65cb\u8f6c\u56fe\u50cf<\/strong><\/h2>\n<p>rotated_image = cv2.warpAffine(image, rotation_matrix, (width, height))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u56fe\u50cf\u989c\u8272\u8f6c\u6362<\/strong><\/li>\n<\/ol>\n<p><p>\u53ef\u4ee5\u4f7f\u7528cv2.cvtColor()\u51fd\u6570\u8fdb\u884c\u989c\u8272\u7a7a\u95f4\u8f6c\u6362\u3002\u4f8b\u5982\uff0c\u5c06\u56fe\u50cf\u4eceBGR\u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe\u50cf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe\u50cf<\/p>\n<p>gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u56fe\u50cf\u7684\u8fb9\u7f18\u68c0\u6d4b<\/p>\n<\/p>\n<p><p>\u8fb9\u7f18\u68c0\u6d4b\u662f\u56fe\u50cf\u5904\u7406\u4e2d\u7684\u4e00\u4e2a\u91cd\u8981\u6b65\u9aa4\uff0cOpenCV\u63d0\u4f9b\u4e86\u591a\u79cd\u8fb9\u7f18\u68c0\u6d4b\u7b97\u6cd5\uff0c\u5982Canny\u8fb9\u7f18\u68c0\u6d4b\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528Canny\u8fb9\u7f18\u68c0\u6d4b\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe\u50cf<\/p>\n<p>gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)<\/p>\n<h2><strong>\u5e94\u7528Canny\u8fb9\u7f18\u68c0\u6d4b<\/strong><\/h2>\n<p>edges = cv2.Canny(gray_image, threshold1, threshold2)<\/p>\n<h2><strong>\u663e\u793a\u8fb9\u7f18\u68c0\u6d4b\u7ed3\u679c<\/strong><\/h2>\n<p>cv2.imshow(&#39;Edges&#39;, edges)<\/p>\n<p>cv2.waitKey(0)<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u56fe\u50cf\u7684\u5f62\u6001\u5b66\u53d8\u6362<\/p>\n<\/p>\n<p><p>\u5f62\u6001\u5b66\u53d8\u6362\u7528\u4e8e\u5904\u7406\u56fe\u50cf\u7684\u5f62\u72b6\u7ed3\u6784\uff0c\u5e38\u7528\u7684\u64cd\u4f5c\u6709\u81a8\u80c0\u3001\u8150\u8680\u3001\u5f00\u8fd0\u7b97\u548c\u95ed\u8fd0\u7b97\u7b49\u3002OpenCV\u63d0\u4f9b\u4e86cv2.erode()\u548ccv2.dilate()\u7b49\u51fd\u6570\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u7ed3\u6784\u5143\u7d20<\/p>\n<p>kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))<\/p>\n<h2><strong>\u8150\u8680\u64cd\u4f5c<\/strong><\/h2>\n<p>eroded_image = cv2.erode(image, kernel)<\/p>\n<h2><strong>\u81a8\u80c0\u64cd\u4f5c<\/strong><\/h2>\n<p>dilated_image = cv2.dilate(image, kernel)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u516d\u3001\u56fe\u50cf\u7684\u8f6e\u5ed3\u68c0\u6d4b<\/p>\n<\/p>\n<p><p>OpenCV\u63d0\u4f9b\u4e86cv2.findContours()\u51fd\u6570\u7528\u4e8e\u68c0\u6d4b\u56fe\u50cf\u4e2d\u7684\u8f6e\u5ed3\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u8f6e\u5ed3\u68c0\u6d4b\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe\u50cf<\/p>\n<p>gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)<\/p>\n<h2><strong>\u5e94\u7528\u9608\u503c<\/strong><\/h2>\n<p>_, thresh = cv2.threshold(gray_image, 127, 255, cv2.THRESH_BINARY)<\/p>\n<h2><strong>\u68c0\u6d4b\u8f6e\u5ed3<\/strong><\/h2>\n<p>contours, _ = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)<\/p>\n<h2><strong>\u7ed8\u5236\u8f6e\u5ed3<\/strong><\/h2>\n<p>cv2.drawContours(image, contours, -1, (0, 255, 0), 3)<\/p>\n<h2><strong>\u663e\u793a\u7ed3\u679c<\/strong><\/h2>\n<p>cv2.imshow(&#39;Contours&#39;, image)<\/p>\n<p>cv2.waitKey(0)<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e03\u3001\u89c6\u9891\u5904\u7406<\/p>\n<\/p>\n<p><p>OpenCV\u4e0d\u4ec5\u53ef\u4ee5\u5904\u7406\u56fe\u50cf\uff0c\u8fd8\u53ef\u4ee5\u5904\u7406\u89c6\u9891\u3002\u53ef\u4ee5\u4f7f\u7528cv2.VideoCapture()\u51fd\u6570\u8bfb\u53d6\u89c6\u9891\u6587\u4ef6\uff0c\u5e76\u9010\u5e27\u5904\u7406\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u8bfb\u53d6\u548c\u663e\u793a\u89c6\u9891\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6253\u5f00\u89c6\u9891\u6587\u4ef6<\/p>\n<p>cap = cv2.VideoCapture(&#39;path_to_video.mp4&#39;)<\/p>\n<p>while cap.isOpened():<\/p>\n<p>    # \u9010\u5e27\u8bfb\u53d6<\/p>\n<p>    ret, frame = cap.read()<\/p>\n<p>    if not ret:<\/p>\n<p>        break<\/p>\n<p>    # \u663e\u793a\u5f53\u524d\u5e27<\/p>\n<p>    cv2.imshow(&#39;Video&#39;, frame)<\/p>\n<p>    # \u6309\u952e\u9000\u51fa<\/p>\n<p>    if cv2.waitKey(1) &amp; 0xFF == ord(&#39;q&#39;):<\/p>\n<p>        break<\/p>\n<h2><strong>\u91ca\u653e\u8d44\u6e90<\/strong><\/h2>\n<p>cap.release()<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u516b\u3001\u5728\u56fe\u50cf\u4e0a\u7ed8\u5236\u5f62\u72b6\u548c\u6587\u672c<\/p>\n<\/p>\n<p><p>OpenCV\u63d0\u4f9b\u4e86\u7ed8\u5236\u5f62\u72b6\u548c\u6587\u672c\u7684\u529f\u80fd\uff0c\u5982\u753b\u7ebf\u3001\u77e9\u5f62\u3001\u5706\u548c\u6587\u5b57\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5728\u56fe\u50cf\u4e0a\u753b\u7ebf<\/p>\n<p>cv2.line(image, (x1, y1), (x2, y2), (255, 0, 0), thickness)<\/p>\n<h2><strong>\u5728\u56fe\u50cf\u4e0a\u753b\u77e9\u5f62<\/strong><\/h2>\n<p>cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), thickness)<\/p>\n<h2><strong>\u5728\u56fe\u50cf\u4e0a\u753b\u5706<\/strong><\/h2>\n<p>cv2.circle(image, (x, y), radius, (0, 0, 255), thickness)<\/p>\n<h2><strong>\u5728\u56fe\u50cf\u4e0a\u5199\u6587\u5b57<\/strong><\/h2>\n<p>cv2.putText(image, &#39;Hello, OpenCV!&#39;, (x, y), cv2.FONT_HERSHEY_SIMPLEX, fontScale, (255, 255, 255), thickness)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e5d\u3001\u56fe\u50cf\u7684\u76f4\u65b9\u56fe\u5747\u8861\u5316<\/p>\n<\/p>\n<p><p>\u76f4\u65b9\u56fe\u5747\u8861\u5316\u662f\u4e00\u79cd\u589e\u5f3a\u56fe\u50cf\u5bf9\u6bd4\u5ea6\u7684\u6280\u672f\uff0cOpenCV\u63d0\u4f9b\u4e86cv2.equalizeHist()\u51fd\u6570\u7528\u4e8e\u7070\u5ea6\u56fe\u50cf\u7684\u76f4\u65b9\u56fe\u5747\u8861\u5316\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe\u50cf<\/p>\n<p>gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)<\/p>\n<h2><strong>\u5e94\u7528\u76f4\u65b9\u56fe\u5747\u8861\u5316<\/strong><\/h2>\n<p>equalized_image = cv2.equalizeHist(gray_image)<\/p>\n<h2><strong>\u663e\u793a\u7ed3\u679c<\/strong><\/h2>\n<p>cv2.imshow(&#39;Equalized Image&#39;, equalized_image)<\/p>\n<p>cv2.waitKey(0)<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5341\u3001\u6a21\u677f\u5339\u914d<\/p>\n<\/p>\n<p><p>\u6a21\u677f\u5339\u914d\u662f\u7528\u4e8e\u5728\u56fe\u50cf\u4e2d\u67e5\u627e\u6a21\u677f\u56fe\u50cf\u4f4d\u7f6e\u7684\u6280\u672f\u3002OpenCV\u63d0\u4f9b\u4e86cv2.matchTemplate()\u51fd\u6570\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u6a21\u677f\u5339\u914d\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8bfb\u53d6\u6a21\u677f\u56fe\u50cf<\/p>\n<p>template = cv2.imread(&#39;path_to_template.jpg&#39;, cv2.IMREAD_GRAYSCALE)<\/p>\n<p>w, h = template.shape[::-1]<\/p>\n<h2><strong>\u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe\u50cf<\/strong><\/h2>\n<p>gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)<\/p>\n<h2><strong>\u5e94\u7528\u6a21\u677f\u5339\u914d<\/strong><\/h2>\n<p>res = cv2.matchTemplate(gray_image, template, cv2.TM_CCOEFF_NORMED)<\/p>\n<h2><strong>\u83b7\u53d6\u5339\u914d\u4f4d\u7f6e<\/strong><\/h2>\n<p>min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)<\/p>\n<h2><strong>\u7ed8\u5236\u5339\u914d\u533a\u57df<\/strong><\/h2>\n<p>top_left = max_loc<\/p>\n<p>bottom_right = (top_left[0] + w, top_left[1] + h)<\/p>\n<p>cv2.rectangle(image, top_left, bottom_right, (0, 255, 0), 2)<\/p>\n<h2><strong>\u663e\u793a\u7ed3\u679c<\/strong><\/h2>\n<p>cv2.imshow(&#39;Matched Image&#39;, image)<\/p>\n<p>cv2.waitKey(0)<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5341\u4e00\u3001\u56fe\u50cf\u7279\u5f81\u68c0\u6d4b\u4e0e\u63cf\u8ff0<\/p>\n<\/p>\n<p><p>OpenCV\u63d0\u4f9b\u4e86\u591a\u79cd\u7b97\u6cd5\u7528\u4e8e\u56fe\u50cf\u7279\u5f81\u68c0\u6d4b\u548c\u63cf\u8ff0\uff0c\u5982SIFT\u3001SURF\u548cORB\u3002\u8fd9\u4e9b\u7b97\u6cd5\u53ef\u4ee5\u7528\u4e8e\u56fe\u50cf\u5339\u914d\u3001\u7269\u4f53\u8bc6\u522b\u7b49\u4efb\u52a1\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528ORB\u7279\u5f81\u68c0\u6d4b\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efaORB\u68c0\u6d4b\u5668<\/p>\n<p>orb = cv2.ORB_create()<\/p>\n<h2><strong>\u68c0\u6d4b\u5173\u952e\u70b9<\/strong><\/h2>\n<p>keypoints = orb.detect(image, None)<\/p>\n<h2><strong>\u8ba1\u7b97\u63cf\u8ff0\u7b26<\/strong><\/h2>\n<p>keypoints, descriptors = orb.compute(image, keypoints)<\/p>\n<h2><strong>\u7ed8\u5236\u5173\u952e\u70b9<\/strong><\/h2>\n<p>image_with_keypoints = cv2.drawKeypoints(image, keypoints, None, color=(0, 255, 0), flags=0)<\/p>\n<h2><strong>\u663e\u793a\u7ed3\u679c<\/strong><\/h2>\n<p>cv2.imshow(&#39;ORB Keypoints&#39;, image_with_keypoints)<\/p>\n<p>cv2.waitKey(0)<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5341\u4e8c\u3001\u56fe\u50cf\u7684\u900f\u89c6\u53d8\u6362<\/p>\n<\/p>\n<p><p>\u900f\u89c6\u53d8\u6362\u7528\u4e8e\u5c06\u56fe\u50cf\u4ece\u4e00\u4e2a\u89c6\u89d2\u8f6c\u6362\u5230\u53e6\u4e00\u4e2a\u89c6\u89d2\u3002OpenCV\u63d0\u4f9b\u4e86cv2.getPerspectiveTransform()\u548ccv2.warpPerspective()\u51fd\u6570\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u56db\u4e2a\u70b9<\/p>\n<p>pts1 = np.float32([[x1, y1], [x2, y2], [x3, y3], [x4, y4]])<\/p>\n<p>pts2 = np.float32([[x1&#39;, y1&#39;], [x2&#39;, y2&#39;], [x3&#39;, y3&#39;], [x4&#39;, y4&#39;]])<\/p>\n<h2><strong>\u83b7\u53d6\u900f\u89c6\u53d8\u6362\u77e9\u9635<\/strong><\/h2>\n<p>matrix = cv2.getPerspectiveTransform(pts1, pts2)<\/p>\n<h2><strong>\u5e94\u7528\u900f\u89c6\u53d8\u6362<\/strong><\/h2>\n<p>warped_image = cv2.warpPerspective(image, matrix, (width, height))<\/p>\n<h2><strong>\u663e\u793a\u7ed3\u679c<\/strong><\/h2>\n<p>cv2.imshow(&#39;Warped Image&#39;, warped_image)<\/p>\n<p>cv2.waitKey(0)<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5341\u4e09\u3001\u56fe\u50cf\u62fc\u63a5<\/p>\n<\/p>\n<p><p>\u56fe\u50cf\u62fc\u63a5\u7528\u4e8e\u5c06\u591a\u5f20\u56fe\u50cf\u5408\u5e76\u4e3a\u4e00\u5f20\u5168\u666f\u56fe\u3002OpenCV\u63d0\u4f9b\u4e86Stitcher\u7c7b\u7528\u4e8e\u56fe\u50cf\u62fc\u63a5\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8bfb\u53d6\u56fe\u50cf<\/p>\n<p>images = [cv2.imread(&#39;path_to_image1.jpg&#39;), cv2.imread(&#39;path_to_image2.jpg&#39;)]<\/p>\n<h2><strong>\u521b\u5efa\u62fc\u63a5\u5668<\/strong><\/h2>\n<p>stitcher = cv2.Stitcher_create()<\/p>\n<h2><strong>\u62fc\u63a5\u56fe\u50cf<\/strong><\/h2>\n<p>status, stitched_image = stitcher.stitch(images)<\/p>\n<h2><strong>\u663e\u793a\u7ed3\u679c<\/strong><\/h2>\n<p>if status == cv2.Stitcher_OK:<\/p>\n<p>    cv2.imshow(&#39;Stitched Image&#39;, stitched_image)<\/p>\n<p>else:<\/p>\n<p>    print(&#39;Stitching failed!&#39;)<\/p>\n<p>cv2.waitKey(0)<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5341\u56db\u3001\u56fe\u50cf\u7684\u80cc\u666f\u5206\u5272<\/p>\n<\/p>\n<p><p>\u80cc\u666f\u5206\u5272\u7528\u4e8e\u4ece\u56fe\u50cf\u4e2d\u5206\u79bb\u524d\u666f\u548c\u80cc\u666f\u3002OpenCV\u63d0\u4f9b\u4e86cv2.createBackgroundSubtractorMOG2()\u548ccv2.createBackgroundSubtractorKNN()\u7b49\u51fd\u6570\u7528\u4e8e\u80cc\u666f\u5206\u5272\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u80cc\u666f\u51cf\u6cd5\u5668<\/p>\n<p>backSub = cv2.createBackgroundSubtractorMOG2()<\/p>\n<h2><strong>\u8bfb\u53d6\u89c6\u9891<\/strong><\/h2>\n<p>cap = cv2.VideoCapture(&#39;path_to_video.mp4&#39;)<\/p>\n<p>while cap.isOpened():<\/p>\n<p>    ret, frame = cap.read()<\/p>\n<p>    if not ret:<\/p>\n<p>        break<\/p>\n<p>    # \u5e94\u7528\u80cc\u666f\u5206\u5272<\/p>\n<p>    fgMask = backSub.apply(frame)<\/p>\n<p>    # \u663e\u793a\u7ed3\u679c<\/p>\n<p>    cv2.imshow(&#39;FG Mask&#39;, fgMask)<\/p>\n<p>    if cv2.waitKey(30) &amp; 0xFF == ord(&#39;q&#39;):<\/p>\n<p>        break<\/p>\n<p>cap.release()<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5341\u4e94\u3001\u9762\u90e8\u68c0\u6d4b<\/p>\n<\/p>\n<p><p>OpenCV\u63d0\u4f9b\u4e86\u9762\u90e8\u68c0\u6d4b\u529f\u80fd\uff0c\u901a\u5e38\u4f7f\u7528\u9884\u8bad\u7ec3\u7684Haar\u7ea7\u8054\u5206\u7c7b\u5668\u8fdb\u884c\u68c0\u6d4b\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u9762\u90e8\u68c0\u6d4b\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u52a0\u8f7d\u9884\u8bad\u7ec3\u7684Haar\u7ea7\u8054\u5206\u7c7b\u5668<\/p>\n<p>face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + &#39;haarcascade_frontalface_default.xml&#39;)<\/p>\n<h2><strong>\u8f6c\u6362\u4e3a\u7070\u5ea6\u56fe\u50cf<\/strong><\/h2>\n<p>gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)<\/p>\n<h2><strong>\u68c0\u6d4b\u9762\u90e8<\/strong><\/h2>\n<p>faces = face_cascade.detectMultiScale(gray_image, scaleFactor=1.1, minNeighbors=5)<\/p>\n<h2><strong>\u7ed8\u5236\u9762\u90e8\u77e9\u5f62<\/strong><\/h2>\n<p>for (x, y, w, h) in faces:<\/p>\n<p>    cv2.rectangle(image, (x, y), (x+w, y+h), (255, 0, 0), 2)<\/p>\n<h2><strong>\u663e\u793a\u7ed3\u679c<\/strong><\/h2>\n<p>cv2.imshow(&#39;Faces&#39;, image)<\/p>\n<p>cv2.waitKey(0)<\/p>\n<p>cv2.destroyAllWindows()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u7684\u4ecb\u7ecd\uff0c\u4f60\u53ef\u4ee5\u638c\u63e1Python\u4e2dOpenCV\u5e93\u7684\u57fa\u672c\u4f7f\u7528\u65b9\u6cd5\u548c\u5e38\u89c1\u5e94\u7528\u573a\u666f\u3002OpenCV\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684\u5e93\uff0c\u5e7f\u6cdb\u5e94\u7528\u4e8e\u56fe\u50cf\u5904\u7406\u3001\u8ba1\u7b97\u673a\u89c6\u89c9\u548c\u673a\u5668\u5b66\u4e60\u9886\u57df\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u80fd\u591f\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u4f7f\u7528OpenCV\u5e93\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>Python\u4e2dcv\u5e93\u7684\u57fa\u672c\u529f\u80fd\u6709\u54ea\u4e9b\uff1f<\/strong><br \/>cv\u5e93\uff0c\u5373OpenCV\uff08Open Source Computer Vision Library\uff09\uff0c\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u8ba1\u7b97\u673a\u89c6\u89c9\u5e93\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\uff0c\u5305\u62ec\u56fe\u50cf\u5904\u7406\u3001\u89c6\u9891\u5206\u6790\u3001\u7279\u5f81\u68c0\u6d4b\u4e0e\u63cf\u8ff0\u3001\u5bf9\u8c61\u8bc6\u522b\u7b49\u3002\u4f7f\u7528Python\u7684cv\u5e93\uff0c\u7528\u6237\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u56fe\u50cf\u7684\u8bfb\u53d6\u3001\u663e\u793a\u3001\u7f16\u8f91\u548c\u4fdd\u5b58\uff0c\u8fdb\u884c\u8fb9\u7f18\u68c0\u6d4b\u3001\u56fe\u50cf\u53d8\u6362\u4ee5\u53ca\u5e94\u7528\u673a\u5668\u5b66\u4e60\u6a21\u578b\u8fdb\u884c\u56fe\u50cf\u5206\u7c7b\u7b49\u64cd\u4f5c\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5b89\u88c5cv\u5e93\uff1f<\/strong><br \/>\u5b89\u88c5cv\u5e93\u975e\u5e38\u7b80\u5355\uff0c\u53ef\u4ee5\u4f7f\u7528pip\u5de5\u5177\u3002\u53ea\u9700\u5728\u7ec8\u7aef\u6216\u547d\u4ee4\u63d0\u793a\u7b26\u4e2d\u8f93\u5165<code>pip install opencv-python<\/code>\u5373\u53ef\u5b89\u88c5\u57fa\u672c\u7684OpenCV\u5e93\u3002\u5982\u679c\u9700\u8981\u66f4\u5168\u9762\u7684\u529f\u80fd\uff0c\u8fd8\u53ef\u4ee5\u5b89\u88c5<code>opencv-python-headless<\/code>\uff0c\u5b83\u9002\u7528\u4e8e\u4e0d\u9700\u8981GUI\u7684\u73af\u5883\uff0c\u547d\u4ee4\u4e3a<code>pip install opencv-python-headless<\/code>\u3002\u786e\u4fdd\u5728\u5b89\u88c5\u524d\u5df2\u5b89\u88c5Python\u548cpip\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528cv\u5e93\u8fdb\u884c\u56fe\u50cf\u5904\u7406\uff1f<\/strong><br \/>\u4f7f\u7528cv\u5e93\u8fdb\u884c\u56fe\u50cf\u5904\u7406\u7684\u57fa\u672c\u6d41\u7a0b\u5305\u62ec\uff1a\u5bfc\u5165cv\u5e93\uff0c\u8bfb\u53d6\u56fe\u50cf\uff0c\u8fdb\u884c\u5904\u7406\uff08\u5982\u8c03\u6574\u5927\u5c0f\u3001\u6ee4\u6ce2\u3001\u8fb9\u7f18\u68c0\u6d4b\u7b49\uff09\uff0c\u7136\u540e\u663e\u793a\u6216\u4fdd\u5b58\u5904\u7406\u540e\u7684\u56fe\u50cf\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">import cv2\n\n# \u8bfb\u53d6\u56fe\u50cf\nimage = cv2.imread(&#39;image.jpg&#39;)\n\n# \u8c03\u6574\u56fe\u50cf\u5927\u5c0f\nresized_image = cv2.resize(image, (640, 480))\n\n# \u5e94\u7528\u8fb9\u7f18\u68c0\u6d4b\nedges = cv2.Canny(resized_image, 100, 200)\n\n# \u663e\u793a\u56fe\u50cf\ncv2.imshow(&#39;Edges&#39;, edges)\ncv2.waitKey(0)\ncv2.destroyAllWindows()\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u7528\u6237\u53ef\u4ee5\u5feb\u901f\u4e0a\u624b\u5e76\u5f00\u59cb\u8fdb\u884c\u5404\u79cd\u56fe\u50cf\u5904\u7406\u4efb\u52a1\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u7684OpenCV\u5e93\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u7528\u4e8e\u56fe\u50cf\u5904\u7406\u3001\u8ba1\u7b97\u673a\u89c6\u89c9\u548c\u673a\u5668\u5b66\u4e60\u3002\u8981\u4f7f\u7528OpenCV\u5e93\uff0c\u4f60\u9700 [&hellip;]","protected":false},"author":3,"featured_media":994934,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/994918"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=994918"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/994918\/revisions"}],"predecessor-version":[{"id":994939,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/994918\/revisions\/994939"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/994934"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=994918"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=994918"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=994918"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}