{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "using Formatting\n", "using StatsBase\n", "using FFTW\n", "using Plots" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Histogram normalized\n" ] }, { "data": { "text/plain": [ "5×5 Array{Int64,2}:\n", " 7 1 1 3 5\n", " 5 3 7 3 5\n", " 8 3 7 3 5\n", " 8 6 7 6 5\n", " 8 3 8 3 5" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "[0.08, 0.28, 0.16, 0.08, 0.08, 0.16, 0.04, 0.12]\n", "Cumsum Histogram normalized\n", "[0.08, 0.36, 0.52, 0.6, 0.68, 0.84, 0.88, 1.0]\n", "Prefered Histogram\n", "[0.125, 0.125, 0.125, 0.125, 0.125, 0.125, 0.125, 0.125]\n", "Cumsum Prefered Histogram\n", "[0.125, 0.25, 0.375, 0.5, 0.625, 0.75, 0.875, 1.0]\n", "out hist\n", "[0.125, 0.375, 0.625, 0.625, 0.75, 0.875, 1.0, 1.0]\n", "out color\n", "[0, 1, 2, 3, 4, 5, 6, 7]\n", "[1.0, 3.0, 5.0, 5.0, 6.0, 7.0, 8.0, 8.0]\n", "\n", "New Histogram\n", "[2, 0, 7, 0, 6, 2, 4, 4]\n", "New Histogram normalized\n", "[0.08, 0.0, 0.28, 0.0, 0.24, 0.08, 0.16, 0.16]\n", "New Cumsum Histogram normalized\n", "[0.08, 0.08, 0.36, 0.36, 0.6, 0.68, 0.84, 1.0]\n" ] } ], "source": [ " I_cau1 = [\n", " 5 0 0 1 2\n", " 2 1 5 1 2\n", " 7 1 5 1 2\n", " 7 4 5 4 3\n", " 7 1 6 1 3\n", " ]\n", "\n", " hist = fit(Histogram, reshape(I_cau1, (length(I_cau1))), nbins=8).weights\n", " normalized_hist = hist ./ sum(hist)\n", " cumsum_normalized_hist = cumsum(normalized_hist)\n", "\n", " println(\"Histogram normalized\")\n", " println(normalized_hist)\n", " println(\"Cumsum Histogram normalized\")\n", " println(cumsum_normalized_hist)\n", "\n", " prefered_hist = fit(Histogram, 0:7, nbins=8).weights\n", " normalized_prefered_hist = prefered_hist ./ sum(prefered_hist)\n", " cumsum_normalized_prefered_hist = cumsum(normalized_prefered_hist)\n", "\n", " println(\"Prefered Histogram\")\n", " println(normalized_prefered_hist)\n", " println(\"Cumsum Prefered Histogram\")\n", " println(cumsum_normalized_prefered_hist)\n", "\n", " # https://dsp.stackexchange.com/questions/16166/histogram-matching-of-two-images-using-cdf\n", " out_hist = zeros(8)\n", " out_color = zeros(8)\n", "\n", " for i in 1:length(out_color)\n", " for k in 1:length(cumsum_normalized_prefered_hist)\n", " if cumsum_normalized_prefered_hist[k] - cumsum_normalized_hist[i] < 0\n", " continue\n", " end\n", " out_hist[i] = cumsum_normalized_prefered_hist[k]\n", " out_color[i] = k\n", " break\n", " end\n", " end\n", "\n", " println(\"out hist\")\n", " println(out_hist)\n", " println(\"out color\")\n", " println([i for i=0:7])\n", " println(out_color)\n", "\n", " for idx in CartesianIndices(I_cau1)\n", " newcolor = out_color[I_cau1[idx] + 1]\n", " I_cau1[idx] = newcolor\n", " end\n", "\n", " display(I_cau1)\n", " println()\n", " hist = fit(Histogram, reshape(I_cau1, (length(I_cau1))), nbins=8).weights\n", " normalized_hist = hist ./ sum(hist)\n", " cumsum_normalized_hist = cumsum(normalized_hist)\n", "\n", " println(\"New Histogram\")\n", " println(hist)\n", " println(\"New Histogram normalized\")\n", " println(normalized_hist)\n", " println(\"New Cumsum Histogram normalized\")\n", " println(cumsum_normalized_hist)\n" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "0\n", "\n", "\n", "2\n", "\n", "\n", "4\n", "\n", "\n", "6\n", "\n", "\n", "0\n", "\n", "\n", "2\n", "\n", "\n", "4\n", "\n", "\n", "6\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "y1\n", "\n", "\n" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# h = fit(Histogram, reshape(I_cau1, (length(I_cau1))), nbins=8)\n", "bar(0:length(hist)-1, hist)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "0\n", "\n", "\n", "2\n", "\n", "\n", "4\n", "\n", "\n", "6\n", "\n", "\n", "0.00\n", "\n", "\n", "0.25\n", "\n", "\n", "0.50\n", "\n", "\n", "0.75\n", "\n", "\n", "1.00\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "y1\n", "\n", "\n" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bar(0:length(prefered_hist)-1, prefered_hist)" ] } ], "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 }