Ask Your Question

jack1's profile - activity

2016-11-29 07:14:57 -0600 commented question which types of descriptors opencv use for machine learning( local or global)

Ok thank you a lot

2016-11-29 05:48:09 -0600 commented question which types of descriptors opencv use for machine learning( local or global)

I use Sift/SURF for the first time and don't know a lot about them. please can you give me an example in java that use theses descriptors for classification

2016-11-29 05:22:46 -0600 commented question which types of descriptors opencv use for machine learning( local or global)

I would like to use SIFT/ SURT/HOG as features for machine learning algorithm like SVM/ANN

2016-11-29 05:14:35 -0600 commented answer which types of descriptors opencv use for machine learning( local or global)

Thank you, so how to use SIFT/SURF or any other descriptors as features for machine learning

2016-11-28 15:39:46 -0600 asked a question which types of descriptors opencv use for machine learning( local or global)

I do some researches about descriptors used in classification algorithms. I use opencv such a library so i should know how opencv machine learning algorithms work and which descriptors they use by default( local like sift and surf or global like color and shape) and if i can use other types of descriptors. Thanks in advance

2016-09-18 17:14:51 -0600 asked a question how to implement neural network using opencv and java

I have to do a project that recognize images using opencv machine learning. I should try many methods. I tried KNN and SVM but i don't know how to implement ANN. Can someone help me? This is the KNN example

public   void train() {

        // Creating Training Data
         Mat trainData = new Mat();
         Mat train_labels = new Mat();

            for (int i = 0; i <6; i++) {

                String path = Environment.getExternalStorageDirectory().toString()
                        + "/Pictures/images/" + i + ".jpg";

                Mat img = Imgcodecs.imread(path);

                Log.i(TAG,"error"+i+img.empty());


                img.convertTo(img, CvType.CV_32FC1); // Convert to float
                Size dsize = new Size(25, 25);
                Imgproc.resize(img, img, dsize);

                img.convertTo(img, CvType.CV_32FC1);
                Mat imgResized = img.reshape(1, 1); // make continuous

                trainData.push_back(imgResized);
                // add 1 item
                train_labels
                        .push_back(new Mat(1, 1, CvType.CV_32SC1, new Scalar(i)));

            }



            Mat response = new Mat();
            Mat tmp;
            tmp = train_labels.reshape(1, 1); // make continuous
            tmp.convertTo(response, CvType.CV_32FC1); // Convert to float
            KNearest knn = KNearest.create();
            knn.train(trainData, Ml.ROW_SAMPLE, train_labels);

            // For Storing training data

            String path = Environment.getExternalStoragePublicDirectory(
                    Environment.DIRECTORY_DOWNLOADS).toString();

            Bitmap bmpOut1 = Bitmap.createBitmap(trainData.cols(),
                    trainData.rows(), Bitmap.Config.ARGB_8888);
            Bitmap bmpOut2 = Bitmap.createBitmap(response.cols(), response.rows(),
                    Bitmap.Config.ARGB_8888);

            response.convertTo(response, CvType.CV_8UC1);
            trainData.convertTo(trainData, CvType.CV_8UC1);

            Utils.matToBitmap(trainData, bmpOut1);
            Utils.matToBitmap(response, bmpOut2);
            // File file = new File(path);
            // file.mkdirs();
            File file = new File(path, "train.png");
            File file2 = new File(path, "response.png");

            OutputStream fout = null;
            OutputStream fout2 = null;

            try {
                fout = new FileOutputStream(file);
                fout2 = new FileOutputStream(file2);

                BufferedOutputStream bos = new BufferedOutputStream(fout);
                BufferedOutputStream bos2 = new BufferedOutputStream(fout2);

                bmpOut1.compress(Bitmap.CompressFormat.PNG, 100, bos);
                bos.flush();
                bos.close();
                bmpOut1.recycle();

                bmpOut2.compress(Bitmap.CompressFormat.PNG, 100, bos2);
                bos2.flush();
                bos2.close();
                bmpOut2.recycle();

            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }

            catch (IOException e) {
                e.printStackTrace();
            }



            // For Accessing training data in BMP file
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inScaled = false;
            Bitmap blankBitmap = BitmapFactory.decodeResource(getResources(),
                    R.drawable.train, o);


            Mat trainData2 = new Mat();
            Utils.bitmapToMat(blankBitmap, trainData2);

            Bitmap blankBitmap2 = BitmapFactory.decodeResource(getResources(),
                    R.drawable.response, o);


            Mat response2 = new Mat();
            Utils.bitmapToMat(blankBitmap2, response2);

            Mat response3 = new Mat();
            Mat trainData3 = new Mat();
             // 1. change the number of channels
            Imgproc.cvtColor(response2, response2, Imgproc.COLOR_BGRA2GRAY);

            Imgproc.cvtColor(trainData2, trainData2, Imgproc.COLOR_BGRA2GRAY);
            response2.convertTo(response3, CvType.CV_32FC1);
            trainData2.convertTo(trainData3, CvType.CV_32FC1);
            // instantiate the KNN object
            KNearest knn2 = KNearest.create();
            knn2.train(trainData3, Ml.ROW_SAMPLE, response3);



                Mat img = Imgcodecs.imread("/storage/emulated/0/DCIM/Camera/IMG.jpg");
                Log.i(TAG, "error1 img" + img.empty());

                Mat test = new Mat();
                Imgproc.resize(img, test, new Size(25, 25));
                test.convertTo(test, CvType.CV_32FC1);
                Mat results = new Mat();
                Mat responses = new Mat();
                Mat dists = new Mat();

                float label = knn.findNearest(test.reshape(1, 1),1, results,responses, dists);

                 if (label==0)
                {
                text.setText("Le reve");
                }
                 else if (label==1) {
                        text.setText("Guernica");
                    } 
                 else if (label==2) {
                        text.setText("La cène");
                    } 
                 else if (label==3) {
                        text.setText("La joconde");
                    } 
                 else if (label==4) {
                        text.setText("la vierge et sainte anne");
                    }
                 else if (label==5 ...
(more)
2016-05-29 05:15:22 -0600 commented answer which distance does the opencv KNN algorithm use?

thank you a lot :)

2016-05-29 05:02:06 -0600 commented question Android svm implementation error

Thanks for your help!!

2016-05-29 04:13:53 -0600 commented question Android svm implementation error

when t tried to predict labels i got 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) for (int i = 0; i < h; i++){

        for (int j=x+1; j < nb.get(i); j++)
        {
            Mat img3 = Imgcodecs.imread("/storage/emulated/0/DCIM/OurText/"+i + j+".png");
            Mat test = new Mat();
            Imgproc.resize(img3, test, new Size(25, 25));
            test.convertTo(test, CvType.CV_32SC1);
            float label = svm.predict(test);
             if (label==1)
            {
                text.setText("A");
            } else if (label==2)  {
                text.setText("C");
            } 
             else if (label==3)  {
                text.setText("D");
            }  


        }
2016-05-28 16:31:20 -0600 asked a question which distance does the opencv KNN algorithm use?

I use opencv KNN but i would know which distance this algorithm use :euclidean, hamming ...

2016-04-20 07:00:55 -0600 commented question How to recognize all the letters in a picture with opencv , KNN and java

Yes i know that i should complete all train images but my problem was that i can't read all letters in the image given by( Mat img = Imgcodecs.imread("/storage/emulated/0/DCIM/Camera/IMG.png");) this code allows me to just read 1 letter not all the letters in the image .I tried to use bounding boxes ,then extract each letters from each box and finally apply KNN algorithm for each one .Is it a good idea ?

2016-04-20 06:52:32 -0600 received badge  Enthusiast
2016-04-19 08:46:23 -0600 asked a question How to recognize all the letters in a picture with opencv , KNN and java

i have to write a java program that recognizes all the letters in a picture taken with android camera .I tried KNN method but i could only recognize a single letter in the picture and i want to recognize all the letters .Can someonr help me. This is my code

public void train (){


    //Creating Training Data
     Mat trainData = new Mat(); 
     Mat train_labels = new Mat();

    for (int i =1; i <15; i++) {
        String path = Environment.getExternalStorageDirectory().toString() + "/Pictures/New Folder/" + i + ".png";

    Mat img =Imgcodecs.imread(path);
    Log.i(TAG,"error debug: "+i+img.empty());

    img.convertTo(img, CvType.CV_32FC1); // Convert  to float
    Size dsize = new Size(25,25); 
     Imgproc.resize(img, img, dsize); 

     img.convertTo(img, CvType.CV_32FC1);
     Mat imgResized = img.reshape(1, 1); //make continuous

     trainData.push_back(imgResized);

    train_labels.push_back(new Mat (1,1,CvType.CV_32FC1,new Scalar(i)));// add 1 item

    }

        Mat response=new Mat();
        Mat tmp;
        tmp= train_labels.reshape(1,1); //make continuous
        tmp.convertTo(response,CvType.CV_32FC1); // Convert  to float*/


     KNearest knn = KNearest.create();
     knn.train(trainData,Ml.ROW_SAMPLE, train_labels);




     //For Storing training data

 String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString() ;


Bitmap bmpOut1 = Bitmap.createBitmap(trainData.cols(), trainData.rows(), Bitmap.Config.ARGB_8888);
Bitmap bmpOut2 = Bitmap.createBitmap(response.cols(), response.rows(), Bitmap.Config.ARGB_8888);

response.convertTo(response,CvType.CV_8UC1);
trainData.convertTo(trainData,CvType.CV_8UC1);

Utils.matToBitmap(trainData, bmpOut1);
Utils.matToBitmap(response, bmpOut2);
//File file = new File(path);
// file.mkdirs();
File file = new File(path, "train.png");
File file2 = new File(path, "response.png");

OutputStream fout = null;
OutputStream fout2 = null;

try {
    fout = new FileOutputStream(file);
    fout2 = new FileOutputStream(file2);

    BufferedOutputStream bos = new BufferedOutputStream(fout);
    BufferedOutputStream bos2 = new BufferedOutputStream(fout2);

    bmpOut1.compress(Bitmap.CompressFormat.PNG, 100, bos);
    bos.flush();
    bos.close();
    bmpOut1.recycle();

    bmpOut2.compress(Bitmap.CompressFormat.PNG, 100, bos2);
    bos2.flush();
    bos2.close();
    bmpOut2.recycle();

} catch (FileNotFoundException e) {
    e.printStackTrace();
}

catch (IOException e) {
    e.printStackTrace();
}


 //For Accessing training data in BMP file  


    BitmapFactory.Options o = new BitmapFactory.Options();
    o.inScaled = false;
    Bitmap blankBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.train,o);
    int trainWidth = blankBitmap.getWidth();
    int trainHeight = blankBitmap.getHeight();



    Mat trainData2 = new Mat();
    Utils.bitmapToMat(blankBitmap,trainData2);

    Bitmap blankBitmap2 = BitmapFactory.decodeResource(getResources(), R.drawable.response,o);
    int resWidth = blankBitmap2.getWidth();
    int resHeight = blankBitmap2.getHeight();

    Log.i(TAG,"res width "+ resWidth +" res Height"+ resHeight);
    Mat response2 = new Mat();
    Utils.bitmapToMat(blankBitmap2,response2);

    Log.i(TAG,"response2 width "+ response2.cols() +" res Height"+ response2.rows());

    Mat response3 = new Mat();
    Mat trainData3 = new Mat();

    Imgproc.cvtColor(response2,response2,Imgproc.COLOR_BGRA2GRAY);       // 1. change the number of channels
    Imgproc.cvtColor(trainData2,trainData2,Imgproc.COLOR_BGRA2GRAY);
    response2.convertTo(response3,CvType.CV_32FC1);
    trainData2.convertTo(trainData3,CvType.CV_32FC1);



    KNearest knn2 = KNearest.create();
    knn2.train(trainData3,Ml.ROW_SAMPLE, response3);


   Mat img = Imgcodecs.imread("/storage/emulated/0/DCIM/Camera/IMG.png");
   Log.i(TAG,"error1 img: "+img.empty());          
    Mat test = new Mat();
    Imgproc.resize(img,test, new Size(25,25) );
    test.convertTo(test, CvType.CV_32FC1);

    Mat results = new Mat();
    Mat responses = new Mat();
    Mat dists = new Mat();

    float label = knn.findNearest(test.reshape(1,1), 1,results,responses,dists);
    Log.i ...
(more)
2016-04-14 09:38:38 -0600 commented question Error when tring to train image with opencv and android

@berak

Mat trainData = new Mat(); 

 Mat train_labels = new Mat();

Mat img =Imgcodecs.imread( "/storage/emulated/0/DCIM/Camera/n1.PNG");

 img.convertTo(img, CvType.CV_32FC1);

Size dsize = new Size(50, 50); 

Imgproc.resize(img, img, dsize);

img.convertTo(img, CvType.CV_32FC1);

 Mat imgResized = img.reshape(1, 1);

 trainData.push_back(img);

train_labels.push_back(new Mat (1,1,CvType.CV_32FC1,new Scalar(1)));;

 KNearest knn = KNearest.create();

 knn.train(trainData,Ml.ROW_SAMPLE, train_labels);

I wrote this but i got this error :OpenCV Error: Assertion failed (samples.type() == CV_32F || samples.type() == CV_32S)

2016-04-14 07:31:33 -0600 commented question Error when tring to train image with opencv and android

I don't understand .How can i add this in my response mat .I am really sorry for my stupidity.(in my case i should train letters )

2016-04-14 07:20:34 -0600 commented question Error when tring to train image with opencv and android

@StevenPuttemans I don't find a good java tutorial that makes things clear to me .

2016-04-14 07:18:33 -0600 commented question Error when tring to train image with opencv and android

tmp=response_array.reshape(1,1); //make continuous
this line still wrong( logCat out put : OpenCV Error: Image step is wrong (The matrix is not continuous, thus its number of rows can not be changed) in cv::Mat cv::Mat::reshape(int, int) const, file /home/maksim/workspace/android-pack/opencv/modules/core/src/matrix.cpp, line 984)

2016-04-14 06:22:46 -0600 commented question Error when tring to train image with opencv and android

I am sorry ,i add it Yes as you said the img is empty :( i will try to solve this error and i will come back with more specific error

2016-04-14 06:06:01 -0600 commented question Error when tring to train image with opencv and android

I am really sorry but i am confused i don't understand the logic of this algorithm .Where should i put sample /response for training

2016-04-14 05:51:50 -0600 received badge  Editor (source)
2016-04-14 05:18:02 -0600 asked a question Error when tring to train image with opencv and android
  public class MainActivity extends Activity {
Button button;

ImageView res4;
TextView T1;
private static final String TAG = "training";

private BaseLoaderCallback mLoaderCallBack = new BaseLoaderCallback(this) {
    public void onManagerConnected(int status) {
        switch (status) {
        case LoaderCallbackInterface.SUCCESS:
            Log.i(TAG, "Open CV loaded successfully");
            break;

        default:
            super.onManagerConnected(status);
            break;
        }
    };
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    setContentView(R.layout.activity_main);
    addListenerOnButton();

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public void onResume() {
    super.onResume();
    OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_3_1_0, this,mLoaderCallBack);

}



public void addListenerOnButton() {

//  imageView = (ImageView) findViewById(R.id.imageView1);

    res4 = (ImageView) findViewById(R.id.imageView2);
    button = (Button) findViewById(R.id.button1);
    button.setOnClickListener(new OnClickListener() {



        @Override
        public void onClick(View arg0) {


                try {

                train();

            } catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            }
        }

    });

}
public void train (){
        //Creating Training Data
     String path = Environment.getExternalStorageDirectory().toString();                  
     Mat trainData = new Mat(); 
     Mat response_array = new Mat();
    Mat img =Imgcodecs.imread("/storage/emulated/0/DCIM/Camera/n1.png");
    img.convertTo(img, CvType.CV_32FC1);
     Size dsize = new Size(50, 50); 
     Imgproc.resize(img, img, dsize);  
     img.convertTo(img, CvType.CV_32FC1);
     Mat imgResized = img.reshape(1, 1);   
     trainData.push_back(imgResized);
     Mat response = new Mat();
     Mat tmp; 
     tmp=response_array.reshape(1,1); //make continuous  `
     tmp.convertTo(response,CvType.CV_32FC1); // Convert  to float  
     KNearest knn = KNearest.create();
     knn.train(trainData,Ml.ROW_SAMPLE, response);

     //For Storing training data
    Bitmap bmpOut1 = Bitmap.createBitmap(trainData.cols(), trainData.rows(), Bitmap.Config.ARGB_8888);
    trainData.convertTo(trainData,CvType.CV_8UC1); 
    Utils.matToBitmap(trainData, bmpOut1);
    File file = new File(path);  
    file.mkdirs(); 
    File file1 = new File(path, "train.png"); 
    OutputStream fout = null;
     try {  fout = new FileOutputStream(file1); 
     BufferedOutputStream bos = new BufferedOutputStream(fout); 
     bmpOut1.compress(Bitmap.CompressFormat.PNG, 100, bos);
     bos.flush(); 
     bos.close();  
     bmpOut1.recycle(); 
     }
     catch (FileNotFoundException e) { e.printStackTrace(); }
     catch (IOException e) {e.printStackTrace();}

     //For Accessing training data in BMP file  

     BitmapFactory.Options o = new BitmapFactory.Options(); 
     o.inScaled = false;
     Bitmap blankBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.train,o);
     int trainWidth = blankBitmap.getWidth();
     int trainHeight = blankBitmap.getHeight();
     Mat trainData2 = new Mat();
     Utils.bitmapToMat(blankBitmap,trainData2); 
     Mat trainData3 = new Mat();
     Mat response3 = new Mat();

     Imgproc.cvtColor(trainData2,trainData2,Imgproc.COLOR_BGRA2GRAY); // 1. change the number of channels; 
     trainData2.convertTo(trainData3,CvType.CV_32FC1); 
     KNearest knn2 = KNearest.create();
     knn.train(trainData3,Ml.ROW_SAMPLE, response3);

}
 }

I get these errors .Please help me and also i don't know how can i recognize my code

04-14 11:57:29.550: I/art(7070): Ignoring second debugger -- accepting and dropping
04-14 11:57:43.890: D/PhoneWindow(9670): *FMB* installDecor mIsFloating : false
04-14 11:57:43.890: D/PhoneWindow(9670): *FMB* installDecor flags : 8454400
04-14 11:57:44.040: D/OpenGLRenderer(9670): Render dirty regions requested: true
04-14 11:57:44.100: D/PhoneWindow(9670): *FMB* isFloatingMenuEnabled mFloatingMenuBtn : null
04-14 11:57:44.100: D/PhoneWindow(9670): *FMB* isFloatingMenuEnabled return false
04-14 11:57:44.150: D/SRIB_DCS(9670): log_dcs ThreadedRenderer::initialize entered! 
04-14 11:57:44.150: I/Adreno-EGL(9670): <qeglDrvAPI_eglInitialize:379>: EGL 1 ...
(more)
2016-04-12 05:03:08 -0600 commented question how to use Opencv machine learning for android project (KNN or SVM algorithms)?

I searched a lot in the internet and i don't found any android example and i don't know how to implement these algorithm :( Can you help me

2016-04-12 04:41:05 -0600 commented question how to use Opencv machine learning for android project (KNN or SVM algorithms)?

Thanks but this is for a java application for desktop ?

2016-04-12 00:22:17 -0600 asked a question how to use Opencv machine learning for android project (KNN or SVM algorithms)?

I need your help .I am a beginner in opencv and i should recognize letters in an image taken by android camera .I should use opencv machine learning such us KNN or SVM .I searched a lot but i don't find documentation for android and java .I don't find even a simple example to understand how can i use machine learning .Can someone help me please . Thank you in advance