Handheld Multi-Frame Super-Resolution Eq. 1

An example from Handheld Multi-Frame Super-Resolution Eq. 1

The original equation:

placeholder

I❤️LA implementation:

`C(x,y)` = (∑_n ∑_i c_n,i ⋅ w_n,i ⋅ R̂_n) / (∑_n ∑_i w_n,i ⋅ R̂_n)

where

c ∈ ℝ^(f×s) : the value of the Bayer pixel
w ∈ ℝ^(f×s) : the local sample weight
R̂ ∈ ℝ^f     : the local robustness

I❤️LA compiled to C++/Eigen:

/*
`C(x,y)` = (∑_n ∑_i c_n,i ⋅ w_n,i ⋅ R̂_n) / (∑_n ∑_i w_n,i ⋅ R̂_n)

where

c ∈ ℝ^(f×s) : the value of the Bayer pixel
w ∈ ℝ^(f×s) : the local sample weight
R̂ ∈ ℝ^f     : the local robustness
*/
#include <Eigen/Core>
#include <Eigen/Dense>
#include <Eigen/Sparse>
#include <iostream>
#include <set>

struct multi_frame_1ResultType {
    double C_left_parenthesis_x_comma_y_right_parenthesis;
    multi_frame_1ResultType(const double & C_left_parenthesis_x_comma_y_right_parenthesis)
    : C_left_parenthesis_x_comma_y_right_parenthesis(C_left_parenthesis_x_comma_y_right_parenthesis)
    {}
};

/**
 * multi_frame_1
 *
 * @param c  the value of the Bayer pixel
 * @param w  the local sample weight
 * @param R̂  the local robustness
 * @return C_left_parenthesis_x_comma_y_right_parenthesis
 */
multi_frame_1ResultType multi_frame_1(
    const Eigen::MatrixXd & c,
    const Eigen::MatrixXd & w,
    const Eigen::VectorXd & R̂)
{
    const long f = c.rows();
    const long s = c.cols();
    assert( c.rows() == f );
    assert( w.rows() == f );
    assert( w.cols() == s );
    assert( R̂.size() == f );

    double sum_0 = 0;
    for(int n=1; n<=R̂.size(); n++){
        double sum_1 = 0;
        for(int i=1; i<=w.cols(); i++){
            sum_1 += c(n-1, i-1) * w(n-1, i-1) * R̂[n-1];
        }
        sum_0 += sum_1;
    }
    double sum_2 = 0;
    for(int n=1; n<=R̂.size(); n++){
        double sum_3 = 0;
        for(int i=1; i<=w.cols(); i++){
            sum_3 += w(n-1, i-1) * R̂[n-1];
        }
        sum_2 += sum_3;
    }
    double C_left_parenthesis_x_comma_y_right_parenthesis = (sum_0) / double((sum_2));

    return multi_frame_1ResultType(C_left_parenthesis_x_comma_y_right_parenthesis);
}


void generateRandomData(Eigen::MatrixXd & c,
    Eigen::MatrixXd & w,
    Eigen::VectorXd & R̂)
{
    const int f = rand()%10;
    const int s = rand()%10;
    c = Eigen::MatrixXd::Random(f, s);
    w = Eigen::MatrixXd::Random(f, s);
    R̂ = Eigen::VectorXd::Random(f);
}


int main(int argc, char *argv[])
{
    srand((int)time(NULL));
    Eigen::MatrixXd c;
    Eigen::MatrixXd w;
    Eigen::VectorXd R̂;
    generateRandomData(c, w, R̂);
    multi_frame_1ResultType func_value = multi_frame_1(c, w, R̂);
    std::cout<<"return value:\n"<<func_value.C_left_parenthesis_x_comma_y_right_parenthesis<<std::endl;
    return 0;
}

I❤️LA compiled to Python/NumPy/SciPy:

"""
`C(x,y)` = (∑_n ∑_i c_n,i ⋅ w_n,i ⋅ R̂_n) / (∑_n ∑_i w_n,i ⋅ R̂_n)

where

c ∈ ℝ^(f×s) : the value of the Bayer pixel
w ∈ ℝ^(f×s) : the local sample weight
R̂ ∈ ℝ^f     : the local robustness
"""
import numpy as np
import scipy
import scipy.linalg
from scipy import sparse
from scipy.integrate import quad
from scipy.optimize import minimize


class multi_frame_1ResultType:
    def __init__( self, C_left_parenthesis_x_comma_y_right_parenthesis):
        self.C_left_parenthesis_x_comma_y_right_parenthesis = C_left_parenthesis_x_comma_y_right_parenthesis


def multi_frame_1(c, w, R̂):
    """
    :param :c : the value of the Bayer pixel
    :param :w : the local sample weight
    :param :R̂ : the local robustness
    """
    c = np.asarray(c, dtype=np.float64)
    w = np.asarray(w, dtype=np.float64)
    R̂ = np.asarray(R̂, dtype=np.float64)

    f = c.shape[0]
    s = c.shape[1]
    assert c.shape == (f, s)
    assert w.shape == (f, s)
    assert R̂.shape == (f,)

    sum_0 = 0
    for n in range(1, len(R̂)+1):
        sum_1 = 0
        for i in range(1, w.shape[1]+1):
            sum_1 += c[n-1, i-1] * w[n-1, i-1] * R̂[n-1]
        sum_0 += sum_1
    sum_2 = 0
    for n in range(1, len(R̂)+1):
        sum_3 = 0
        for i in range(1, w.shape[1]+1):
            sum_3 += w[n-1, i-1] * R̂[n-1]
        sum_2 += sum_3
    C_left_parenthesis_x_comma_y_right_parenthesis = (sum_0) / (sum_2)
    return multi_frame_1ResultType(C_left_parenthesis_x_comma_y_right_parenthesis)


def generateRandomData():
    f = np.random.randint(10)
    s = np.random.randint(10)
    c = np.random.randn(f, s)
    w = np.random.randn(f, s)
    R̂ = np.random.randn(f)
    return c, w, R̂


if __name__ == '__main__':
    c, w, R̂ = generateRandomData()
    print("c:", c)
    print("w:", w)
    print("R̂:", R̂)
    func_value = multi_frame_1(c, w, R̂)
    print("return value: ", func_value.C_left_parenthesis_x_comma_y_right_parenthesis)

I❤️LA compiled to MATLAB:

function output = multi_frame_1(c, w, R_hat)
% output = multi_frame_1(c, w, R̂)
%
%    `C(x,y)` = (∑_n ∑_i c_n,i ⋅ w_n,i ⋅ R̂_n) / (∑_n ∑_i w_n,i ⋅ R̂_n)
%    
%    where
%    
%    c ∈ ℝ^(f×s) : the value of the Bayer pixel
%    w ∈ ℝ^(f×s) : the local sample weight
%    R̂ ∈ ℝ^f     : the local robustness
    if nargin==0
        warning('generating random input data');
        [c, w, R_hat] = generateRandomData();
    end
    function [c, w, R_hat] = generateRandomData()
        f = randi(10);
        s = randi(10);
        c = randn(f, s);
        w = randn(f, s);
        R_hat = randn(f,1);
    end

    R_hat = reshape(R_hat,[],1);

    f = size(c, 1);
    s = size(c, 2);
    assert( isequal(size(c), [f, s]) );
    assert( isequal(size(w), [f, s]) );
    assert( numel(R_hat) == f );

    sum_0 = 0;
    for n = 1:size(R_hat,1)
        sum_1 = 0;
        for i = 1:size(w,2)
            sum_1 = sum_1 + c(n, i) * w(n, i) * R_hat(n);
        end
        sum_0 = sum_0 + sum_1;
    end
    sum_2 = 0;
    for n = 1:size(R_hat,1)
        sum_3 = 0;
        for i = 1:size(w,2)
            sum_3 = sum_3 + w(n, i) * R_hat(n);
        end
        sum_2 = sum_2 + sum_3;
    end
    C_x_y = (sum_0) / (sum_2);
    output.C_x_y = C_x_y;
end

I❤️LA compiled to LaTeX:

\documentclass[12pt]{article}
\usepackage{mathdots}
\usepackage[bb=boondox]{mathalfa}
\usepackage{mathtools}
\usepackage{amssymb}
\usepackage{libertine}
\DeclareMathOperator*{\argmax}{arg\,max}
\DeclareMathOperator*{\argmin}{arg\,min}
\usepackage[paperheight=8in,paperwidth=4in,margin=.3in,heightrounded]{geometry}
\let\originalleft\left
\let\originalright\right
\renewcommand{\left}{\mathopen{}\mathclose\bgroup\originalleft}
\renewcommand{\right}{\aftergroup\egroup\originalright}
\begin{document}

\begin{center}
\resizebox{\textwidth}{!} 
{
\begin{minipage}[c]{\textwidth}
\begin{align*}
\textit{C(x,y)} & = \frac{\sum_\mathit{n} \sum_\mathit{i} \mathit{c}_{\mathit{n}, \mathit{i}} \cdot \mathit{w}_{\mathit{n}, \mathit{i}} \cdot \textit{R̂}_{ \mathit{n} }}{\sum_\mathit{n} \sum_\mathit{i} \mathit{w}_{\mathit{n}, \mathit{i}} \cdot \textit{R̂}_{ \mathit{n} }} \\
\intertext{where} 
\mathit{c} & \in \mathbb{R}^{ \mathit{f} \times \mathit{s} } \text{ the value of the Bayer pixel} \\
\mathit{w} & \in \mathbb{R}^{ \mathit{f} \times \mathit{s} } \text{ the local sample weight} \\
\textit{R̂} & \in \mathbb{R}^{ \mathit{f}} \text{ the local robustness} \\
\\
\end{align*}
\end{minipage}
}
\end{center}

\end{document}

I❤️LA LaTeX output: