コード例 #1
0
ファイル: integralshift.py プロジェクト: odebeir/ivctrack
    def __init__(self,array):
        self.m,self.n = array.shape
        COL,ROW = np.meshgrid(range(0,self.n),range(0,self.m))

        self.mat = integral_image(array)
        self.r_mat = integral_image(ROW*array)
        self.c_mat = integral_image(COL*array)
コード例 #2
0
	def integral_image(self, img):
		feature = []
		gray  = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
		gray  = cv2.resize(gray, (170,170))
		#gray  = cv2.medianBlur(gray, 9)
		_,gray = cv2.threshold(gray,70,255,cv2.THRESH_BINARY_INV)
		h,w = gray.shape
		integral = integral_image(gray)


		# feature.append((float)(integral[84,169]))
		# feature.append((float)(integral[169,169] - integral[84,169]))
		feature.append((float)(integral[h-1,w/2]))
		feature.append((float)(integral[h-1,w-1] - integral[h-1,w/2]))

		feature.append((float)(integral[h/4,w-1]))
		feature.append((float)(integral[h-1,w-1] - integral[h*3/4, w*3/4]))
		# feature.append((float)(integral[h/2,w/2]))
		# feature.append((float)(integral[h/2, w-1] - integral[h/2,w/2]))
		# feature.append((float)(integral[h-1,w/2]  - integral[h/2,w/2]))
		# feature.append((float)(integral[h-1,w-1] + integral[h/2,w/2] - integral[h-1,w/2] - integral[h/2,w-1]))

		for i,f in enumerate(feature):
			feature[i] = (feature[i]/integral[h-1,w-1])

		# feature = np.array(integral, dtype='float').reshape(170*170)
		# feature = feature/feature[170*170-1]
		feature = np.array(feature)
		print feature
		return feature
コード例 #3
0
ファイル: censure.py プロジェクト: borevitzlab/scikit-image
def _filter_image(image, min_scale, max_scale, mode):

    response = np.zeros((image.shape[0], image.shape[1],
                         max_scale - min_scale + 1), dtype=np.double)

    if mode == 'dob':

        # make response[:, :, i] contiguous memory block
        item_size = response.itemsize
        response.strides = (item_size * response.shape[1], item_size,
                            item_size * response.shape[0] * response.shape[1])

        integral_img = integral_image(image)

        for i in range(max_scale - min_scale + 1):
            n = min_scale + i

            # Constant multipliers for the outer region and the inner region
            # of the bi-level 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))

            _censure_dob_loop(n, integral_img, response[:, :, i],
                              inner_weight, outer_weight)

    # NOTE : For the Octagon shaped filter, we implemented and evaluated the
    # slanted integral image based image filtering but the performance was
    # more or less equal to image filtering using
    # scipy.ndimage.filters.convolve(). Hence we have decided to use the
    # later for a much cleaner implementation.
    elif mode == 'octagon':
        # TODO : Decide the shapes of Octagon filters for scales > 7

        for i in range(max_scale - min_scale + 1):
            mo, no = OCTAGON_OUTER_SHAPE[min_scale + i - 1]
            mi, ni = OCTAGON_INNER_SHAPE[min_scale + i - 1]
            response[:, :, i] = convolve(image,
                                         _octagon_kernel(mo, no, mi, ni))

    elif mode == 'star':

        for i in range(max_scale - min_scale + 1):
            m = STAR_SHAPE[STAR_FILTER_SHAPE[min_scale + i - 1][0]]
            n = STAR_SHAPE[STAR_FILTER_SHAPE[min_scale + i - 1][1]]
            response[:, :, i] = convolve(image, _star_kernel(m, n))

    return response
コード例 #4
0
ファイル: misc.py プロジェクト: gitter-badger/WSItk
def intg_image(image):
    """
    Computes tha integral image following the convention that the
    first row and column should be 0s.

    :param image: numpy.array
     A 2D array (single channel image).

    :return:
     A 2D array, with shape (image.shape[0]+1, image.shape[1]+1).
    """

    if image.ndim != 2:
        raise ValueError('The image must be single channel.')

    image = np.pad(integral_image(image), ((1,0),(1,0)), mode=nstr(b'constant'), constant_values=0)

    return image
# end intg_image
コード例 #5
0
ファイル: brief.py プロジェクト: zehnsechs/python
    def describe(self,features,img,use_orient):
        patch_rad = self.patch_size/2
        nr_kp = len(features)
        (heigth,width) = img.shape
        int_img = tf.integral_image(img)
        #test = cv2.integral(img)
        like_cv_img = np.concatenate((np.array([[0]*(heigth+1)]).T,np.concatenate(([[0]*width],int_img),axis = 0)),axis = 1)
        i = 0
        #print (like_cv_img == test).all()
        like_cv_img = np.int32(like_cv_img)
        #print like_cv_img
        #print test
        print heigth,width
        print nr_kp
        half_ker = self.kernel/2
        print half_ker


        def sum(y,x,(s_an,c_an)):
            if use_orient:
                rx = int(float(x)*c_an - float(y)*s_an)
                ry = int(float(x)*s_an + float(y)*c_an)

                if (rx > 24): 
                    rx = 24 
                if (rx < -24):
                    rx = -24
                if (ry > 24):
                    ry = 24
                if (ry < -24):
                    ry = -24;
                x = rx
                y = ry

            img_x = int(fx+0.5 + x)
            img_y = int(fy+0.5 + y)
            #print fy,fx,img_y,img_x
            #print tf.integrate(int_img,img_y + half_ker , img_x + half_ker , img_y - half_ker +1, img_x - half_ker +1) 
            #print test.dtype ,test[img_y + half_ker + 1, img_x + half_ker + 1] ,  int(test[img_y + half_ker + 1, img_x - half_ker ]) , int(test[img_y - half_ker , img_x + half_ker + 1]) ,  int(test[img_y - half_ker , img_x - half_ker ])
            #print int_img.dtype ,int_img[img_y + half_ker + 1, img_x + half_ker + 1] ,  int(int_img[img_y + half_ker + 1, img_x - half_ker ]) , int(int_img[img_y - half_ker , img_x + half_ker + 1]) ,  int(int_img[img_y - half_ker , img_x - half_ker ])
            #print int(test[img_y + half_ker + 1, img_x + half_ker + 1]) -  int(test[img_y + half_ker + 1, img_x - half_ker ]) - int(test[img_y - half_ker , img_x + half_ker + 1]) +  int(test[img_y - half_ker , img_x - half_ker ])
            #print '-----------------------'
            return int(like_cv_img[img_y + half_ker + 1, img_x + half_ker + 1]) -  int(like_cv_img[img_y + half_ker + 1, img_x - half_ker ]) - int(like_cv_img[img_y - half_ker , img_x + half_ker + 1]) +  int(like_cv_img[img_y - half_ker , img_x - half_ker ])
コード例 #6
0
    def test_single_mblbp(self):

        # Create dummy matrix where first and fifth rectangles have greater
        # value than the central one. Therefore, the following bits
        # should be 1.
        test_img = np.zeros((9, 9), dtype='uint8')
        test_img[3:6, 3:6] = 1
        test_img[:3, :3] = 255
        test_img[6:, 6:] = 255

        # MB-LBP is filled in reverse order. So the first and fifth bits from
        # the end should be filled.
        correct_answer = 0b10001000

        int_img = integral_image(test_img)

        lbp_code = multiblock_lbp(int_img, 0, 0, 3, 3)

        np.testing.assert_equal(lbp_code, correct_answer)
コード例 #7
0
def intg_image(image):
    """
    Computes tha integral image following the convention that the
    first row and column should be 0s.

    :param image: numpy.array
     A 2D array (single channel image).

    :return:
     A 2D array, with shape (image.shape[0]+1, image.shape[1]+1).
    """

    if image.ndim != 2:
        raise ValueError('The image must be single channel.')

    image = np.pad(integral_image(image), ((1, 0), (1, 0)),
                   mode=nstr(b'constant'),
                   constant_values=0)

    return image


# end intg_image
コード例 #8
0
ファイル: test_haar.py プロジェクト: tthomas88/scikit-image
def test_haar_like_feature_precomputed(feature_type):
    img = np.ones((5, 5), dtype=np.int8)
    img_ii = integral_image(img)
    if isinstance(feature_type, list):
        # shuffle the index of the feature to be sure that we are output
        # the features in the same order
        shuffle(feature_type)
        feat_coord, feat_type = zip(
            *
            [haar_like_feature_coord(5, 5, feat_t) for feat_t in feature_type])
        feat_coord = np.concatenate(feat_coord)
        feat_type = np.concatenate(feat_type)
    else:
        feat_coord, feat_type = haar_like_feature_coord(5, 5, feature_type)
    haar_feature_precomputed = haar_like_feature(img_ii,
                                                 0,
                                                 0,
                                                 5,
                                                 5,
                                                 feature_type=feat_type,
                                                 feature_coord=feat_coord)
    haar_feature = haar_like_feature(img_ii, 0, 0, 5, 5, feature_type)
    assert_array_equal(haar_feature_precomputed, haar_feature)
コード例 #9
0
def compareScikit():
    test_img = np.round(np.random.rand(36, 36) * 256)
    int_img = integral_image(test_img)
    matriks = genAverageMat(test_img, 2, 3)
    mat_lbp = lbpCompare(matriks)
    lbp_2 = multiblock_lbp(int_img, 0, 9, 3, 2)
コード例 #10
0
images = os.listdir("images")
import json
import numpy as np
from skimage import io
from skimage.color import rgb2gray
from skimage.transform import integral_image as integral_image
import matplotlib.pyplot as plt
from skimage.feature import hessian_matrix_det
from skimage.feature import hessian_matrix, peak_local_max

# In[129]:

for m in range(len(images)):
    image = io.imread("images/" + images[m])
    image = rgb2gray(image)
    image = integral_image(image)
    scales = np.linspace(1, 8, 20)
    hessians = []
    for s in scales:
        h = hessian_matrix_det(image, s)
        hessians.append(h)

    h_cuboid = np.dstack(hessians)
    blobs = peak_local_max(h_cuboid)
    d = [blobs[i] for i in range(1, len(blobs), 500)]
    a = image
    Dict = []
    for i in d:
        Dict.append((i[0].item(), i[1].item(), i[2].item()))
    D = {images[m]: Dict}
コード例 #11
0
ファイル: main.py プロジェクト: IlyaGutnikov/DIP_Labs
        dark = (i[x+h-1, y+w-1] + i[int(x+h/2-1), y-1]) - (i[int(x+h/2-1), y+w-1] + i[x+h-1, y-1])
    elif f == 1:
        bright = (i[x+h-1, int(y+w/2-1)] + i[x-1, y-1]) - (i[x-1, int(y+w/2-1)] + i[x+h-1, y-1])
        dark = (i[x+h-1, y+w-1] + i[x-1, int(y+w/2-1)]) - (i[x+h-1, int(y+w/2-1)] + i[x-1, y+w-1])
    #print(bright)
    #print(dark)
    haar_feature_val = bright-dark
    #print(haar_feature_val)
    return haar_feature_val

# Загрузка изображения
image = io.imread("images/002.jpg")
# Перевод в полутоновый формат
image_halftone = color.rgb2gray(image)
# Специальный формат изображения для нахождения признаков
im = integral_image(image_halftone)

# Матрицы для нахождения сохраненых признаков
matrix = np.ndarray(shape=(len(im), len(im[0])))
matrix_bool = np.ndarray(shape=(len(im), len(im[0])), dtype=bool)

# Получение признаков Хаара
for i in range(len(im) - 5):
    for j in range(len(im[i]) - 5):
        # получили матрицу значений Хаара
        matrix[i, j] = haar_feature(im, i, j, 0, 1)

# Фильтр для признаков Хаара
filter_number_min = 50
filter_number_max = 80
コード例 #12
0
def extract_feature_image(img, feature_type, feature_coord=None):
    """Extract the haar feature for the current image"""
    ii = integral_image(img)
    return haar_like_feature(ii, 0, 0, ii.shape[0], ii.shape[1],
                             feature_type=feature_type,
                             feature_coord=feature_coord)
コード例 #13
0
def test_validity():
    y = np.arange(12).reshape((4, 3))

    y = (np.random.random((50, 50)) * 255).astype(np.uint8)
    assert_equal(integral_image(y)[-1, -1],
                 y.sum())
コード例 #14
0
import numpy as np
import pytest
from numpy.testing import assert_allclose, assert_equal

from skimage.transform import integral_image, integrate


np.random.seed(0)
x = (np.random.rand(50, 50) * 255).astype(np.uint8)
s = integral_image(x)


@pytest.mark.parametrize(
    'dtype', [np.float16, np.float32, np.float64, np.uint8, np.int32]
)
@pytest.mark.parametrize('dtype_as_kwarg', [False, True])
def test_integral_image_validity(dtype, dtype_as_kwarg):
    rstate = np.random.RandomState(1234)
    dtype_kwarg = dtype if dtype_as_kwarg else None
    y = (rstate.rand(20, 20) * 255).astype(dtype)
    out = integral_image(y, dtype=dtype_kwarg)
    if y.dtype.kind == 'f':
        if dtype_as_kwarg:
            assert out.dtype == dtype
            rtol = 1e-3 if dtype == np.float16 else 1e-7
            assert_allclose(out[-1, -1], y.sum(dtype=np.float64), rtol=rtol)
        else:
            assert out.dtype == np.float64
            assert_allclose(out[-1, -1], y.sum(dtype=np.float64))
    else:
        assert out.dtype.kind == y.dtype.kind
コード例 #15
0
from skimage.data import lfw_subset
from skimage.transform import integral_image
from skimage.feature import haar_like_feature
from skimage.feature import haar_like_feature_coord
from skimage.feature import draw_haar_like_feature


    #### get the images from example dataset face recognition

images = lfw_subset()
### experimenting with the HAAR like features
feature_types = ['type-2-x', 'type-2-y']
face2rec = images[0]
plt.imshow(face2rec)
ii = integral_image(face2rec)
plt.imshow(ii)
feature_coord, _ = haar_like_feature_coord(3, 3)#, 'type-3-y')
haar_feat = draw_haar_like_feature(ii, 0, 0,
                               images.shape[2],
                               images.shape[1],
                               feature_coord,
                               alpha=0.1)

plt.imshow(face2rec)
plt.imshow(haar_feat)
plt.show()
?draw_haar_like_feature
images.shape
#?haar_like_feature_coord
#rgb2gray(img)
コード例 #16
0
def run(img):
    return to_base64(integral_image(img))
コード例 #17
0
def get_adaboost_haar(img,index_,f_type,f_coord):
    int_img = integral_image(np.asarray(img))
    return haar_like_feature(int_img, 0, 0, int_img.shape[0],int_img.shape[1],\
                      f_type[index_:index_+1],f_coord[index_:index_+1])
コード例 #18
0
def test_validity():
    y = np.arange(12).reshape((4, 3))

    y = (np.random.random((50, 50)) * 255).astype(np.uint8)
    assert_equal(integral_image(y)[-1, -1], y.sum())
コード例 #19
0
                                     y1,
                                     x1,
                                     lbp_code=code,
                                     alpha=0.5)
    plt.ion()
    plt.imshow(img_target)

    #plt.draw()

    #plt.show()
    plt.pause(0.05)


img = data.coins()

int_img = integral_image(img)

p_blocks = 3
h_size = 256
n_features = 59
slider = 3

int_img_target = int_img[20:80, 15:75]

region_print(img, 0, 20, 15, 60 / 3, 60 / 3)

features_target = histogram_feature(img, int_img_target, p_blocks, n_features,
                                    h_size, slider)

region = 60
threshold = 0
def extract_feature_image(img, feature_type, feature_coord=None):
    """Extract the haar feature for the current image"""
    ii = integral_image(img)
    return haar_like_feature(ii, 0, 0, ii.shape[0], ii.shape[1],
                             feature_type=feature_type,
                             feature_coord=feature_coord)
コード例 #21
0
 def set_ii_searchregion(self):
     Boosting.ii_search_region = integral_image(
         self.frame[int(self.search_region[1]):int(self.search_region[3]) +
                    1,
                    int(self.search_region[0]):int(self.search_region[2]) +
                    1])
コード例 #22
0
            for center_y, center_x, radius in zip(cy, cx, radii):
                circy, circx = circle_perimeter(center_y, center_x, radius)
                img[circy, circx] = 255
        elif transform[0].lower() == "median-blur":
            exitStr = "(applying median)"
            img = img_as_ubyte(median(img, disk(int(transform[1]))))
            #io.imshow(img)
            #io.show()
        elif transform[0].lower() == "log":
            img = img_as_ubyte(adjust_log(img, float(transform[1])))
            #io.imshow(img)
            #io.show()
        elif transform[0].lower() == "integral-blur":
            exitStr = "(applying integral_image)"
            img = img_as_ubyte(integral_image(img))
            #io.imshow(img)
            #io.show()
        elif transform[0].lower() == "canny-edge":
            exitStr = "(applying canny-edge)"
            img = img_as_ubyte(
                canny(img, float(transform[1]), float(transform[2]),
                      float(transform[3])))
            #io.imshow(img)
            #io.show()
        elif transform[0].lower() == "rank-order":
            exitStr = "(applying rank_order)"
            img, _ = rank_order(img)
            #io.imshow(img)
            #io.show()
            #elif transform[0].lower() == "resize":
コード例 #23
0
        roi = cv2.resize(roi, (20, 20), interpolation=cv2.INTER_NEAREST)
    except Exception as e:
        print(e)
        print(roi.shape)
        roi = np.zeros((20, 20))
    roi = integral_image(roi)
    f1 = np.sum(roi[10:, :] - roi[:10, :]) / (20 * 20)
    f2 = np.sum(roi[:, 10:] - roi[:, :10]) / (20 * 20)
    f3 = np.sum(roi[10:, 10:] - roi[:10, :10]) / (20 * 20)
    f4 = np.sum(roi[10:, :10] - roi[:10, 10:]) / (20 * 20)
    return [f1, f2, f3, f4]


if __name__ == "__main__":
    img = np.ones((20, 20), dtype=np.uint8)
    img_ii = integral_image(img)
    fh = basic_haar_features(img_ii)
    print(fh)
    print(len(fh))

from datastructure.image_handler import ImageHandler
from datastructure.matrix_handler import MatrixHandler


class Haar:
    def __init__(self):
        self.pool_count = multiprocessing.cpu_count()

    def _compute_for_roi(self, region_of_interest):
        height, width = region_of_interest.shape[:2]
        desc = haar_like_feature(region_of_interest,
コード例 #24
0
i = 0
while i < len(feat_coord):
    if (feat_coord[i][0][1][0] - feat_coord[i][0][0][0])**2 + (
            feat_coord[i][0][1][1] - feat_coord[i][0][0][1])**2 < radius:
        feat_coord = np.delete(feat_coord, i)
        feat_type = np.delete(feat_type, i)
    else:
        i += 1
# one over 4
feat_coord = feat_coord[::4]
feat_type = feat_type[::4]
print('features', feat_coord.shape)

first = True
for image in x_train:
    int_image = transform.integral_image(image)
    features = feature.haar_like_feature(int_image,
                                         0,
                                         0,
                                         28,
                                         28,
                                         feature_type=feat_type,
                                         feature_coord=feat_coord)
    if first:
        ftrain = [features]
    else:
        ftrain = np.append(ftrain, [features], axis=0)
    first = False

# TRAINING
コード例 #25
0
def get_rotated_integral_image(im, deg):
    # TODO this might break at the edges as the true image is rotated off
    return integral_image(imrotate(im, deg, 'nearest'))
コード例 #26
0
 def process(self, im):
     return integral_image(im)
コード例 #27
0
def haar_feature(img,feature_type_):
    int_img = integral_image(img)
    return haar_like_feature(int_img, 0, 0, int_img.shape[0],int_img.shape[1],feature_type_)
コード例 #28
0
import numpy as np
from numpy.testing import assert_equal
from skimage.transform import integral_image

# Create test matrix where first and fifth rectangles starting
# from top left clockwise have greater value than the central one.
test_img = np.zeros((9, 9), dtype='uint8')
test_img[3:6, 3:6] = 1
test_img[:3, :3] = 50
test_img[6:, 6:] = 50

# First and fifth bits should be filled. This correct value will
#  be compared to the computed one.
correct_answer = 0b10001000

int_img = integral_image(test_img)

lbp_code = multiblock_lbp(int_img, 0, 0, 3, 3)

assert_equal(correct_answer, lbp_code)

######################################################################
# Now let's apply the operator to a real image and see how the visualization
# works.

from skimage import data
from matplotlib import pyplot as plt
from skimage.feature import draw_multiblock_lbp

test_img = data.coins()
コード例 #29
0
    elif sourcer_params['color_model'] == "hls":
      img = cv2.cvtColor(img, cv2.COLOR_RGB2HLS)
    elif sourcer_params['color_model'] == "yuv":
      img = cv2.cvtColor(img, cv2.COLOR_RGB2YUV)
    elif sourcer_params['color_model'] == "ycrcb":
      img = cv2.cvtColor(img, cv2.COLOR_RGB2YCR_CB)
    elif sourcer_params['color_model'] == "grey":
      img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    else: 
      raise Exception('ERROR:', 'No se puede cambiar de color')
    
    '''hogA_img = img[:, :, 0]
    hogB_img = img[:, :, 1]
    hogC_img = img[:, :, 2]'''
    
    return img#, hogA_img, hogB_img, hogC_img


 

svm = joblib.load('D:\Documents\OPENCV\MODELS\HAAR_SVM_MODEL_0.9501557632398754.pkl')

img = cv2.imread('D:\\Documents\\OPENCV\\TRAINING\\6.jpg')
imgGREY = change_color(img, sourcer_params)
imgX = integral_image(imgGREY)        
features = haar_like_feature(imgX, 0, 0, 40, 40, feature_types)

nbr = svm.predict(np.array([features]))
print(nbr[0])
end = timer()
print("{0:.3f}".format(end - start)+' seconds') # Time in seconds
コード例 #30
0
    def setup(self):

        upstream = self.get_upstream_provider()
        self.upstream_spec = upstream.spec

        if self.mask and self.min_masked > 0:

            assert self.mask in self.upstream_spec, (
                "Upstream provider does not have %s"%self.mask)
            self.mask_spec = self.upstream_spec.array_specs[self.mask]

            logger.info("requesting complete mask...")

            mask_request = BatchRequest({self.mask: self.mask_spec})
            mask_batch = upstream.request_batch(mask_request)

            logger.info("allocating mask integral array...")

            mask_data = mask_batch.arrays[self.mask].data
            mask_integral_dtype = np.uint64
            logger.debug("mask size is %s", mask_data.size)
            if mask_data.size < 2**32:
                mask_integral_dtype = np.uint32
            if mask_data.size < 2**16:
                mask_integral_dtype = np.uint16
            logger.debug("chose %s as integral array dtype", mask_integral_dtype)

            self.mask_integral = np.array(mask_data > 0, dtype=mask_integral_dtype)
            self.mask_integral = integral_image(self.mask_integral).astype(mask_integral_dtype)

        if self.ensure_nonempty:

            assert self.ensure_nonempty in self.upstream_spec, (
                "Upstream provider does not have %s"%self.ensure_nonempty)
            graph_spec = self.upstream_spec.graph_specs[self.ensure_nonempty]


            logger.info("requesting all %s points...", self.ensure_nonempty)

            nonempty_request = BatchRequest({self.ensure_nonempty: graph_spec})
            nonempty_batch = upstream.request_batch(nonempty_request)

            self.points = cKDTree(
                [p.location for p in nonempty_batch[self.ensure_nonempty].nodes]
            )

            point_counts = self.points.query_ball_point(
                [p.location for p in nonempty_batch[self.ensure_nonempty].nodes],
                r=self.point_balance_radius,
            )
            weights = [1 / len(point_count) for point_count in point_counts]
            self.cumulative_weights = list(itertools.accumulate(weights))

            logger.debug("retrieved %d points", len(self.points.data))

        # clear bounding boxes of all provided arrays and points --
        # RandomLocation does not have limits (offsets are ignored)
        for key, spec in self.spec.items():
            if spec.roi is not None:
                spec.roi.set_shape(None)
                self.updates(key, spec)
コード例 #31
0
    ccount = len(contours)
    if show_plot:
        plt.title("T.R. Singh et. al LATT $(w=%d, k=%g)$ contours=%d" % (wsize, k, ccount))
        plt.imshow(mask, cmap='gray')
        plt.show()
    return mask, contours

if otsu_tweak is not None:
    mask = mask_otsu(img, otsu_tweak, process_how)
    threshold_process_how = "modified Otsu"
elif block_size is not None:
    #timg = np.arange(100, dtype=np.float64).reshape(10,10)
    timg = np.arange(100, dtype=np.int32).reshape(10,10)
    print "timg convolved with 3x3 unit matrix"
    print ndimage.convolve(timg, np.array([[1,1,1],[1,1,1],[1,1,1]]), mode='constant', cval=0)
    tiimg = integral_image(timg)
    print "integral image of 10x10 0..99"
    print tiimg

    d = 1
    pad = 2
    print "lcl sum loop d=%d" % d
    print local_sum_loop(np.pad(tiimg, pad, mode='constant'), d, pad)

    d = 2
    pad = 2
    print "lcl sum loop d=%d" % d
    print local_sum_loop(np.pad(tiimg, pad, mode='constant'), d, pad)

    print "timg convolved with 5x5 unit matrix"
    print ndimage.convolve(timg, np.array([[1]*5]*5), mode='constant', cval=0)
コード例 #32
0
                             ii.shape[1],
                             feature_type=feature_type,
                             feature_coord=feature_coord)


TOTAL = path + path2
print(path)
img = cv2.imread(path)
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
feat_coord, feat_type = haar_like_feature_coord(2, 2, feature_types)

features = draw_haar_like_feature(img, 2, 3, 39, 39, feat_coord)

#X = delayed(extract_feature_image(img, feature_types)
#for imgs in img)
#print(X)

#x= extract_feature_image(img,'type-4', feature_coord=None)

img2 = integral_image(img)
feature = haar_like_feature(img, 0, 0, 7, 7, feature_types)
print(len(feature))
print(feature)

#img = cv2.imread('D:\Documents\OPENCV\DB_PLATES\carro (1).jpg')

cv2.namedWindow('Digito', cv2.WINDOW_NORMAL)
cv2.imshow('Digito', features)
cv2.waitKey()
cv2.destroyAllWindows()
コード例 #33
0
import numpy as np
from numpy.testing import *

from skimage.transform import integral_image, integrate

x = (np.random.random((50, 50)) * 255).astype(np.uint8)
s = integral_image(x)


def test_validity():
    y = np.arange(12).reshape((4, 3))

    y = (np.random.random((50, 50)) * 255).astype(np.uint8)
    assert_equal(integral_image(y)[-1, -1],
                 y.sum())


def test_basic():
    assert_equal(x[12:24, 10:20].sum(), integrate(s, 12, 10, 23, 19))
    assert_equal(x[:20, :20].sum(), integrate(s, 0, 0, 19, 19))
    assert_equal(x[:20, 10:20].sum(), integrate(s, 0, 10, 19, 19))
    assert_equal(x[10:20, :20].sum(), integrate(s, 10, 0, 19, 19))


def test_single():
    assert_equal(x[0, 0], integrate(s, 0, 0, 0, 0))
    assert_equal(x[10, 10], integrate(s, 10, 10, 10, 10))

def test_vectorized_integrate():
    r0 = np.array([12, 0, 0, 10, 0, 10, 30])
    c0 = np.array([10, 0, 10, 0, 0, 10, 31])
コード例 #34
0
ファイル: CodeClubHWSF.py プロジェクト: ASJ2110/HWSF
import numpy as np
from matplotlib import pyplot as plt
from skimage import io
from skimage import data
from skimage.color import rgb2gray
from skimage.transform import resize, integral_image

ori = io.imread('./img2.png')
res = resize(ori, (24, 24))
mono = rgb2gray(res)
inter = integral_image(mono)

fig, axes = plt.subplots(1, 4, figsize=(16, 4))
ax = axes.ravel()

ax[0].imshow(ori)
ax[1].imshow(res)
ax[2].imshow(mono, cmap=plt.cm.gray)
ax[3].imshow(inter)

fig.tight_layout()
plt.show()
コード例 #35
0
ファイル: blob.py プロジェクト: foogunlana/scikit-image
def blob_doh(image, min_sigma=1, max_sigma=30, num_sigma=10, threshold=0.01,
             overlap=.5, log_scale=False):
    """Finds blobs in the given grayscale image.

    Blobs are found using the Determinant of Hessian method [1]_. For each blob
    found, the method returns its coordinates and the standard deviation
    of the Gaussian Kernel used for the Hessian matrix whose determinant
    detected the blob. Determinant of Hessians is approximated using [2]_.

    Parameters
    ----------
    image : ndarray
        Input grayscale image.Blobs can either be light on dark or vice versa.
    min_sigma : float, optional
        The minimum standard deviation for Gaussian Kernel used to compute
        Hessian matrix. Keep this low to detect smaller blobs.
    max_sigma : float, optional
        The maximum standard deviation for Gaussian Kernel used to compute
        Hessian matrix. Keep this high to detect larger blobs.
    num_sigma : int, optional
        The number of intermediate values of standard deviations to consider
        between `min_sigma` and `max_sigma`.
    threshold : float, optional.
        The absolute lower bound for scale space maxima. Local maxima smaller
        than thresh are ignored. Reduce this to detect less prominent blobs.
    overlap : float, optional
        A value between 0 and 1. If the area of two blobs overlaps by a
        fraction greater than `threshold`, the smaller blob is eliminated.
    log_scale : bool, optional
        If set intermediate values of standard deviations are interpolated
        using a logarithmic scale to the base `10`. If not, linear
        interpolation is used.

    Returns
    -------
    A : (n, 3) ndarray
        A 2d array with each row representing 3 values, ``(y,x,sigma)``
        where ``(y,x)`` are coordinates of the blob and ``sigma`` is the
        standard deviation of the Gaussian kernel of the Hessian Matrix whose
        determinant detected the blob.

    References
    ----------
    .. [1] http://en.wikipedia.org/wiki/Blob_detection#The_determinant_of_the_Hessian

    .. [2] Herbert Bay, Andreas Ess, Tinne Tuytelaars, Luc Van Gool,
           "SURF: Speeded Up Robust Features"
           ftp://ftp.vision.ee.ethz.ch/publications/articles/eth_biwi_00517.pdf

    Examples
    --------
    >>> from skimage import data, feature
    >>> img = data.coins()
    >>> feature.blob_doh(img)
    array([[121, 271,  30],
           [123,  44,  23],
           [123, 205,  20],
           [124, 336,  20],
           [126, 101,  20],
           [126, 153,  20],
           [156, 302,  30],
           [185, 348,  30],
           [192, 212,  23],
           [193, 275,  23],
           [195, 100,  23],
           [197,  44,  20],
           [197, 153,  20],
           [260, 173,  30],
           [262, 243,  23],
           [265, 113,  23],
           [270, 363,  30]])


    Notes
    -----
    The radius of each blob is approximately `sigma`.
    Computation of Determinant of Hessians is independent of the standard
    deviation. Therefore detecting larger blobs won't take more time. In
    methods line :py:meth:`blob_dog` and :py:meth:`blob_log` the computation
    of Gaussians for larger `sigma` takes more time. The downside is that
    this method can't be used for detecting blobs of radius less than `3px`
    due to the box filters used in the approximation of Hessian Determinant.
    """

    assert_nD(image, 2)

    image = img_as_float(image)
    image = integral_image(image)

    if log_scale:
        start, stop = log(min_sigma, 10), log(max_sigma, 10)
        sigma_list = np.logspace(start, stop, num_sigma)
    else:
        sigma_list = np.linspace(min_sigma, max_sigma, num_sigma)

    hessian_images = [_hessian_matrix_det(image, s) for s in sigma_list]
    image_cube = np.dstack(hessian_images)

    local_maxima = peak_local_max(image_cube, threshold_abs=threshold,
                                  footprint=np.ones((3, 3, 3)),
                                  threshold_rel=0.0,
                                  exclude_border=False)

    # Convert the last index to its corresponding scale value
    local_maxima[:, 2] = sigma_list[local_maxima[:, 2]]
    return _prune_blobs(local_maxima, overlap)
コード例 #36
0
def test():
    im = cv2.cvtColor(cv2.imread("image/0.jpg"), cv2.COLOR_BGR2GRAY)
    im_ii = integral_image(im)
    print(im_ii.shape)
    feature = haar_like_feature(im_ii, 0, 0, 10, 10, 'type-3-x')
    print(feature)
コード例 #37
0
 def integral(self):
     return integral_image(self.gray())
コード例 #38
0
def blob_doh(image,
             min_sigma=1,
             max_sigma=30,
             num_sigma=10,
             threshold=0.01,
             overlap=.5,
             log_scale=False):
    """Finds blobs in the given grayscale image.

    Adapted for dask from scikit-image.feature.blob_doh
    Blobs are found using the Determinant of Hessian method [1]_. For each blob
    found, the method returns its coordinates and the standard deviation
    of the Gaussian Kernel used for the Hessian matrix whose determinant
    detected the blob. Determinant of Hessians is approximated using [2]_.
    Parameters
    ----------
    image : 2D dask array
        Input grayscale image. Blobs can either be light on dark or vice versa.
    min_sigma : float, optional
        The minimum standard deviation for Gaussian Kernel used to compute
        Hessian matrix. Keep this low to detect smaller blobs.
    max_sigma : float, optional
        The maximum standard deviation for Gaussian Kernel used to compute
        Hessian matrix. Keep this high to detect larger blobs.
    num_sigma : int, optional
        The number of intermediate values of standard deviations to consider
        between `min_sigma` and `max_sigma`.
    threshold : float, optional.
        The absolute lower bound for scale space maxima. Local maxima smaller
        than thresh are ignored. Reduce this to detect less prominent blobs.
    overlap : float, optional
        A value between 0 and 1. If the area of two blobs overlaps by a
        fraction greater than `threshold`, the smaller blob is eliminated.
    log_scale : bool, optional
        If set intermediate values of standard deviations are interpolated
        using a logarithmic scale to the base `10`. If not, linear
        interpolation is used.
    Returns
    -------
    A : (n, 3) ndarray
        A 2d array with each row representing 3 values, ``(y,x,sigma)``
        where ``(y,x)`` are coordinates of the blob and ``sigma`` is the
        standard deviation of the Gaussian kernel of the Hessian Matrix whose
        determinant detected the blob.
    References
    ----------
    .. [1] https://en.wikipedia.org/wiki/Blob_detection#The_
    determinant_of_the_Hessian
    .. [2] Herbert Bay, Andreas Ess, Tinne Tuytelaars, Luc Van Gool,
           "SURF: Speeded Up Robust Features"
           ftp://ftp.vision.ee.ethz.ch/publications/articles/eth_biwi_00517.pdf
    Examples
    --------
    >>> from skimage import data
    >>> import dask.array as da
    >>> from dask_image.ndfeature import blob_doh
    >>> blob_dog(da.from_array(data.coins()))
    array([[270.        , 363.        ,  30.        ],
           [265.        , 113.        ,  23.55555556],
           [262.        , 243.        ,  23.55555556],
           [260.        , 173.        ,  30.        ],
           [197.        , 153.        ,  20.33333333],
           [197.        ,  44.        ,  20.33333333],
           [195.        , 100.        ,  23.55555556],
           [193.        , 275.        ,  23.55555556],
           [192.        , 212.        ,  23.55555556],
           [185.        , 348.        ,  30.        ],
           [156.        , 302.        ,  30.        ],
           [126.        , 153.        ,  20.33333333],
           [126.        , 101.        ,  20.33333333],
           [124.        , 336.        ,  20.33333333],
           [123.        , 205.        ,  20.33333333],
           [123.        ,  44.        ,  23.55555556],
           [121.        , 271.        ,  30.        ]])
    Notes
    -----
    The radius of each blob is approximately `sigma`.
    Computation of Determinant of Hessians is independent of the standard
    deviation. Therefore detecting larger blobs won't take more time. In
    methods line :py:meth:`blob_dog` and :py:meth:`blob_log` the computation
    of Gaussians for larger `sigma` takes more time. The downside is that
    this method can't be used for detecting blobs of radius less than `3px`
    due to the box filters used in the approximation of Hessian Determinant
    and that the algorithm is currently limited to 2 dimensions.
    """

    # check that 2D limitation is met
    if image.ndim != 2:
        raise ValueError('Blob detection with determinant of hessian requires\
         2D array')

    # get float integral image to compute determinant of hessian
    image = _daskarray_to_float(image)
    image = integral_image(image)

    # get sequence of sigmas
    if log_scale is True:
        start, stop = math.log(min_sigma, 10), math.log(max_sigma, 10)
        sigma_list = np.logspace(start, stop, num_sigma)
    else:
        sigma_list = np.linspace(min_sigma, max_sigma, num_sigma)

    # map hessian determinant cython function to array chunks
    depth = int(np.ceil(max_sigma * math.sqrt(image.ndim)))
    hessian_images = [
        image.map_overlap(_hessian_matrix_det,
                          depth=depth,
                          sigma=s,
                          dtype=image.dtype) for s in sigma_list
    ]

    # stack transformed images
    image_stack = da.dstack(hessian_images)

    # rechunk along sigma axis
    chunk_shape = image_stack.chunks
    new_shape = chunk_shape[:-1] + ((sum(chunk_shape[-1]), ), )
    image_stack = image_stack.rechunk(chunks=new_shape)

    # get coordinates of local maxima in transformed stack
    local_maxima = peak_local_max(image_stack,
                                  threshold=threshold,
                                  footprint=np.ones((3, ) * image_stack.ndim),
                                  exclude_border=False)

    # Catch no peaks
    if local_maxima.size == 0:
        return np.empty((0, 3))

    # Convert local_maxima to float64
    lm = local_maxima.astype(np.float64)

    # Convert the last index to its corresponding scale value
    lm[:, -1] = sigma_list[local_maxima[:, -1]]

    # prune blobs that are too close together
    return _prune_blobs(lm, overlap)
コード例 #39
0
)
len(tr_face_data), len(tr_non_face_data), len(tr_face_labels), len(
    tr_non_face_labels), len(te_face_data), len(te_non_face_data), len(
        te_face_labels), len(te_non_face_labels)

# In[ ]:

tr_data = np.concatenate((tr_face_data, tr_non_face_data))
te_data = np.concatenate((te_face_data, te_non_face_data))

# # Extracting HAAR Features and HAAR Co-ordinates

# In[ ]:

X_train = np.array([
    haar_like_feature(integral_image(tr_data[i]),
                      width=face_dim[0],
                      height=face_dim[1],
                      r=0,
                      c=0) for i in range(len(tr_data))
])
y_train = np.array([+1] * len(tr_face_data) + [-1] * len(tr_non_face_data))

# In[ ]:

X_test = np.array([
    haar_like_feature(integral_image(te_data[i]),
                      width=face_dim[0],
                      height=face_dim[1],
                      r=0,
                      c=0) for i in range(len(te_data))
コード例 #40
0
ファイル: testmethods.py プロジェクト: vicchu/Road_Extraction
from __future__ import print_function
from skimage.feature import multiblock_lbp, draw_multiblock_lbp
import numpy as np
from numpy.testing import assert_equal
from skimage.transform import integral_image
from matplotlib import pyplot as plt
# Create test matrix where first and fifth rectangles starting
# from top left clockwise have greater value than the central one.
test_img = np.zeros((9, 9), dtype='uint8')
test_img[3:6, 3:6] = 1
test_img[:3, :3] = 50
test_img[6:, 6:] = 50

print(test_img)
# First and fifth bits should be filled. This correct value will
#  be compared to the computed one.
correct_answer = 0b10001000

int_img = integral_image(test_img)
print(int_img)

lbp_code = multiblock_lbp(int_img, 0, 0, 3, 3)
print(lbp_code)
img = draw_multiblock_lbp(test_img, 0, 0, 90, 90, lbp_code=lbp_code, alpha=0.5)

plt.imshow(img, interpolation='nearest')

plt.show()