Bag of words

Simple bag of words image mathing class

class machinevisiontoolbox.BagOfWords.BagOfWords(images, k=2000, nstopwords=0, attempts=1, seed=None)[source]
wwfv(i=None)[source]

Weighted word frequency vector for image

Parameters:

i (int, optional) – image within bag, defaults to all images

Returns:

word frequency vector or vectors

Return type:

ndarray(K), ndarray(N,K)

This is the word-frequency vector for the i’th image in the bag. The angle between any two WFVs is an indication of image similarity.

If i is None then the word-frequency matrix is returned, where the columns are the word-frequency vectors for the images in the bag.

Note

The word vector is expensive to compute so a lazy evaluation is performed on the first call to this method.

property nimages

Number of images associated in the bag

Returns:

number of images

Return type:

int

property images

Images associated with this bag

Returns:

images associated with this bag

Return type:

Image iterable

Note

Only valid if the bag was constructed from images rather than features.

property k

Number of words in the visual vocabulary

Returns:

number of words

Return type:

int

Seealso:

nstopwords

property words

Word labels for every feature

Returns:

word labels

Return type:

ndarray(N)

Word labels are arranged such that the top nstopwords labels are stop words.

Seealso:

nstopwords

word(f)[source]

Word labels for original feature

Returns:

word labels

Return type:

ndarray(N)

Word labels are arranged such that the top nstopwords labels

property nwords

Number of usable words

Returns:

number of usable words

Return type:

int

This is k - nstopwords.

Seealso:

k nstopwords

property nstopwords

Number of stop words

Returns:

Number of stop words

Return type:

int

Seealso:

k nwords

property firststop

First stop word

Returns:

word index of first stop word

Return type:

int

property centroids

Word feature centroids

Returns:

centroids of visual word features

Return type:

ndarray(k,N)

Is an array with one row per visual word, and the row is the feature descriptor vector. eg. for SIFT features it is 128 elements.

Centroids are arranged such that the last nstopwords rows correspond to the stop words. After clustering against the centroids, any word with a label >= nstopwords is a stop word.

Note

The stop words are kept in the centroid array for the recall process.

Seealso:

similarity

similarity(arg)[source]

Compute similarity between bag and query images

Parameters:

other (BagOfWords) – bag of words

Returns:

confusion matrix

Return type:

ndarray(M,N)

The array has rows corresponding to the images in self and columns corresponding to the images in other.

Seealso:

closest

features(word)[source]

Get features corresponding to word

Parameters:

word (int) – visual word label

Returns:

features corresponding to this label

Return type:

BaseFeature2D

Return a slice of the image features corresponding to this word label. The .id attribute of each feature indicates which image in the bag it belongs to.

occurrence(word)[source]

Number of occurrences of specified word

Parameters:

word (int) – visual word label

Returns:

total number of times that visual word appears in this bag

Return type:

int

wordfreq()[source]

Get visual word frequency

Returns:

visual words, visual word frequency

Return type:

ndarray, ndarray

Returns two arrays, one containing all visual words, the other containing the frequency of the corresponding word across all images.

closest(S, i)[source]

Find closest image

Parameters:
  • S (ndarray(N,M)) – bag similarity matrix

  • i (int) – the query image index

Returns:

index of the recalled image and similarity

Return type:

int, float

Seealso:

similarity

contains(word)[source]

Images that contain specified word

Parameters:

word (int) – visual word label

Returns:

list of images containing this word

Return type:

list

Seealso:

exemplars

exemplars(word, images=None, maxperimage=2, columns=10, max=None, width=50, **kwargs)[source]

Composite image containing exemplars of specified word

Parameters:
  • word (int) – visual word label

  • images – the set of images corresponding to this bag, only required if the bag was constructed from features not images.

  • maxperimage (int, optional) – maximum number of exemplars drawn from any one image, defaults to 2

  • columns (int, optional) – number of exemplar images in each row, defaults to 10

  • max (int, optional) – maximum number of exemplar images, defaults to None

  • width (int, optional) – width of image thumbnail, defaults to 50

Returns:

composite image

Return type:

Image

Produces a grid of examples of a particular visual word.

Seealso:

contains support Tile