{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "93ee02c5-ef3a-45a8-8b70-8fbea3d13e3f", "metadata": {}, "outputs": [], "source": [ "#!pip install opencv-python" ] }, { "cell_type": "code", "execution_count": 2, "id": "13e35e47-b4ac-435e-9f08-548e9f2bf8fe", "metadata": {}, "outputs": [], "source": [ "import cv2\n", "import matplotlib.pyplot as plt\n", "import numpy as np" ] }, { "cell_type": "markdown", "id": "48657490-18f4-4543-a04d-a47d3fc224bc", "metadata": {}, "source": [ "## gray image " ] }, { "cell_type": "markdown", "id": "db920f95-e851-4817-aeab-2d401f0a3a22", "metadata": {}, "source": [ "### zadanie 1:\n", "Znaleźć plik jpeg w internecie i zapisać go w zajecia7/image.jpeg" ] }, { "cell_type": "code", "execution_count": 3, "id": "4b4b3502-678b-4206-a35c-09d0feb7c9ff", "metadata": {}, "outputs": [ { "ename": "NameError", "evalue": "name 'image' is not defined", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[3], line 3\u001b[0m\n\u001b[1;32m 1\u001b[0m image_path \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mimage.jpeg\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 2\u001b[0m aimage \u001b[38;5;241m=\u001b[39m cv2\u001b[38;5;241m.\u001b[39mimread(image_path)\n\u001b[0;32m----> 3\u001b[0m gray_image \u001b[38;5;241m=\u001b[39m cv2\u001b[38;5;241m.\u001b[39mcvtColor(image, cv2\u001b[38;5;241m.\u001b[39mCOLOR_BGR2GRAY)\n", "\u001b[0;31mNameError\u001b[0m: name 'image' is not defined" ] } ], "source": [ "image_path = \"image.jpeg\"\n", "image = cv2.imread(image_path)\n", "gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)" ] }, { "cell_type": "code", "execution_count": null, "id": "97e093c0-d5c8-4555-8f10-20913c46be8a", "metadata": {}, "outputs": [], "source": [ "gray_image" ] }, { "cell_type": "code", "execution_count": null, "id": "02a1fa48-c769-4d3c-912d-a6b48bcb7cad", "metadata": {}, "outputs": [], "source": [ "type(gray_image)" ] }, { "cell_type": "code", "execution_count": null, "id": "0eada3fe-14f5-49e3-b8da-863fe412269e", "metadata": {}, "outputs": [], "source": [ "gray_image.shape" ] }, { "cell_type": "code", "execution_count": null, "id": "fe949d46-8dcb-4ce7-a364-fa8f7b672cd4", "metadata": {}, "outputs": [], "source": [ "def display_image(image):\n", " image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n", " plt.imshow(image_rgb)\n", " plt.axis('off') # Turn off axes for better visualization\n", " plt.show()" ] }, { "cell_type": "code", "execution_count": null, "id": "335d9707-5191-495e-a83d-c6edfccd2aac", "metadata": { "scrolled": true }, "outputs": [], "source": [ "display_image(gray_image)" ] }, { "cell_type": "markdown", "id": "24d328f0-cd9c-43aa-b141-09cac4526f1c", "metadata": {}, "source": [ "## Colour image" ] }, { "cell_type": "code", "execution_count": null, "id": "6e693e57-1836-435a-8653-0eccf90db806", "metadata": {}, "outputs": [], "source": [ "image_path = \"image.jpeg\"\n", "image = cv2.imread(image_path)" ] }, { "cell_type": "code", "execution_count": null, "id": "43e9aa78-dd87-482a-bc7a-005d069aa5b4", "metadata": { "scrolled": true }, "outputs": [], "source": [ "image" ] }, { "cell_type": "code", "execution_count": null, "id": "693ea83b-d225-4aaa-9c73-dce6ba5ecbcb", "metadata": {}, "outputs": [], "source": [ "type(image)" ] }, { "cell_type": "code", "execution_count": null, "id": "ca7dfca2-9dcf-44d0-ba88-63d9f4b30ee4", "metadata": {}, "outputs": [], "source": [ "image.shape" ] }, { "cell_type": "code", "execution_count": null, "id": "3facc994-28d2-41df-ad7c-4a5232c00d44", "metadata": {}, "outputs": [], "source": [ "display_image(image)" ] }, { "cell_type": "markdown", "id": "08b91659-4e61-4e3f-b7d9-87f0ca940eec", "metadata": {}, "source": [ "## plot gray histogram" ] }, { "cell_type": "code", "execution_count": null, "id": "2b9ca017-fce6-4475-acf4-c695c9a26c28", "metadata": {}, "outputs": [], "source": [ "colors = ('b', 'g', 'r')\n", "plt.figure()\n", "plt.title('gray histogram')\n", "plt.xlabel(\"Intensity\")\n", "plt.ylabel(\"Count\")\n", "\n", "hist = cv2.calcHist([gray_image], [0], None, [256], [0, 256])\n", "plt.plot(hist, color='k')\n", "plt.xlim([0, 256])\n" ] }, { "cell_type": "markdown", "id": "9da5405e-2244-42da-a9d2-a772f7ec839e", "metadata": {}, "source": [ "## plot BGR histogram" ] }, { "cell_type": "code", "execution_count": null, "id": "01578d20-c4d1-405d-92b3-49dca00c18e2", "metadata": {}, "outputs": [], "source": [ "colors = ('b', 'g', 'r')\n", "plt.figure()\n", "plt.title('colour histogram')\n", "plt.xlabel(\"Intensity\")\n", "plt.ylabel(\"Count\")\n", "\n", "for i, col in enumerate(colors):\n", " hist = cv2.calcHist([image], [i], None, [256], [0, 256])\n", " plt.plot(hist, color=col)\n", " plt.xlim([0, 256])\n" ] }, { "cell_type": "markdown", "id": "9ff191d8-d8a0-4ced-83e3-f2ea612e2235", "metadata": {}, "source": [ "### Image manipulation" ] }, { "cell_type": "code", "execution_count": null, "id": "99a849b8-6888-47ec-bc82-2081b3339fbc", "metadata": {}, "outputs": [], "source": [ "gray_image" ] }, { "cell_type": "code", "execution_count": null, "id": "60aea8ab-d00c-4191-a1a2-efb34e002f25", "metadata": {}, "outputs": [], "source": [ "gray_image_2 = gray_image.copy()" ] }, { "cell_type": "code", "execution_count": null, "id": "69178e92-8dbb-4225-8214-0762bb3059b8", "metadata": {}, "outputs": [], "source": [ "gray_image_2[:100, :100] = 0\n", "gray_image_2[-50:, -50:] = 0" ] }, { "cell_type": "code", "execution_count": null, "id": "96933658-1f43-407b-8c0d-a9d70df55217", "metadata": {}, "outputs": [], "source": [ "display_image(gray_image)" ] }, { "cell_type": "code", "execution_count": null, "id": "1c9f2872-5125-4e76-9bfb-75b0e295aba3", "metadata": {}, "outputs": [], "source": [ "gray_image_3 = gray_image.copy()" ] }, { "cell_type": "code", "execution_count": null, "id": "c83a98e5-bd8e-4958-a77d-9de29d9c87d7", "metadata": {}, "outputs": [], "source": [ "binary_image = np.where(gray_image_3 > 128, 255, 0).astype(np.uint8)" ] }, { "cell_type": "code", "execution_count": null, "id": "7fbdea96-4330-48a2-879b-2aebc02c90d7", "metadata": { "scrolled": true }, "outputs": [], "source": [ "display_image(binary_image)" ] }, { "cell_type": "code", "execution_count": null, "id": "d6ed47e6-437e-461c-abfe-75b3e8e27126", "metadata": {}, "outputs": [], "source": [ "gray_image_4 = gray_image.copy()" ] }, { "cell_type": "code", "execution_count": null, "id": "d6f48c0a-7a9b-4779-b5f4-ad6ab3de79c4", "metadata": {}, "outputs": [], "source": [ "gray_image_4 = gray_image_4[:,::-1]" ] }, { "cell_type": "code", "execution_count": null, "id": "b5f1a58f-5290-43e0-9e05-cf0c1bf462f5", "metadata": {}, "outputs": [], "source": [ "display_image(gray_image_4)" ] }, { "cell_type": "code", "execution_count": null, "id": "da05ee84-6739-4e68-918e-24329cc760ed", "metadata": {}, "outputs": [], "source": [ "gray_image_5 = gray_image.copy()" ] }, { "cell_type": "code", "execution_count": null, "id": "907c80ca-18b4-4798-ae39-a2668aa640fc", "metadata": {}, "outputs": [], "source": [ "gray_image_5 = gray_image_5[::-1,:]" ] }, { "cell_type": "code", "execution_count": null, "id": "ba190a3f-75fc-4fde-873a-f971730a2c7c", "metadata": {}, "outputs": [], "source": [ "display_image(gray_image_5)" ] }, { "cell_type": "markdown", "id": "7a1aca09-d574-435a-b594-c694304f713f", "metadata": {}, "source": [ "### Zadanie 2 \n", "Wymyślić 3 inne manipulacje ze zdjęciem za pomocą numpy (można wspomagać się internetem) i wykonać je na swoim obrazku" ] } ], "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.11.7" } }, "nbformat": 4, "nbformat_minor": 5 }