<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def _calc_block_mean_variance(image, mask, blocksize):
 """Adaptively determines image background. Args: image: image converted 1-channel image. mask: 1-channel mask, same size as image. blocksize: adaptive algorithm parameter. Returns: image of same size as input with foreground inpainted with background. """

    I = image.copy()
    I_f = I.astype(np.float32) / 255.  # Used for mean and std.

    result = np.zeros(
        (image.shape[0] / blocksize, image.shape[1] / blocksize),
        dtype=np.float32)

    for i in xrange(0, image.shape[0] - blocksize, blocksize):
        for j in xrange(0, image.shape[1] - blocksize, blocksize):

            patch = I_f[i:i+blocksize+1, j:j+blocksize+1]
            mask_patch = mask[i:i+blocksize+1, j:j+blocksize+1]

            tmp1 = np.zeros((blocksize, blocksize))
            tmp2 = np.zeros((blocksize, blocksize))
            mean, std_dev = cv2.meanStdDev(patch, tmp1, tmp2, mask_patch)

            value = 0
            if std_dev[0][0] > MEAN_VARIANCE_THRESHOLD:
                value = mean[0][0]

            result[i/blocksize, j/blocksize] = value

    small_image = cv2.resize(I, (image.shape[1] / blocksize,
                                 image.shape[0] / blocksize))

    res, inpaintmask = cv2.threshold(result, 0.02, 1, cv2.THRESH_BINARY)

    inpainted = cv2.inpaint(small_image, inpaintmask.astype(np.uint8), 5,
                            cv2.INPAINT_TELEA)

    res = cv2.resize(inpainted, (image.shape[1], image.shape[0]))

    return res