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/assignment/assignment_IMG_9056.ipynb

999 lines
787 KiB
Plaintext
Raw Normal View History

2019-05-17 02:33:41 +07:00
{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"┌ Info: Recompiling stale cache file /home/luibo/.julia/compiled/v1.2/ImageFeatures/JMzL1.ji for ImageFeatures [92ff4b2b-8094-53d3-b29d-97f740f06cef]\n",
"└ @ Base loading.jl:1187\n",
"┌ Warning: Package Distributions does not have Test in its dependencies:\n",
"│ - If you have Distributions checked out for development and have\n",
"│ added Test as a dependency but haven't updated your primary\n",
"│ environment's manifest file, try `Pkg.resolve()`.\n",
"│ - Otherwise you may need to report an issue with Distributions\n",
"└ Loading Test into Distributions from project dependency, future warnings for Distributions are suppressed.\n",
"┌ Warning: The call to compilecache failed to create a usable precompiled cache file for ImageFeatures [92ff4b2b-8094-53d3-b29d-97f740f06cef]\n",
"│ exception = ErrorException(\"Required dependency Distributions [31c24e10-a181-5473-b8eb-7969acd0382f] failed to load from a cache file.\")\n",
"└ @ Base loading.jl:972\n",
"┌ Info: Recompiling stale cache file /home/luibo/.julia/compiled/v1.2/Distributions/xILW0.ji for Distributions [31c24e10-a181-5473-b8eb-7969acd0382f]\n",
"└ @ Base loading.jl:1187\n",
"┌ Warning: Package Distributions does not have Test in its dependencies:\n",
"│ - If you have Distributions checked out for development and have\n",
"│ added Test as a dependency but haven't updated your primary\n",
"│ environment's manifest file, try `Pkg.resolve()`.\n",
"│ - Otherwise you may need to report an issue with Distributions\n",
"└ Loading Test into Distributions from project dependency, future warnings for Distributions are suppressed.\n"
]
}
],
"source": [
"using Images\n",
"using TestImages\n",
"using FileIO\n",
"using Colors\n",
"using ImageFeatures\n",
"using Formatting\n",
"# using Plots\n",
"using PyPlot\n",
"using DSP\n",
"using FixedPointNumbers\n",
"using Statistics"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"projectiveTransform (generic function with 1 method)"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"function backwardTransform(algo, M, P, outSize=nothing)\n",
" M = inv(M)\n",
" rows, cols = size(P)\n",
" if outSize == nothing\n",
" outSize = size(P)\n",
" end\n",
" out = zeros(eltype(P), outSize)\n",
"\n",
" for index in CartesianIndices(out)\n",
" y, x = Tuple(index)\n",
" sourceIndex = M * [x; y; 1]\n",
" sourceIndex ./= sourceIndex[end]\n",
" x1, y1 = round.(Int32, sourceIndex)\n",
" if (x1 < 1 || y1 < 1 || x1 > cols || y1 > rows)\n",
" continue\n",
" end\n",
" out[index] = P[y1, x1]\n",
" end\n",
" out\n",
"end\n",
"\n",
"function createProjectiveMatrix(points)\n",
" if length(points) != 4\n",
" error(\"Not correct number of point\")\n",
" end\n",
"\n",
" A = zeros(Int64, 2*length(points), 8)\n",
" B = zeros(Int64, 2*length(points), 1)\n",
" for i in 1:length(points)\n",
" x, y, x1, y1 = points[i]\n",
" A[2*i - 1,:] = [x y 1 0 0 0 -x*x1 -x1*y]\n",
" A[2*i,:] = [0 0 0 x y 1 -x*y1 -y*y1]\n",
" B[2*i - 1] = x1\n",
" B[2*i] = y1\n",
" end\n",
"\n",
" H = vcat(A\\B, 1)\n",
" reshape(H, (3,3))'\n",
"end\n",
"\n",
"function projectiveTransform(picture, points, outSize)\n",
" M = createProjectiveMatrix(points)\n",
" backwardTransform(\"zero\", M, picture, outSize)\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"hough (generic function with 1 method)"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"function hough(img)\n",
" rows, cols = size(img)\n",
" D = round(Int32, sqrt(rows ^ 2 + cols ^ 2))\n",
" rhos = -D:1:D\n",
" thetas = deg2rad.(-90:1:90)\n",
"\n",
" A = zeros(Int32, length(rhos), length(thetas))\n",
" sins, coss = sin.(thetas), cos.(thetas)\n",
" \n",
" for idx in CartesianIndices(img)\n",
" if img[idx] == 0\n",
" # 0 or false\n",
" continue\n",
" end\n",
"\n",
" y, x = Tuple(idx)\n",
" for thetaIdx in 1:length(thetas)\n",
" rho = x * coss[thetaIdx] + y * sins[thetaIdx]\n",
" rho = round(Int32, rho)\n",
" A[rho + D, thetaIdx] += 1\n",
" end\n",
" end\n",
" \n",
" rowsA, colsA = size(A)\n",
" for idx in CartesianIndices(A)\n",
" y, x = Tuple(idx)\n",
" if x == 1 || y == 1 || x == colsA || y == rowsA\n",
" continue\n",
" end\n",
" C = A[idx]\n",
" C1 = (C < A[y - 1, x - 1]) || (C < A[y - 1, x])\n",
" C2 = (C < A[y - 1, x + 1]) || (C < A[y, x + 1])\n",
" C3 = (C < A[y + 1, x + 1]) || (C < A[y + 1, x])\n",
" C4 = (C < A[y + 1, x - 1]) || (C < A[y, x - 1])\n",
" if C1 || C2 || C3 || C4\n",
" A[idx] = 0\n",
" end\n",
" end\n",
"\n",
" A, rhos, thetas\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3×3 Array{Int64,2}:\n",
" 1 2 1\n",
" 0 0 0\n",
" -1 -2 -1"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"gaussian_kernel = [\n",
" 1 4 6 4 1\n",
" 4 16 24 16 4\n",
" 6 24 36 24 6\n",
" 4 16 24 16 4\n",
" 1 4 6 4 1\n",
"] ./ 256\n",
"\n",
"sobel_x_kernel = [\n",
" -1 0 1\n",
" -2 0 2\n",
" -1 0 1\n",
"]\n",
"sobel_y_kernel = rotl90(sobel_x_kernel)"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [
{
"ename": "InexactError",
"evalue": "InexactError: trunc(Int32, -3.439150402e9)",
"output_type": "error",
"traceback": [
"InexactError: trunc(Int32, -3.439150402e9)",
"",
"Stacktrace:",
" [1] trunc at ./float.jl:675 [inlined]",
" [2] round at ./float.jl:361 [inlined]",
" [3] _broadcast_getindex_evalf at ./broadcast.jl:624 [inlined]",
" [4] _broadcast_getindex at ./broadcast.jl:607 [inlined]",
" [5] getindex at ./broadcast.jl:557 [inlined]",
" [6] macro expansion at ./broadcast.jl:887 [inlined]",
" [7] macro expansion at ./simdloop.jl:73 [inlined]",
" [8] copyto! at ./broadcast.jl:886 [inlined]",
" [9] copyto! at ./broadcast.jl:841 [inlined]",
" [10] copy at ./broadcast.jl:817 [inlined]",
" [11] materialize at ./broadcast.jl:797 [inlined]",
" [12] backwardTransform(::String, ::LinearAlgebra.Adjoint{Float64,Array{Float64,2}}, ::Array{RGB{Normed{UInt8,8}},2}, ::Tuple{Int64,Int64}) at ./In[3]:13",
" [13] projectiveTransform(::Array{RGB{Normed{UInt8,8}},2}, ::Array{Array{Int64,2},1}, ::Tuple{Int64,Int64}) at ./In[3]:43",
" [14] top-level scope at In[35]:14"
]
}
],
"source": [
"img_file = \"IPCV-TestImage/IMG_9056.JPG\"\n",
"rm(\"output/$(img_file)\", recursive=true, force=true)\n",
"mkpath(\"output/$(img_file)\")\n",
"\n",
"img = load(img_file)\n",
"A4size = (3508, 2480)\n",
"A4row, A4col = A4size\n",
"points = [\n",
" [3060 492 1 1],\n",
" [3257 2199 A4col 1],\n",
" [536 970 A4col A4row],\n",
" [978 2572 1 A4row]\n",
"]\n",
"img = projectiveTransform(img, points, A4size)\n",
"\n",
"display(img)\n",
"\n",
"img_gray = Float64.(Gray.(img))\n",
"sobel_x_kernel = Float64.(sobel_x_kernel)\n",
"sobel_y_kernel = Float64.(sobel_y_kernel)\n",
"\n",
"img_blur = conv2(img_gray, gaussian_kernel)\n",
"img_edge_x = conv2(img_blur, sobel_x_kernel)\n",
"img_edge_y = conv2(img_blur, sobel_y_kernel)\n",
"\n",
"img_edge = round.(Int32, sqrt.((img_edge_x .^ 2) .+ (img_edge_y .^ 2)))\n",
"img_edge[img_edge .> 0] .= 1\n",
"\n",
"# img_canny = canny(img, (Percentile(90), Percentile(10)))\n",
"# display(img_canny)\n",
"\n",
"\n",
"img_bool = zeros(Bool, size(img_edge))\n",
"img_bool[img_edge .> 0] .= true\n",
"display(Gray.(img_edge))\n",
"display()"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(2482, 0.0)"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"(2484, 0.0)"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"(-5, -90.0)"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"(3, 90.0)"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"(-4, -90.0)"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"(2, 90.0)"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"(1079, -26.0)"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"(1068, -26.0)"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"(-6, -90.0)"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"(4, 90.0)"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"(-3, -90.0)"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"(1, 90.0)"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"(257, -17.0)"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"(533, 83.0)"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"(454, 83.0)"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"(2, 0.0)"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"(1013, 81.0)"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"(806, 82.0)"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"(1008, 81.0)"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"(721, 82.0)"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"(1325, 80.0)"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"(1320, 80.0)"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"(458, 83.0)"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"(544, -20.0)"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"(1553, 79.0)"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"(1558, 79.0)"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"(-3512, -90.0)"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"(3510, 90.0)"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"(-3513, -90.0)"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"(3511, 90.0)"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"(821, -23.0)"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"(723, 82.0)"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"(456, 83.0)"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"(1103, 81.0)"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"(1224, 80.0)"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"(259, -17.0)"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"(1070, -26.0)"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"(534, -21.0)"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"(518, -21.0)"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"(535, 83.0)"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"(230, -18.0)"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"(2339, 76.0)"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"(2344, 76.0)"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"(1802, 78.0)"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"(2064, 77.0)"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"(808, 82.0)"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"(1797, 78.0)"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"(1663, 79.0)"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"(2059, 77.0)"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"(1081, -26.0)"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"acc, rhos, thetas = hough(img_edge)\n",
"for i in 1:50\n",
" r_idx, t_idx = Tuple(argmax(acc))\n",
" acc[r_idx, t_idx] = 0\n",
" display((rhos[r_idx], rad2deg(thetas[t_idx])))\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAeMAAAGiCAYAAADUc67xAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAAPYQAAD2EBqD+naQAAADl0RVh0U29mdHdhcmUA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QFTSmmHT/Xkt2C90lrjNCVCF9+3/wGR8ieyMgb41re+xR/8wR/wm7/5m/z2b/82f/EXf8HLywt/+Id/+K96nZAi6rsBxCGQ2ipR/QssdSWn2Z1YUFIXCIGtbKNi9SxWzdDJF1hrjZiU0swdcuujKh5wkQkxBkQ69E4OilqnWaPWEZiToiqsW0OjjEwv0Lp5JivKlDNmdSA5ikYhdt9IHqgaRkAFNPl1mXRSmui9UkohjMo3MJz02GiSE2rQW0eD0qyhEkZlHoCOhNdKuLVCq4pE8/eIkVrrsQH2DRcMeq9jMxpqCqPakyCIjGSmK9OUKaVSuwzEyp1+mtJRLfQ+kgIgxtdgve+N+XxmyhVMkKAOxQZl2zZijtStknPGzDidTizbQoyeRKy3GylnmvXjs1wuF2pvrMvGlCOX84VlvZKzB9PeO2qBGIXa8CCGcDmfjwTMczM9fvbPUY/PUdaVFgK9uROM6hVTAaKC1E6jkcNEN3d2rTmaEkOAERTL1ui4U8EMUaWLA3IxRkpxJGav9CQEvy8xeoWbIta6tyAGAgPdv29r4zUjcVTLQQOhu5PsxUA2YsgQBB0Vo69jI8R4IBiaImUgHVZ9TfTq++s07kkfAbxs3YNcSvTWMRUCEEOEAKU0ghjNDD1gdJh
"text/plain": [
"Figure(PyObject <Figure size 640x480 with 1 Axes>)"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"acc, rhos, thetas = hough(img_edge)\n",
"rows, cols = size(img)\n",
"my_dpi = 96\n",
"\n",
"plt = PyPlot\n",
"# PyPlot.svg(true)\n",
"plt.figure(figsize=(cols/my_dpi, rows/my_dpi), dpi=my_dpi)\n",
"\n",
"plot_img = plt.imread(img_file)\n",
"fig, ax = plt.subplots()\n",
"# fig = plt.figure(figsize=(cols/2, rows/2))\n",
"# fig.set_size_inches(cols,rows)\n",
"\n",
"xs = []\n",
"ys = []\n",
"\n",
"ax.imshow(plot_img)\n",
"while true\n",
" r_idx, t_idx = Tuple(argmax(acc))\n",
" if (acc[r_idx, t_idx] < 800)\n",
" break\n",
" end\n",
" acc[r_idx, t_idx] = 0\n",
"\n",
" rho, theta = rhos[r_idx], thetas[t_idx]\n",
" theta = rad2deg(theta)\n",
" # display(theta)\n",
" if (theta == 90 || theta == -90)\n",
" y = rho / sind(theta)\n",
" ax.axhline(y, linewidth=1)\n",
" push!(ys,y)\n",
" elseif (theta == 0)\n",
" x = rho\n",
" ax.axvline(x, linewidth=1)\n",
" push!(xs,x)\n",
" else\n",
" continue\n",
" end\n",
"end\n",
"savefig(\"output/$(img_file)/hough.svg\", dpi=my_dpi * 20)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3-element Array{Any,1}:\n",
" 2\n",
" 2482\n",
" 2484"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"12-element Array{Any,1}:\n",
" 1.0\n",
" 2.0\n",
" 3.0\n",
" 3.0\n",
" 4.0\n",
" 4.0\n",
" 5.0\n",
" 6.0\n",
" 3510.0\n",
" 3511.0\n",
" 3512.0\n",
" 3513.0"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAjsAAAGiCAYAAAABVwdNAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAAPYQAAD2EBqD+naQAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4zLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvnQurowAAIABJREFUeJzsvU3PbUtS3/mLyMy19t7POfcWBW4QMhbd/gBGQlaJacuSP4LHDD3wpEYwscWI/gBmhNQSQ74BE9RDtyzB1AwatVqWMC+Fq+495zx7r5WZET2IWOs51UZym67bFNc7pKt7Xp6z93rJjPjHP/4RKe7uPO1pT3va0572tKd9S03/ri/gaU972tOe9rSnPe2btCfYedrTnva0pz3tad9qe4Kdpz3taU972tOe9q22J9h52tOe9rSnPe1p32p7gp2nPe1pT3va0572rbYn2Hna0572tK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+vyxrPQASRcq7TZVnO9Tjn5HK5xDPwN0AmDlUL4rDWC0nIne+5iMZ1kNfsJRgW/MxyPwdEwaLEurQx0VZZykprjZf3X+Qei+eqGs/c/QCcBFg9gFCCnKJvay+ef6wjPdgP1wTgwcBgfr6jg2E5rJWKeAAstRlr3SdVC7OPZAyA4kgtuA2cGWup8OZ3kh0rpbCujZYMkzj4HLRSKUIy0yCWfsALZVEofgKtfXRKXRgeSZCZUddyrpNSYv/WWjmA5eVyQRXWS7yPdy9XWg2mo7WGFFBX6lr48PWnSAbNef1wp4hyv99PMGVmvFxv/Nmf/Rnrco29ZsaYAa57D+B
"text/plain": [
"Figure(PyObject <Figure size 640x480 with 1 Axes>)"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"xs = sort(xs)\n",
"ys = sort(ys)\n",
"\n",
"display(xs)\n",
"display(ys)\n",
"\n",
"function get_arround(arr)\n",
" out = Float64[]\n",
" tmp = Float64[]\n",
" last = 0\n",
" for val in arr\n",
" if last == 0\n",
" last = val\n",
" continue\n",
" end\n",
" push!(tmp, val)\n",
" if abs(val - last) > 10\n",
" # push!(out, round(Int32,mean(tmp)))\n",
" push!(out, tmp[1])\n",
" tmp = []\n",
" end\n",
" last = val\n",
" end\n",
" Int32.(out)\n",
"end\n",
"\n",
"xss = get_arround(Float64.(xs))\n",
"yss = get_arround(Float64.(ys))\n",
" \n",
"# display(xss)\n",
"# display(yss)\n",
" \n",
"plt.figure(figsize=(cols/my_dpi, rows/my_dpi), dpi=my_dpi)\n",
"plot_img = plt.imread(img_file)\n",
"fig, ax = plt.subplots()\n",
"ax.imshow(plot_img)\n",
"for y in yss\n",
" ax.axhline(y, linewidth=1)\n",
"end\n",
"for x in xss\n",
" ax.axvline(x, linewidth=1)\n",
"end\n",
"savefig(\"output/$(img_file)/better_hough.svg\", dpi=my_dpi * 20)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"sub_imgs = []\n",
"for i in 4:length(xss)-1\n",
" mkpath(\"output/$(img_file)/$(i-1)\")\n",
" # display((i, !(i in [3,4,5,7])))\n",
"# if (!(i in [3,4,5,7,8]))\n",
"# continue\n",
"# end\n",
" for j in 6:length(yss)-1\n",
"# if j < 4\n",
"# continue\n",
"# end\n",
" c = xss[i-1]:xss[i]\n",
" r = yss[j-1]:yss[j]\n",
" sub_img = img[r,c]\n",
" push!(sub_imgs, sub_img)\n",
" display(sub_img)\n",
" # display(\"======================\")\n",
" save(\"output/$(img_file)/$(i-1)/$(j-1).png\", sub_img)\n",
" end\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"# sub_img = sub_imgs[28]\n",
"for sub_img in sub_imgs\n",
" display(sub_img)\n",
" sub_img_f = Float64.(Gray.(sub_img))\n",
" gray_point = vec(sum(sub_img_f, dims=1))\n",
" gray_maximum = maximum(gray_point)\n",
" gray_point[gray_point .< 10] .= 0\n",
" gray_point[abs.(gray_point .- gray_maximum) .< 10] .= gray_maximum\n",
"\n",
" # display(gray_point)\n",
" plt.plot(gray_point)\n",
"\n",
" digit_start = 1\n",
" digit_end = length(gray_point)\n",
" slide = 0\n",
" window_size = 0\n",
"\n",
" for (index, value) in enumerate(gray_point)\n",
" if value == 0 || value == gray_maximum\n",
" if window_size > 2\n",
" digit_end = index\n",
" break\n",
" elseif slide == 1\n",
" slide = 0\n",
" end\n",
" continue\n",
" end\n",
" if slide == 0\n",
" digit_start = index - 1\n",
" slide = 1\n",
" window_size = 1\n",
" elseif index - digit_start > 1\n",
" digit_start = index\n",
" window_size = 1\n",
" end\n",
" window_size += 1\n",
" end\n",
"\n",
" if (digit_start < 11)\n",
" digit_start = 11\n",
" end\n",
" display((digit_start, digit_end))\n",
" display(sub_img[:, digit_start-10:digit_end])\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"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
}