\section{Data Size Decision Framework}
\label{sec:design}

This section presents the proposed method to outcome a data array than have less size compare to jpeg image when we can tolerate some error of data. 


\subsection{Heuristic Data Resolution Determination}

For each frame, we can use a context-free language to represent it.
\begin{center}
\begin{tabular}{rl}
    $S \rightarrow\ $ & $R$ \\
    $R \rightarrow\ $ & $\alpha$\\
    $R \rightarrow\ $ & $\beta RRRR$
\end{tabular}
\end{center}

$R$ means a region of image, and it can either use the average $\alpha$ of the pixels in the region to represent whole region or separate into four regions and left a remainder $\beta$. Dependence on the image size we desired, we can choose the amount of separating regions.  
The context-free grammar start from a region contain whole image. For each $R$ we calculate a heuristic value $h$ which is based on the quality of data we can improve by separate it in to smaller regions. After some operation, we can encode the image into a string $\omega$. One of the possible outcome is 
$\beta_1\beta_2\alpha_1\alpha_2\alpha_3\alpha_4\alpha_5\alpha_6\beta_3
\alpha_7\alpha_8\alpha_9\beta_4\alpha_{10}\alpha_{11}\alpha_{12}\alpha_{13}$. 
Figure~\ref{fig:ContextFreeString} shows how the image is separated into several regions. By this method, we can continuously separate regions until the file size excess our requirement or the error rate less than a threshold. 
%Each time we separate a region, the size of file will increased 4 bytes, and start from 1 byte. the final size 

\begin{figure}[htbp]
	\centering
 	\includegraphics[width=\columnwidth]{figures/ContextFreeString.pdf}
	\caption{Region separate by CFG}
	\label{fig:ContextFreeString}
\end{figure} 

The heuristic function in the proposed method is the sum of squared error of the pixels in the region. We have also try to use the total squared error it can reduce as the heuristic function, but it will easily get stuck at a local minimum. 

\subsection{Data Structure and Region Selection Algorithm}

We use the sum of squared error of pixels in the region when we use the average of them to replace them as the heuristic value. In order to reduce the heuristic value's calculating time, we design a four dimension segment tree to preprocess all possible regions. For each node, it store the range on both width and height it covered, mean $E[X]$, and squared mean $E[X^2]$ of pixels in the region. By the property of segment tree, tree root start from $0$, and each node $X_i$ has four child $X_{i\times 4+1}$, $X_{i\times 4+2}$, $X_{i\times 4+3}$ and $X_{i\times 4+4}$. Hence, we only need to allocate an large array and recursively process all nodes form root. Algorithm~\ref{code:SegmentTreePreprocess} shows how we generate the tree.

\begin{algorithm*}[h]
	\caption{Segment Tree Preprocess}
	\label{code:SegmentTreePreprocess}
	\begin{algorithmic}[1]
		\State $Tree = Array()$
		\Function{setTreeNode}{$x, left, right, top, bottom$}
			\If {$left = right$ \and $top = bottom$}
				\State $Tree[x].Sum = Image[left][top]$
				\State $Tree[x].SquareSum = Image[left][top]^2$
			\Else
				\State $setTreeNode(4x+1, left, (left+right)/2, top, (top+bottom)/2)$
				\State $setTreeNode(4x+2, (left+right)/2, right, top, (top+bottom)/2)$
				\State $setTreeNode(4x+3, left, (left+right)/2, (top+bottom)/2, bottom)$
				\State $setTreeNode(4x+4, (left+right)/2, right, (top+bottom)/2, bottom)$
				\State $Tree[x].Sum = \sum\limits_{i = 4x+1}^{4x+4} Tree[i].sum $
				\State $Tree[x].SquareSum = \sum\limits_{i = 4x+1}^{4x+4} Tree[i].SquareSum$
			\EndIf
			\State $Tree[x].SquaredError = Tree[x].SquareSum - \frac{Tree[x].Sum^2}{(right-left+1)\times(bottom-top+1)}$ 
		\EndFunction
		\State $setTreeNode(0, 0, Image.Width, 0, Image.Height)$
	\end{algorithmic}
\end{algorithm*}

For region selection, we use a priority queue to retrieve the region of considerate regions with highest value. The priority queue start with only root of the segment tree. For each round the priority queue pop the item with highest value and push all its child in to the queue. Algorithm~\ref{code:RegionSelection} shows how we select a region by the priority queue. After the selection finished, we will generate the data string to be sent. The regions in $seperatedRegions$ will be $\beta$ and others in $PriorityQueue$ will be the average value, and then compress the string by Huffman Coding.

\begin{algorithm*}[h]
	\caption{Region Selection}
	\label{code:RegionSelection}
	\begin{algorithmic}[1]
		\State $seperatedRegions = Array()$
		\State $PriorityQueue = Heap()$
		\State $PriorityQueue.Push(Tree[0].SquaredError, 0)$
		\For{\State $i = 0..SeperateRounds$}
			\State $value, x = PriorityQueue.Pop()$
			\State $seperatedRegions.push(x)$
			\State $PriorityQueue.Push(Tree[4x+1].SquaredError, 4x+1)$
			\State $PriorityQueue.Push(Tree[4x+2].SquaredError, 4x+2)$
			\State $PriorityQueue.Push(Tree[4x+3].SquaredError, 4x+3)$
			\State $PriorityQueue.Push(Tree[4x+4].SquaredError, 4x+4)$
		\EndFor
	\end{algorithmic}
\end{algorithm*}

