def compute(self, frame):
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
descriptor = []
dominantGradients = np.zeros_like(frame)
maxGradient = cv2.filter2D(frame, cv2.CV_32F, self.kernels[0])
maxGradient = np.absolute(maxGradient)
for k in range(1,len(self.kernels)):
kernel = self.kernels[k]
gradient = cv2.filter2D(frame, cv2.CV_32F, kernel)
gradient = np.absolute(gradient)
np.maximum(maxGradient, gradient, maxGradient)
indices = (maxGradient == gradient)
dominantGradients[indices] = k
frameH, frameW = frame.shape
for row in range(self.rows):
for col in range(self.cols):
mask = np.zeros_like(frame)
mask[((frameH/self.rows)*row):((frameH/self.rows)*(row+1)),(frameW/self.cols)*col:((frameW/self.cols)*(col+1))] = 255
hist = cv2.calcHist([dominantGradients], [0], mask, self.bins, self.range)
hist = cv2.normalize(hist, None)
descriptor.append(hist)
return np.concatenate([x for x in descriptor])
python类CV_32F的实例源码
def hog(img):
h, w = img.shape
gx = cv2.Sobel(img, cv2.CV_32F, 1, 0)
gy = cv2.Sobel(img, cv2.CV_32F, 0, 1)
mag, ang = cv2.cartToPolar(gx, gy)
bins = np.int32(bin_n*ang/(2*np.pi)) # quantizing binvalues in (0...16)
bin_cells = ()
mag_cells = ()
for i in range(wc):
for j in range(hc):
bin_cells += (bins[j*h/hc:(j+1)*h/hc, i*w/wc:(i+1)*w/wc],)
mag_cells += (mag[j*h/hc:(j+1)*h/hc, i*w/wc:(i+1)*w/wc],)
#np.bincount() return times of each number appear
hists = [np.bincount(b.ravel(), m.ravel(), bin_n) for b, m in zip(bin_cells, mag_cells)]
hist = np.hstack(hists) # hist is a 16*wc*hc vector
return hist
def compute(self, frame):
#frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
dx = cv2.filter2D(frame, cv2.CV_32F, self.xkernel)
dy = cv2.filter2D(frame, cv2.CV_32F, self.ykernel)
orientations = np.zeros_like(dx)
magnitudes = np.zeros_like(dx)
cv2.cartToPolar(dx,dy, magnitudes,orientations)
descriptor = []
frameH, frameW = frame.shape
mask_threshold = magnitudes <= self.threshold
for row in range(self.rows):
for col in range(self.cols):
mask = np.zeros_like(frame)
mask[((frameH/self.rows)*row):((frameH/self.rows)*(row+1)),(frameW/self.cols)*col:((frameW/self.cols)*(col+1))] = 1
mask[mask_threshold] = 0
a_, b_ = mask.shape
hist = cv2.calcHist([orientations], self.channel, mask, [self.bins], self.range)
hist = cv2.normalize(hist, None)
descriptor.append(hist)
return np.concatenate([x for x in descriptor])
def get_mag_ang(img):
"""
Gets image gradient (magnitude) and orientation (angle)
Args:
img
Returns:
Gradient, orientation
"""
img = np.sqrt(img)
gx = cv2.Sobel(np.float32(img), cv2.CV_32F, 1, 0)
gy = cv2.Sobel(np.float32(img), cv2.CV_32F, 0, 1)
mag, ang = cv2.cartToPolar(gx, gy)
return mag, ang, gx, gy
def hog(img):
h, w = img.shape
gx = cv2.Sobel(img, cv2.CV_32F, 1, 0)
gy = cv2.Sobel(img, cv2.CV_32F, 0, 1)
mag, ang = cv2.cartToPolar(gx, gy)
bins = np.int32(bin_n*ang/(2*np.pi)) # quantizing binvalues in (0...16)
bin_cells = ()
mag_cells = ()
for i in range(wc):
for j in range(hc):
bin_cells += (bins[j*h/hc:(j+1)*h/hc, i*w/wc:(i+1)*w/wc],)
mag_cells += (mag[j*h/hc:(j+1)*h/hc, i*w/wc:(i+1)*w/wc],)
hists = [np.bincount(b.ravel(), m.ravel(), bin_n) for b, m in zip(bin_cells, mag_cells)]
hist = np.hstack(hists) # hist is a 16*wc*hc vector
return hist
def hog(img):
h, w = img.shape
gx = cv2.Sobel(img, cv2.CV_32F, 1, 0)
gy = cv2.Sobel(img, cv2.CV_32F, 0, 1)
mag, ang = cv2.cartToPolar(gx, gy)
bins = np.int32(bin_n*ang/(2*np.pi)) # quantizing binvalues in (0...bin_n)
bin_cells = ()
mag_cells = ()
for i in range(wc):
for j in range(hc):
bin_cells += (bins[j*h/hc:(j+1)*h/hc, i*w/wc:(i+1)*w/wc],)
mag_cells += (mag[j*h/hc:(j+1)*h/hc, i*w/wc:(i+1)*w/wc],)
hists = [np.bincount(b.ravel(), m.ravel(), bin_n) for b, m in zip(bin_cells, mag_cells)]
hist = np.hstack(hists) # hist is a bin_n*wc*hc vector
return hist
def hog(img):
h, w = img.shape
gx = cv2.Sobel(img, cv2.CV_32F, 1, 0)
gy = cv2.Sobel(img, cv2.CV_32F, 0, 1)
mag, ang = cv2.cartToPolar(gx, gy)
bins = np.int32(bin_n*ang/(2*np.pi)) # quantizing binvalues in (0...16)
bin_cells = ()
mag_cells = ()
for i in range(wc):
for j in range(hc):
bin_cells += (bins[j*h/hc:(j+1)*h/hc, i*w/wc:(i+1)*w/wc],)
mag_cells += (mag[j*h/hc:(j+1)*h/hc, i*w/wc:(i+1)*w/wc],)
hists = [np.bincount(b.ravel(), m.ravel(), bin_n) for b, m in zip(bin_cells, mag_cells)]
hist = np.hstack(hists) # hist is a 16*wc*hc vector
return hist
def removeBackground(self, image):
gray = np.float32(cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)) / 255
if self.background_gaussian is None or self.background_gaussian.shape[0] != Configuration.gaussian_kernel_size:
self.background_gaussian = cv2.getGaussianKernel(Configuration.gaussian_kernel_size, -1, cv2.CV_32F)
background = cv2.sepFilter2D(gray, cv2.CV_32F, self.background_gaussian, self.background_gaussian)
result = gray - background
result = result * self.mask
mi = np.min(result)
ma = np.max(result)
#result = (result - mi) / (ma - mi)
return result / ma
def get_mag_avg(img):
img = np.sqrt(img)
kernels = get_kernels()
mag = np.zeros(img.shape, dtype='float32')
for kernel_filter in kernels:
gx = cv2.filter2D(np.float32(img), cv2.CV_32F, kernel_filter[1], borderType=cv2.BORDER_REFLECT)
gy = cv2.filter2D(np.float32(img), cv2.CV_32F, kernel_filter[0], borderType=cv2.BORDER_REFLECT)
mag += cv2.magnitude(gx, gy)
mag /= len(kernels)
return mag
def preprocess_hog(digits):
samples = []
for img in digits:
gx = cv2.Sobel(img, cv2.CV_32F, 1, 0)
gy = cv2.Sobel(img, cv2.CV_32F, 0, 1)
mag, ang = cv2.cartToPolar(gx, gy)
bin_n = 16
bin = np.int32(bin_n*ang/(2*np.pi))
bin_cells = bin[:100,:100], bin[100:,:100], bin[:100,100:], bin[100:,100:]
mag_cells = mag[:100,:100], mag[100:,:100], mag[:100,100:], mag[100:,100:]
hists = [np.bincount(b.ravel(), m.ravel(), bin_n) for b, m in zip(bin_cells, mag_cells)]
hist = np.hstack(hists)
# transform to Hellinger kernel
eps = 1e-7
hist /= hist.sum() + eps
hist = np.sqrt(hist)
hist /= norm(hist) + eps
samples.append(hist)
return np.float32(samples)
#Here goes my wrappers:
def hog_single(img):
samples=[]
gx = cv2.Sobel(img, cv2.CV_32F, 1, 0)
gy = cv2.Sobel(img, cv2.CV_32F, 0, 1)
mag, ang = cv2.cartToPolar(gx, gy)
bin_n = 16
bin = np.int32(bin_n*ang/(2*np.pi))
bin_cells = bin[:100,:100], bin[100:,:100], bin[:100,100:], bin[100:,100:]
mag_cells = mag[:100,:100], mag[100:,:100], mag[:100,100:], mag[100:,100:]
hists = [np.bincount(b.ravel(), m.ravel(), bin_n) for b, m in zip(bin_cells, mag_cells)]
hist = np.hstack(hists)
# transform to Hellinger kernel
eps = 1e-7
hist /= hist.sum() + eps
hist = np.sqrt(hist)
hist /= norm(hist) + eps
samples.append(hist)
return np.float32(samples)
#using Compute_hog too much time !
def get_init_process_img(roi_img):
"""
?????????????????????????????????????
:param roi_img: ndarray
:return: ndarray
"""
h = cv2.Sobel(roi_img, cv2.CV_32F, 0, 1, -1)
v = cv2.Sobel(roi_img, cv2.CV_32F, 1, 0, -1)
img = cv2.add(h, v)
img = cv2.convertScaleAbs(img)
img = cv2.GaussianBlur(img, (3, 3), 0)
ret, img = cv2.threshold(img, 120, 255, cv2.THRESH_BINARY)
kernel = np.ones((1, 1), np.uint8)
img = cv2.erode(img, kernel, iterations=1)
img = cv2.dilate(img, kernel, iterations=2)
img = cv2.erode(img, kernel, iterations=1)
img = cv2.dilate(img, kernel, iterations=2)
img = auto_canny(img)
return img
def blur_measure(im):
""" See cv::videostab::calcBlurriness """
H, W = im.shape[:2]
gx = cv2.Sobel(im, cv2.CV_32F, 1, 0)
gy = cv2.Sobel(im, cv2.CV_32F, 0, 1)
norm_gx, norm_gy = cv2.norm(gx), cv2.norm(gy)
return 1.0 / ((norm_gx ** 2 + norm_gy ** 2) / (H * W + 1e-6))
def im_normalize(im, lo=0, hi=255, dtype='uint8'):
return cv2.normalize(im, alpha=lo, beta=hi, norm_type=cv2.NORM_MINMAX, dtype={'uint8': cv2.CV_8U, \
'float32': cv2.CV_32F, \
'float64': cv2.CV_64F}[dtype])
def blur_image(self, save=False, show=False):
if self.part is None:
psf = self.PSFs
else:
psf = [self.PSFs[self.part]]
yN, xN, channel = self.shape
key, kex = self.PSFs[0].shape
delta = yN - key
assert delta >= 0, 'resolution of image should be higher than kernel'
result=[]
if len(psf) > 1:
for p in psf:
tmp = np.pad(p, delta // 2, 'constant')
cv2.normalize(tmp, tmp, alpha=0, beta=1, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_32F)
# blured = np.zeros(self.shape)
blured = cv2.normalize(self.original, self.original, alpha=0, beta=1, norm_type=cv2.NORM_MINMAX,
dtype=cv2.CV_32F)
blured[:, :, 0] = np.array(signal.fftconvolve(blured[:, :, 0], tmp, 'same'))
blured[:, :, 1] = np.array(signal.fftconvolve(blured[:, :, 1], tmp, 'same'))
blured[:, :, 2] = np.array(signal.fftconvolve(blured[:, :, 2], tmp, 'same'))
blured = cv2.normalize(blured, blured, alpha=0, beta=1, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_32F)
blured = cv2.cvtColor(blured, cv2.COLOR_RGB2BGR)
result.append(np.abs(blured))
else:
psf = psf[0]
tmp = np.pad(psf, delta // 2, 'constant')
cv2.normalize(tmp, tmp, alpha=0, beta=1, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_32F)
blured = cv2.normalize(self.original, self.original, alpha=0, beta=1, norm_type=cv2.NORM_MINMAX,
dtype=cv2.CV_32F)
blured[:, :, 0] = np.array(signal.fftconvolve(blured[:, :, 0], tmp, 'same'))
blured[:, :, 1] = np.array(signal.fftconvolve(blured[:, :, 1], tmp, 'same'))
blured[:, :, 2] = np.array(signal.fftconvolve(blured[:, :, 2], tmp, 'same'))
blured = cv2.normalize(blured, blured, alpha=0, beta=1, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_32F)
blured = cv2.cvtColor(blured, cv2.COLOR_RGB2BGR)
result.append(np.abs(blured))
self.result = result
if show or save:
self.__plot_canvas(show, save)
def hog(img, bin_n=8, cell_size=4):
img = cv2.resize(img,(128,128))
gx = cv2.Sobel(img, cv2.CV_32F, 1, 0)
gy = cv2.Sobel(img, cv2.CV_32F, 0, 1)
mag, ang = cv2.cartToPolar(gx, gy)
bin = np.int32(bin_n*ang/(2*np.pi))
bin_cells = []
mag_cells = []
cellx = celly = cell_size
for i in range(0,img.shape[0]/celly):
for j in range(0,img.shape[1]/cellx):
bin_cells.append(bin[i*celly : i*celly+celly, j*cellx : j*cellx+cellx])
mag_cells.append(mag[i*celly : i*celly+celly, j*cellx : j*cellx+cellx])
hists = [np.bincount(b.ravel(), m.ravel(), bin_n) for b, m in zip(bin_cells, mag_cells)]
hist = np.hstack(hists)
# transform to Hellinger kernel
eps = 1e-7
hist /= hist.sum() + eps
hist = np.sqrt(hist)
hist /= norm(hist) + eps
hist_out = np.reshape(hist,(32,32,8))
return hist_out
def read_array(filename):
with open(filename, 'rb') as fp:
type_code = np.fromstring(fp.read(4), dtype=np.int32)
shape_size = np.fromstring(fp.read(4), dtype=np.int32)
shape = np.fromstring(fp.read(4 * shape_size), dtype=np.int32)
if type_code == cv2.CV_32F:
dtype = np.float32
if type_code == cv2.CV_64F:
dtype = np.float64
return np.fromstring(fp.read(), dtype=dtype).reshape(shape)
def write_array(filename, array):
with open(filename, 'wb') as fp:
if array.dtype == np.float32:
typecode = cv2.CV_32F
elif array.dtype == np.float64:
typecode = cv2.CV_64F
else:
raise ValueError("type is not supported")
fp.write(np.array(typecode, dtype=np.int32).tostring())
fp.write(np.array(len(array.shape), dtype=np.int32).tostring())
fp.write(np.array(array.shape, dtype=np.int32).tostring())
fp.write(array.tostring())
def compute_gradient(img, use_scharr=True):
if use_scharr:
norm_factor = 32
gradx = cv2.Scharr(img, cv2.CV_32F, 1, 0, scale=1.0/norm_factor)
grady = cv2.Scharr(img, cv2.CV_32F, 0, 1, scale=1.0/norm_factor)
else:
kx = cv2.getDerivKernels(1, 0, ksize=1, normalize=True)
ky = cv2.getDerivKernels(0, 1, ksize=1, normalize=True)
gradx = cv2.sepFilter2D(img, cv2.CV_32F, kx[0], kx[1])
grady = cv2.sepFilter2D(img, cv2.CV_32F, ky[0], ky[1])
gradient = np.dstack([gradx, grady])
return gradient
def build_filters():
filters = []
ksize = 9
#define the range for theta and nu
for theta in np.arange(0, np.pi, np.pi / 8):
for nu in np.arange(0, 6*np.pi/4 , np.pi / 4):
kern = cv2.getGaborKernel((ksize, ksize), 1.0, theta, nu, 0.5, 0, ktype=cv2.CV_32F)
kern /= 1.5*kern.sum()
filters.append(kern)
return filters
#function to convolve the image with the filters
def get_gradient(im):
# Calculate the x and y gradients using Sobel operator
grad_x = cv2.Sobel(im,cv2.CV_32F,1,0,ksize=3)
grad_y = cv2.Sobel(im,cv2.CV_32F,0,1,ksize=3)
# Combine the two gradients
grad = cv2.addWeighted(np.absolute(grad_x), 0.5, np.absolute(grad_y), 0.5, 0)
# print grad.dtype
# print grad.shape
return grad
# Based on: http://www.learnopencv.com/image-alignment-ecc-in-opencv-c-python/
def _get_gradient_magnitude(im):
"Get magnitude of gradient for given image"
ddepth = cv2.CV_32F
dx = cv2.Sobel(im, ddepth, 1, 0)
dy = cv2.Sobel(im, ddepth, 0, 1)
dxabs = cv2.convertScaleAbs(dx)
dyabs = cv2.convertScaleAbs(dy)
mag = cv2.addWeighted(dxabs, 0.5, dyabs, 0.5, 0)
return np.average(mag)
def prep_gabor(n_orientations=32, sigma=3., lambd=10., gamma=.5, psi=1., kernel_size=None, theta_skip=4):
"""
Prepare the Gabor kernels
Args:
n_orientations (int)
sigma (float): The standard deviation.
lambd (float): The wavelength of the sinusoidal factor.
gamma (float): The spatial aspect ratio.
psi (float): The phase offset.
kernel_size (tuple): The Gabor kernel size.
theta_skip (int): The `theta` skip factor.
"""
if not isinstance(kernel_size, tuple):
kernel_size = (15, 15)
# Prepare Gabor kernels.
kernels = list()
# kernel = resize(gabor_kernel(frequency,
# theta=theta,
# bandwidth=lambd,
# sigma_x=sigma,
# sigma_y=sigma,
# offset=psi)
for th in range(0, n_orientations, theta_skip):
# The kernel orientation.
theta = np.pi * th / n_orientations
kernel = cv2.getGaborKernel(kernel_size, sigma, theta, lambd, gamma, psi, ktype=cv2.CV_32F)
kernel /= 1.5 * kernel.sum()
kernels.append(np.float32(kernel))
return kernels
def _pre_processing(self, img):
self.input_image_origin = img
self.input_image = deepcopy(img)
self.input_image = cv2.resize(self.input_image, (self.image_size, self.image_size))
self.input_image = cv2.normalize(self.input_image, self.input_image, alpha=0, beta=1, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_32F)
self.input_image = [self.input_image]
self.input_image = np.array(self.input_image)
self.input_image = self.input_image.astype(np.float32)
self.input_image = self.input_image.reshape(-1, self.image_size, self.image_size, self.num_channels)
def process(state, W, H):
state = cv2.resize(state, (W, H))
state = cv2.cvtColor(state, cv2.COLOR_RGB2GRAY)
#cv2.imwrite('test.png', state)
#state = cv2.normalize(state, state, alpha=0, beta=1, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_32F)
state = np.reshape(state, [W, H, 1])
return state
def get_gradient(self,im) :
# Calculate the x and y gradients using Sobel operator
grad_x = cv2.Sobel(im,cv2.CV_32F,1,0,ksize=3)
grad_y = cv2.Sobel(im,cv2.CV_32F,0,1,ksize=3)
# Combine the two gradients
grad = cv2.addWeighted(np.absolute(grad_x), 0.5, np.absolute(grad_y), 0.5, 0)
return grad
def parse_array(array):
type_code = np.asscalar(np.fromstring(array[0:4], dtype=np.int32))
shape_size = np.asscalar(np.fromstring(array[4:8], dtype=np.int32))
shape = np.fromstring(array[8: 8+4 * shape_size], dtype=np.int32)
if type_code == 5:#cv2.CV_32F:
dtype = np.float32
if type_code == 6:#cv2.CV_64F:
dtype = np.float64
return np.fromstring(array[8+4 * shape_size:], dtype=dtype).reshape(shape)
def getImage(path):
"""
Args:
path: path to image
Returns:
image at path
"""
img = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
return cv2.normalize(img, None, 0.0, 1.0, cv2.NORM_MINMAX, cv2.CV_32F)
# ============================================================= #
def detect(self, image):
floatimage = cv2.cvtColor(np.float32(image), cv2.COLOR_BGR2GRAY) / 255
if self.gaussian is None or self.gaussian.shape[0] != Configuration.log_kernel_size:
self.gaussian = cv2.getGaussianKernel(Configuration.log_kernel_size, -1, cv2.CV_32F)
gaussian_filtered = cv2.sepFilter2D(floatimage, cv2.CV_32F, self.gaussian, self.gaussian)
# LoG
filtered = cv2.Laplacian(gaussian_filtered, cv2.CV_32F, ksize=Configuration.log_block_size)
# DoG
#gaussian2 = cv2.getGaussianKernel(Configuration.log_block_size, -1, cv2.CV_32F)
#gaussian_filtered2 = cv2.sepFilter2D(floatimage, cv2.CV_32F, gaussian2, gaussian2)
#filtered = gaussian_filtered - gaussian_filtered2
mi = np.min(filtered)
ma = np.max(filtered)
if mi - ma != 0:
filtered = 1 - (filtered - mi) / (ma - mi)
_, thresholded = cv2.threshold(filtered, Configuration.log_threshold, 1.0, cv2.THRESH_BINARY)
self.debug = thresholded
thresholded = np.uint8(thresholded)
contours = None
if int(cv2.__version__.split('.')[0]) == 2:
contours, _ = cv2.findContours(thresholded, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
else:
_, contours, _ = cv2.findContours(thresholded, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
candidates = []
for i in range(len(contours)):
rect = cv2.boundingRect(contours[i])
v1 = rect[0:2]
v2 = np.add(rect[0:2], rect[2:4])
if rect[2] < Configuration.log_max_rect_size and rect[3] < Configuration.log_max_rect_size:
roi = floatimage[v1[1]:v2[1], v1[0]:v2[0]]
_, _, _, maxLoc = cv2.minMaxLoc(roi)
maxLoc = np.add(maxLoc, v1)
candidates.append(maxLoc)
self.candidates = candidates
return candidates
def convolve_gabor(bd, image_min, image_max, scales):
"""
Convolves an image with a series of Gabor kernels
Args:
bd (2d array)
image_min (int or float)
image_max (int or float)
scales (1d array like)
"""
if bd.dtype != 'uint8':
bd = np.uint8(rescale_intensity(bd,
in_range=(image_min,
image_max),
out_range=(0, 255)))
# Each set of Gabor kernels
# has 8 orientations.
out_block = np.empty((8*len(scales),
bd.shape[0],
bd.shape[1]), dtype='float32')
ki = 0
for scale in scales:
# Check for even or
# odd scale size.
if scale % 2 == 0:
ssub = 1
else:
ssub = 0
gabor_kernels = prep_gabor(kernel_size=(scale-ssub, scale-ssub))
for kernel in gabor_kernels:
# TODO: pad array?
out_block[ki] = cv2.filter2D(bd, cv2.CV_32F, kernel)
ki += 1
return out_block