{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "09af40b0",
   "metadata": {},
   "source": [
    "# TP1- OpenCV\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d93ebe23",
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "82898d85",
   "metadata": {},
   "source": [
    "## Exercise I - Image conversion\n",
    "\n",
    "Given an RGB colored image, write a function that converts the image into gray scale\n",
    "one. The gray value of a pixel having the triplet (R, G, B) is the one value (R+G+B)/3.\n",
    "Compare your result to the command in OpenCV\n",
    "\n",
    "```python\n",
    "img_gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)\n",
    "```\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ee54691e",
   "metadata": {},
   "outputs": [],
   "source": [
    "def convertToGrayScale(imagePath):\n",
    "    image = cv2.imread(imagePath)\n",
    "    grayImage = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\n",
    "    cv2.imshow(\"Gray Image\", grayImage)\n",
    "    cv2.waitKey(0)\n",
    "    cv2.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "19adbf24",
   "metadata": {},
   "source": [
    "## Exercise II- Image conversion\n",
    "\n",
    "Given an image that may be an RGB colored image or a grayscale one, write a\n",
    "function that converts the image into binary one.\n",
    "Compare your result to the command in OpenCV “(thresh, blackAndWhiteImage)\n",
    "= cv2.threshold(img_gray, 127, 255, cv2.THRESH_BINARY)”.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cbda82b7",
   "metadata": {},
   "outputs": [],
   "source": [
    "def convertToBinary(imagePath, threshold=128):\n",
    "    image = cv2.imread(imagePath, cv2.IMREAD_GRAYSCALE)\n",
    "    _, binaryImage = cv2.threshold(image, threshold, 255, cv2.THRESH_BINARY)\n",
    "    cv2.imshow(\"Binary Image\", binaryImage)\n",
    "    cv2.waitKey(0)\n",
    "    cv2.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "64fcdd69",
   "metadata": {},
   "source": [
    "## Exercise III - Plotting each channel separately\n",
    "\n",
    "In this exercise, we want to take a colored image and plot each channel separately. To\n",
    "do so, the first method is to plot each channel separately as if it is a gray scale image. In\n",
    "the second method, to show the amount of red in each pixel, we leave the red channel\n",
    "untouched and we set the two channels (green and blue) to zero and then we show the\n",
    "image.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cc32344e",
   "metadata": {},
   "outputs": [],
   "source": [
    "def plotImage_1(imagePath):\n",
    "    image = cv2.imread(imagePath)\n",
    "    (B, G, R) = cv2.split(image)\n",
    "\n",
    "    zeros = np.zeros(image.shape[:2], dtype=\"uint8\")\n",
    "\n",
    "    cv2.imshow(\"Red Channel\", cv2.merge([zeros, zeros, R]))\n",
    "    cv2.imshow(\"Green Channel\", cv2.merge([zeros, G, zeros]))\n",
    "    cv2.imshow(\"Blue Channel\", cv2.merge([B, zeros, zeros]))\n",
    "\n",
    "    cv2.waitKey(0)\n",
    "    cv2.destroyAllWindows()\n",
    "\n",
    "\n",
    "def plotImage_2(imagePath):\n",
    "    image = cv2.imread(imagePath)\n",
    "    (B, G, R) = cv2.split(image)\n",
    "\n",
    "    cv2.imshow(\"Red Channel\", R)\n",
    "    cv2.imshow(\"Green Channel\", G)\n",
    "    cv2.imshow(\"Blue Channel\", B)\n",
    "\n",
    "    cv2.waitKey(0)\n",
    "    cv2.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ed988f24",
   "metadata": {},
   "source": [
    "## Exercise IV - Histogram\n",
    "\n",
    "We want to compute the histogram of gray scale images. An histogram of a gray scale\n",
    "image is a 256-array that counts how many times a gray value is present in the image.\n",
    "For example, the value at position 0 in the array will contain how many pixels in the\n",
    "image have the gray value 0 and so on. Write a function that compute the histogram of\n",
    "a gray scale image and plot the histogram using the matplotlib as follows:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "38322523",
   "metadata": {},
   "outputs": [],
   "source": [
    "def plotHistogram(imagePath):\n",
    "    image = cv2.imread(imagePath, cv2.IMREAD_GRAYSCALE)\n",
    "    histogram = cv2.calcHist([image], [0], None, [256], [0, 256])\n",
    "\n",
    "    plt.plot(histogram)\n",
    "    plt.title(\"Grayscale Histogram\")\n",
    "    plt.xlabel(\"Pixel Value\")\n",
    "    plt.ylabel(\"Frequency\")\n",
    "    plt.xlim([0, 256])\n",
    "    plt.show()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python",
   "version": "3.13.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
