Ask Your Question

Nani's profile - activity

2017-09-11 04:27:19 -0600 commented answer svm.save() is not working opencv4android

According to the discussion above, saved model using 2.4 is not compatible with 3.2. My solution was to do everything in

2017-09-08 06:31:36 -0600 commented question image quality test

@Ziri No, I did not solve this. Do you have any idea?

2017-08-22 09:58:39 -0600 commented question image quality test

I think I can use Hough Circle Transform to detect the circles inside the plate. Shall I count the number of circles afterward?

2017-08-22 07:11:39 -0600 asked a question image quality test

I would like to do an image quality test before process the image with my app. I would like first to identify if the image of specific type for example my app reads Elisa plates with 96 wells. If the user take image of another thing, the app will not allow this. Is this possible using opencv. Any hints or links is appreciated.

2017-07-26 11:11:09 -0600 commented question Strange XML file behaviour

Thank you so much @berak for the support. I knew the problem now: it is because I put the constructor after defining the SVM parameters. It seems I have now good accuracy and I changed the c variables and it makes difference.

2017-07-25 08:17:19 -0600 commented question Strange XML file behaviour

Thanks. I did not know that even the constructor will train the model with out calling the method .train(). The problem why the number of support vector in the first case is 1 while in the second case is 78 which exactly equal to my training data. the other problem in the first case all the predictions will be -1 while on the second case the predictions will be 1.

2017-07-25 07:10:33 -0600 commented question Strange XML file behaviour

Thanks for your comment. I passed them in the constructor but it doesn't work. I explained this in the question.

classifier = new CvSVM(trainingData, classes, new Mat(), new Mat(), params); classifier.save(XML1.toString()); classifier.train(trainingData, classes); classifier.save(XML.toString());

2017-07-25 06:50:55 -0600 asked a question Strange XML file behaviour

I trained the svm classifier using the following code and it gave me wrong classification and when i looked at the saved xml file, i found strange contents. Only one vector is in the xml file and the classification is -1 for all the test data. I found that the classifier will be the same before and after classifier.train(...). Here is the code:

    Log.i(TAG,"Training...");
    v_features.copyTo(trainingData);
    trainingData.convertTo(trainingData, CvType.CV_32F);
    trainingLabels.copyTo(classes);
    classes.convertTo(classes, CvType.CV_32S);
    classes.convertTo(classes, CvType.CV_32S);
    CvSVMParams params = new CvSVMParams();
    //first method working fine
    params.set_svm_type(CvSVM.C_SVC);
    params.set_kernel_type(CvSVM.LINEAR);
    params.set_gamma(0.01);
    params.set_nu(0.5);
    params.set_C(1000);
    TermCriteria criteria = new TermCriteria(TermCriteria.MAX_ITER,100, 1e-6);
    params.set_term_crit(criteria);
    // initialize SVM object to avoid being Null object
    classifier = new CvSVM(trainingData, classes, new Mat(), new Mat(), params);
    classifier.save(XML1.toString());
    classifier.train(trainingData, classes, new Mat(), new Mat(), params);
    classifier.save(XML.toString());

Here the xml file:

<?xml version="1.0"?>
<opencv_storage>
<my_svm type_id="opencv-ml-svm">
<svm_type>C_SVC</svm_type>
<kernel>
<type>LINEAR</type>
</kernel>
<C>1000.</C>
<term_criteria>
<epsilon>2.2204460492503131e-16</epsilon>
<iterations>100</iterations>
</term_criteria>
<var_all>18</var_all>
<var_count>18</var_count>
<class_count>2</class_count>
<class_labels type_id="opencv-matrix">
<rows>1</rows>
<cols>2</cols>
<dt>i</dt>
<data> -1 1</data>
</class_labels>
<sv_total>1</sv_total>
<support_vectors>
<_> -9.17065036e-18 7.35845397e-18 -5.99237967e-18 4.50786054e-18 1.91758168e-18 1.38351872e-18 -3.33754597e-18 1.15368551e-17 -2.76280041e-18 3.05637369e-19 -6.48722237e-19 -2.11365533e-18 -1.00750228e-10 3.54062057e-10 1.56639299e-10 0. 3.43815959e-14 -2.05199830e-14</_>
</support_vectors>
-<decision_functions>
-<_>
<sv_count>1</sv_count>
<rho>-4.7668412450595643e-01</rho>
<alpha> 1.</alpha>
<index> 0</index>
</_>
</decision_functions>
</my_svm>
</opencv_storage>

while when I change the line  classifier.train(trainingData, classes, new Mat(), new Mat(), params); to  classifier.train(trainingData, classes);, the classifier will take the default values instead of the assigned parameter and the decision function will be 1.
here is the xml file:
<?xml version="1.0"?>
-<opencv_storage>
-<my_svm type_id="opencv-ml-svm">
<svm_type>C_SVC</svm_type>
-<kernel>
<type>RBF</type>
<gamma>1.</gamma>
</kernel>
<C>1.</C>
-<term_criteria>
<epsilon>1.1920928955078125e-07</epsilon>
<iterations>1000</iterations>
</term_criteria>
<var_all>18</var_all>
<var_count>18</var_count>
<class_count>2</class_count>
-<class_labels type_id="opencv-matrix">
<rows>1</rows>
<cols>2</cols>
<dt>i</dt>
<data> -1 1</data>
</class_labels>
<sv_total>78</sv_total>
-<support_vectors>
<_> 3.82838593e+01 7.11920929e+00 -4.67479944e+00 1.22517595e+01 3.15141797e+00 2.35791826e+00 4.94117661e+01 13. -1. -9.08270061e-01 -1.86607766e+00 -1.55849302e+00 1.53999995e+10 531000000. 229000000. 0. 4.85476406e+04 -2.60734746e+04</_>

<_> 3.96895714e+01 9.19926834e+00 -3.27970743e+00 1.25011663e+01 3.11253858e+00 1.48126924e+00 4.98039207e+01 14. -1. -8.09072375e-01 -1.54238451e+00 -1.53902304e+00 471000000. 25300000. 3218436. 0. 1.16598379e+04 -2.38160229e+03</_>

<_> 3.66479874e+01 9.63044548e+00 -8.07036877e+00 8.72248554e+00 2.52424383e+00 2.28012586e+00 4.86274529e+01 14. -4. -1.37340033e ...
(more)
2017-07-24 08:20:29 -0600 commented question SVM cross validation parameters optimisation and accuracy

Sorry for any inconvenience I caused. I will delete my comment as it seems inappropriate.

2017-07-20 07:15:35 -0600 commented question SVM cross validation parameters optimisation and accuracy

Thanks, we will try this.

2017-07-20 04:43:03 -0600 commented question SVM cross validation parameters optimisation and accuracy

The problem is that my colleague who work on matlab get nearly 97% accuracy with this data. Is the problem with the opencv and android as they are open source or other criteria. however, my app is a diagnosis app which give result of a disease based on analyzing colors in image.

2017-07-20 03:12:42 -0600 commented question SVM cross validation parameters optimisation and accuracy

Thanks @StevenPuttemans, I will try to increase my data but my question is can I get at 90% accuracy with this data as we need it for real testing or it will be difficult?.

2017-07-18 09:23:03 -0600 commented question SVM cross validation parameters optimisation and accuracy

Thanks @StevenPuttemans for your comment. I have 88 record with 40 negative images and 48 positive images. I suppose this due to the parameters as I have all the predictions of the test dataset 1.

2017-07-18 08:31:05 -0600 marked best answer find contours miss some objects

I use the following code to read through all objects that I segmented from my image which should be ordered in row and columns as semi-circle (because of segmentation and morphological processing for reducing noise):

    Imgproc.Canny(srcImg, srcImg, 50, 150);
    Imgcodecs.imwrite("/mnt/sdcard/DCIM/cannyImg.jpg", srcImg);//check

    List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
    Mat hierarchy = new Mat();
    Imgproc.findContours(srcImg, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE, new Point(0,0));

    //int index = 0;
    //double maximc = Imgproc.contourArea(contours.get(0));
    for (int contourIdx = 1; contourIdx < contours.size(); contourIdx++) {
        double temp;
        temp = Imgproc.contourArea(contours.get(contourIdx));
        if (temp > 100) { 
                           // condition to differentiate between noise and objects
            Mat drawing = Mat.zeros(srcImg.size(), CvType.CV_8UC1);
            Imgproc.drawContours(drawing, contours, contourIdx, new Scalar(255), -1);

            Mat resultMat = new Mat();
            maskImg.copyTo(resultMat, drawing);
            Imgcodecs.imwrite("/mnt/sdcard/DCIM/resultImg" + contourIdx + ".jpg", resultMat);//check
         }   
    }

however, the loop can not read important objects in my image even canny image is correct and can identify all the objects. My questions are: in which order find contours read objects? and also is there another way in Opencv to read through all objects in the image other than find contours? last question i used the size of contours to differentiate between the objects and the noise, so is this Ok or you can suggest other methods.

Attached is the result image and the one with red circles correctly identified while the others which I need not being identified.

Any help is appreciated

2017-07-18 08:30:59 -0600 received badge  Self-Learner (source)
2017-07-17 06:52:28 -0600 asked a question SVM cross validation parameters optimisation and accuracy

I use the following code to train the svm using k-fold cross-validation but the prediction accuracy is low. What I am doing wrong and how to programmatically calculate the accuracy of the classifier using cross-validation.

Log.i(TAG,"Training..."); params.set_svm_type(CvSVM.C_SVC); params.set_kernel_type(CvSVM.RBF); params.set_C(1.0); params.set_degree(0.0); params.set_coef0(0.0); params.set_gamma(1.0); params.set_term_crit(new TermCriteria(TermCriteria.EPS, 10000, 1e-12));

    // k-fold cross validation
    int kFolds = 10;

    CvParamGrid C = new CvParamGrid();
    CvParamGrid p = new CvParamGrid();
    CvParamGrid nu = new CvParamGrid();
    CvParamGrid gamma = new CvParamGrid();
    CvParamGrid coeff = new CvParamGrid();
    CvParamGrid degree = new CvParamGrid();
    gamma.set_step(0.0);

    // initialize SVM object to avoid being Null object
    classifier = new CvSVM(trainingData, classes, new Mat(), new Mat(), params);

    classifier.train_auto(trainingData, classes, new Mat(), new Mat(), params, kFolds, C, gamma, p, nu, coeff, degree, false);

    classifier.save(XML.toString());
    Log.i(TAG,"Training Done & Trained Model Saved");
2017-07-13 09:04:50 -0600 answered a question find contours miss some objects

I threshold the image for other processing before finding contours then I applied Canny and this causes my problem and after removing canny and use RETR_CCOMP mode which gives 2 level of hierarchy, I can now get the correct result.

2017-07-12 10:05:51 -0600 commented question find contours miss some objects

If I change the retrieval mode to RETR_LIST or RETR_CCOMP, I can get all the objects but with redundancy then how to get rid of redundant objects is my question.

2017-07-12 08:54:54 -0600 commented question find contours miss some objects

@berak, do you know how I can find the objects myself by using the findnonezero() or countnonezero() functions to read all the objects and how to calculate the black distance between white objects.

Please help.

2017-07-07 09:57:09 -0600 commented question svm.save() is not working opencv4android

I used this function but no luck. Thanks for your care. I really appreciate it.

2017-07-07 09:24:07 -0600 commented question svm.save() is not working opencv4android

Here is the result of debugging provided by a user countering the same problem: cause= Exception, detailMessage= "Unknown Exception" , stackTrace=> StackTraceElement[0] , suppressedExeptions= Collections$UnmodifiableRandomAccessList,

Details are in here

2017-07-07 08:01:18 -0600 commented question svm.save() is not working opencv4android

Thanks @berak to save my time. Sadly, I will degrade to version 2.4 as it seems there is no straightforward solutions to save and load svm in opencv 3.x.

2017-07-07 07:01:26 -0600 commented question svm.save() is not working opencv4android

@berak One of the solutions I tried is to use opencv 2.4 to train the model because it allow me to save the model to Android sd without any problems and then load the saved model in my previous app which uses opencv3.2 to load the model and do the predictions but i countered this error: OpenCV Error: Assertion failed (samples.cols == var_count && samples.type() == CV_32F) in virtual float cv::ml::SVMImpl::predict(cv::InputArray, cv::OutputArray, int) const, file /build/master_pack-android/opencv/modules/ml/src/svm.cpp, line 1930 ml::predict_10() caught cv::Exception: /build/master_pack-android/opencv/modules/ml/src/svm.cpp:1930: error: (-215) samp Does the saved model using opencv 2.4 differes from opencv 3.2.

Any hints regarding this.

2017-07-05 07:39:50 -0600 commented answer cannot save SVM in OpenCV4Android 3.2 after upgrade: Fatal signal 11

@wes.y.w. Have you found a solution for this. I am suffering from the same error.

2017-07-05 07:37:06 -0600 commented question svm.save() is not working opencv4android

here is some additional information about the crash:

07-05 13:30:07.298 9699-9699/? A/DEBUG: * * 07-05 13:30:07.299 9699-9699/? A/DEBUG: Build fingerprint: 'samsung/hero2ltexx/hero2lte:7.0/NRD90M/G935FXXU1DQD7:user/release-keys' 07-05 13:30:07.299 9699-9699/? A/DEBUG: Revision: '9' 07-05 13:30:07.299 9699-9699/? A/DEBUG: ABI: 'arm64' 07-05 13:30:07.299 9699-9699/? A/DEBUG: pid: 8798, tid: 8798, name: esar.opencvtest >>> com.example.antesar.opencvtest <<< 07-05 13:30:07.299 9699-9699/? A/DEBUG: signal 7 (SIGBUS), code 1 (BUS_ADRALN), fault addr 0x7b00000001 07-05 13:30:07.300 9699-9699/? A/DEBUG: x0 0000007b326cc340 x1 0000007fd02c4580 x2 0000007b00000001 x3 0000007b4114e1b2 07-05 13:30:07.300 9699-9699/? A/DEBUG:

2017-07-05 06:03:10 -0600 commented question svm.save() is not working opencv4android

I knew that, I just added the function save( ) again in the SVM class which already exist in org.opencv.core.algorithm but no change. I thought if I add it to the SVM class, it can work as the load function.

2017-07-05 05:42:08 -0600 commented question svm.save() is not working opencv4android

In the SVM class I also added to add the function save() as in load() but nothing changed.

//javadoc: SVM::save(filepath)
public void save(String filename)
{

    save_0(nativeObj, filename);

    return;
}
2017-07-05 05:40:30 -0600 commented question svm.save() is not working opencv4android

I tried to call all the functions in public class Algorithm but all of them gives me SIGSEGV error not only the save() when using SVM:

// C++:  String getDefaultName()
private static native String getDefaultName_0(long nativeObj);

// C++:  void clear()
private static native void clear_0(long nativeObj);

// C++:  void save(String filename)
protected static native void save_0(long nativeObj, String filename);

// native support for java finalize()
private static native void delete(long nativeObj);
2017-07-05 05:30:19 -0600 asked a question Train, save, and load svm classifier out of Android

Is there any way to train a dataset outside of Android and then save it and uploaded to Android as I counter a problem in using the save() function to save to serialize and store SVM to disk.

Any suggestions or solution to my problem.

2017-07-05 05:25:09 -0600 commented question svm.save() is not working opencv4android

@berak please can you suggest any thing based on the last findings, I tried to access all the functions in Algorithm (getDefaultName(), Clear(), save(), and finalize()) and all of them gave me the same segmentation fault and this can be due to calling functions from wrong places or if there is no constructor. Is there any idea solving this as this is the fourth day of the problem and i can not resolve it?

2017-07-05 05:20:12 -0600 commented question svm.save() is not working opencv4android

For svm.load(), I upgraded to opencv version 3.2 and I found the required function and it is working fine.

2017-07-03 10:07:05 -0600 commented answer Color based classification using SVM

which website?

2017-07-03 08:06:42 -0600 asked a question svm.save() is not working opencv4android

I trained the SVM classifier which is working perfectly fine but when I tried to save the classifier, it gave me this error: Fatal signal 11 (SIGSEGV), code 1, fault addr 0x0 in tid 17404. After exploration i thing it seems to be a bug in Opencv 3.10 for Android but I can not find a proper solution. Unanswered question is in here. I use opencv 3.10 for Android, one proposed solution is to downgrade to 2.4 and I do not want to degrade as I have done lots of things using this version.

I would like to update the information here: save() in opencv is inherited from org.opencv.core.algorithms and there is no implementation of the function in the svm html description.

svm.load() also does not work as there is no implementation in Opencv 3.10, so please suggest me to downgrade to 2.4 or not?

Any help is more than appreciated.

2017-07-03 06:35:50 -0600 commented answer Color based classification using SVM

I think the bug is still exist and is not solved and there is a solution in here but I can not apply this as I only see svm.java and i cannot see the svm.cpp in my Android studio app.

2017-07-03 06:11:27 -0600 commented question [android] Save and load model in opencv 3.0.0 android

I have exactly the same problem like you @Minerva, have you found any solution to your problem?

2017-07-03 05:44:32 -0600 marked best answer Color based classification using SVM

I would like to ask a question about how to use SVM to classify images based on colors:

I have a dataset which contains 15 features extracted using simple image histogram includes mean, stddev, mode, and other statistics accompanied with a label in the same dataset.

How I can proceed from this dataset and how to change it to xml file store and load it in my Android app. Do I need to separate the dataset into training and label data, etcl.

I know there is lots of material in the website but a kind help from you will save my time.

Here the code I developed and I have this error: E/cv::error(): OpenCV Error: Assertion failed (samples.cols == var_count && samples.type() == CV_32F) in virtual float cv::ml::SVMImpl::predict(cv::InputArray, cv::OutputArray, int) const, file /Volumes/Linux/builds/master_pack-android/opencv/modules/ml/src/svm.cpp, line 1930

    int trainingRow = 18;
    int trainingCol = 18;
    Mat trainingDataMat = new Mat(trainingRow, trainingCol, CvType.CV_32FC1);
    int testingRow = 1;
    int testingCol = 18;
    Mat testingDataMat = new Mat(testingRow, testingCol, CvType.CV_32FC1);


    featureVector = "featureVector";
    double[][] trainingList = new double[trainingRow][trainingCol];
    trainingList = readFromFile(featureVector, trainingRow, trainingCol);
    for(int i=0;i<trainingRow;i++){
        for(int j=0;j<trainingCol;j++) {
            Log.i(TAG, "Data of data" + "[" + i + "," + j + "]" + " " + trainingList[i][j]);
            trainingDataMat.put(i, j, trainingList[i][j]);
        }
        v_features.push_back(trainingDataMat.reshape(1,1));
    }

    featureVector = "label";
    int[] labelList = new int[trainingRow];
    labelList = readFromFile1(featureVector);
    for(int i=0;i<trainingRow;i++){
        Log.i(TAG, "Data of label " + "[" + i + "]" + " " + labelList[i]);
        Mat labelsMat = new Mat(1, 1, CvType.CV_32SC1, new Scalar(labelList[i]));
        trainingLabels.push_back(labelsMat);
    }
    Log.i(TAG,"Training...");
    v_features.copyTo(trainingData);
    trainingData.convertTo(trainingData, CvType.CV_32F);
    trainingLabels.copyTo(classes);
    classes.convertTo(classes, CvType.CV_32S);
    classifier.setType(SVM.C_SVC);
    classifier.setKernel(SVM.LINEAR);
    classifier.setGamma(0.5);
    classifier.setNu(0.5);
    classifier.setC(1);
    TermCriteria criteria = new TermCriteria(TermCriteria.EPS + TermCriteria.MAX_ITER,100,0.1);
    classifier.setTermCriteria(criteria);

    classifier.train(trainingData, Ml.ROW_SAMPLE, classes);

    Log.i(TAG,"Done Training");

    Log.i(TAG,"Testing...");

    featureVector = "testVector";
    double[][] testingList = new double[testingRow][testingCol];
    testingList = readFromFile(featureVector, testingRow, testingCol);
    for(int i=0;i<testingRow;i++) {
        for (int j = 0; j < testingCol; j++) {
            Log.i(TAG, "Data of test" + "[" + i + "," + j + "]" + " " + testingList[i][j]);
            testingDataMat.put(i, j, testingList[i][j]);
        }
        vt_features.push_back(testingDataMat.reshape(1, 1));
    }

    vt_features.copyTo(testingData);
    testingData.convertTo(testingData, CvType.CV_32F);

    Log.i(TAG, "classification result" + classifier.predict(testingData));
2017-07-03 05:44:29 -0600 commented answer Color based classification using SVM

Many thanks, I will try to solve this.