我有以下代码:
´
[...]cvCvtColor(subtraction, grayBuffer, CV_BGR2GRAY);
cvThreshold(grayBuffer, grayBuffer, 127, 255, CV_THRESH_BINARY);
cvFindContours(grayBuffer, mem, contours, Loader.sizeof(CvContour.class) , CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0));
//cvFitLine(Mat(contours[0]),lines,2,0,0.01,0.01);
cvFitLine(contours, CV_DIST_L2, 2, 0.01, 0.01, line);
int lefty = (int)((int)(-line[2]*line[1]/line[0])+line[3]);
int righty = (int) ((int)((grayBuffer.width()-line[2])*line[1]/line[0])+line[3]);
line(grayBuffer.width()-1,0,righty,lefty); [...]´
,这给了我一个致命错误。
有人知道如何使用fitLine吗?
谢谢:)
下面是一个使用cvFitLine(为Processing语言编写,它提供setup()和exit()方法)的javacv程序:
import com.googlecode.javacv.*;
import com.googlecode.javacv.cpp.*;
import com.googlecode.javacpp.IntPointer;
final int numPoints = 10;
int pt2[] = new int[numPoints*2], x = 0, y = 1;
float testPar[] = new float[4];// to store the results
void setup() {
for (int i = 0; i < numPoints; i++) {
pt2[ i*2 ] = i * 5; pt2[ i*2 + 1 ] = i * 5;
}
opencv_core.CvMat point_mat = opencv_core.cvMat(1, numPoints, opencv_core.CV_32SC2, new IntPointer(pt2));
opencv_imgproc.cvFitLine(point_mat, opencv_imgproc.CV_DIST_HUBER, 0, 0.01, 0.01,testPar);
for (int i = 0; i < testPar.length; i++)
println("i: " + i + ", testPar: " + testPar[i]);
exit();
}
输出:i: 0, testPar: 0.70710677
i: 1, testPar: 0.70710677
i: 2, testPar: 22.5
i: 3, testPar: 22.5