Skip to content

Instantly share code, notes, and snippets.

@pgolding
Last active October 27, 2018 19:42
Show Gist options
  • Save pgolding/618c65e98b70261c616ada422f09373d to your computer and use it in GitHub Desktop.
Save pgolding/618c65e98b70261c616ada422f09373d to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {},
"cell_type": "markdown",
"source": "# Pixel Math\n\nA visual reminder of pixel math."
},
{
"metadata": {
"trusted": true,
"collapsed": false
},
"cell_type": "code",
"source": "import numpy as np\n\n# Make a 2x2 array of R,G,B pixels (or BGR for CV2)\npixel00 = [255,255,255]\npixel01 = [200,200,200]\npixel10 = [0,0,0]\npixel11 = [100,100,100]\nd = np.array([[pixel00, pixel01],[pixel10,pixel11]])",
"execution_count": 21,
"outputs": []
},
{
"metadata": {
"trusted": true,
"collapsed": false
},
"cell_type": "code",
"source": "# Confirm the shape\nd.shape",
"execution_count": 22,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": "(2, 2, 3)"
},
"metadata": {},
"execution_count": 22
}
]
},
{
"metadata": {
"trusted": true,
"collapsed": false
},
"cell_type": "code",
"source": "# view the data\nd",
"execution_count": 23,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": "array([[[255, 255, 255],\n [200, 200, 200]],\n\n [[ 0, 0, 0],\n [100, 100, 100]]])"
},
"metadata": {},
"execution_count": 23
}
]
},
{
"metadata": {
"trusted": true,
"collapsed": true
},
"cell_type": "code",
"source": "# Now load a 32x32 RGB image from the cifar10 dataset\nimport cv2\nim = cv2.imread(\"airplane4.png\")",
"execution_count": 24,
"outputs": []
},
{
"metadata": {
"trusted": true,
"collapsed": false
},
"cell_type": "code",
"source": "im.shape",
"execution_count": 25,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": "(32, 32, 3)"
},
"metadata": {},
"execution_count": 25
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "![title](https://user-images.githubusercontent.com/28526/47608637-a42dc000-d9e5-11e8-9f3b-4d73051d0db0.png)"
},
{
"metadata": {
"trusted": true,
"collapsed": false
},
"cell_type": "code",
"source": "# Looks similar to 2x2\nim[0]",
"execution_count": 27,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": "array([[177, 124, 57],\n [170, 122, 57],\n [174, 123, 57],\n [179, 123, 59],\n [178, 125, 62],\n [178, 128, 61],\n [179, 129, 62],\n [178, 129, 61],\n [180, 132, 59],\n [183, 132, 61],\n [183, 132, 61],\n [184, 133, 62],\n [184, 133, 62],\n [185, 134, 63],\n [185, 134, 63],\n [185, 134, 63],\n [186, 136, 64],\n [187, 136, 65],\n [188, 135, 65],\n [188, 135, 64],\n [188, 136, 64],\n [189, 138, 63],\n [190, 139, 61],\n [190, 137, 64],\n [190, 137, 64],\n [189, 136, 64],\n [184, 134, 67],\n [182, 134, 67],\n [183, 134, 67],\n [184, 131, 69],\n [189, 131, 65],\n [186, 132, 63]], dtype=uint8)"
},
"metadata": {},
"execution_count": 27
}
]
},
{
"metadata": {
"trusted": true,
"collapsed": false
},
"cell_type": "code",
"source": "# to normalize for feeding into a ML net - e.g. neural network\nd = d / 255.0",
"execution_count": 9,
"outputs": []
},
{
"metadata": {
"trusted": true,
"collapsed": false
},
"cell_type": "code",
"source": "d",
"execution_count": 11,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": "array([[[1. , 1. , 1. ],\n [0.78431373, 0.78431373, 0.78431373]],\n\n [[0. , 0. , 0. ],\n [0.39215686, 0.39215686, 0.39215686]]])"
},
"metadata": {},
"execution_count": 11
}
]
},
{
"metadata": {
"trusted": true,
"collapsed": false
},
"cell_type": "code",
"source": "# reshape to feed as single feature vector\nd.reshape(d.shape[0] * d.shape[1] * d.shape[2])",
"execution_count": 20,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": "array([1. , 1. , 1. , 0.78431373, 0.78431373,\n 0.78431373, 0. , 0. , 0. , 0.39215686,\n 0.39215686, 0.39215686])"
},
"metadata": {},
"execution_count": 20
}
]
},
{
"metadata": {
"trusted": true,
"collapsed": true
},
"cell_type": "code",
"source": "",
"execution_count": null,
"outputs": []
}
],
"metadata": {
"kernelspec": {
"name": "conda-env-ocr-py",
"display_name": "Python [conda env:ocr]",
"language": "python"
},
"language_info": {
"nbconvert_exporter": "python",
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"name": "python",
"pygments_lexer": "ipython3",
"version": "3.5.4",
"mimetype": "text/x-python",
"file_extension": ".py"
},
"gist_id": "618c65e98b70261c616ada422f09373d"
},
"nbformat": 4,
"nbformat_minor": 0
}
@pgolding
Copy link
Author

Link to image:

airplane4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment