You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
211 lines
5.0 KiB
Plaintext
211 lines
5.0 KiB
Plaintext
6 months ago
|
{
|
||
|
"cells": [
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 1,
|
||
|
"id": "d8731107-ae34-4759-a7be-c590f025b8d7",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"import cv2\n",
|
||
|
"import numpy as np"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 2,
|
||
|
"id": "69a79561-8c31-4867-bc56-c25ca2dda93a",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"\"\"\"加权平均灰度处理\"\"\"\n",
|
||
|
"def frame_gray(frame):\n",
|
||
|
" return np.dot(frame[..., :], np.array([0.114, 0.578, 0.299]))"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 3,
|
||
|
"id": "b275698e-9437-44f7-a793-96a64942e491",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"\"\"\"帧差法\"\"\"\n",
|
||
|
"def diff_algorithm(curr, next, threshold):\n",
|
||
|
" curr = frame_gray(curr)\n",
|
||
|
" next = frame_gray(next)\n",
|
||
|
" diff = np.abs(curr - next)\n",
|
||
|
" diff[np.where(diff < threshold)] = 0\n",
|
||
|
" diff[np.where(diff >= threshold)] = 255\n",
|
||
|
" return diff"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 4,
|
||
|
"id": "0331ed3e-761f-4e21-9465-558e49cdd442",
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"text/plain": [
|
||
|
"(2, 2, 2, 3)"
|
||
|
]
|
||
|
},
|
||
|
"execution_count": 4,
|
||
|
"metadata": {},
|
||
|
"output_type": "execute_result"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"(2, 2) + (2,3)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 5,
|
||
|
"id": "ab51ab14-d78e-411d-81ca-8635ca5dc4e2",
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"text/plain": [
|
||
|
"[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]"
|
||
|
]
|
||
|
},
|
||
|
"execution_count": 5,
|
||
|
"metadata": {},
|
||
|
"output_type": "execute_result"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"list(range(5 * 3 - 1))"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 6,
|
||
|
"id": "df7c110d-c845-4751-af8f-c28709b9b7aa",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"\"\"\"腐蚀\"\"\"\n",
|
||
|
"def dilate(gray, kernel):\n",
|
||
|
" h_k, w_k = kernel.shape[0], kernel.shape[1]\n",
|
||
|
" h_g, w_g = gray.shape[0], gray.shape[1]\n",
|
||
|
" h, w = h_g + (h_k // 2) * 2, w_g + (w_k // 2) * 2\n",
|
||
|
" size = np.maximum(h, w)\n",
|
||
|
" temp = np.ones((size, size)) * 255\n",
|
||
|
" temp[h_k // 2: h - (h_k // 2), w_k // 2: w - (w_k // 2)] = gray\n",
|
||
|
" return temp\n",
|
||
|
" "
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 7,
|
||
|
"id": "e0c94bfd-ec08-4ec4-a599-71c635fc74f5",
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"text/plain": [
|
||
|
"'膨胀'"
|
||
|
]
|
||
|
},
|
||
|
"execution_count": 7,
|
||
|
"metadata": {},
|
||
|
"output_type": "execute_result"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"\"\"\"膨胀\"\"\""
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 17,
|
||
|
"id": "bac46e68-03ad-4961-a729-77f36e155fe5",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"def image_preprocessing(frame,kernel_1,kernel_2):\n",
|
||
|
" frame = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)#rgb转hsv\n",
|
||
|
" frame = cv2.dilate(frame, kernel_1, 1)#膨胀\n",
|
||
|
" frame = cv2.erode(frame, kernel_2, iterations=1)#腐蚀\n",
|
||
|
" return frame"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 19,
|
||
|
"id": "d21a5933-b44a-494d-a37a-c63973fd4a73",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"import cv2\n",
|
||
|
"import numpy as np\n",
|
||
|
"cap = cv2.VideoCapture(0)\n",
|
||
|
"kernel_1 = np.ones((3, 3), dtype=np.uint8) # 卷积核变为4*4\n",
|
||
|
"kernel_2 = np.ones((3, 3), dtype=np.uint8)\n",
|
||
|
"ret, curr_frame = cap.read()\n",
|
||
|
"curr_frame = image_preprocessing(curr_frame, kernel_1, kernel_2)\n",
|
||
|
"while True:\n",
|
||
|
" ret, next_frame = cap.read()\n",
|
||
|
" if ret:\n",
|
||
|
" next_frame = image_preprocessing(next_frame, kernel_1, kernel_2)\n",
|
||
|
" diff = cv2.absdiff(next_frame, curr_frame)\n",
|
||
|
" _, thresh = cv2.threshold(diff, 30, 255, cv2.THRESH_BINARY)\n",
|
||
|
" gaussian = cv2.GaussianBlur(thresh, (3, 3), 0)\n",
|
||
|
" gaussian = cv2.flip(gaussian, 180)\n",
|
||
|
" cv2.imshow('video', gaussian)\n",
|
||
|
" k = cv2.waitKey(30) & 0xff\n",
|
||
|
" if k == 27:\n",
|
||
|
" break\n",
|
||
|
" else:\n",
|
||
|
" break\n",
|
||
|
" curr_frame = next_frame\n",
|
||
|
"cv2.destroyAllWindows()\n",
|
||
|
"cap.release()"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"id": "a3aeb064-bf91-4c65-ac9f-f26db688fc33",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": []
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"id": "cb0c8568-4002-448d-8740-057b839422a3",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": []
|
||
|
}
|
||
|
],
|
||
|
"metadata": {
|
||
|
"kernelspec": {
|
||
|
"display_name": "Python 3 (ipykernel)",
|
||
|
"language": "python",
|
||
|
"name": "python3"
|
||
|
},
|
||
|
"language_info": {
|
||
|
"codemirror_mode": {
|
||
|
"name": "ipython",
|
||
|
"version": 3
|
||
|
},
|
||
|
"file_extension": ".py",
|
||
|
"mimetype": "text/x-python",
|
||
|
"name": "python",
|
||
|
"nbconvert_exporter": "python",
|
||
|
"pygments_lexer": "ipython3",
|
||
|
"version": "3.8.0"
|
||
|
}
|
||
|
},
|
||
|
"nbformat": 4,
|
||
|
"nbformat_minor": 5
|
||
|
}
|