This repository has been archived on 2021-02-05. You can view files and clone it, but cannot push or open issues or pull requests.
ipcv/julia_notebook/jpeg.ipynb
Nguyễn Anh Khoa a43837539e init
2019-05-14 22:37:19 +07:00

410 lines
17 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"using Images\n",
"using FFTW\n",
"using Formatting"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"8×8 Array{Int64,2}:\n",
" 1 2 6 7 15 16 28 29\n",
" 3 5 8 14 17 27 30 43\n",
" 4 9 13 18 26 31 42 44\n",
" 10 12 19 25 32 41 45 54\n",
" 11 20 24 33 40 46 53 55\n",
" 21 23 34 39 47 52 56 61\n",
" 22 35 38 48 51 57 60 62\n",
" 36 37 49 50 58 59 63 64"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"LUMINANCE_QUANTIZATION_TABLE = [\n",
" 16 11 10 16 24 40 51 61 \n",
" 12 12 14 19 26 58 60 55 \n",
" 14 13 16 24 40 57 69 56 \n",
" 14 17 22 29 51 87 80 62 \n",
" 18 22 37 56 68 109 103 77 \n",
" 24 35 55 64 81 104 113 92 \n",
" 49 64 78 87 103 121 120 101 \n",
" 72 92 95 98 112 100 103 99 \n",
" ]\n",
"\n",
"ZIGZAG = [\n",
" 0 1 5 6 14 15 27 28\n",
" 2 4 7 13 16 26 29 42\n",
" 3 8 12 17 25 30 41 43\n",
" 9 11 18 24 31 40 44 53\n",
" 10 19 23 32 39 45 52 54\n",
" 20 22 33 38 46 51 55 60\n",
" 21 34 37 47 50 56 59 61\n",
" 35 36 48 49 57 58 62 63\n",
" ] .+ 1"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"to_zigzag (generic function with 1 method)"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"function to_zigzag(block)\n",
" out_block = zeros(eltype(block), size(ZIGZAG))\n",
" for idx in CartesianIndices(ZIGZAG)\n",
" out_block[ZIGZAG[idx]] = block[idx]\n",
" # display(out_block)\n",
" end\n",
" reshape(out_block, (1,64))\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"720×1280 Array{Float32,2}:\n",
" 39.2166 39.4639 39.4639 39.0577 … 37.4843 37.4843 37.4843 37.4843\n",
" 39.4639 39.4639 39.0577 39.0577 37.4843 37.4843 37.4843 37.4843\n",
" 39.4639 39.0577 39.0577 39.0577 37.4843 37.4843 37.4843 37.4843\n",
" 39.0577 39.0577 39.0577 39.9165 38.0348 37.4843 37.4843 37.4843\n",
" 39.0577 39.9165 39.9165 39.9165 38.0348 37.176 37.4843 37.4843\n",
" 39.9165 39.9165 39.9165 40.1639 … 38.0348 38.0348 37.4843 37.4843\n",
" 39.9165 39.9165 39.9165 40.1639 38.0348 38.0348 37.4843 37.4843\n",
" 39.9165 39.9165 39.9165 40.1639 38.1327 38.0348 38.3432 37.4843\n",
" 39.4639 39.4639 39.9165 40.1639 39.4493 38.3432 38.0348 38.5484\n",
" 39.4639 39.4639 39.0577 40.1639 39.4493 38.3432 38.0348 38.5484\n",
" 39.4639 39.4639 39.0577 40.1639 … 39.4493 38.5905 38.0348 38.0348\n",
" 39.4639 39.0577 39.0577 40.1639 39.1925 39.4493 38.5905 38.0348\n",
" 39.0577 39.0577 39.0577 40.1639 39.1925 39.4493 38.5905 38.5905\n",
" ⋮ ⋱ \n",
" 30.5854 31.1969 31.7105 32.261 37.663 37.663 37.663 37.663 \n",
" 31.3927 31.3927 31.4021 31.3927 38.2135 38.2135 38.2135 38.4608\n",
" 31.64 31.64 31.64 30.5244 … 38.4608 38.4608 39.3196 39.3196\n",
" 31.3738 30.515 30.515 29.6562 38.4608 38.4608 39.3196 39.3196\n",
" 28.751 30.7666 30.7202 27.99 39.3196 39.3196 39.3196 39.3196\n",
" 29.049 31.4253 28.8394 30.0572 38.4608 38.4608 38.4608 38.4608\n",
" 29.6141 29.3899 26.2063 32.4233 38.2135 38.2135 38.2135 38.2135\n",
" 29.7077 29.1984 33.8936 25.0958 … 37.663 37.663 37.663 37.663 \n",
" 28.7364 32.2369 29.0447 29.6003 37.663 37.663 37.663 37.663 \n",
" 32.5504 24.4422 29.8614 31.7749 37.663 37.663 37.663 37.663 \n",
" 29.876 31.5507 29.8468 28.1386 37.3546 37.3546 37.3546 37.3546\n",
" 31.5645 30.7108 31.397 29.6708 38.2135 38.2135 38.2135 37.3546"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"8×8 Array{Float32,2}:\n",
" 55.4277 55.736 54.8772 54.8772 55.736 55.736 55.736 55.736 \n",
" 56.2866 56.2866 57.4537 57.4537 56.5949 55.736 55.736 55.736 \n",
" 57.9948 57.9948 57.1359 56.0813 55.2225 55.2225 55.736 56.5949\n",
" 58.2421 58.2421 58.2421 58.2421 58.2421 58.0463 56.9401 56.9401\n",
" 57.3833 58.4894 61.0659 62.7836 63.6424 61.6774 57.7989 55.2225\n",
" 58.4894 58.4894 58.9326 60.6502 61.5091 61.0659 58.2421 56.0813\n",
" 61.0659 59.6935 59.7914 59.1799 58.9326 58.0832 58.2421 57.7989\n",
" 61.0659 62.27 64.0855 64.0246 62.3679 59.8009 58.2421 56.9401"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"8×8 Array{Float32,2}:\n",
" -71.5723 -71.264 -72.1228 -72.1228 … -71.264 -71.264 -71.264 \n",
" -70.7134 -70.7134 -69.5463 -69.5463 -71.264 -71.264 -71.264 \n",
" -69.0052 -69.0052 -69.8641 -70.9187 -71.7775 -71.264 -70.4051\n",
" -68.7579 -68.7579 -68.7579 -68.7579 -68.9537 -70.0599 -70.0599\n",
" -69.6167 -68.5106 -65.9341 -64.2164 -65.3226 -69.2011 -71.7775\n",
" -68.5106 -68.5106 -68.0674 -66.3498 … -65.9341 -68.7579 -70.9187\n",
" -65.9341 -67.3065 -67.2086 -67.8201 -68.9168 -68.7579 -69.2011\n",
" -65.9341 -64.73 -62.9145 -62.9754 -67.1991 -68.7579 -70.0599"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"8×8 Array{Float32,2}:\n",
" -550.578 4.7784 … 0.00653681 0.205445 0.212533 \n",
" -13.5756 -2.92463 -0.472033 -0.130214 -0.218505 \n",
" -1.48771 1.21404 0.279535 -0.391432 0.330444 \n",
" -0.673342 -2.97597 -0.222817 -0.0539551 -0.0560064\n",
" 3.04324 0.102968 -0.474424 -0.0202312 0.0131407\n",
" -3.61937 -0.509858 … -0.253103 -0.123185 -0.0314786\n",
" -0.353786 0.00731296 -0.460769 -0.278703 -0.466107 \n",
" -0.00664318 0.52792 -0.0775939 -0.022749 0.307393 "
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"8×8 Array{Int32,2}:\n",
" -34 0 -1 0 0 0 0 0\n",
" -1 0 0 0 0 0 0 0\n",
" 0 0 0 0 0 0 0 0\n",
" 0 0 0 0 0 0 0 0\n",
" 0 0 0 0 0 0 0 0\n",
" 0 0 0 0 0 0 0 0\n",
" 0 0 0 0 0 0 0 0\n",
" 0 0 0 0 0 0 0 0"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"1×64 Array{Int32,2}:\n",
" -34 0 -1 0 0 -1 0 0 0 0 0 … 0 0 0 0 0 0 0 0 0 0 0 0"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"14400-element Array{Any,1}:\n",
" -44\n",
" -43\n",
" -43\n",
" -42\n",
" -42\n",
" -42\n",
" -41\n",
" -41\n",
" -41\n",
" -41\n",
" -40\n",
" -40\n",
" -39\n",
" ⋮\n",
" -42\n",
" -41\n",
" -41\n",
" -41\n",
" -41\n",
" -42\n",
" -43\n",
" -43\n",
" -44\n",
" -44\n",
" -44\n",
" -45"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"14400-element Array{Any,1}:\n",
" Int32[0, 0, 0, 0, 0, 0, 0, 0, 0, 0 … 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] \n",
" Int32[0, 0, 0, 0, 0, 0, 0, 0, 0, 0 … 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] \n",
" Int32[0, 0, 0, 0, 0, 0, 0, 0, 0, 0 … 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] \n",
" Int32[0, 0, 0, 0, 0, 0, 0, 0, 0, 0 … 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] \n",
" Int32[0, 0, 0, 0, 0, 0, 0, 0, 0, 0 … 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] \n",
" Int32[0, 0, 0, 0, 0, 0, 0, 0, 0, 0 … 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] \n",
" Int32[0, 0, 0, 0, 0, 0, 0, 0, 0, 0 … 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] \n",
" Int32[0, 0, 0, 0, 0, 0, 0, 0, 0, 0 … 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] \n",
" Int32[0, 0, 0, 0, 0, 0, 0, 0, 0, 0 … 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] \n",
" Int32[0, 0, 0, 0, 0, 0, 0, 0, 0, 0 … 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] \n",
" Int32[0, 0, 0, 0, 0, 0, 0, 0, 0, 0 … 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] \n",
" Int32[0, 0, 0, 0, 0, 0, 0, 0, 0, 0 … 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] \n",
" Int32[0, 0, 0, 0, 0, 0, 0, 0, 0, 0 … 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] \n",
" ⋮ \n",
" Int32[0, 0, 0, 0, 0, 0, 0, 0, 0, 0 … 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] \n",
" Int32[-1, 0, 0, 0, 0, 0, 0, 0, 0, 0 … 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]\n",
" Int32[0, 0, 0, 0, 0, 0, 0, 0, 0, 0 … 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] \n",
" Int32[0, 0, 0, 0, 0, 0, 0, 0, 0, 0 … 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] \n",
" Int32[1, 0, 0, 0, 0, 0, 0, 0, 0, 0 … 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] \n",
" Int32[0, 0, 0, 0, 0, 0, 0, 0, 0, 0 … 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] \n",
" Int32[0, 0, 0, 0, 0, 0, 0, 0, 0, 0 … 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] \n",
" Int32[0, 0, 0, 0, 0, 0, 0, 0, 0, 0 … 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] \n",
" Int32[0, 0, 0, 0, 0, 0, 0, 0, 0, 0 … 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] \n",
" Int32[0, 0, 0, 0, 0, 0, 0, 0, 0, 0 … 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] \n",
" Int32[0, 0, 0, 0, 0, 0, 0, 0, 0, 0 … 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] \n",
" Int32[0, 0, 0, 0, 0, 0, 0, 0, 0, 0 … 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] "
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"720×1280 Array{Float32,2}:\n",
" 141.368 141.226 141.226 141.956 … 143.855 143.855 143.855 143.855\n",
" 141.226 141.226 141.956 141.956 143.855 143.855 143.855 143.855\n",
" 141.226 141.956 141.956 141.956 143.855 143.855 143.855 143.855\n",
" 141.956 141.956 141.956 141.956 145.025 143.855 143.855 143.855\n",
" 141.956 141.956 141.956 141.956 145.025 145.025 143.855 143.855\n",
" 141.956 141.956 141.956 141.813 … 145.025 145.025 143.855 143.855\n",
" 141.956 141.956 141.956 141.813 145.025 145.025 143.855 143.855\n",
" 141.956 141.956 141.956 141.813 145.464 145.025 143.855 143.855\n",
" 141.226 141.226 141.956 141.813 143.713 143.855 145.025 144.728\n",
" 141.226 141.226 141.956 141.813 143.713 143.855 145.025 144.728\n",
" 141.226 141.226 141.956 141.813 … 143.713 143.713 145.025 145.025\n",
" 141.226 141.956 141.956 141.813 143.861 143.713 143.713 145.025\n",
" 141.956 141.956 141.956 141.813 143.861 143.713 143.713 143.713\n",
" ⋮ ⋱ \n",
" 130.487 130.63 130.333 131.503 138.299 138.299 138.299 138.299\n",
" 131.508 131.508 131.503 131.508 139.469 139.469 139.469 139.326\n",
" 131.366 131.366 131.366 131.514 … 139.326 139.326 139.326 139.326\n",
" 131.519 131.519 131.519 131.519 139.326 139.326 139.326 139.326\n",
" 130.059 133.853 132.392 130.498 139.326 139.326 139.326 139.326\n",
" 133.853 130.498 130.503 133.271 139.326 139.326 139.326 139.326\n",
" 132.535 131.673 134.006 134.879 139.469 139.469 139.469 139.469\n",
" 130.498 133.271 135.022 131.673 … 138.299 138.299 138.299 138.299\n",
" 132.546 133.995 131.376 130.064 138.299 138.299 138.299 138.299\n",
" 130.344 132.546 132.392 133.271 138.299 138.299 138.299 138.299\n",
" 129.905 132.409 134.879 134.874 139.469 139.469 139.469 139.469\n",
" 134.879 132.398 130.514 129.032 139.469 139.469 139.469 139.469"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"720×1280 Array{Float32,2}:\n",
" 115.448 114.641 114.641 114.938 … 115.459 115.459 115.459 115.459\n",
" 114.641 114.641 114.938 114.938 115.459 115.459 115.459 115.459\n",
" 114.641 114.938 114.938 114.938 115.459 115.459 115.459 115.459\n",
" 114.938 114.938 114.938 114.938 115.684 115.459 115.459 115.459\n",
" 114.938 114.938 114.938 114.938 115.684 115.684 115.459 115.459\n",
" 114.938 114.938 114.938 114.131 … 115.684 115.684 115.459 115.459\n",
" 114.938 114.938 114.938 114.131 115.684 115.684 115.459 115.459\n",
" 114.938 114.938 114.938 114.131 115.612 115.684 115.459 115.459\n",
" 114.641 114.641 114.938 114.131 114.652 115.459 115.684 116.562\n",
" 114.641 114.641 114.938 114.131 114.652 115.459 115.684 116.562\n",
" 114.641 114.641 114.938 114.131 … 114.652 114.652 115.684 115.684\n",
" 114.641 114.938 114.938 114.131 114.213 114.652 114.652 115.684\n",
" 114.938 114.938 114.938 114.131 114.213 114.652 114.652 114.652\n",
" ⋮ ⋱ \n",
" 128.011 128.818 129.696 129.921 114.702 114.702 114.702 114.702\n",
" 128.675 128.675 129.921 128.675 114.927 114.927 114.927 114.12 \n",
" 127.868 127.868 127.868 127.429 … 114.12 114.12 114.12 114.12 \n",
" 126.182 126.182 126.182 126.182 114.12 114.12 114.12 114.12 \n",
" 125.59 127.878 127.286 125.518 114.12 114.12 114.12 114.12 \n",
" 127.878 125.518 124.272 127.143 114.12 114.12 114.12 114.12 \n",
" 128.093 124.497 126.193 127.296 114.927 114.927 114.927 114.927\n",
" 125.518 127.143 128.103 124.497 … 114.702 114.702 114.702 114.702\n",
" 125.6 128.685 125.375 124.343 114.702 114.702 114.702 114.702\n",
" 127.204 125.6 127.286 127.143 114.702 114.702 114.702 114.702\n",
" 127.275 123.547 127.296 128.543 114.927 114.927 114.927 114.927\n",
" 127.296 126.04 121.78 126.172 114.927 114.927 114.927 114.927"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"img = load(\"../images/f124074.jpg\")\n",
"img_ycbcr = channelview(YCbCr.(img))\n",
"\n",
"for c in 1:3\n",
" channel = img_ycbcr[c,:,:]\n",
" row, col = size(channel)\n",
"\n",
" display(channel)\n",
" \n",
" DC = []\n",
" AC = []\n",
"\n",
" for i in 1:8:row\n",
" for j in 1:8:col\n",
" block = channel[i:i+7, j:j+7]\n",
"\n",
" block_shifted = block .- 127\n",
" block_dct = FFTW.dct(block_shifted)\n",
"\n",
" block_quantization = round.(Int32, block_dct ./ LUMINANCE_QUANTIZATION_TABLE)\n",
" # block_quantization = block_dct ./ LUMINANCE_QUANTIZATION_TABLE\n",
" block_zigzag = to_zigzag(block_quantization)\n",
"\n",
" if c == 1 && i == 1 + 8*10 && j == 1 + 8*3\n",
" display(block)\n",
" display(block_shifted)\n",
" display(block_dct)\n",
" display(block_quantization)\n",
" display(block_zigzag)\n",
" end\n",
"\n",
" # block_zigzag = collect(block_zigzag)\n",
" push!(DC, block_zigzag[1])\n",
" push!(AC, block_zigzag[2:end])\n",
" end\n",
" end\n",
"\n",
" if c == 1\n",
" display(DC)\n",
" display(AC)\n",
" end\n",
"end\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Julia 1.2.0-DEV",
"language": "julia",
"name": "julia-1.2"
},
"language_info": {
"file_extension": ".jl",
"mimetype": "application/julia",
"name": "julia",
"version": "1.2.0"
}
},
"nbformat": 4,
"nbformat_minor": 2
}