<commit_before>import numpy as np
from scipy.ndimage.filters import maximum_filter, minimum_filter

from ..transform import integral_image
from ..feature.corner import _compute_auto_correlation
from ..util import img_as_float

from .censure_cy import _censure_dob_loop, _slanted_integral_image, _censure_octagon_loop


def _get_filtered_image(image, n_scales, mode):
    # TODO : Implement the STAR mode
    if mode == 'DoB':
        scales = np.zeros((image.shape[0], image.shape[1], n_scales))
        for i in range(n_scales):
            n = i + 1
            # Constant multipliers for the outer region and the inner region
            # of the bilevel filters with the constraint of keeping the DC bias
            # 0.
            inner_weight = (1.0 / (2 * n + 1)**2)
            outer_weight = (1.0 / (12 * n**2 + 4 * n))
            integral_img = integral_image(image)
            filtered_image = np.zeros(image.shape)
            _censure_dob_loop(image, n, integral_img, filtered_image, inner_weight, outer_weight)
            scales[:, :, i] = filtered_image
        return scales

    elif mode == 'Octagon':
        # TODO : Decide the shapes of Octagon filters for scales > 7
        outer_shape = [(5, 2), (5, 3), (7, 3), (9, 4), (9, 7), (13, 7), (15, 10)]
        inner_shape = [(3, 0), (3, 1), (3, 2), (5, 2), (5, 3), (5, 4), (5, 5)]
        scales = np.zeros((image.shape[0], image.shape[1], n_scales))
        integral_img = integral_image(image)
        integral_img1 = _slanted_integral_image_modes(image, 1)
        integral_img2 = _slanted_integral_image_modes(image, 2)
        integral_img3 = _slanted_integral_image_modes(image, 3)
        integral_img4 = _slanted_integral_image_modes(image, 4)

        for k in range(n_scales):
            n = k + 1
            filtered_image = np.zeros(image.shape)
            mo = outer_shape[n - 1][0]
            no = outer_shape[n - 1][1]
            mi = inner_shape[n - 1][0]
            ni = inner_shape[n - 1][1]
            outer_pixels = (mo + 2 * no)**2 - 2 * no * (no + 1)
            inner_pixels = (mi + 2 * ni)**2 - 2 * ni * (ni + 1)
            outer_weight = 1.0 / (outer_pixels - inner_pixels)
            inner_weight = 1.0 / inner_pixels

            _censure_octagon_loop(image, integral_img, integral_img1, integral_img2, integral_img3, integral_img4, filtered_image, outer_weight, inner_weight, mo, no, mi, ni)

            scales[:, :, k] = filtered_image
        return scales


def _slanted_integral_image_modes(img, mode=1):
    if mode == 1:
        """
        The following figures describe area that is summed up to calculate
        the value at point @ in slanted integral image. The subtended at @ is
        135 degrees.
<commit_msg>Using convolve in _get_filtered_image for mode=Octagon<commit_after>import numpy as np
from scipy.ndimage.filters import maximum_filter, minimum_filter, convolve

from ..transform import integral_image
from ..feature.corner import _compute_auto_correlation
from ..util import img_as_float
from ..morphology import convex_hull_image

from .censure_cy import _censure_dob_loop, _slanted_integral_image, _censure_octagon_loop


def _get_filtered_image(image, n_scales, mode):
    # TODO : Implement the STAR mode
    scales = np.zeros((image.shape[0], image.shape[1], n_scales), dtype=np.double)
    if mode == 'DoB':
        for i in range(n_scales):
            n = i + 1
            # Constant multipliers for the outer region and the inner region
            # of the bilevel filters with the constraint of keeping the DC bias
            # 0.
            inner_weight = (1.0 / (2 * n + 1)**2)
            outer_weight = (1.0 / (12 * n**2 + 4 * n))
            integral_img = integral_image(image)
            filtered_image = np.zeros(image.shape)
            _censure_dob_loop(image, n, integral_img, filtered_image, inner_weight, outer_weight)
            scales[:, :, i] = filtered_image


    elif mode == 'Octagon':
        # TODO : Decide the shapes of Octagon filters for scales > 7
        outer_shape = [(5, 2), (5, 3), (7, 3), (9, 4), (9, 7), (13, 7), (15, 10)]
        inner_shape = [(3, 0), (3, 1), (3, 2), (5, 2), (5, 3), (5, 4), (5, 5)]
        for i in range(n_scales):
            scales[:, :, i] = convolve(image, _octagon_filter(outer_shape[i][0], outer_shape[i][1], inner_shape[i][0], inner_shape[i][1]))
        """
        integral_img = integral_image(image)
        integral_img1 = _slanted_integral_image_modes(image, 1)
        integral_img2 = _slanted_integral_image_modes(image, 2)
        integral_img3 = _slanted_integral_image_modes(image, 3)
        integral_img4 = _slanted_integral_image_modes(image, 4)

        for k in range(n_scales):
            n = k + 1
            filtered_image = np.zeros(image.shape)
            mo = outer_shape[n - 1][0]
            no = outer_shape[n - 1][1]
            mi = inner_shape[n - 1][0]
            ni = inner_shape[n - 1][1]
            outer_pixels = (mo + 2 * no)**2 - 2 * no * (no + 1)
            inner_pixels = (mi + 2 * ni)**2 - 2 * ni * (ni + 1)
            outer_weight = 1.0 / (outer_pixels - inner_pixels)
            inner_weight = 1.0 / inner_pixels

            _censure_octagon_loop(image, integral_img, integral_img1, integral_img2, integral_img3, integral_img4, filtered_image, outer_weight, inner_weight, mo, no, mi, ni)

            scales[:, :, k] = filtered_image
            """
    return scales


def _oct(m, n):
    f = np.zeros((m + 2*n, m + 2*n))
    f[0, n] = 1
    f[n, 0] = 1
    f[0, m + n -1] = 1
    f[m + n - 1, 0] = 1
    f[-1, n] = 1 
    f[n, -1] = 1
    f[-1, m + n - 1] = 1
    f[m + n - 1, -1] = 1
    return convex_hull_image(f).astype(int)


def _octagon_filter(mo, no, mi, ni):
    outer = (mo + 2 * no)**2 - 2 * no * (no + 1)
    inner = (mi + 2 * ni)**2 - 2 * ni * (ni + 1)
    outer_wt = 1.0 / (outer - inner)
    inner_wt = 1.0 / inner
    c = ((mo + 2 * no) - (mi + 2 * ni)) / 2
    outer_oct = _oct(mo, no)
    inner_oct = np.zeros((mo + 2 * no, mo + 2 * no))
    inner_oct[c:-c, c:-c] = _oct(mi, ni)
    bfilter = outer_wt * outer_oct - (outer_wt + inner_wt) * inner_oct
    return bfilter


def _filter_using_convolve(image, n, mode='DoB'):

    if mode == 'DoB':
        inner_wt = (1.0 / (2*n + 1)**2)
        outer_wt = (1.0 / (12*n**2 + 4*n))
        dob_filter = np.zeros((4 * n + 1, 4 * n + 1))
        dob_filter[:] = outer_wt
        dob_filter[n : 3 * n + 1, n : 3 * n + 1] = - inner_wt
        return convolve(image, dob_filter)

    elif mode == 'Octagon':
        outer_shape = [(5, 2), (5, 3), (7, 3), (9, 4), (9, 7), (13, 7), (15, 10)]
        inner_shape = [(3, 0), (3, 1), (3, 2), (5, 2), (5, 3), (5, 4), (5, 5)]
        return convolve(image, _octagon_filter(outer_shape[n - 1][0], outer_shape[n - 1][1], inner_shape[n - 1][0], inner_shape[n - 1][1]))


def _slanted_integral_image_modes(img, mode=1):
    if mode == 1:
        """
        The following figures describe area that is summed up to calculate
        the value at point @ in slanted integral image. The subtended at @ is
        135 degrees.

{'fraction_non_alphanumeric': 0.13346501128668173, 'fraction_numerical': 0.03851580135440181, 'mean_word_length': 2.388623326959847, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 18, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4908224', 'n_tokens_mistral': 2674, 'n_tokens_neox': 2460, 'n_words': 701}