gpt4 book ai didi

c - OpenCV 和 C,cvPreprocessCategoricalResponses 中的错误参数(响应 #0 不是整数)

转载 作者:行者123 更新时间:2023-11-30 16:57:24 26 4
gpt4 key购买 nike

大家好,我在运行程序时遇到了这个错误。

我用这个函数创建了一个浮点值矩阵

void calcolaSubpixel(IplImage *GT, costi ** totalCostMatrix, float ** subpixelM, int h, int w){
int modulo;
int subpixel;
int i,j;
for (i = 0; i < h; i++) {
for (j = 0; j < w; j++) {
//TODO i valori ottneuti vanno 0-1, io li voglio da -0.5,0.5
int modulo = (CV_IMAGE_ELEM(GT, ushort, i, j))%256;
float subpixel01 = modulo/256.0f;
subpixelM[i][j] = subpixel01;
}
}
}

然后我用这个函数将它们放入一个 float 组中

void fillLabels( IplImage *GT, float** subpixelM, float * labels){
int h = GT->height, w = GT->width;
int i,j,L = 0;

for (i = 0; i < h; i++) {
for (j = 0; j < w; j++) {
//se il pixel in esame della ground truth non ha intensita 0 lo considero,
//altrimenti lo inserisco nella label:
if (CV_IMAGE_ELEM(GT, ushort, i, j) != 0) {
labels[L] = subpixelM[i][j];
L++;
}
}
}
}

并将此数组标签用于Mat labelsMat(nRighe, 1, CV_32FC1, labels);,其中nRighe是之前计算的用于教授SVM的值

CvSVM SVM;
SVM.train(trainingDatasMat, labelsMat, Mat(), Mat(), params);

trainingDatasMat 不会给我带来任何麻烦,但是像这样构建的 labelsMat' 返回

OpenCV Error: Bad argument (response #0 is not integral) in cvPreprocessCategoricalResponses, file /home/vision/opencv-2.4.11/modules/ml/src/inner_functions.cpp, line 715 terminate called after throwing an instance of 'cv::Exception' what(): /home/vision/opencv-2.4.11/modules/ml/src/inner_functions.cpp:715: error: (-5) response #0 is not integral in function cvPreprocessCategoricalResponses

有什么建议可以解决这个问题吗?谢谢

这是主要内容

#include "cost.h"
#include "disparity.h"
#include "fixed_window.h"
#include "confidence.h"
#include "utils.h"
#include "stereoPipeline.h"
#include "interpolation.h"
#include "SGM.h"
#include "learning.h"
#include<iostream>
#include<iomanip>
#include<cv.h>

using namespace cv;
using namespace std;


int main(int argc, char *argv[]) {
int dMax=15;
if (argc != 4)
{
printf("Usage: %s <image_id> <gaps> < <algo>\n", argv[0]);
return -1;
}
int id=atoi(argv[1]);

//sistema l'id per evitare problemi con il numero di cifre
char q[10];
if(id < 10){sprintf(q, "00%d", id); }
if(id >= 10 && id <= 99){sprintf(q, "0%d", id); }
if(id >= 100){sprintf(q, "%d", id); }

int gaps=atoi(argv[2]);
int algo=atoi(argv[3]);

IplImage *L, *R, *GT;
// ***********************************************************
// ***********************************************************
// ****** STEREO ALGORITHM ******
// ***********************************************************
// ***********************************************************
char filenameL[100];
char filenameR[100];
char filenameGT[100];


//carico il nome delle immagini
sprintf(filenameL,"/KITTI/image_0/000%s_10.png",q);
sprintf(filenameR,"/KITTI/image_1/000%s_10.png",q);
sprintf(filenameGT,"/KITTI/disp_occ/000%s_10.png",q);


// load grayscale images
L = cvLoadImage(filenameL, CV_LOAD_IMAGE_GRAYSCALE);
R = cvLoadImage(filenameR, CV_LOAD_IMAGE_GRAYSCALE);
GT= cvLoadImage(filenameGT, CV_LOAD_IMAGE_UNCHANGED );

dMax=kitti_dMax(GT);


int h = L->height, w = L->width;
IplImage* DisparityL = cvCreateImage(cvGetSize(L),8,1);
IplImage* DisparityR = cvCreateImage(cvGetSize(R),8,1);

t_DSI* DSI=create_DSI(w,h,dMax);
t_DSI *boxFilteredDSI=create_DSI(w,h,dMax);
t_DSI* outDSI=create_DSI(w,h,dMax);


// point-wise cost (Absolute Difference or Hamming distance on Census transforms)
if (algo == 0)
AbsoluteDifferenceCost(L, R, dMax, DSI);
else
HammingDistanceCost(L, R, dMax, 5, DSI);

// TAD aggregation
BoxFiltering(DSI, boxFilteredDSI, 5, 100);

disparity_map(boxFilteredDSI,DisparityL, true);
disparity_map_R(boxFilteredDSI,DisparityR, true);
// show results
/*cvShowImage("Left",L);
cvShowImage("FW Left",DisparityL);
cvShowImage("FW Right",DisparityR);
cvWaitKey(0);*/

//matrice disparità
int **disparityMatrix;
disparityMatrix= (int **) calloc(h,sizeof(int *));
int z=0;
for (z;z<h;z++){
disparityMatrix[z]=(int *) calloc(w,sizeof (int ));
}

//matrice dei costi
costi ** totalCostMatrix;
totalCostMatrix= (costi **) calloc(h,sizeof(costi *));
for (z=0;z<h;z++){
totalCostMatrix[z]=(costi *) calloc(w,sizeof (costi ));
}


SGM(boxFilteredDSI, outDSI, dMax, 30, 300, 255);
disparity_map(outDSI,DisparityL, true);
disparity_map_R(outDSI,DisparityR, true);

//findtotalcost riempie la matrice ---->> test con outdSI usata per SGM
findTotalCost(outDSI,totalCostMatrix);

int nRighe = linesLength(GT);
printf ("\n\n%d\n", nRighe);
float * labels;
labels = (float *) calloc(nRighe, sizeof(float));
float ** trainingDatas;
trainingDatas = (float **) calloc(nRighe, sizeof(float *));
for (z=0;z<nRighe;z++){
trainingDatas[z] = (float *) calloc(3, sizeof(float));
}

//matricisubpixels
float ** subpixelM;
subpixelM= (float **) calloc(h,sizeof(float *));
for (z=0;z<h;z++){
subpixelM[z]=(float *) calloc(w,sizeof (float ));
}

//riempio i vettori labels e traingDatas
//labels -> valori della GT
//traingDatas -> i costi
calcolaSubpixel(GT, totalCostMatrix, subpixelM, h, w);
fillLabels(GT, subpixelM, labels);
fillTrainingdatas(GT, totalCostMatrix, trainingDatas);



//test
labels[5] = 1.0;
//trainingDatas[0][0] = 1;
//trainingDatas[0][1] = 1;
//trainingDatas[0][2] = 1;


//imposto le label e i dati di training per effettuare il learning
Mat labelsMat(nRighe, 1, CV_32FC1, labels);
Mat trainingDatasMat(nRighe, 3, CV_32FC1, trainingDatas);

// Set up SVM's parameters
CvSVMParams params;
params.svm_type = CvSVM::C_SVC;
params.kernel_type = CvSVM::LINEAR;
params.term_crit = cvTermCriteria(CV_TERMCRIT_ITER, 100, 1e-6);

// Train the SVM
CvSVM SVM;
SVM.train(trainingDatasMat, labelsMat, Mat(), Mat(), params);

最佳答案

通过 SVM 的这些设置,labelsMat 中的所有值都必须是整数。一个可能的解决方案是从 calcolaSubpixel 中删除以下行。

float subpixel01 = modulo/256.0f;

但是,您的代码还可能存在其他问题。特别是,如果您想使用数据指针初始化 cv::Mat,则该指针必须是指向所需基础类型的连续内存的指针(考虑维度和步长)。在您的代码中,数据指针设置为 float* 指针数组(trainingDatas 是 float**)。正确的方法是将 float*(指向 nRighe*3 浮点)传递给 trainingDatasMat 的构造函数。

话虽这么说,一种更简单的方法是允许 cv::Mat 为您进行内存管理。大致如下:

Mat trainingDatasMat(nRighe, 3, CV_32FC1);
fillTrainingdatas(GT, totalCostMatrix, (float*) trainingDatasMat.data);

关于c - OpenCV 和 C,cvPreprocessCategoricalResponses 中的错误参数(响应 #0 不是整数),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39550399/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com