// Mahalanobis distance
float nv_mahalanobis(const nv_cov_t *cov, const nv_matrix_t *x, int xm)
{
	int n;
	nv_matrix_t *y = nv_matrix_alloc(x->n, 1);
	nv_matrix_t *x2 = nv_matrix_alloc(x->n, 1);
	float distance;
	float delta2 = 0.0f;

	nv_matrix_zero(y);
	nv_matrix_zero(x2);
	for (n = 0; n < x2->n; ++n) {
		NV_MAT_V(x2, 0, n) = NV_MAT_V(x, xm, n) - NV_MAT_V(cov->u, 0, n);
	}
	nv_gemv(y, 0, NV_MAT_TR, cov->eigen_vec, x2, xm);
	for (n = 0; n < x->n; ++n) {
		float ev = NV_MAT_V(cov->eigen_val, n, 0);
		float xv = NV_MAT_V(y, 0, n);
		delta2 += (xv * xv) / ev;
	}

	distance = sqrtf(delta2);
	nv_matrix_free(&x2);
	nv_matrix_free(&y);

	return distance;
}
Пример #2
0
void
nv_mlp_predict_vector(const nv_mlp_t *mlp,
					  nv_matrix_t *p, int p_j,
					  const nv_matrix_t *x, int x_j)
{
	int m;
	float y;
	nv_matrix_t *input_y = nv_matrix_alloc(mlp->hidden, 1);
	nv_matrix_t *hidden_y = nv_matrix_alloc(mlp->output, 1);
	float dropout_scale = 1.0f - mlp->dropout;
	float noise_scale = 1.0f - mlp->noise;
	
#ifdef _OPENMP
#pragma omp parallel for private(y)
#endif
	for (m = 0; m < mlp->hidden; ++m) {
		y = NV_MAT_V(mlp->input_bias, m, 0) * NV_MLP_BIAS;
		y += nv_vector_dot(x, x_j, mlp->input_w, m) * noise_scale;
		NV_MAT_V(input_y, 0, m) = nv_mlp_sigmoid(y) * dropout_scale;
	}
	for (m = 0; m < mlp->output; ++m) {
		y = NV_MAT_V(mlp->hidden_bias, m, 0) * NV_MLP_BIAS;
		y += nv_vector_dot(input_y, 0, mlp->hidden_w, m);
		NV_MAT_V(hidden_y, 0, m) = nv_mlp_sigmoid(y);
	}
	nv_mlp_softmax(p, p_j, hidden_y, 0);

	nv_matrix_free(&input_y);
	nv_matrix_free(&hidden_y);
}
Пример #3
0
/* 回帰 */
void nv_mlp_regression(const nv_mlp_t *mlp, 
					   const nv_matrix_t *x, int xm, nv_matrix_t *out, int om)
{
	int m;
	float y;
	nv_matrix_t *input_y = nv_matrix_alloc(mlp->input_w->m, 1);
	nv_matrix_t *hidden_y = nv_matrix_alloc(mlp->hidden_w->m, 1);

#ifdef _OPENMP
#pragma omp parallel for private(y)
#endif
	for (m = 0; m < mlp->input_w->m; ++m) {
		y = NV_MAT_V(mlp->input_bias, m, 0)  * NV_MLP_BIAS;
		y += nv_vector_dot(x, xm, mlp->input_w, m);
		NV_MAT_V(input_y, 0, m) = nv_mlp_sigmoid(y);
	}

	for (m = 0; m < mlp->hidden_w->m; ++m) {
		y = NV_MAT_V(mlp->hidden_bias, m, 0) * NV_MLP_BIAS;
		y += nv_vector_dot(input_y, 0, mlp->hidden_w, m);
		NV_MAT_V(hidden_y, 0, m) = y;
	}

	nv_vector_copy(out, om, hidden_y, 0);

	nv_matrix_free(&input_y);
	nv_matrix_free(&hidden_y);
}
Пример #4
0
static nv_matrix_t *
conv_image2vec(const nv_bgseg_t *bg,
			   const nv_matrix_t *image)
{
	nv_matrix_t *vec;
	nv_matrix_t *smooth;
	nv_matrix_t *resize = NULL, *gray = NULL;
	int i;
	float scale = (float)bg->size / (float)NV_MAX(image->rows, image->cols);

	if (scale != 1.0f) {
		resize = nv_matrix3d_alloc(image->n,
								   NV_ROUND_INT(image->rows * scale),
								   NV_ROUND_INT(image->cols * scale));
		nv_resize(resize, image);
		image = resize;
	}
	if (image->n != 1) {
		gray = nv_matrix3d_alloc(1, image->rows, image->cols);
		nv_gray(gray, image);
		image = gray;
	}
	vec = nv_matrix_alloc(image->rows * image->cols, 1);
	smooth = nv_matrix_clone(image);
	nv_gaussian5x5(smooth, 0, image, 0);

	for (i = 0; i < image->m; ++i) {
		NV_MAT_V(vec, 0, i) = NV_MAT_V(smooth, i, 0);
	}
	nv_matrix_free(&smooth);
	nv_matrix_free(&gray);
	nv_matrix_free(&resize);
	
	return vec;
}
Пример #5
0
float nv_mlp_predict(const nv_mlp_t *mlp,
					 const nv_matrix_t *x, int xm, int cls)
{
	int m;
	float y;
	nv_matrix_t *input_y = nv_matrix_alloc(mlp->input_w->m, 1);
	nv_matrix_t *hidden_y = nv_matrix_alloc(mlp->output, 1);
	nv_matrix_t *output_y = nv_matrix_alloc(mlp->output, 1);
	float p;
	double dropout_scale = 1.0 - mlp->dropout;
	float noise_scale = 1.0f - mlp->noise;
	
#ifdef _OPENMP
#pragma omp parallel for private(y)
#endif
	for (m = 0; m < mlp->hidden; ++m) {
		y = NV_MAT_V(mlp->input_bias, m, 0) * NV_MLP_BIAS;
		y += nv_vector_dot(x, xm, mlp->input_w, m) * noise_scale;
		NV_MAT_V(input_y, 0, m) = nv_mlp_sigmoid(y) * dropout_scale;
	}

	for (m = 0; m < mlp->output; ++m) {
		y = NV_MAT_V(mlp->hidden_bias, m, 0) * NV_MLP_BIAS;
		y += nv_vector_dot(input_y, 0, mlp->hidden_w, m);
		NV_MAT_V(hidden_y, 0, m) = nv_mlp_sigmoid(y);
	}
	nv_mlp_softmax(output_y, 0, hidden_y, 0);
	p = NV_MAT_V(output_y, 0, cls);

	nv_matrix_free(&input_y);
	nv_matrix_free(&hidden_y);
	nv_matrix_free(&output_y);

	return p;
}
Пример #6
0
void
nv_matrix_sort(nv_matrix_t *mat, int sort_column_n, nv_sort_dir_e dir)
{
	nv_matrix_t *sort_data = nv_matrix_alloc(2, mat->m);
	nv_matrix_t *tmp = nv_matrix_alloc(mat->n, mat->m);
	int m;

	for (m = 0; m < mat->m; ++m) {
		NV_MAT_V(sort_data, m, 0) = NV_MAT_V(mat, m, sort_column_n);
		NV_MAT_V(sort_data, m, 1) = (float)m;
	}
	if (dir == NV_SORT_DIR_ASC) {
		qsort(sort_data->v, sort_data->m,
			sort_data->step * sizeof(float), nv_column_cmp_asc);
	} else {
		qsort(sort_data->v, sort_data->m,
			sort_data->step * sizeof(float), nv_column_cmp_desc);
	}
	for (m = 0; m < mat->m; ++m) {
		nv_vector_copy(tmp, m, mat, (int)NV_MAT_V(sort_data, m, 1));
	}
	nv_matrix_copy(mat, 0, tmp, 0, mat->m);

	nv_matrix_free(&sort_data);
	nv_matrix_free(&tmp);
}
int
main(void)
{
	nv_matrix_t *data = nv_matrix_alloc(PATCH_SIZE * PATCH_SIZE * 3, SAMPLES);
	nv_matrix_t *centroids = nv_matrix_alloc(data->n, CENTROIDS);
	nv_matrix_t *zca_u = nv_matrix_alloc(data->n, data->n);
	nv_matrix_t *zca_m = nv_matrix_alloc(data->n, 1);
	std::vector<fileinfo_t> list;
	
	fileinfo_read(list, TRAIN_FILE);
	printf("read file list %d\n", (int)list.size());
	patch_sampling(data, list);
	printf("end patch sampling\n");
	
	nv_standardize_local_all(data, 10.0f);
	nv_zca_train(zca_m, 0, zca_u, data, 0.1f);
	nv_zca_whitening_all(data, zca_m, 0, zca_u);
	printf("end whitening\n");
	kmeans(centroids, data);
	
	nv_save_matrix_bin("zca_u.mat", zca_u);
	nv_save_matrix_bin("zca_m.mat", zca_m);
	nv_save_matrix_bin("centroids.mat", centroids);
	
	nv_matrix_free(&data);
	nv_matrix_free(&zca_u);
	nv_matrix_free(&zca_m);
	nv_matrix_free(&centroids);
	
	return 0;
}
Пример #8
0
void
nv_test_knn_lmca(const nv_matrix_t *train_data,
				 const nv_matrix_t *train_labels,
				 const nv_matrix_t *test_data,
				 const nv_matrix_t *test_labels)
{
	nv_matrix_t *train_data_lmca = nv_matrix_alloc(DIM, train_data->m);
	nv_matrix_t *vec = nv_matrix_alloc(DIM, 1);
	nv_matrix_t *l = nv_matrix_alloc(train_data->n, DIM);
	int i, ok;
	nv_knn_result_t results[KNN_K];
	long t;
	
	NV_TEST_NAME;
	nv_lmca_progress(1);
	
	printf("train: %d, test: %d, %ddim ->  %ddim\n",
		   train_data->m,
		   test_data->m,
		   train_data->n,
		   DIM
		);
	t = nv_clock();
	nv_lmca(l, train_data, train_labels, NK, MK, MARGIN, PUSH_RATIO, DELTA, EPOCH);
	printf("-- %ldms\n", nv_clock() - t);
	nv_lmca_projection_all(train_data_lmca, l, train_data);
	
	ok = 0;
	for (i = 0; i < test_data->m; ++i) {
		int knn[NV_TEST_DATA_K] = {0};
		int j, n, max_v, max_i;

		nv_lmca_projection(vec, 0, l, test_data, i);
		
		n = nv_knn(results, KNN_K, train_data_lmca, vec, 0);
		for (j = 0; j < n; ++j) {
			++knn[NV_MAT_VI(train_labels, results[j].index, 0)];
		}
		max_v = max_i= 0;
		for (j = 0; j < NV_TEST_DATA_K; ++j) {
			if (max_v < knn[j]) {
				max_v = knn[j];
				max_i = j;
			}
		}
		if (max_i == NV_MAT_VI(test_labels, i, 0)) {
			++ok;
		}
	}
	printf("Accuracy = %f%% (%d/%d)\n",
		   (float)ok / test_data->m * 100.0f,
		   ok, test_data->m);
	
	nv_matrix_free(&train_data_lmca);
	nv_matrix_free(&vec);
	nv_matrix_free(&l);
	
	fflush(stdout);
}
Пример #9
0
void
nv_bgseg_free(nv_bgseg_t **bg)
{
	if (bg && *bg) {
		nv_matrix_free(&(*bg)->av);
		nv_matrix_free(&(*bg)->sgm);
		nv_free(*bg);
		*bg = NULL;
	}
}
Пример #10
0
void
nv_arow_free(nv_arow_t **arow)
{
	if (arow && *arow) {
		nv_matrix_free(&(*arow)->w);
		nv_matrix_free(&(*arow)->bias);
		nv_free(*arow);
		*arow = NULL;
	}
}
Пример #11
0
void nv_shapecontext_free(nv_shapecontext_t **sctx)
{
	if (*sctx) {
		nv_matrix_free(&(*sctx)->sctx);
		nv_matrix_free(&(*sctx)->tan_angle);
		nv_matrix_free(&(*sctx)->coodinate);
		nv_matrix_free(&(*sctx)->radius);
		nv_free(*sctx);
		*sctx = NULL;
	}
}
Пример #12
0
void nv_mlp_free(nv_mlp_t **mlp)
{
	if (*mlp) {
		nv_matrix_free(&(*mlp)->input_w);
		nv_matrix_free(&(*mlp)->input_bias);
		nv_matrix_free(&(*mlp)->hidden_w);
		nv_matrix_free(&(*mlp)->hidden_bias);
		nv_free(*mlp);
		*mlp = NULL;
	}
}
Пример #13
0
void
nv_plsi_free(nv_plsi_t **p)
{
	if (*p) {
		nv_matrix_free(&(*p)->z);
		nv_matrix_free(&(*p)->dz);
		nv_matrix_free(&(*p)->wz);
		nv_free(*p);
		*p = NULL;
	}
}
Пример #14
0
void 
nv_pstable_free(nv_pstable_t **ps)
{
	if (ps != NULL && *ps != NULL) {
		nv_matrix_free(&(*ps)->a);
		nv_matrix_free(&(*ps)->b);
		nv_matrix_free(&(*ps)->r1);
		nv_matrix_free(&(*ps)->r2);
		nv_free(*ps);
		*ps = NULL;
	}
}
Пример #15
0
	void
	clear_vq_table(void)
	{
		if (m_vq_table[0]) {
			nv_matrix_free(&m_vq_table[0]);
			m_vq_table[0] = NULL;
		}
		if (m_vq_table[1]) {
			nv_matrix_free(&m_vq_table[1]);
			m_vq_table[1] = NULL;
		}
	}
Пример #16
0
	void
	extract_dense(nv_matrix_t *vlad, int j,
				  const nv_matrix_t *image,
				  nv_keypoint_dense_t *dense,
				  int ndense
		)
	{
		NV_ASSERT(vlad->n == DIM);
		int desc_m;
		nv_matrix_t *key_vec;
		nv_matrix_t *desc_vec;
		nv_matrix_t *resize, *gray, *smooth;
		
		int i;
		int km = 0;

		if (m_fit_area == 0) {
			float scale = IMG_SIZE() / (float)NV_MAX(image->rows, image->cols);	
			resize = nv_matrix3d_alloc(3, (int)(image->rows * scale),
									   (int)(image->cols * scale));
		} else {
			float axis_ratio = (float)image->rows / image->cols;
			int new_cols = (int)sqrtf(m_fit_area / axis_ratio);
			int new_rows = (int)((float)m_fit_area / new_cols);
			resize = nv_matrix3d_alloc(3, new_rows, new_cols);
		}
		gray = nv_matrix3d_alloc(1, resize->rows, resize->cols);
		smooth = nv_matrix3d_alloc(1, resize->rows, resize->cols);
		
		for (i = 0; i < ndense; ++i) {
			km += dense[i].rows * dense[i].cols;
		}
		km *= 2;
		key_vec = nv_matrix_alloc(NV_KEYPOINT_KEYPOINT_N, km);
		desc_vec = nv_matrix_alloc(NV_KEYPOINT_DESC_N, km);
		nv_resize(resize, image);
		nv_gray(gray, resize);
		nv_gaussian5x5(smooth, 0, gray, 0);
		
		nv_matrix_zero(desc_vec);
		nv_matrix_zero(key_vec);
		
		desc_m = nv_keypoint_dense_ex(m_ctx, key_vec, desc_vec, smooth, 0,
									  dense, ndense);
		feature_vector(vlad, j, key_vec, desc_vec, desc_m);
		
		nv_matrix_free(&gray);
		nv_matrix_free(&resize);
		nv_matrix_free(&smooth);
		nv_matrix_free(&key_vec);
		nv_matrix_free(&desc_vec);
	}
Пример #17
0
int
main(void)
{
	nv_matrix_t *data = nv_load_matrix_bin("train_data.mat");
	nv_matrix_t *labels = nv_load_matrix_bin("train_labels.mat");
	nv_matrix_t *test_data = nv_load_matrix_bin("test_data.mat");
	nv_matrix_t *test_labels = nv_load_matrix_bin("test_labels.mat");
	int i, ok;
	int k = 0;
	nv_lr_t *lr;
	
	printf("train: %d, %ddim\ntest: %d\n",
		   data->m,
		   data->n,
		   test_data->m
		);
	ok = 0;

	for (i = 0; i < labels->m; ++i) {
		k = NV_MAX(k, NV_MAT_VI(labels, i, 0));
	}
	k += 1;
	lr = nv_lr_alloc(data->n, k);
	nv_lr_progress(1);
	nv_lr_init(lr, data);
	nv_lr_train(lr,
				data, labels,
				NV_LR_PARAM(300, 0.0001f, NV_LR_REG_L2, 0.01f, 0));
				//NV_LR_PARAM(100, 0.1e-10f, NV_LR_REG_L2, 0.01, 0));				
	ok = 0;
	for (i = 0; i < test_data->m; ++i) {
		if (nv_lr_predict_label(lr, test_data, i) == NV_MAT_VI(test_labels, i, 0)) {
			++ok;
		}
	}
	printf("Accuracy = %f%% (%d/%d)\n",
		   (float)ok / test_data->m * 100.0f,
		   ok, test_data->m);
	nv_matrix_free(&data);
	nv_matrix_free(&labels);
	nv_matrix_free(&test_data);
	nv_matrix_free(&test_labels);
	nv_lr_free(&lr);
	
	fflush(stdout);

	return 0;
}
Пример #18
0
int nv_mlp_predict_label(const nv_mlp_t *mlp, const nv_matrix_t *x, int xm)
{
	int m;
	int label = -1;
	float max_output = -FLT_MAX;
	nv_matrix_t *input_y = nv_matrix_alloc(mlp->input_w->m, 1);
	float dropout_scale = 1.0f - mlp->dropout;
	float noise_scale = 1.0f - mlp->noise;

#ifdef _OPENMP
#pragma omp parallel for
#endif
	for (m = 0; m < mlp->hidden; ++m) {
		float y = NV_MAT_V(mlp->input_bias, m, 0) * NV_MLP_BIAS;
		y += nv_vector_dot(x, xm, mlp->input_w, m) * noise_scale;
		NV_MAT_V(input_y, 0, m) = nv_mlp_sigmoid(y) * dropout_scale;
	}
	for (m = 0; m < mlp->output; ++m) {
		float y = NV_MAT_V(mlp->hidden_bias, m, 0) * NV_MLP_BIAS;
		y += nv_vector_dot(input_y, 0, mlp->hidden_w, m);
		if (max_output < y) {
			label = m;
			max_output = y;
		}
	}
	nv_matrix_free(&input_y);

	return label;
}
Пример #19
0
float 
nv_gaussian_log_predict(int npca, const nv_cov_t *cov, const nv_matrix_t *x, int xm)
{
	static const float LOG_PI_0_5_NEGA = -0.5723649f;
	nv_matrix_t *y = nv_matrix_alloc(x->n, 1);
	int n;
	float p;

	NV_ASSERT(npca <= x->n);

	if (npca == 0) {
		npca = (int)(x->n * 0.4f);
	}
	p = LOG_PI_0_5_NEGA * npca;

	nv_vector_sub(y, 0, x, xm, cov->u, 0);
	for (n = 0; n < npca; ++n) {
		float xv = nv_vector_dot(cov->eigen_vec, n, y, 0);
		float ev = NV_MAT_V(cov->eigen_val, n, 0) * 2.0f;
		p += -(0.5f * logf(ev)) - (xv * xv) / (ev);
	}
	nv_matrix_free(&y);

	return p;
}
Пример #20
0
/* この関数は次元が高いと確率が小さくなりすぎて数値計算できないので
 * 次元が高い場合は, この関数値の対数であるnv_gaussian_log_predictを使うこと.
 */
float 
nv_gaussian_predict(const nv_cov_t *cov, const nv_matrix_t *x, int xm)
{
	int n;
	nv_matrix_t *y = nv_matrix_alloc(x->n, 2);
	float p = 1.0f;
	float d = (float)x->n;
	float delta2 = 0.0f;
	float lambda = 1.0f;

	nv_vector_sub(y, 0, x, xm, cov->u, 0);
	nv_matrix_mulv(y, 1, cov->eigen_vec, NV_MAT_TR, y, 0);
	for (n = 0; n < x->n; ++n) {
		float ev = NV_MAT_V(cov->eigen_val, n, 0);
		float xv = NV_MAT_V(y, 1, n);
		if (ev > 0.0f) {
			delta2 += (xv * xv) / ev;
			lambda *= sqrtf(ev);
		}
	}
	p = (1.0f / powf(2.0f * NV_PI, d / 2.0f)) * (1.0f / lambda) * expf(-0.5f * delta2);

	nv_matrix_free(&y);

	return p;
}
Пример #21
0
static void
nv_test_klr_tree_inherit(const nv_matrix_t *data,const nv_matrix_t *labels,
						 nv_klr_tree_t *tree, const nv_klr_tree_t *base)
{
	nv_matrix_t *cluster_labels = nv_matrix_alloc(1, data->m);
	float purity;
	int i;

	NV_TEST_NAME;
	printf("tree: ");
	for (i = 0; i < tree->height; ++i) {
		if (i != 0) {
			printf(", ");
		}
		printf("%d", tree->dim[i]);
	}
	printf("\n");

	if (base == NULL) {
		nv_klr_tree_train(tree, data, NV_LR_PARAM(4, 0.1f, NV_LR_REG_L1, 0.0001f, 1), 100);
	} else {
		nv_klr_tree_inherit_train(tree, base,
								  data, NV_LR_PARAM(4, 0.1f, NV_LR_REG_L1, 0.0001f, 1), 100);
	}
	for (i = 0; i < data->m; ++i) {
		NV_MAT_V(cluster_labels, i, 0) = (float)nv_klr_tree_predict_label(tree, data, i);
	}
	purity = nv_purity(K, NV_TEST_DATA_K, cluster_labels, labels);
	printf("purity: %f\n", purity);
	NV_ASSERT(purity > 0.5f);
	
	nv_matrix_free(&cluster_labels);
}
Пример #22
0
static void nv_test_klr_tree_ex(const nv_matrix_t *data, const nv_matrix_t *labels,
								int *width, int height)
{
	nv_matrix_t *cluster_labels = nv_matrix_alloc(1, data->m);
	nv_klr_tree_t *tree = nv_klr_tree_alloc(data->n, width, height);
	float purity;
	int i;

	NV_TEST_NAME;
	printf("tree: ");
	for (i = 0; i < height; ++i) {
		if (i != 0) {
			printf(", ");
		}
		printf("%d", width[i]);
	}
	printf("\n");

	nv_klr_tree_train(tree, data, NV_LR_PARAM(4, 0.1f, NV_LR_REG_L1, 0.0001f, 1), 100);
	for (i = 0; i < data->m; ++i) {
		NV_MAT_V(cluster_labels, i, 0) = (float)nv_klr_tree_predict_label(tree, data, i);
	}
	purity = nv_purity(K, NV_TEST_DATA_K, cluster_labels, labels);
	printf("purity: %f\n", purity);
	NV_ASSERT(purity > 0.5f);
	
	nv_klr_tree_free(&tree);
	nv_matrix_free(&cluster_labels);
}
Пример #23
0
void
kmeans(nv_matrix_t *centroids,
	   const nv_matrix_t *data)
{
	nv_matrix_t *cluster_labels = nv_matrix_alloc(1, data->m);
	nv_matrix_t *count = nv_matrix_alloc(1, CENTROIDS);

	nv_matrix_zero(count);
	nv_matrix_zero(centroids);
	nv_matrix_zero(cluster_labels);
	
	nv_kmeans_progress(1);
	nv_kmeans(centroids, count, cluster_labels, data, CENTROIDS, 50);

	nv_matrix_free(&cluster_labels);
	nv_matrix_free(&count);
}
Пример #24
0
void
nv_pa_free(nv_pa_t **pa)
{
	if (pa && *pa) {
		nv_matrix_free(&(*pa)->w);
		nv_free(*pa);
		*pa = NULL;
	}
}
Пример #25
0
void
otama_image_free(otama_image_t **image)
{
	if (image && *image) {
		nv_matrix_free(&(*image)->image);
		nv_free(*image);
		*image = NULL;
	}
}
Пример #26
0
void 
nv_lr_free(nv_lr_t **lr)
{
	if (*lr) {
		nv_matrix_free(&(*lr)->w);
		nv_free(*lr);
		*lr = NULL;
	}
}
Пример #27
0
/*
 * 45°回転したIntegral Image
 */
void 
nv_integral_tilted(nv_matrix_t *integral,
				   const nv_matrix_t *img, int channel)
{
	int row, col, scol, srow;
	int erow = img->rows + 1;
	int ecol = img->cols + 1;
	nv_matrix_t *prev_tilted = nv_matrix_alloc(img->cols + 1, 1);

	NV_ASSERT(
		integral->rows - 1 == img->rows 
		&& integral->cols - 1 == img->cols
	);

	nv_matrix_zero(prev_tilted);
	nv_matrix_zero(integral);

	for (scol = img->cols; scol > 0; --scol) {
		float tilted_sum = 0.0f;
		for (row = 1, col = scol; row < erow && col < ecol; ++row, ++col) {
			float tilted_val = NV_MAT3D_V(img, row - 1, col - 1, channel);
			if (col + 1 == ecol) {
				NV_MAT3D_V(integral, row, col, 0) = 
					NV_MAT3D_V(integral, row - 1, col, 0)
					+ tilted_sum + tilted_val;
			} else {
				NV_MAT3D_V(integral, row, col, 0) = 
					NV_MAT3D_V(integral, row - 1, col + 1, 0) 
					+ NV_MAT_V(prev_tilted, 0, col)
					+ tilted_sum + tilted_val;
			}
			tilted_sum += tilted_val;
			NV_MAT_V(prev_tilted, 0, col) = tilted_sum;
		}
	}
	for (srow = 2; srow < erow; ++srow) {
		float tilted_sum = 0.0f;
		for (row = srow, col = 1; row < erow && col < ecol; ++row, ++col) {
			float tilted_val = NV_MAT3D_V(img, row - 1, col - 1, channel);
			if (col + 1 == ecol) {
				NV_MAT3D_V(integral, row, col, 0) = 
					NV_MAT3D_V(integral, row - 1, col, 0)
					+ tilted_sum + tilted_val;
			} else {
				NV_MAT3D_V(integral, row, col, 0) = 
					NV_MAT3D_V(integral, row - 1, col + 1, 0) 
					+ NV_MAT_V(prev_tilted, 0, col)
					+ tilted_sum + tilted_val;
			}
			tilted_sum += tilted_val;
			NV_MAT_V(prev_tilted, 0, col) = tilted_sum;
		}
	}

	nv_matrix_free(&prev_tilted);
}
Пример #28
0
void
patch_sampling(nv_matrix_t *samples, std::vector<fileinfo_t> &list)
{
	nv_matrix_t *data = nv_matrix_alloc(PATCH_SIZE * PATCH_SIZE * 3,
										(int)((IMG_SIZE-PATCH_SIZE) * (IMG_SIZE-PATCH_SIZE) * list.size()));
	int data_index = 0;
	int i;
	
	nv_matrix_zero(data);
#ifdef _OPENMP
#pragma omp parallel for schedule(dynamic, 1)
#endif
	for (i = 0; i < (int)list.size(); ++i) {
		nv_matrix_t *src;
		nv_matrix_t *patches;
		src = nv_load_image(list[i].file.c_str());
		if (!src) {
			fprintf(stderr, "open filed: %s\n", list[i].file.c_str());
			exit(-1);
		}
		
		patches = nv_patch_matrix_alloc(src, PATCH_SIZE);
		nv_patch_extract(patches, src, PATCH_SIZE);
		
#ifdef _OPENMP
#pragma omp critical (patch_sampling)
#endif
		{
			int j;
			for (j = 0; j < patches->m; ++j) {
				nv_vector_copy(data, data_index, patches, j);
				data_index += 1;
			}
		}
		
		nv_matrix_free(&src);
		nv_matrix_free(&patches);
	}
	nv_vector_shuffle(data);
	nv_matrix_m(data, NV_MIN(samples->m, data_index));
	nv_matrix_copy_all(samples, data);
	nv_matrix_free(&data);
}
Пример #29
0
void
nv_bgseg_update(nv_bgseg_t *bg, nv_matrix_t *mask, const nv_matrix_t *frame)
{
	nv_matrix_t *tmp, *tmp2, *mask_vec, *lower, *upper;
	
	NV_ASSERT(bg->init_2nd_finished == 1);

	tmp = conv_image2vec(bg, frame);
	lower = nv_matrix_clone(tmp);
	upper = nv_matrix_clone(tmp);
	mask_vec = nv_matrix_clone(tmp);
	tmp2 = nv_matrix_dup(tmp);
	
	// range
	nv_vector_sub(lower, 0, bg->av, 0, bg->sgm, 0);
	nv_vector_subs(lower, 0, lower, 0, bg->zeta);
	nv_vector_add(upper, 0, bg->av, 0, bg->sgm, 0);
	nv_vector_adds(upper, 0, upper, 0, bg->zeta);
	nv_vector_in_range10(mask_vec, 0, lower, 0, upper, 0, tmp, 0);
	
	// update amplitude
	nv_vector_sub(tmp, 0, tmp, 0, bg->av, 0);
	nv_vector_mul(tmp,  0, tmp, 0, tmp, 0);
	nv_vector_muls(tmp, 0, tmp, 0, 2.0f);
	nv_vector_sqrt(tmp, 0, tmp, 0);
	
	// update background
	running_avg(bg->av, tmp2, bg->bg_v, mask_vec, 1.0f);
	running_avg(bg->sgm, tmp, bg->bg_v, mask_vec, 1.0f);
	
	// update foreground
	running_avg(bg->sgm, tmp, bg->fg_v, mask_vec, 0.0f);
	
	conv_mask(bg, mask, mask_vec);
	
	nv_matrix_free(&tmp);
	nv_matrix_free(&mask_vec);
	nv_matrix_free(&tmp2);
	nv_matrix_free(&upper);
	nv_matrix_free(&lower);
}
Пример #30
0
	inline char *
	serialize(const nv_matrix_t *vlad, int j)
	{
		nv_matrix_t *mat = nv_matrix_alloc(vlad->n, 1);
		char *s;
		
		nv_vector_copy(mat, 0, vlad, j);
		s = nv_serialize_matrix(mat);
		nv_matrix_free(&mat);
		
		return s;
	}