{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 183,
   "id": "5a56346b",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from matplotlib import pyplot as plt\n",
    "from itertools import product\n",
    "import os\n",
    "import sys\n",
    "from PIL import Image\n",
    "from scipy.optimize import minimize,linprog\n",
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "id": "5997a0a2",
   "metadata": {},
   "outputs": [],
   "source": [
    "def file_extractor(dirname=\"images\"):\n",
    "    files = os.listdir(dirname)\n",
    "    scenes = []\n",
    "    for file in files:\n",
    "        scenes.append(os.path.join(dirname, file))\n",
    "    return scenes\n",
    "\n",
    "def image_extractor(scenes):\n",
    "    image_folder = []\n",
    "    for scene in scenes:\n",
    "        files = os.listdir(scene)\n",
    "        for file in files:\n",
    "            image_folder.append(os.path.join(scene, file))\n",
    "    images = []\n",
    "    for folder in image_folder:\n",
    "        ims = os.listdir(folder)\n",
    "        for im in ims:\n",
    "            if im[-4:] == \".jp4\" or im[-7:] == \"_6.tiff\":\n",
    "                continue\n",
    "            else:\n",
    "                images.append(os.path.join(folder, im))\n",
    "    return images #returns a list of file paths to .tiff files in the specified directory given in file_extractor\n",
    "\n",
    "def im_distribution(images, num):\n",
    "    \"\"\"\n",
    "    Function that extracts tiff files from specific cameras and returns a list of all\n",
    "    the tiff files corresponding to that camera. i.e. all pictures labeled \"_7.tiff\" or otherwise\n",
    "    specified camera numbers.\n",
    "    \n",
    "    Parameters:\n",
    "        images (list): list of all tiff files, regardless of classification. This is NOT a list of directories but\n",
    "        of specific tiff files that can be opened right away. This is the list that we iterate through and \n",
    "        divide.\n",
    "        \n",
    "        num (str): a string designation for the camera number that we want to extract i.e. \"14\" for double digits\n",
    "        of \"_1\" for single digits.\n",
    "        \n",
    "    Returns:\n",
    "        tiff (list): A list of tiff files that have the specified designation from num. They are the files extracted\n",
    "        from the 'images' list that correspond to the given num.\n",
    "    \"\"\"\n",
    "    tiff = []\n",
    "    for im in images:\n",
    "        if im[-7:-6] == num:\n",
    "            tiff.append(im)\n",
    "    return tiff"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d016e765",
   "metadata": {},
   "source": [
    "## use scipy.optimize.minimize"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 177,
   "id": "20cca46f",
   "metadata": {},
   "outputs": [],
   "source": [
    "def plot_hist(tiff_list):\n",
    "    \"\"\"\n",
    "    This function is the leftovers from the first attempt to plot histograms.\n",
    "    As it stands it needs some work in order to function again. We will\n",
    "    fix this later. 1/25/22\n",
    "    \"\"\"\n",
    "    '''jj = 0\n",
    "    fig, axs = plt.subplots(nrows=2, ncols=2, figsize=(15,12))\n",
    "    for cam, ax in zip(cameras, axs.ravel()):\n",
    "        diff = []\n",
    "        for ii in range(len(cam)):\n",
    "            image = Image.open(cam[ii])    #Open the image and read it as an Image object\n",
    "            image = np.array(image)[1:,:]    #Convert to an array, leaving out the first row because the first row is just housekeeping data\n",
    "            ar1, ar2 = image.shape\n",
    "            ind1, ind2 = np.random.randint(1,ar1-1), np.random.randint(1,ar2-1) #ind1 randomly selects a row, ind2 randomly selects a column, \n",
    "                                                                            #this is now a random pixel selection within the image\n",
    "            \n",
    "            surrounding = []                                                #initialize a list to be filled the 8 surrounding pixels\n",
    "            for i,j in product(np.arange(-1,2), repeat=2):                  #Iterate through the combinations of surrounding pixel indices\n",
    "                if i == 0 and j == 0:                                       #Avoid the target pixel\n",
    "                    continue\n",
    "                else:\n",
    "                    surrounding.append(image[ind1+i, ind1+j])               #Add the other 8 pixels to the list\n",
    "            diff.append(np.max(surrounding)-np.min(surrounding))\n",
    "        ax.hist(diff)\n",
    "        ax.set_title(f\"tiff {jj}\")\n",
    "        jj += 1\n",
    "    plt.tight_layout()\n",
    "    plt.show()\n",
    "    return '''\n",
    "\n",
    "    image = tiff_list\n",
    "    image = Image.open(image)    #Open the image and read it as an Image object\n",
    "    image = np.array(image)[1:,:]    #Convert to an array, leaving out the first row because the first row is just housekeeping data\n",
    "    row, col = image.shape\n",
    "    predict = np.empty([row,col])     # create a empty matrix to update prediction\n",
    "    predict[0,:] = image[0,:]       # keep the first row from the image\n",
    "    predict[:,0] = image[:,0]       # keep the first columen from the image\n",
    "    predict[-1,:] = image[-1,:]       # keep the first row from the image\n",
    "    predict[:,-1] = image[:,-1]       # keep the first columen from the image\n",
    "    diff = np.empty([row,col])\n",
    "    diff[0,:] = np.zeros(col)       # keep the first row from the image\n",
    "    diff[:,0] = np.zeros(row)\n",
    "    diff[-1,:] = np.zeros(col)       # keep the first row from the image\n",
    "    diff[:,-1] = np.zeros(row)\n",
    "    guess = [100,100,100]           # initial guess for the variables\n",
    "    for r in range(1,row-1):                  # loop through the rth row\n",
    "        for c in range(1,col-1):              # loop through the cth column\n",
    "            #surrounding = np.array([predict[r-1,c-1], predict[r-1,c], predict[r-1,c+1], predict[r,c-1]])\n",
    "            actual_surrounding = np.array([image[r-1,c-1], image[r-1,c], image[r-1,c+1], image[r,c-1]])\n",
    "            #z = np.array([image[r-1,c-1], image[r-1,c], image[r-1,c+1], image[r,c-1]])\n",
    "            x = np.array([-1,0,1,-1])\n",
    "            y = np.array([-1,-1,-1,0])\n",
    "            def E2(var):\n",
    "                return sum((actual_surrounding - var[0]*x + var[1]*y + var[2])**2)\n",
    "            res = minimize(E2,guess)\n",
    "            var = res.x\n",
    "            guess = var\n",
    "            #predict[r,c] = np.mean(actual_surrounding)       # take the mean of the previous 4 pixels\n",
    "            predict[r,c] = np.abs(var[2])\n",
    "            diff[r,c] = (np.max(actual_surrounding)-np.min(actual_surrounding))\n",
    "\n",
    "    predict = np.ravel(predict[1:-1,1:-1])\n",
    "    diff = np.ravel(diff[1:-1,1:-1])\n",
    "    image = np.ravel(image[1:-1,1:-1])\n",
    "    return image, predict, diff"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 178,
   "id": "d78b8bdb",
   "metadata": {},
   "outputs": [],
   "source": [
    "scenes = file_extractor()\n",
    "images = image_extractor(scenes)\n",
    "start = time.time()\n",
    "image, predict, diff = plot_hist(images[0])\n",
    "end = time.time()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 179,
   "id": "fffb55a4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "mean of error = 18.740279171184074\n",
      "time to run = 867.391261100769\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 170212 characters]\n",
      "text/plain": [
       "<Figure size 720x720 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig = plt.figure(figsize = (10,10))\n",
    "ax = fig.add_subplot()\n",
    "x = np.abs(predict-image)\n",
    "y = diff\n",
    "print(\"mean of error = \" + str(np.mean(np.abs(image-predict))))\n",
    "print(\"time to run = \" + str(end - start))\n",
    "\n",
    "plt.plot(x,y,'o',alpha = 0.2)\n",
    "plt.rcParams.update({'font.size': 20})\n",
    "plt.xlabel(\"differnece to the true value\" )\n",
    "plt.ylabel(\"differnece of min and max of true value of the surroundings\")\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 180,
   "id": "49dd5521",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 10212 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.hist(np.abs(image - predict),bins = 50)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 181,
   "id": "56194121",
   "metadata": {},
   "outputs": [],
   "source": [
    "mask1 = x <= 50\n",
    "x_50 = x[mask1]\n",
    "y_50 = y[mask1]\n",
    "mask2 = y_50 <= 100\n",
    "y_50 = y_50[mask2]\n",
    "x_50 = x_50[mask2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 182,
   "id": "6a683d40",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 219856 characters]\n",
      "text/plain": [
       "<Figure size 720x720 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig = plt.figure(figsize = (10,10))\n",
    "ax = fig.add_subplot()\n",
    "x = np.abs(predict-image)\n",
    "y = diff\n",
    "plt.hexbin(x_50,y_50,gridsize=50)\n",
    "plt.rcParams.update({'font.size': 10})\n",
    "plt.xlabel(\"differnece to the true value\" )\n",
    "plt.ylabel(\"differnece of min and max of true value of the surroundings\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 169,
   "id": "73412bc9",
   "metadata": {},
   "outputs": [],
   "source": [
    "def plot_hist(tiff_list):\n",
    "    \"\"\"\n",
    "    This function is the leftovers from the first attempt to plot histograms.\n",
    "    As it stands it needs some work in order to function again. We will\n",
    "    fix this later. 1/25/22\n",
    "    \"\"\"\n",
    "    '''jj = 0\n",
    "    fig, axs = plt.subplots(nrows=2, ncols=2, figsize=(15,12))\n",
    "    for cam, ax in zip(cameras, axs.ravel()):\n",
    "        diff = []\n",
    "        for ii in range(len(cam)):\n",
    "            image = Image.open(cam[ii])    #Open the image and read it as an Image object\n",
    "            image = np.array(image)[1:,:]    #Convert to an array, leaving out the first row because the first row is just housekeeping data\n",
    "            ar1, ar2 = image.shape\n",
    "            ind1, ind2 = np.random.randint(1,ar1-1), np.random.randint(1,ar2-1) #ind1 randomly selects a row, ind2 randomly selects a column, \n",
    "                                                                            #this is now a random pixel selection within the image\n",
    "            \n",
    "            surrounding = []                                                #initialize a list to be filled the 8 surrounding pixels\n",
    "            for i,j in product(np.arange(-1,2), repeat=2):                  #Iterate through the combinations of surrounding pixel indices\n",
    "                if i == 0 and j == 0:                                       #Avoid the target pixel\n",
    "                    continue\n",
    "                else:\n",
    "                    surrounding.append(image[ind1+i, ind1+j])               #Add the other 8 pixels to the list\n",
    "            diff.append(np.max(surrounding)-np.min(surrounding))\n",
    "        ax.hist(diff)\n",
    "        ax.set_title(f\"tiff {jj}\")\n",
    "        jj += 1\n",
    "    plt.tight_layout()\n",
    "    plt.show()\n",
    "    return '''\n",
    "\n",
    "    image = tiff_list\n",
    "    image = Image.open(image)    #Open the image and read it as an Image object\n",
    "    image = np.array(image)[1:,:]    #Convert to an array, leaving out the first row because the first row is just housekeeping data\n",
    "    row, col = image.shape\n",
    "    predict = np.empty([row,col])     # create a empty matrix to update prediction\n",
    "    predict[0,:] = image[0,:]       # keep the first row from the image\n",
    "    predict[:,0] = image[:,0]       # keep the first columen from the image\n",
    "    predict[-1,:] = image[-1,:]       # keep the first row from the image\n",
    "    predict[:,-1] = image[:,-1]       # keep the first columen from the image\n",
    "    diff = np.empty([row,col])\n",
    "    diff[0,:] = np.zeros(col)       # keep the first row from the image\n",
    "    diff[:,0] = np.zeros(row)\n",
    "    diff[-1,:] = np.zeros(col)       # keep the first row from the image\n",
    "    diff[:,-1] = np.zeros(row)\n",
    "    for r in range(1,row-1):                  # loop through the rth row\n",
    "        for c in range(1,col-1):              # loop through the cth column\n",
    "            #surrounding = np.array([predict[r-1,c-1], predict[r-1,c], predict[r-1,c+1], predict[r,c-1]])\n",
    "            actual_surrounding = np.array([image[r-1,c-1], image[r-1,c], image[r-1,c+1], image[r,c-1]])\n",
    "            #z = np.array([image[r-1,c-1], image[r-1,c], image[r-1,c+1], image[r,c-1]])\n",
    "            #x = np.array([-1,0,1,-1])\n",
    "            #y = np.array([-1,-1,-1,0])\n",
    "            '''def E2(var):\n",
    "                return sum((actual_surrounding - var[0]*x + var[1]*y + var[2])**2)\n",
    "            guess = [100,100,100]\n",
    "            res = minimize(E2,guess)\n",
    "            var = res.x'''\n",
    "            predict[r,c] = np.mean(actual_surrounding)       # take the mean of the previous 4 pixels\n",
    "            #predict[r,c] = np.abs(var[2])\n",
    "            diff[r,c] = (np.max(actual_surrounding)-np.min(actual_surrounding))\n",
    "\n",
    "    predict = np.ravel(predict[1:-1,1:-1])\n",
    "    diff = np.ravel(diff[1:-1,1:-1])\n",
    "    image = np.ravel(image[1:-1,1:-1])\n",
    "    return image, predict, diff"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 170,
   "id": "1bc0bed5",
   "metadata": {},
   "outputs": [],
   "source": [
    "scenes = file_extractor()\n",
    "images = image_extractor(scenes)\n",
    "image, predict, diff = plot_hist(images[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 171,
   "id": "554a5b80",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 213984 characters]\n",
      "text/plain": [
       "<Figure size 720x720 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig = plt.figure(figsize = (10,10))\n",
    "ax = fig.add_subplot()\n",
    "x = np.abs(predict-image)\n",
    "y = diff\n",
    "plt.plot(x,y,'o',alpha = 0.2)\n",
    "plt.rcParams.update({'font.size': 20})\n",
    "plt.xlabel(\"differnece to the true value\" )\n",
    "plt.ylabel(\"differnece of min and max of true value of the surroundings\")\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "13c62625",
   "metadata": {},
   "outputs": [],
   "source": [
    "mask1 = x <= 70\n",
    "x_50 = x[mask1]\n",
    "y_50 = y[mask1]\n",
    "mask2 = y_50 <= 70\n",
    "y_50 = y_50[mask2]\n",
    "x_50 = x_50[mask2]\n",
    "fig = plt.figure(figsize = (10,10))\n",
    "ax = fig.add_subplot()\n",
    "x = np.abs(predict-image)\n",
    "y = diff\n",
    "plt.hexbin(x_50,y_50,gridsize=50)\n",
    "plt.rcParams.update({'font.size': 10})\n",
    "plt.xlabel(\"differnece to the true value\" )\n",
    "plt.ylabel(\"differnece of min and max of true value of the surroundings\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "133bd909",
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.hist(np.abs(),bins = 100)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "21bbcc57",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "id": "3a4e59ea",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 7284 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "id": "a6839449",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "50.111402052984204"
      ]
     },
     "execution_count": 108,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.mean(diff)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d6177ff1",
   "metadata": {},
   "outputs": [],
   "source": [
    "scenes = file_extractor()\n",
    "images = image_extractor(scenes)\n",
    "index = 1\n",
    "for im in images[:4]:\n",
    "    image, predict, diff = plot_hist(im)\n",
    "    fig = plt.figure(figsize = (20,20))\n",
    "    ax = fig.subplot(4,2, index)\n",
    "    x = np.abs(predict-image)\n",
    "    y = diff\n",
    "    plt.plot(x,y,'o',alpha = 0.2)\n",
    "    plt.rcParams.update({'font.size': 20})\n",
    "    plt.xlabel(\"differnece to the true value\" )\n",
    "    plt.ylabel(\"differnece of min and max of true value of the surroundings\")\n",
    "    mask1 = x <= 70\n",
    "    x_50 = x[mask1]\n",
    "    y_50 = y[mask1]\n",
    "    mask2 = y_50 <= 70\n",
    "    y_50 = y_50[mask2]\n",
    "    x_50 = x_50[mask2]\n",
    "    fig = plt.figure(figsize = (10,10))\n",
    "    ax = fig.add_subplot()\n",
    "    x = np.abs(predict-image)\n",
    "    y = diff\n",
    "    plt.hexbin(x_50,y_50,gridsize=50)\n",
    "    plt.rcParams.update({'font.size': 10})\n",
    "    plt.xlabel(\"differnece to the true value\" )\n",
    "    plt.ylabel(\"differnece of min and max of true value of the surroundings\")\n",
    "\n",
    "    index += 1"
   ]
  }
 ],
 "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.9.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
