Commit 174904aa authored by Nathaniel Callens's avatar Nathaniel Callens

deletions

parent 3ee02b10
This diff is collapsed.
This diff is collapsed.
......@@ -156,7 +156,7 @@
{
"cell_type": "code",
"execution_count": 16,
"id": "f62c1af6",
"id": "48abcf1e",
"metadata": {},
"outputs": [],
"source": [
......@@ -308,7 +308,7 @@
{
"cell_type": "code",
"execution_count": 35,
"id": "9e91c81d",
"id": "0afd3bef",
"metadata": {},
"outputs": [],
"source": [
......@@ -480,7 +480,7 @@
{
"cell_type": "code",
"execution_count": 38,
"id": "d342f424",
"id": "329cc11b",
"metadata": {},
"outputs": [
{
......@@ -596,7 +596,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "c0bb307b",
"id": "a2582804",
"metadata": {},
"outputs": [],
"source": [
......@@ -662,7 +662,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "671f7847",
"id": "487fc2f2",
"metadata": {},
"outputs": [],
"source": [
......@@ -693,7 +693,7 @@
{
"cell_type": "code",
"execution_count": 39,
"id": "eec0746a",
"id": "b4998aef",
"metadata": {},
"outputs": [
{
......@@ -720,7 +720,7 @@
}
],
"source": [
"def plot_hist_lstsq(tiff_list):\n",
"def predict_pix_lstsq(tiff_list):\n",
"\n",
" image = tiff_list\n",
" image = Image.open(image) #Open the image and read it as an Image object\n",
......@@ -775,7 +775,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "700f6e7f",
"id": "db376cb9",
"metadata": {},
"outputs": [],
"source": [
......@@ -798,7 +798,7 @@
{
"cell_type": "code",
"execution_count": 43,
"id": "0c297da9",
"id": "7575133b",
"metadata": {},
"outputs": [
{
......@@ -829,7 +829,7 @@
{
"cell_type": "code",
"execution_count": 44,
"id": "d7fc288d",
"id": "dcc26973",
"metadata": {},
"outputs": [
{
......@@ -842,9 +842,72 @@
}
],
"source": [
"fre = rel_freq(list(res))\n",
"print(np.array(fre)@np.array(entropy))\n",
"print(3.93012/15)"
"def predict_pix(tiff_image, difference = True):\n",
" \"\"\"\n",
" This function predict the pixel values excluding the boundary.\n",
" Using the 4 neighbor pixel values and MSE to predict the next pixel value\n",
" (-1,1) (0,1) (1,1) => relative position of the 4 other given values\n",
" (-1,0) (0,0) => (0,0) is the one we want to predict\n",
" take the derivative of mean square error to solve for the system of equation \n",
" A = np.array([[3,0,-1],[0,3,3],[1,-3,-4]])\n",
" A @ [a, b, c] = [-z0+z2-z3, z0+z1+z2, -z0-z1-z2-z3] where z0 = (-1,1), z1 = (0,1), z2 = (1,1), z3 = (-1,0)\n",
" and the predicted pixel value is c.\n",
" \n",
" Input:\n",
" tiff_image (string): path to the tiff file\n",
" \n",
" Return:\n",
" image (512 X 640): original image \n",
" predict (325380,): predicted image excluding the boundary\n",
" diff. (325380,): IF difference = TRUE, difference between the min and max of four neighbors exclude the boundary\n",
" ELSE: the residuals of the four nearest pixels to a fitted hyperplane\n",
" error (325380,): difference between the original image and predicted image\n",
" A (3 X 3): system of equation\n",
" \"\"\"\n",
" image = Image.open(tiff_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",
" image = image.astype(int)\n",
" print(image.shape)\n",
" # use \n",
" A = np.array([[3,0,-1],[0,3,3],[1,-3,-4]]) # the matrix for system of equation\n",
" # where z0 = (-1,1), z1 = (0,1), z2 = (1,1), z3 = (-1,0)\n",
" z0 = image[0:-2,0:-2] # get all the first pixel for the entire image\n",
" z1 = image[0:-2,1:-1] # get all the second pixel for the entire image\n",
" z2 = image[0:-2,2::] # get all the third pixel for the entire image\n",
" z3 = image[1:-1,0:-2] # get all the forth pixel for the entire image\n",
" # calculate the out put of the system of equation\n",
" y0 = np.ravel(-z0+z2-z3)\n",
" y1 = np.ravel(z0+z1+z2)\n",
" y2 = np.ravel(-z0-z1-z2-z3)\n",
" y = np.vstack((y0,y1,y2))\n",
" # use numpy solver to solve the system of equations all at once\n",
" #predict = np.floor(np.linalg.solve(A,y)[-1])\n",
" predict = np.round(np.round((np.linalg.solve(A,y)[-1]),1))\n",
" \n",
" #Matrix system of points that will be used to solve the least squares fitting hyperplane\n",
" points = np.array([[-1,-1,1], [-1,0,1], [-1,1,1], [0,-1,1]])\n",
" \n",
" # flatten the neighbor pixlels and stack them together\n",
" z0 = np.ravel(z0)\n",
" z1 = np.ravel(z1)\n",
" z2 = np.ravel(z2)\n",
" z3 = np.ravel(z3)\n",
" neighbor = np.vstack((z0,z1,z2,z3)).T\n",
" \n",
" if difference:\n",
" # calculate the difference\n",
" diff = np.max(neighbor,axis = 1) - np.min(neighbor, axis=1)\n",
" \n",
" else:\n",
" #Compute the best fitting hyperplane using least squares\n",
" #The res is the residuals of the four points used to fit the hyperplane (summed distance of each of the \n",
" #points to the hyperplane), it is a measure of gradient\n",
" f, diff, rank, s = la.lstsq(points, neighbor.T, rcond=None) \n",
" \n",
" # calculate the error\n",
" error = np.ravel(image[1:-1,1:-1])-predict\n",
" \n",
" return image, predict, diff, error, A\n"
]
}
],
......
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "dbef8759",
"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\n",
"from time import time"
]
},
{
"cell_type": "code",
"execution_count": 44,
"id": "b7a550e0",
"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:-5] == num:\n",
" tiff.append(im)\n",
" return tiff"
]
},
{
"cell_type": "code",
"execution_count": 51,
"id": "9ed20f84",
"metadata": {},
"outputs": [],
"source": [
"def plot_hist(tiff_list,i):\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",
" \n",
"\n",
" image = tiff_list[i]\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",
" image = image.astype(int)\n",
" A = np.array([[3,0,-1],[0,3,3],[1,-3,-4]]) # the matrix for system of equation\n",
" z0 = image[0:-2,0:-2] # get all the first pixel for the entire image\n",
" z1 = image[0:-2,1:-1] # get all the second pixel for the entire image\n",
" z2 = image[0:-2,2::] # get all the third pixel for the entire image\n",
" z3 = image[1:-1,0:-2] # get all the forth pixel for the entire image\n",
" # calculate the out put of the system of equation\n",
" y0 = np.ravel(-z0+z2-z3)\n",
" y1 = np.ravel(z0+z1+z2)\n",
" y2 = np.ravel(-z0-z1-z2-z3)\n",
" y = np.vstack((y0,y1,y2))\n",
" # use numpy solver to solve the system of equations all at once\n",
" predict = np.linalg.solve(A,y)[-1]\n",
" # flatten the neighbor pixlels and stack them together\n",
" z0 = np.ravel(z0)\n",
" z1 = np.ravel(z1)\n",
" z2 = np.ravel(z2)\n",
" z3 = np.ravel(z3)\n",
" neighbor = np.vstack((z0,z1,z2,z3)).T\n",
" # calculate the difference\n",
" diff = np.max(neighbor,axis = 1) - np.min(neighbor, axis=1)\n",
" # flatten the image to a vector\n",
" image = np.ravel(image[1:-1,1:-1])\n",
" return image, predict, diff\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 49,
"id": "8e3ef654",
"metadata": {},
"outputs": [],
"source": [
"scenes = file_extractor()\n",
"images = image_extractor(scenes)\n",
"num_images = im_distribution(images, \"_9\")\n",
"error_mean = []\n",
"error_mean1 = []\n",
"diff_mean = []\n",
"times = []\n",
"times1 = []\n",
"all_error = []\n",
"for i in range(len(num_images)):\n",
" \"\"\"start1 = time()\n",
" image_1, predict_1, difference_1, x_s_1 = plot_hist(num_images, i, \"second\")\n",
" stop1 = time()\n",
" times1.append(stop1-start1)\n",
" error1 = np.abs(image_1-predict_1)\n",
" error_mean1.append(np.mean(np.ravel(error1)))\"\"\"\n",
" start = time()\n",
" image, predict, difference = plot_hist(num_images, i)\n",
" stop = time()\n",
" times.append(stop-start)\n",
" error = np.abs(image-predict)\n",
" all_error.append(np.ravel(error))\n",
" error_mean.append(np.mean(np.ravel(error)))\n",
" diff_mean.append(np.mean(np.ravel(difference)))"
]
},
{
"cell_type": "code",
"execution_count": 52,
"id": "fa65dcd6",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Average Error First and Second Added: 20.017164930235474\n",
"Standard Deviaiton of Mean Errors: 0.16101183692475135\n",
"Average Difference: 53.678648426455226\n",
"Average Time per Image for First: 0.04535740613937378\n"
]
}
],
"source": [
"print(f\"Average Error First and Second Added: {np.mean(error_mean)}\")\n",
"print(f\"Standard Deviaiton of Mean Errors: {np.sqrt(np.var(error_mean))}\")\n",
"print(f\"Average Difference: {np.mean(diff_mean)}\")\n",
"print(f\"Average Time per Image for First: {np.mean(times)}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b7e88aab",
"metadata": {},
"outputs": [],
"source": []
}
],
"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
}
This diff is collapsed.
......@@ -512,7 +512,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.1"
"version": "3.8.11"
}
},
"nbformat": 4,
......
This diff is collapsed.
......@@ -156,7 +156,7 @@
{
"cell_type": "code",
"execution_count": 16,
"id": "962f1139",
"id": "48abcf1e",
"metadata": {},
"outputs": [],
"source": [
......@@ -308,7 +308,7 @@
{
"cell_type": "code",
"execution_count": 35,
"id": "489f0def",
"id": "0afd3bef",
"metadata": {},
"outputs": [],
"source": [
......@@ -480,7 +480,7 @@
{
"cell_type": "code",
"execution_count": 38,
"id": "4fd1482b",
"id": "329cc11b",
"metadata": {},
"outputs": [
{
......@@ -596,7 +596,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "63ba5ba1",
"id": "a2582804",
"metadata": {},
"outputs": [],
"source": [
......@@ -662,7 +662,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "e5f6e2d4",
"id": "487fc2f2",
"metadata": {},
"outputs": [],
"source": [
......@@ -693,7 +693,7 @@
{
"cell_type": "code",
"execution_count": 39,
"id": "db1a1c1f",
"id": "b4998aef",
"metadata": {},
"outputs": [
{
......@@ -775,7 +775,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "926bac7c",
"id": "db376cb9",
"metadata": {},
"outputs": [],
"source": [
......@@ -798,7 +798,7 @@
{
"cell_type": "code",
"execution_count": 43,
"id": "aef3da82",
"id": "7575133b",
"metadata": {},
"outputs": [
{
......@@ -829,7 +829,7 @@
{
"cell_type": "code",
"execution_count": 44,
"id": "87e480ca",
"id": "dcc26973",
"metadata": {},
"outputs": [
{
......@@ -842,9 +842,72 @@
}
],
"source": [
"fre = rel_freq(list(res))\n",
"print(np.array(fre)@np.array(entropy))\n",
"print(3.93012/15)"
"def predict_pix(tiff_image, difference = True):\n",
" \"\"\"\n",
" This function predict the pixel values excluding the boundary.\n",
" Using the 4 neighbor pixel values and MSE to predict the next pixel value\n",
" (-1,1) (0,1) (1,1) => relative position of the 4 other given values\n",
" (-1,0) (0,0) => (0,0) is the one we want to predict\n",
" take the derivative of mean square error to solve for the system of equation \n",
" A = np.array([[3,0,-1],[0,3,3],[1,-3,-4]])\n",
" A @ [a, b, c] = [-z0+z2-z3, z0+z1+z2, -z0-z1-z2-z3] where z0 = (-1,1), z1 = (0,1), z2 = (1,1), z3 = (-1,0)\n",
" and the predicted pixel value is c.\n",
" \n",
" Input:\n",
" tiff_image (string): path to the tiff file\n",
" \n",
" Return:\n",
" image (512 X 640): original image \n",
" predict (325380,): predicted image excluding the boundary\n",
" diff. (325380,): IF difference = TRUE, difference between the min and max of four neighbors exclude the boundary\n",
" ELSE: the residuals of the four nearest pixels to a fitted hyperplane\n",
" error (325380,): difference between the original image and predicted image\n",
" A (3 X 3): system of equation\n",
" \"\"\"\n",
" image = Image.open(tiff_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",
" image = image.astype(int)\n",
" print(image.shape)\n",
" # use \n",
" A = np.array([[3,0,-1],[0,3,3],[1,-3,-4]]) # the matrix for system of equation\n",
" # where z0 = (-1,1), z1 = (0,1), z2 = (1,1), z3 = (-1,0)\n",
" z0 = image[0:-2,0:-2] # get all the first pixel for the entire image\n",
" z1 = image[0:-2,1:-1] # get all the second pixel for the entire image\n",
" z2 = image[0:-2,2::] # get all the third pixel for the entire image\n",
" z3 = image[1:-1,0:-2] # get all the forth pixel for the entire image\n",
" # calculate the out put of the system of equation\n",
" y0 = np.ravel(-z0+z2-z3)\n",
" y1 = np.ravel(z0+z1+z2)\n",
" y2 = np.ravel(-z0-z1-z2-z3)\n",
" y = np.vstack((y0,y1,y2))\n",
" # use numpy solver to solve the system of equations all at once\n",
" #predict = np.floor(np.linalg.solve(A,y)[-1])\n",
" predict = np.round(np.round((np.linalg.solve(A,y)[-1]),1))\n",
" \n",
" #Matrix system of points that will be used to solve the least squares fitting hyperplane\n",
" points = np.array([[-1,-1,1], [-1,0,1], [-1,1,1], [0,-1,1]])\n",
" \n",
" # flatten the neighbor pixlels and stack them together\n",
" z0 = np.ravel(z0)\n",
" z1 = np.ravel(z1)\n",
" z2 = np.ravel(z2)\n",
" z3 = np.ravel(z3)\n",
" neighbor = np.vstack((z0,z1,z2,z3)).T\n",
" \n",
" if difference:\n",
" # calculate the difference\n",
" diff = np.max(neighbor,axis = 1) - np.min(neighbor, axis=1)\n",
" \n",
" else:\n",
" #Compute the best fitting hyperplane using least squares\n",
" #The res is the residuals of the four points used to fit the hyperplane (summed distance of each of the \n",
" #points to the hyperplane), it is a measure of gradient\n",
" f, diff, rank, s = la.lstsq(points, neighbor.T, rcond=None) \n",
" \n",
" # calculate the error\n",
" error = np.ravel(image[1:-1,1:-1])-predict\n",
" \n",
" return image, predict, diff, error, A\n"
]
}
],
......
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment