如何用opencv比较android中的2个图像?

时间:2018-01-09 16:12:43

标签: android opencv

我是android和openCV的新手,我正在研究" Android应用程序:植物疾病分析仪"

以下是我的工作流程

1.我的画廊里有静电植物病 2.End-用户可以捕获植物疾病并提交给我的申请。 3.我想将处理过的图像与我的画廊(疾病)进行比较,以获得最强烈的类似疾病 谁能告诉我什么是最好的算法? 我一直在搜索谷歌,但没有运气,我尝试过 下面的代码片段,我尝试使用 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);

}

我尝试在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;
        }
    }

但是每当我没有得到正确的输出时,故障图像就会出现在输出中。请帮助我,我遵循正确的方法,如果没有,可以有人建议我必须遵循哪种方法。

0 个答案:

没有答案