{"id":1956,"date":"2022-01-13T16:35:36","date_gmt":"2022-01-13T08:35:36","guid":{"rendered":"http:\/\/139.9.1.231\/?p=1956"},"modified":"2022-01-13T17:20:23","modified_gmt":"2022-01-13T09:20:23","slug":"opencv1","status":"publish","type":"post","link":"http:\/\/139.9.1.231\/index.php\/2022\/01\/13\/opencv1\/","title":{"rendered":"opencv \u89c6\u9891\u5bf9\u8c61\u8ffd\u8e2a"},"content":{"rendered":"\n<p>1\u3001\u80cc\u666f\u5206\u79bb\u65b9\u6cd5<\/p>\n\n\n\n<ul><li>\u80cc\u666f\u5206\u79bb\uff08BS\uff09\u662f\u4e00\u79cd\u901a\u8fc7\u4f7f\u7528\u9759\u6001\u76f8\u673a\u6765\u751f\u6210\u524d\u666f\u63a9\u7801\uff08\u5373\u5305\u542b\u5c5e\u4e8e\u573a\u666f\u4e2d\u7684\u79fb\u52a8\u5bf9\u8c61\u50cf\u7d20\u7684\u4e8c\u8fdb\u5236\u56fe\u50cf\uff09\u7684\u5e38\u7528\u6280\u672f\u3002<\/li><li>\u987e\u540d\u601d\u4e49\uff0cBS\u8ba1\u7b97\u524d\u666f\u63a9\u7801\uff0c\u5728\u5f53\u524d\u5e27\u4e0e\u80cc\u666f\u6a21\u578b\u4e4b\u95f4\u6267\u884c\u51cf\u6cd5\u8fd0\u7b97\uff0c\u5176\u4e2d\u5305\u542b\u573a\u666f\u7684\u9759\u6001\u90e8\u5206\uff0c\u6216\u8005\u66f4\u4e00\u822c\u800c\u8a00\uff0c\u8003\u8651\u5230\u6240\u89c2\u5bdf\u573a\u666f\u7684\u7279\u5f81\uff0c\u53ef\u4ee5\u5c06\u5176\u89c6\u4e3a\u80cc\u666f\u7684\u6240\u6709\u5185\u5bb9\u3002<\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image is-style-default\"><img src=\"http:\/\/qiniu.aihubs.net\/Background_Subtraction_Tutorial_Scheme.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>\u80cc\u666f\u5efa\u6a21\u5305\u62ec\u4e24\u4e2a\u4e3b\u8981\u6b65\u9aa4\uff1a 1. \u80cc\u666f\u521d\u59cb\u5316\uff1b 2. \u80cc\u666f\u66f4\u65b0\u3002<\/p>\n\n\n\n<p>\u7b2c\u4e00\u6b65\uff0c\u8ba1\u7b97\u80cc\u666f\u7684\u521d\u59cb\u6a21\u578b\uff0c\u800c\u5728\u7b2c\u4e8c\u6b65\u4e2d\uff0c\u66f4\u65b0\u6a21\u578b\u4ee5\u9002\u5e94\u573a\u666f\u4e2d\u53ef\u80fd\u7684\u53d8\u5316\u3002<\/p>\n\n\n\n<p>\u5728\u672c\u6559\u7a0b\u4e2d\uff0c\u6211\u4eec\u5c06\u5b66\u4e60\u5982\u4f55\u4f7f\u7528OpenCV\u4e2d\u7684BS\u3002<\/p>\n\n\n\n<h3 id=\"_2\">\u76ee\u6807<a href=\"http:\/\/woshicver.com\/Seventh\/6_1_%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8%E8%83%8C%E6%99%AF%E5%88%86%E7%A6%BB%E6%96%B9%E6%B3%95\/#_2\"><\/a><\/h3>\n\n\n\n<p>\u5728\u672c\u6559\u7a0b\u4e2d\uff0c\u60a8\u5c06\u5b66\u4e60\u5982\u4f55\uff1a 1. \u4f7f\u7528**cv::VideoCapture**\u4ece\u89c6\u9891\u6216\u56fe\u50cf\u5e8f\u5217\u4e2d\u8bfb\u53d6\u6570\u636e\uff1b 2. \u901a\u8fc7\u4f7f\u7528**cv::BackgroundSubtractor**\u7c7b\u521b\u5efa\u548c\u66f4\u65b0\u80cc\u666f\u7c7b\uff1b 3. \u901a\u8fc7\u4f7f\u7528**cv::imshow**\u83b7\u53d6\u5e76\u663e\u793a\u524d\u666f\u8499\u7248\uff1b<\/p>\n\n\n\n<h4 id=\"_3\">\u4ee3\u7801<a href=\"http:\/\/woshicver.com\/Seventh\/6_1_%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8%E8%83%8C%E6%99%AF%E5%88%86%E7%A6%BB%E6%96%B9%E6%B3%95\/#_3\"><\/a><\/h4>\n\n\n\n<p>\u5728\u4e0b\u9762\uff0c\u60a8\u53ef\u4ee5\u627e\u5230\u6e90\u4ee3\u7801\u3002\u6211\u4eec\u5c06\u8ba9\u7528\u6237\u9009\u62e9\u5904\u7406\u89c6\u9891\u6587\u4ef6\u6216\u56fe\u50cf\u5e8f\u5217\u3002\u5728\u6b64\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c06\u4f7f\u7528**cv::BackgroundSubtractorMOG2**\u751f\u6210\u524d\u666f\u63a9\u7801\u3002<\/p>\n\n\n\n<p>\u7ed3\u679c\u548c\u8f93\u5165\u6570\u636e\u5c06\u663e\u793a\u5728\u5c4f\u5e55\u4e0a\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>from<\/strong> __future__ <strong>import<\/strong> print_function\n<strong>import<\/strong> cv2 <strong>as<\/strong> cv\n<strong>import<\/strong> argparse\nparser = argparse.ArgumentParser(description='This program shows how to use background subtraction methods provided by \\\n                                              OpenCV. You can process both videos and images.')\nparser.add_argument('--input', type=str, help='Path to a video or a sequence of image.', default='vtest.avi')\nparser.add_argument('--algo', type=str, help='Background subtraction method (KNN, MOG2).', default='MOG2')\nargs = parser.parse_args()\n<strong>if<\/strong> args.algo == 'MOG2':\n    backSub = cv.createBackgroundSubtractorMOG2()\n<strong>else<\/strong>:\n    backSub = cv.createBackgroundSubtractorKNN()\ncapture = cv.VideoCapture(cv.samples.findFileOrKeep(args.input))\n<strong>if<\/strong> <strong>not<\/strong> capture.isOpened:\n    print('Unable to open: ' + args.input)\n    exit(0)\n<strong>while<\/strong> <strong>True<\/strong>:\n    ret, frame = capture.read()\n    <strong>if<\/strong> frame <strong>is<\/strong> <strong>None<\/strong>:\n        <strong>break<\/strong>\n\n    fgMask = backSub.apply(frame)\n\n\n    cv.rectangle(frame, (10, 2), (100,20), (255,255,255), -1)\n    cv.putText(frame, str(capture.get(cv.CAP_PROP_POS_FRAMES)), (15, 15),\n               cv.FONT_HERSHEY_SIMPLEX, 0.5 , (0,0,0))\n\n\n    cv.imshow('Frame', frame)\n    cv.imshow('FG Mask', fgMask)\n\n    keyboard = cv.waitKey(30)\n    <strong>if<\/strong> keyboard == 'q' <strong>or<\/strong> keyboard == 27:\n        <strong>break<\/strong><\/code><\/pre>\n\n\n\n<p>2\u3001Meanshift\u548cCamshift<\/p>\n\n\n\n<h3 id=\"meanshift\">Meanshift<a href=\"http:\/\/woshicver.com\/Seventh\/6_2_Meanshift%E5%92%8CCamshift\/#meanshift\"><\/a><\/h3>\n\n\n\n<p>Meanshift\u80cc\u540e\u7684\u76f4\u89c9\u5f88\u7b80\u5355\uff0c\u5047\u8bbe\u4f60\u6709\u70b9\u7684\u96c6\u5408\u3002\uff08\u5b83\u53ef\u4ee5\u662f\u50cf\u7d20\u5206\u5e03\uff0c\u4f8b\u5982\u76f4\u65b9\u56fe\u53cd\u6295\u5f71\uff09\u3002\u4f60\u4f1a\u5f97\u5230\u4e00\u4e2a\u5c0f\u7a97\u53e3\uff08\u53ef\u80fd\u662f\u4e00\u4e2a\u5706\u5f62\uff09\uff0c\u5e76\u4e14\u5fc5\u987b\u5c06\u8be5\u7a97\u53e3\u79fb\u5230\u6700\u5927\u50cf\u7d20\u5bc6\u5ea6\uff08\u6216\u6700\u5927\u70b9\u6570\uff09\u7684\u533a\u57df\u3002\u5982\u4e0b\u56fe\u6240\u793a\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image is-style-default\"><img src=\"http:\/\/qiniu.aihubs.net\/meanshift_basics.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<p>\u521d\u59cb\u7a97\u53e3\u4ee5\u84dd\u8272\u5706\u5708\u663e\u793a\uff0c\u540d\u79f0\u4e3a\u201cC1\u201d\u3002\u5176\u539f\u59cb\u4e2d\u5fc3\u4ee5\u84dd\u8272\u77e9\u5f62\u6807\u8bb0\uff0c\u540d\u79f0\u4e3a\u201cC1_o\u201d\u3002\u4f46\u662f\uff0c\u5982\u679c\u627e\u5230\u8be5\u7a97\u53e3\u5185\u70b9\u7684\u8d28\u5fc3\uff0c\u5219\u4f1a\u5f97\u5230\u70b9\u201cC1_r\u201d\uff08\u6807\u8bb0\u4e3a\u84dd\u8272\u5c0f\u5706\u5708\uff09\uff0c\u5b83\u662f\u7a97\u53e3\u7684\u771f\u5b9e\u8d28\u5fc3\u3002\u5f53\u7136\uff0c\u5b83\u4eec\u4e0d\u5339\u914d\u3002\u56e0\u6b64\uff0c\u79fb\u52a8\u7a97\u53e3\uff0c\u4f7f\u65b0\u7a97\u53e3\u7684\u5706\u4e0e\u4e0a\u4e00\u4e2a\u8d28\u5fc3\u5339\u914d\u3002\u518d\u6b21\u627e\u5230\u65b0\u7684\u8d28\u5fc3\u3002\u5f88\u53ef\u80fd\u4e0d\u4f1a\u5339\u914d\u3002\u56e0\u6b64\uff0c\u518d\u6b21\u79fb\u52a8\u5b83\uff0c\u5e76\u7ee7\u7eed\u8fed\u4ee3\uff0c\u4ee5\u4f7f\u7a97\u53e3\u7684\u4e2d\u5fc3\u53ca\u5176\u8d28\u5fc3\u843d\u5728\u540c\u4e00\u4f4d\u7f6e\uff08\u6216\u5728\u5f88\u5c0f\u7684\u671f\u671b\u8bef\u5dee\u5185\uff09\u3002\u56e0\u6b64\uff0c\u6700\u7ec8\u60a8\u83b7\u5f97\u7684\u662f\u4e00\u4e2a\u5177\u6709\u6700\u5927\u50cf\u7d20\u5206\u5e03\u7684\u7a97\u53e3\u3002\u5b83\u5e26\u6709\u4e00\u4e2a\u7eff\u8272\u5706\u5708\uff0c\u540d\u4e3a\u201cC2\u201d\u3002\u6b63\u5982\u60a8\u5728\u56fe\u50cf\u4e2d\u770b\u5230\u7684\uff0c\u5b83\u5177\u6709\u6700\u5927\u7684\u70b9\u6570\u3002\u6574\u4e2a\u8fc7\u7a0b\u5728\u4e0b\u9762\u7684\u9759\u6001\u56fe\u50cf\u4e0a\u6f14\u793a\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image is-style-default\"><img src=\"http:\/\/qiniu.aihubs.net\/camshift_face.gif\" alt=\"\"\/><\/figure>\n\n\n\n<p>\u56e0\u6b64\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u4f20\u9012\u76f4\u65b9\u56fe\u53cd\u6295\u5f71\u56fe\u50cf\u548c\u521d\u59cb\u76ee\u6807\u4f4d\u7f6e\u3002\u5f53\u5bf9\u8c61\u79fb\u52a8\u65f6\uff0c\u663e\u7136\u8be5\u79fb\u52a8\u4f1a\u53cd\u6620\u5728\u76f4\u65b9\u56fe\u53cd\u6295\u5f71\u56fe\u50cf\u4e2d\u3002\u7ed3\u679c\uff0cmeanshift\u7b97\u6cd5\u5c06\u7a97\u53e3\u79fb\u52a8\u5230\u6700\u5927\u5bc6\u5ea6\u7684\u65b0\u4f4d\u7f6e\u3002<\/p>\n\n\n\n<h3 id=\"opencvmeanshift\">OpenCV\u4e2d\u7684Meanshift<a href=\"http:\/\/woshicver.com\/Seventh\/6_2_Meanshift%E5%92%8CCamshift\/#opencvmeanshift\"><\/a><\/h3>\n\n\n\n<p>\u8981\u5728OpenCV\u4e2d\u4f7f\u7528meanshift\uff0c\u9996\u5148\u6211\u4eec\u9700\u8981\u8bbe\u7f6e\u76ee\u6807\uff0c\u627e\u5230\u5176\u76f4\u65b9\u56fe\uff0c\u4ee5\u4fbf\u6211\u4eec\u53ef\u4ee5\u5c06\u76ee\u6807\u53cd\u6295\u5f71\u5230\u6bcf\u5e27\u4e0a\u4ee5\u8ba1\u7b97\u5747\u503c\u504f\u79fb\u3002\u6211\u4eec\u8fd8\u9700\u8981\u63d0\u4f9b\u7a97\u53e3\u7684\u521d\u59cb\u4f4d\u7f6e\u3002\u5bf9\u4e8e\u76f4\u65b9\u56fe\uff0c\u6b64\u5904\u4ec5\u8003\u8651\u8272\u76f8\u3002\u53e6\u5916\uff0c\u4e3a\u907f\u514d\u7531\u4e8e\u5149\u7ebf\u4e0d\u8db3\u800c\u4ea7\u751f\u9519\u8bef\u7684\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528**cv.inRange**()\u51fd\u6570\u4e22\u5f03\u5149\u7ebf\u4e0d\u8db3\u7684\u503c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>import<\/strong> numpy <strong>as<\/strong> np\n<strong>import<\/strong> cv2 <strong>as<\/strong> cv\n<strong>import<\/strong> argparse\nparser = argparse.ArgumentParser(description='This sample demonstrates the meanshift algorithm. \\\n                                              The example file can be downloaded from: \\\n                                              https:&#47;&#47;www.bogotobogo.com\/python\/OpenCV_Python\/images\/mean_shift_tracking\/slow_traffic_small.mp4')\nparser.add_argument('image', type=str, help='path to image file')\nargs = parser.parse_args()\ncap = cv.VideoCapture(args.image)\n<em># \u89c6\u9891\u7684\u7b2c\u4e00\u5e27<\/em>\nret,frame = cap.read()\n<em># \u8bbe\u7f6e\u7a97\u53e3\u7684\u521d\u59cb\u4f4d\u7f6e<\/em>\nx, y, w, h = 300, 200, 100, 50 <em># simply hardcoded the values<\/em>\ntrack_window = (x, y, w, h)\n<em># \u8bbe\u7f6e\u521d\u59cbROI\u6765\u8ffd\u8e2a<\/em>\nroi = frame&#91;y:y+h, x:x+w]\nhsv_roi =  cv.cvtColor(roi, cv.COLOR_BGR2HSV)\nmask = cv.inRange(hsv_roi, np.array((0., 60.,32.)), np.array((180.,255.,255.)))\nroi_hist = cv.calcHist(&#91;hsv_roi],&#91;0],mask,&#91;180],&#91;0,180])\ncv.normalize(roi_hist,roi_hist,0,255,cv.NORM_MINMAX)\n<em># \u8bbe\u7f6e\u7ec8\u6b62\u6761\u4ef6\uff0c\u53ef\u4ee5\u662f10\u6b21\u8fed\u4ee3\uff0c\u4e5f\u53ef\u4ee5\u81f3\u5c11\u79fb\u52a81 pt<\/em>\nterm_crit = ( cv.TERM_CRITERIA_EPS | cv.TERM_CRITERIA_COUNT, 10, 1 )\n<strong>while<\/strong>(1):\n    ret, frame = cap.read()\n    <strong>if<\/strong> ret == <strong>True<\/strong>:\n        hsv = cv.cvtColor(frame, cv.COLOR_BGR2HSV)\n        dst = cv.calcBackProject(&#91;hsv],&#91;0],roi_hist,&#91;0,180],1)\n        <em># \u5e94\u7528meanshift\u6765\u83b7\u53d6\u65b0\u4f4d\u7f6e<\/em>\n        ret, track_window = cv.meanShift(dst, track_window, term_crit)\n        <em># \u5728\u56fe\u50cf\u4e0a\u7ed8\u5236<\/em>\n        x,y,w,h = track_window\n        img2 = cv.rectangle(frame, (x,y), (x+w,y+h), 255,2)\n        cv.imshow('img2',img2)\n        k = cv.waitKey(30) &amp; 0xff\n        <strong>if<\/strong> k == 27:\n            <strong>break<\/strong>\n    <strong>else<\/strong>:\n        <strong>break<\/strong><\/code><\/pre>\n\n\n\n<p>\u6211\u4f7f\u7528\u7684\u89c6\u9891\u4e2d\u7684\u4e09\u5e27\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image is-style-default\"><img src=\"http:\/\/qiniu.aihubs.net\/camshift_result.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<h2 id=\"camshift\">Camshift<a href=\"http:\/\/woshicver.com\/Seventh\/6_2_Meanshift%E5%92%8CCamshift\/#camshift\"><\/a><\/h2>\n\n\n\n<p>\u60a8\u662f\u5426\u5bc6\u5207\u5173\u6ce8\u4e86\u6700\u540e\u7ed3\u679c\uff1f\u8fd9\u513f\u5b58\u5728\u4e00\u4e2a\u95ee\u9898\u3002\u65e0\u8bba\u6c7d\u8f66\u79bb\u76f8\u673a\u5f88\u8fd1\u6216\u975e\u5e38\u8fd1\uff0c\u6211\u4eec\u7684\u7a97\u53e3\u59cb\u7ec8\u5177\u6709\u76f8\u540c\u7684\u5927\u5c0f\u3002\u8fd9\u662f\u4e0d\u597d\u7684\u3002\u6211\u4eec\u9700\u8981\u6839\u636e\u76ee\u6807\u7684\u5927\u5c0f\u548c\u65cb\u8f6c\u6765\u8c03\u6574\u7a97\u53e3\u5927\u5c0f\u3002\u8be5\u89e3\u51b3\u65b9\u6848\u518d\u6b21\u6765\u81ea\u201c OpenCV Labs\u201d\uff0c\u5b83\u88ab\u79f0\u4e3aGary\u5e03\u62c9\u5fb7\u65af\u57fa\uff08Gary Bradsky\uff09\u5728\u51761998\u5e74\u7684\u8bba\u6587\u201c\u7528\u4e8e\u611f\u77e5\u7528\u6237\u754c\u9762\u4e2d\u7684\u8ba1\u7b97\u673a\u89c6\u89c9\u9762\u90e8\u8ddf\u8e2a\u201d\u4e2d\u53d1\u8868\u7684CAMshift\uff08\u8fde\u7eed\u81ea\u9002\u5e94\u5747\u503c\u504f\u79fb\uff09[26]\u3002 \u5b83\u9996\u5148\u5e94\u7528Meanshift\u3002\u4e00\u65e6Meanshift\u6536\u655b\uff0c\u5b83\u5c06\u66f4\u65b0\u7a97\u53e3\u7684\u5927\u5c0f\u4e3a<code>s = 2 \\times \\sqrt{\\frac{M_{00}}{256}}<\/code>\u3002\u5b83\u8fd8\u53ef\u4ee5\u8ba1\u7b97\u51fa\u6700\u5408\u9002\u7684\u692d\u5706\u7684\u65b9\u5411\u3002\u518d\u6b21\u5c06\u5747\u503c\u504f\u79fb\u5e94\u7528\u4e8e\u65b0\u7684\u7f29\u653e\u641c\u7d22\u7a97\u53e3\u548c\u5148\u524d\u7684\u7a97\u53e3\u4f4d\u7f6e\u3002\u8be5\u8fc7\u7a0b\u4e00\u76f4\u6301\u7eed\u5230\u8fbe\u5230\u8981\u6c42\u7684\u7cbe\u5ea6\u4e3a\u6b62\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image is-style-default\"><img src=\"http:\/\/qiniu.aihubs.net\/camshift_face.gif\" alt=\"camshift_face\"\/><\/figure>\n\n\n\n<h3 id=\"opencvcamshift\">OpenCV\u4e2d\u7684Camshift<a href=\"http:\/\/woshicver.com\/Seventh\/6_2_Meanshift%E5%92%8CCamshift\/#opencvcamshift\"><\/a><\/h3>\n\n\n\n<p>\u5b83\u4e0emeanshift\u76f8\u4f3c\uff0c\u4f46\u662f\u8fd4\u56de\u4e00\u4e2a\u65cb\u8f6c\u7684\u77e9\u5f62\uff08\u5373\u6211\u4eec\u7684\u7ed3\u679c\uff09\u548cbox\u53c2\u6570\uff08\u7528\u4e8e\u5728\u4e0b\u4e00\u6b21\u8fed\u4ee3\u4e2d\u4f5c\u4e3a\u641c\u7d22\u7a97\u53e3\u4f20\u9012\uff09\u3002\u8bf7\u53c2\u89c1\u4e0b\u9762\u7684\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>import<\/strong> numpy <strong>as<\/strong> np\n<strong>import<\/strong> cv2 <strong>as<\/strong> cv\n<strong>import<\/strong> argparse\nparser = argparse.ArgumentParser(description='This sample demonstrates the camshift algorithm. \\\n                                              The example file can be downloaded from: \\\n                                              https:&#47;&#47;www.bogotobogo.com\/python\/OpenCV_Python\/images\/mean_shift_tracking\/slow_traffic_small.mp4')\nparser.add_argument('image', type=str, help='path to image file')\nargs = parser.parse_args()\ncap = cv.VideoCapture(args.image)\n<em># \u83b7\u53d6\u89c6\u9891\u7b2c\u4e00\u5e27<\/em>\nret,frame = cap.read()\n<em># \u8bbe\u7f6e\u521d\u59cb\u7a97\u53e3<\/em>\nx, y, w, h = 300, 200, 100, 50 <em># simply hardcoded the values<\/em>\ntrack_window = (x, y, w, h)\n<em># \u8bbe\u7f6e\u8ffd\u8e2a\u7684ROI\u7a97\u53e3<\/em>\nroi = frame&#91;y:y+h, x:x+w]\nhsv_roi =  cv.cvtColor(roi, cv.COLOR_BGR2HSV)\nmask = cv.inRange(hsv_roi, np.array((0., 60.,32.)), np.array((180.,255.,255.)))\nroi_hist = cv.calcHist(&#91;hsv_roi],&#91;0],mask,&#91;180],&#91;0,180])\ncv.normalize(roi_hist,roi_hist,0,255,cv.NORM_MINMAX)\n<em># \u8bbe\u7f6e\u7ec8\u6b62\u6761\u4ef6\uff0c\u53ef\u4ee5\u662f10\u6b21\u8fed\u4ee3\uff0c\u6709\u53ef\u4ee5\u81f3\u5c11\u79fb\u52a81\u4e2a\u50cf\u7d20<\/em>\nterm_crit = ( cv.TERM_CRITERIA_EPS | cv.TERM_CRITERIA_COUNT, 10, 1 )\n<strong>while<\/strong>(1):\n    ret, frame = cap.read()\n    <strong>if<\/strong> ret == <strong>True<\/strong>:\n        hsv = cv.cvtColor(frame, cv.COLOR_BGR2HSV)\n        dst = cv.calcBackProject(&#91;hsv],&#91;0],roi_hist,&#91;0,180],1)\n        <em># \u5e94\u7528camshift \u5230\u65b0\u4f4d\u7f6e<\/em>\n        ret, track_window = cv.CamShift(dst, track_window, term_crit)\n        <em># \u5728\u56fe\u50cf\u4e0a\u753b\u51fa\u6765<\/em>\n        pts = cv.boxPoints(ret)\n        pts = np.int0(pts)\n        img2 = cv.polylines(frame,&#91;pts],<strong>True<\/strong>, 255,2)\n        cv.imshow('img2',img2)\n        k = cv.waitKey(30) &amp; 0xff\n        <strong>if<\/strong> k == 27:\n            <strong>break<\/strong>\n    <strong>else<\/strong>:\n        <strong>break<\/strong><\/code><\/pre>\n\n\n\n<p>\u4e09\u5e27\u7684\u7ed3\u679c\u5982\u4e0b&nbsp;<img alt=\"\" src=\"http:\/\/qiniu.aihubs.net\/camshift_result.jpg\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1\u3001\u80cc\u666f\u5206\u79bb\u65b9\u6cd5 \u80cc\u666f\u5206\u79bb\uff08BS\uff09\u662f\u4e00\u79cd\u901a\u8fc7\u4f7f\u7528\u9759\u6001\u76f8\u673a\u6765\u751f\u6210\u524d\u666f\u63a9\u7801\uff08\u5373\u5305\u542b\u5c5e\u4e8e\u573a\u666f\u4e2d\u7684\u79fb\u52a8\u5bf9\u8c61\u50cf\u7d20\u7684\u4e8c\u8fdb\u5236 &hellip; <a href=\"http:\/\/139.9.1.231\/index.php\/2022\/01\/13\/opencv1\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">opencv \u89c6\u9891\u5bf9\u8c61\u8ffd\u8e2a<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[15],"tags":[],"_links":{"self":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/1956"}],"collection":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/comments?post=1956"}],"version-history":[{"count":2,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/1956\/revisions"}],"predecessor-version":[{"id":1958,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/1956\/revisions\/1958"}],"wp:attachment":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/media?parent=1956"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/categories?post=1956"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/tags?post=1956"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}