我在Android项目中导入了OpenCV。我试图找到一些包含轮廓的数字然后在其上计算HOG以进行分类。我现在面临的主要问题。在visual studio Python中。我能够获得一些数字但是在Android环境中我只得到0x1描述符数组,这似乎是空的。
我的图片尺寸=(24,54)
Python中的代码有效:
winSize = (5,5)
blockSize = (5,5)
blockStride = (5,5)
cellSize = (5,5)
nbins = 9
derivAperture = 1
winSigma = -1.
histogramNormType = 0
L2HysThreshold = 0.2
gammaCorrection = 1
nlevels = 16
signedGradients = True
hog = cv2.HOGDescriptor(winSize,blockSize,blockStride,cellSize,nbins,derivAperture,winSigma,\
histogramNormType,L2HysThreshold,gammaCorrection,nlevels)
# img variable is thresholded img that contains only number, and hist value after computing is (360,1)
hist=hog.compute(img)
但是在Java中,
private void createHOGDescriptor()
{
Size winSize = new Size(5,5);
Size blockSize = new Size(5,5);
Size blockStrinde = new Size(5,5);
Size cellSize = new Size(5,5);
int nbins = 9;
int derivAperture = 9;
int winSigma = -1;
int histogNormType = 0;
double L2HysThreshold = 0.2;
boolean gammaCorrection = true;
int nLevels = 16;
boolean signedGradients = true;
hog = new HOGDescriptor(winSize, blockSize, blockStrinde, cellSize, nbins,derivAperture, winSigma, histogNormType, L2HysThreshold, gammaCorrection,nLevels,signedGradients);
}
我创建HOG描述符然后将hog传递给另一个必须使用hog功能的代码部分。
for (int i = 0; i < contours.size(); i++)
{
MatOfPoint contour = new MatOfPoint(contours.get(i).toArray());
Rect rect = Imgproc.boundingRect(contour);
double area = Imgproc.contourArea(contour);
double ratio = rect.height*1.0 / rect.width;
if (rect.height > rect.width && area > 50 && ratio >= 1.2 && ratio <= 2){
roiMat = img.submat(rect);
Imgproc.cvtColor(roiMat,roiMat, Imgproc.COLOR_BGR2GRAY);
Imgproc.threshold(roiMat, roiMat, 127, 255, Imgproc.THRESH_OTSU);
Imgproc.resize(roiMat,roiMat, new Size(54,24));
hogDescriptor.compute(roiMat, descriptors);
String a = "" + rect.width + " " + rect.height + " " + descriptors.size();
Imgproc.putText(img, a,new Point(rect.x, rect.y), 3, 0.5,new Scalar(255,0,0,255),2);
Imgproc.rectangle(img, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0,255,0));
//Log.println(descriptors.size());
}
}
上面的代码没有那么复杂的东西找到轮廓及其位置然后调整找到的轮廓。然后运行hog.compute但它总是为零。那我怎么解决这个问题呢?