Ich hoffe ihr könnt mir bei meinem folgenden Problem ein wenig helfen.
Ich versuche mithilfe des Berechnens "Co-occurrence matrices" von Objekte selbige später in einem anderen Bild wiederzufinden. (Objekterkennung)
Ich habe dazu diesen passenden Codeschnipsel gefunden, jedoch bekomme ich immer nur ein schwarzes Bild als Output image (Wenigstens stimmt die Auflösung schonmal )
Wahrscheinlich muss ich etwas anderes beim zweiten Paramter angeben, leider steht aber auch im Kommentar nicht, wozu der Parameter labels nötig sein sollte. Habt ihr da irgendwelche Ideen? Bisher habe ich mithilfe von PIL ein Bild in ein NumPy Array umgewandelt und z.B. in die Variable 'a' gespeichert und a sowohl als ersten als auch als zweiten Paramter übergeben (scale habe ich bei Standard auf 3 gelassen)
Zusammenfassen kann ich sagen, was für ein Array möchte labels?
Vielen Dank,
Thegecko
Code: Alles auswählen
import numpy as np
import scipy.ndimage as scind
def cooccurrence(quantized_image, labels, scale=3):
"""Calculates co-occurrence matrices for all the objects in the image.
Return an array P of shape (nobjects, nlevels, nlevels) such that
P[o, :, :] is the cooccurence matrix for object o.
quantized_image -- a numpy array of integer type
labels -- a numpy array of integer type
scale -- an integer
For each object O, the cooccurrence matrix is defined as follows.
Given a row number I in the matrix, let A be the set of pixels in
O with gray level I, excluding pixels in the rightmost S
columns of the image. Let B be the set of pixels in O that are S
pixels to the right of a pixel in A. Row I of the cooccurence
matrix is the gray-level histogram of the pixels in B.
"""
nlevels = quantized_image.max() + 1
nobjects = labels.max()
image_a = quantized_image[:, :-scale]
image_b = quantized_image[:, scale:]
labels_ab = labels[:, :-scale]
equilabel = ((labels[:, :-scale] == labels[:, scale:]) & (labels[:,:-scale] > 0))
P, bins_P = np.histogramdd([labels_ab[equilabel]-1,image_a[equilabel], image_b[equilabel]], (nobjects, nlevels, nlevels))
pixel_count = fix(scind.sum(equilabel, labels[:,:-scale],np.arange(nobjects)+1))
pixel_count = np.tile(pixel_count[:,np.newaxis,np.newaxis],1,nlevels,nlevels))
return (P.astype(float) / pixel_count.astype(float), nlevels)
def fix(whatever_it_returned):
"""Convert a result from scipy.ndimage to a numpy array
scipy.ndimage has the annoying habit of returning a single, bare
value instead of an array if the indexes passed in are of length 1.
For instance:
scind.maximum(image, labels, [1]) returns a float
but
scind.maximum(image, labels, [1,2]) returns a list
"""
if getattr(whatever_it_returned,"__getitem__",False):
return np.array(whatever_it_returned)
else:
return np.array([whatever_it_returned])