0

I am very new to android and openCV, I'M working on "Android application: plant disease Analyzer".

Below is my workflow:

1.I have static plant diseases in my gallery 2.End-user can capture an plant disease and submit to my application. 3.I want to compare the processed image with my gallery(Diseases) to get the most similar disease Can any one tell me What will be the best algorithm to use?, I have been searching on google but no luck,I tried with below code snippet, which I tried using openCV :

   BitmapFactory.Options bmOptions = new BitmapFactory.Options();
        Bitmap camerabitmap = BitmapFactory.decodeFile(cameraimage, 
         bmOptions);
     Bitmap galareybitmap = 
   BitmapFactory.decodeFile(galImage.getAbsolutePath(), bmOptions);
   private double imageProcessing(Bitmap cameraimage,Bitmap 
    galimagebitmap,String galimagename) throws IOException {
    Mat img1 = new Mat();
    Utils.bitmapToMat(cameraimage, img1);
    Mat img2 = new Mat();
    Utils.bitmapToMat(gallimagebitmap, img2);
    Imgproc.cvtColor(img1, img1, Imgproc.COLOR_RGBA2GRAY);
    Imgproc.cvtColor(img2, img2, Imgproc.COLOR_RGBA2GRAY);
    img1.convertTo(img1, CvType.CV_32F);
    img2.convertTo(img2, CvType.CV_32F);
    //Log.d("ImageComparator", "img1:"+img1.rows()+"x"+img1.cols()+" img2:"+img2.rows()+"x"+img2.cols());
    Mat hist1 = new Mat();
    Mat hist2 = new Mat();
    MatOfInt histSize = new MatOfInt(180);
    MatOfInt channels = new MatOfInt(0);
    ArrayList<Mat> bgr_planes1= new ArrayList<Mat>();
    ArrayList<Mat> bgr_planes2= new ArrayList<Mat>();
    Core.split(img1, bgr_planes1);
    Core.split(img2, bgr_planes2);
    MatOfFloat histRanges = new MatOfFloat(0f, 256f);
    boolean accumulate = false;
    Imgproc.calcHist(bgr_planes1, channels, new Mat(), hist1, histSize, histRanges);
    Core.normalize(hist1, hist1, 0, hist1.rows(), Core.NORM_MINMAX, -1, new Mat());
    Imgproc.calcHist(bgr_planes2, channels, new Mat(), hist2, histSize, histRanges);
    Core.normalize(hist2, hist2, 0, hist2.rows(), Core.NORM_MINMAX, -1, new Mat());
    /  img1.convertTo(img1, CvType.CV_32F);
    //  img2.convertTo(img2, CvType.CV_32F);
    hist1.convertTo(hist1, CvType.CV_32F);
    hist2.convertTo(hist2, CvType.CV_32F);
    return Imgproc.compareHist(hist1, hist2,3);

}

I tried with template matching also in opencv

     int match_method=Imgproc.TM_CCOEFF_NORMED;
     Mat temp = Imgcodecs.imread(tempim,Imgcodecs.CV_LOAD_IMAGE_GRAYSCALE );
     Mat img = Imgcodecs.imread(sourceim,Imgcodecs.CV_LOAD_IMAGE_GRAYSCALE 
     );
    Size sz = new Size(200, 200);
    Mat resizeimage = new Mat();
    Imgproc.resize(img, resizeimage, sz);
    Mat sourceimage = resizeimage;

    Mat resizeimage2 = new Mat();
    Imgproc.resize(temp, resizeimage2, sz);
    Mat templateimage = resizeimage2;

    int result_cols = sourceimage.cols() - templateimage.cols() + 1;
    int result_rows = sourceimage.rows() - templateimage.rows() + 1;
    Mat result = new Mat(result_rows, result_cols, CvType.CV_32FC1);
    Imgproc.matchTemplate(sourceimage,templateimage, result, match_method);
    //Core.normalize(result, result, 0, 1, Core.NORM_MINMAX, -1, new Mat());
    Imgproc.threshold(result, result,0.1,1,Imgproc.THRESH_TOZERO);

    Point matchLoc,maxLoc,minLoc;
    Core.MinMaxLocResult mmr;
    boolean iterate = true;
    double minlocvalue,maxlocvalue,minminvalue,maxmaxvalue;
    while(true){
        mmr = Core.minMaxLoc(result);
        if (match_method == Imgproc.TM_SQDIFF || match_method == Imgproc.TM_SQDIFF_NORMED) {
            matchLoc = mmr.minLoc;
            minminvalue = mmr.minVal; // test
        } else {
            matchLoc = mmr.maxLoc;
            maxmaxvalue = mmr.minVal; // test
        }
        Log.d(TAG, "mmr.maxVal : "+mmr.maxVal);
        if(mmr.maxVal >=0.2)
        {
            Log.d(TAG, "imagemathed..");

            Imgproc.rectangle(sourceimage, matchLoc, new Point(matchLoc.x + templateimage.cols(),
                    matchLoc.y + templateimage.rows()), new Scalar(0, 255, 0));
            Imgcodecs.imwrite(outFile, img);
            Mat image = Imgcodecs.imread(outFile);
            try {
                Bitmap bm = Bitmap.createBitmap(image.cols(), image.rows(),  Bitmap.Config.RGB_565);
                Utils.matToBitmap(image, bm);
                System.out.println("MinVal "+bm);
            }catch(Exception e){
                e.printStackTrace();
            }
            return true;
        }else {
            Log.d(TAG, "image not mathced..");
            return false;
        }
    }

But every time I'M not getting the proper output, fault images are coming in the output.Please help me out, AM I following right approach, If not can some one suggest me which approach i have to follow.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • 1
    You need some machine learning algorithm which compares the images and returns the most similar photo as result. You can start from here with [TensorFlow](https://www.tensorflow.org/tutorials/image_recognition). This is not something you can do by comparing the bytes of the images. – MatPag Jan 09 '18 at 16:16
  • Machine learning is a bit over the top, but i would use Mini TensorFlow – Remario Jan 09 '18 at 16:29
  • Thanks for the reply, Is there any way to do using openCV? – Ram3559879 Jan 09 '18 at 17:39

0 Answers0