widzenie-komputerowe-MP/1_zad_odp.ipynb

177 lines
229 KiB
Plaintext
Raw Normal View History

2022-10-16 21:00:59 +02:00
{
"cells": [
{
"cell_type": "markdown",
"id": "08a9cccb-e107-4062-80a5-5cd53bf609e7",
"metadata": {},
"source": [
"## Ćwiczenie 1\n",
"\n",
"Wczytaj plik [`img/soyjaks.jpg`](https://knowyourmeme.com/memes/two-soyjaks-pointing) i spróbuj odtworzyć poniższy obrazek."
]
},
{
"cell_type": "markdown",
"id": "8f2c7557-6200-4d7a-8276-82f40aa08eba",
"metadata": {},
"source": [
"![Two Soyjaks Pointing](img/soyjaks-final.png)"
]
},
{
"cell_type": "code",
"execution_count": 399,
"id": "fe514d4d",
"metadata": {},
"outputs": [],
"source": [
"import cv2 as cv\n",
"import matplotlib.pyplot as plt\n",
"import matplotlib\n",
"%matplotlib inline"
]
},
{
"cell_type": "code",
"execution_count": 400,
"id": "ae6c6ab1",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<matplotlib.image.AxesImage at 0x7faec3255bb0>"
]
},
"execution_count": 400,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAOIAAAD8CAYAAABqzHg8AAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjQuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8rg+JYAAAACXBIWXMAAAsTAAALEwEAmpwYAAEAAElEQVR4nOz9d5ilR3nnD3+q6kknd+6e0NMTNZJGWSAQEhkkTA4GHDCG5XVYZ3ud97e2d732etev065xABMMeG2wDdggkBACJIJQzpqcQ+d08hOq6v2jnnO6e6QBX79dvzvXXqq5errPOc95Yt3pe9/3t4S1lufGc+O58X92yP/TJ/DceG48N54TxOfGc+OiGM8J4nPjuXERjOcE8bnx3LgIxnOC+Nx4blwE4zlBfG48Ny6C8a8iiEKI1wghDgohjgghfvVf4xjPjefG/01D/O/OIwohFHAIeDVwBngA+H5r7dP/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GCfHQuAiLY1EggywfaBfgRAIkQEZPcBIrANwHOjiI4QCpfrzTemMQiIRmY/0UoTy8QKBIWV6cYnRiRHiRIMnEVIiRE8hg/R9/EKNICiSdhoYnaJzqyewWEGuCAEhMMKuWeNnhWnc+D8G1qwf1sJwzWeqtAUxm2GswMTLmEaFVtxhUERExSqTOy7Fjwp0k5jF5Xnqq6usLC+zOL/IwvwSswtLLC6tsrzSpN7s0mqnGOMECmFzlNXkggZ9kIFckfdRVbMOlTUgYHmljudF+H6R5ZVFqtUS3TjBCPoIZ6fdodls0enGeH4AWYTvhSgvQngBqXYCL4TEYpFCOqBBgEJSHazRbHYAQRhGdLoZSao
"text/plain": [
"<Figure size 432x288 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"image = cv.imread(\"img/soyjaks.jpg\", cv.IMREAD_COLOR)\n",
"\n",
"image = cv.cvtColor(image, cv.COLOR_BGR2RGB)\n",
"image = image[0:520, 45:490]\n",
"cv.circle(image, (276, 226), 50, (205, 100, 229), 8)\n",
"\n",
"cv.putText(image, 'OMG not chikenz!', (10, 100), cv.FONT_HERSHEY_PLAIN , 3, (255,0,0), 5, cv.FILLED)\n",
"plt.imshow(image)"
]
},
{
"cell_type": "markdown",
"id": "209ce6e2",
"metadata": {},
"source": [
"## Ćwiczenie 2\n",
"\n",
"Załaduj obrazy `img/pipe.png` oraz `img/man-without-pipe.png` i wykonaj operacje tak, aby uzyskać poniższy obraz.\n"
]
},
{
"cell_type": "markdown",
"id": "f9c71d3f",
"metadata": {},
"source": [
"![Człowiek z fajką](img/man-with-pipe.png)"
]
},
{
"cell_type": "code",
"execution_count": 401,
"id": "745da0ae",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<matplotlib.image.AxesImage at 0x7faec31ce1f0>"
]
},
"execution_count": 401,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAQAAAAD8CAYAAACYVXq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Xh1/lKq8q6biBCbuYoS6m0Zvl1CIFaCiFm1q2wlY1pyCSEVgtgzjGkyMPxgcOYzTGlQBDl5eWZUioSLOR+fHrHkAO1bizzSkDIYyKGaM5o21jXdY+UgiTIZmytmd2kHDmMI+frwlaqLfUAoraIg0eClksHSx7ucl3zbbdUwLaGeltaqh3/vT12FxXs+fZ9sCD7avMXedQl8mZ3NxP7vnH6cvbQXYIgGiyc38/d31J3Z2Am3e4+E6jWGzZ2/xm7nfR39w2zPwcaPdL4fcc/MhDw7aH7ZRbPmXbv6H/fv7WH13S0Vxo5BcaUQRsxmtcspYIq87oCgZwy5/VCa42tFkLKPD4ceTwk1looIsyXmWEc+eZn76ja+IvffkK+feYwJNZ1JaWRd+/ec76cQZXT4cS7p8S2bSzLyofjgdOU+OUvf8HxdGSdr3z77XecLzOgTOPIVpWPn18RCbx790SKgWVd2NaVdd2o1aoHZd1opRJTQBtsW2EL8Fped9CxtIaEwOEwEWmcXy6U1tBmu3BKmRgCpTYioO4MqhaawhAj67paBUFCx5qYhswwTkhIttvrRkCJYaCqGIYQhCCRWguiDa1Q1w0ISIB1WVCFbV3JeaD49xqGRIjRFroKy3Jl21a7vkFIUYhxcCCvh8e63/MgDrxxtzT8EAm3qF66YfbQ+X6t9R3/+8bRtx7d3ULHKe6tTtTT8P4KBdVGUyFouC3dPXTAPY/hOGi7pQG74YP0MpK/ifQvAW+c1533uvujv4co5p7EHdYPW1w/vhA
"text/plain": [
"<Figure size 432x288 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"\n",
"pipe = cv.imread(\"img/pipe.png\", cv.IMREAD_UNCHANGED)\n",
"pipe = cv.cvtColor(pipe, cv.COLOR_BGRA2RGBA)\n",
"\n",
"pipe = cv.resize(pipe, None, fx=0.55, fy=0.55)\n",
"pipe = cv.flip(pipe, 1)\n",
"\n",
"man = cv.imread(\"img/man-without-pipe.png\", cv.IMREAD_UNCHANGED)\n",
"\n",
"man = cv.cvtColor(man, cv.COLOR_BGR2RGB)\n",
"\n",
"alpha_s = pipe[:, :, 3] / 255.0\n",
"\n",
"x_offset=70\n",
"y_offset=280\n",
"y1, y2 = y_offset, y_offset + pipe.shape[0]\n",
"x1, x2 = x_offset, x_offset + pipe.shape[1]\n",
"\n",
"alpha_s = pipe[:, :, 3] / 255.0\n",
"alpha_l = 1.0 - alpha_s\n",
"\n",
"for c in range(0, 3):\n",
" man[y1:y2, x1:x2, c] = (alpha_s * pipe[:, :, c] +\n",
" alpha_l * man[y1:y2, x1:x2, c])\n",
"plt.imshow(man)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.8.12 64-bit",
"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.12"
},
"vscode": {
"interpreter": {
"hash": "31f2aee4e71d21fbe5cf8b01ff0e069b9275f58929596ceb00d14d90e3e16cd6"
}
}
},
"nbformat": 4,
"nbformat_minor": 5
}