opencv实现PCA的一个程序,输出的特征值有问题,指点下问题出在哪??
#include"stdafx.h"#include<stdio.h>#include<cv.h>#include<highgui.h>voidmain(){floatV...
#include "stdafx.h"
#include <stdio.h>
#include <cv.h>
#include <highgui.h>
void main()
{float Vector[10][10000];
char fileName[40];
int img_num;
int start;
for(img_num=0;img_num<10;img_num++)
{sprintf(fileName, "test%d.jpg", img_num+1);
IplImage *img_src = cvLoadImage(fileName, CV_LOAD_IMAGE_GRAYSCALE);
IplImage *img_gray = cvCreateImage(cvSize(100,100), 8, 1);
cvResize(img_src, img_gray);
CvMat *Mat_tmp = cvCreateMat(img_gray->height, img_gray->width, CV_32FC1);
cvConvert(img_gray, Mat_tmp);
start = 0;
for(int i=0;i<Mat_tmp->cols;i++)
{for(int j=0;j<Mat_tmp->rows;j++)
{double value = cvmGet(Mat_tmp, i, j);
Vector[img_num][start] = value;
start++;}}
cvReleaseImage(&img_src);
cvReleaseImage(&img_gray);
cvReleaseMat(&Mat_tmp);}
CvMat data;
cvInitMatHeader( &data, 10, 100*100, CV_32FC1, Vector);
CvMat *pAvgVector = cvCreateMat( 1, 100*100, CV_32FC1);
CvMat *pEigenValue = cvCreateMat( 1, 10, CV_32FC1);
CvMat *pEigenVector = cvCreateMat( 10, 100*100, CV_32FC1);
cvCalcPCA(&data, pAvgVector, pEigenValue, pEigenVector, CV_PCA_DATA_AS_ROW);
for(int i=0; i<10; i++)
{printf("%f\n", cvmGet(pEigenValue, 0, i));}
CvMat *mat = cvCreateMat(100, 100, CV_32FC1);
start = 0;
for(int i=0; i<100; i++)
{for(int j=0; j<100; j++){
cvmSet(mat, i, j, cvmGet(pEigenVector, 0, start));
start++;}}
cvNormalize(mat, mat, 255);
cvShowImage("1", mat);
cvWaitKey(0);
cvReleaseMat(&mat);
cvReleaseMat(&pAvgVector);
cvReleaseMat(&pEigenValue);
cvReleaseMat(&pEigenVector);
} 展开
#include <stdio.h>
#include <cv.h>
#include <highgui.h>
void main()
{float Vector[10][10000];
char fileName[40];
int img_num;
int start;
for(img_num=0;img_num<10;img_num++)
{sprintf(fileName, "test%d.jpg", img_num+1);
IplImage *img_src = cvLoadImage(fileName, CV_LOAD_IMAGE_GRAYSCALE);
IplImage *img_gray = cvCreateImage(cvSize(100,100), 8, 1);
cvResize(img_src, img_gray);
CvMat *Mat_tmp = cvCreateMat(img_gray->height, img_gray->width, CV_32FC1);
cvConvert(img_gray, Mat_tmp);
start = 0;
for(int i=0;i<Mat_tmp->cols;i++)
{for(int j=0;j<Mat_tmp->rows;j++)
{double value = cvmGet(Mat_tmp, i, j);
Vector[img_num][start] = value;
start++;}}
cvReleaseImage(&img_src);
cvReleaseImage(&img_gray);
cvReleaseMat(&Mat_tmp);}
CvMat data;
cvInitMatHeader( &data, 10, 100*100, CV_32FC1, Vector);
CvMat *pAvgVector = cvCreateMat( 1, 100*100, CV_32FC1);
CvMat *pEigenValue = cvCreateMat( 1, 10, CV_32FC1);
CvMat *pEigenVector = cvCreateMat( 10, 100*100, CV_32FC1);
cvCalcPCA(&data, pAvgVector, pEigenValue, pEigenVector, CV_PCA_DATA_AS_ROW);
for(int i=0; i<10; i++)
{printf("%f\n", cvmGet(pEigenValue, 0, i));}
CvMat *mat = cvCreateMat(100, 100, CV_32FC1);
start = 0;
for(int i=0; i<100; i++)
{for(int j=0; j<100; j++){
cvmSet(mat, i, j, cvmGet(pEigenVector, 0, start));
start++;}}
cvNormalize(mat, mat, 255);
cvShowImage("1", mat);
cvWaitKey(0);
cvReleaseMat(&mat);
cvReleaseMat(&pAvgVector);
cvReleaseMat(&pEigenValue);
cvReleaseMat(&pEigenVector);
} 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询