{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "07b1c445",
   "metadata": {},
   "source": "# OpenCV\n\n## Intro\n\nOpenCV is considered as the well-known library for computer vision. This library can be used\nby several programming languages including the python.\nIn this introduction, we will try to take a look at some of the functions in this library and then\ntry to implement some others by ourselves.\nThis tutorial will include how to:\n\n1. Read an image\n2. Extract the RGB values of a pixel\n3. Resize the Image\n4. Draw a Rectangle\n5. Display text\n6. Open and play a video\n\nThis is the original image that we will manipulate throughout this tutorial. You can use your image.\n\n![TP-1 Image](https://media.hadihz.me/tp1_image_38cc97fc0c.jpg)\n"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "68745af9",
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7a47598c",
   "metadata": {},
   "source": "## Reading an Image\n"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b525218a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Reading the image using imread() function\n",
    "image = cv2.imread(\"tp1-image.jpg\")\n",
    "\n",
    "# Extracting the heigh and width of an image\n",
    "h, w = image.shape[:2]\n",
    "\n",
    "# Displaying the height and width\n",
    "print(f\"Height: {h}, Width: {w}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3860c5fa",
   "metadata": {},
   "source": "## Extracting the RGB values of a pixel\n\nIn OpenCV, the order of color channel is **BGR** and not **RGB**.\n"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7e448f16",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Get color of pixel at (100, 100)\n",
    "(B, G, R) = image[100, 100]\n",
    "print(f\"R: {R}, G: {G}, B: {B}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4996cd36",
   "metadata": {},
   "source": "## Extracting a sub-region of the image\n"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f3a1f514",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Extract and display ROI\n",
    "roi = image[100:500, 200:700]\n",
    "cv2.imshow(\"ROI\", roi)\n",
    "cv2.waitKey(0)\n",
    "cv2.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a6fde7ec",
   "metadata": {},
   "source": "## Resizing the image\n"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "44fd7097",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Resize image to 300x300\n",
    "resize = cv2.resize(image, (300, 300))\n",
    "cv2.imshow(\"Resize\", resize)\n",
    "cv2.waitKey(0)\n",
    "cv2.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c5cbe135",
   "metadata": {},
   "source": "## Drawing a Rectangle\n"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "77fbd162",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Draw rectangle on image\n",
    "output = image.copy()\n",
    "rectangle = cv2.rectangle(output, (1500, 900), (600, 400), (255, 0, 0), 2)\n",
    "cv2.imshow(\"Rectangle\", rectangle)\n",
    "cv2.waitKey(0)\n",
    "cv2.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e802e96e",
   "metadata": {},
   "source": "## Displaying text\n"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "13c97c29",
   "metadata": {},
   "outputs": [],
   "source": [
    "text = cv2.putText(\n",
    "    output, \"IN433\", (400, 400), cv2.FONT_HERSHEY_SIMPLEX, 3, (0, 255, 0), 5\n",
    ")\n",
    "cv2.imshow(\"Text\", text)\n",
    "cv2.waitKey(0)\n",
    "cv2.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ba985274",
   "metadata": {},
   "source": "## Play a video\n"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "685b8d93",
   "metadata": {},
   "outputs": [],
   "source": [
    "def play_videoFile(filePath, mirror=False):\n",
    "    cap = cv2.VideoCapture(filePath)\n",
    "    cv2.namedWindow(\"Video Life2Coding\", cv2.WINDOW_NORMAL)\n",
    "    while True:\n",
    "        ret_val, frame = cap.read()\n",
    "        if mirror:\n",
    "            frame = cv2.flip(frame, 1)\n",
    "        cv2.imshow(\"Video Life2Coding\", frame)\n",
    "        if cv2.waitKey(1) == 27:\n",
    "            break  # esc to quit\n",
    "\n",
    "    cap.release()\n",
    "    cv2.destroyAllWindows()\n",
    "\n",
    "\n",
    "play_videoFile(\"tp1-video.mp4\", mirror=True)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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.13.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}