Android HOGDescriptor return a 0x1 descriptor array

asked 2018-06-08 13:07:30 -0600

updated 2018-06-08 13:32:53 -0600

berak gravatar image

Hi everyone,

I have imported opencv in my android project. I am trying to find some number containg contours then compute HOG on it for classification. Main problem I am facing now. In visual studio python. I was able to get some numbers but in android environment I only get 0x1 descriptor array which seems empty.

My image size is = (24,54)

The code in python that works:

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)

But in 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);
}

I create HOG descriptor then passed hog to another code part that have to use hog feature.

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());
    }
}

The code above does not that much complicated things finds the contours and its positions then resize the found contour. So how can i solve this problem ? This is very very important for me.

edit retag flag offensive close merge delete