2014-06-25 76 views
-2

我是OpenCV的初學者。 我正在嘗試使用多個臨時模板進行模板匹配。在Android中與多個模板匹配的模板

單模板我從這裏 OpenCV Template Matching example in Android

我搜索在互聯網上獲得源代碼,我無法找到滿足我的要求適當Android或Java代碼。 我有C++代碼,但我不知道如何翻譯它。 你能幫我找到一個合適的java或android代碼嗎?或者請幫助我將這個C++代碼翻譯成java,我可以在android應用程序中使用它。

預先感謝您。

C++代碼

#include "opencv2/highgui/highgui.hpp" 
#include "opencv2/imgproc/imgproc.hpp" 
#include <iostream> 
#include <stdio.h> 
using namespace std; 
using namespace cv; 

bool FindTemplate(Mat Img_Scene_Bgr,Mat Img_Template_Bgr,Point &Point_TemplateLocation) 
{ 
    // `Img_Scene_Bgr` and `Img_Template_Bgr` are the reference and template image 

    cv::Mat Img_Result_Float(Img_Scene_Bgr.rows-Img_Template_Bgr.rows+1, Img_Scene_Bgr.cols-Img_Template_Bgr.cols+1, CV_32FC1); 
    cv::matchTemplate(Img_Template_Bgr, Img_Scene_Bgr, Img_Result_Float, CV_TM_CCOEFF_NORMED); 
    normalisasi normalize(Img_Result_Float, Img_Result_Float, 0, 1, NORM_MINMAX, -1, Mat()); 

    double minval, maxval, threshold = 0.7; 
    cv::Point minloc, maxloc; 
    cv::minMaxLoc(Img_Result_Float, &minval, &maxval, &minloc, &maxloc); 

    if (maxval >= threshold) 
    { 
     Point_TemplateLocation= maxloc; 
     return true; 
    } 
    else 
    { 
     return false; 
    } 
} 

int main(int argc, char** argv) 
{ 
    Mat Img_Scene; 
    Mat Img_Template_1; 
    Mat Img_Template_2; 
    Mat Img_Result; 
    char* image_window = "Source Image"; 
    char* result_window = "Result window"; 
    /// Load image and template 
    Img_Scene = imread("SceneImage.png", 1); 
    Img_Template_1 = imread("Templ1.png", 1); 
    Img_Template_2 = imread("Templ2.png", 1); 

    if(Img_Scene.data== NULL||Img_Template_1.data==NULL||Img_Template_2.data==NULL) 
    { 
     cout<<"Image Not Found"; 
     return 0; 
    } 

    Img_Result= Img_Scene.clone(); 
    Vector<Mat> List_Template_Img; 
    List_Template_Img.push_back(Img_Template_1);//Otherwise Get some folder & add the Files in it 
    List_Template_Img.push_back(Img_Template_2); 

    Point Point_TemplateLocation; 
    for (int i = 0; i < List_Template_Img.size(); i++) 
    { 
     if(!FindTemplate(Img_Scene,List_Template_Img[i],Point_TemplateLocation)) 
     { 
      cout<<"No Match Found"; 
     } 
     /// Show me what you got 
     rectangle(Img_Result, Point_TemplateLocation, Point(Point_TemplateLocation.x + Img_Template_1.cols , Point_TemplateLocation.y + Img_Template_1.rows), Scalar(0,0,255), 2, 8, 0); 
     putText(Img_Result, format("Object %d ",i),Point(Point_TemplateLocation.x + Img_Template_1.cols/4 , Point_TemplateLocation.y + Img_Template_1.rows/2),1,1,Scalar(255,0,0),1,-1); 
    } 
    /// Create windows 
    namedWindow(image_window, CV_WINDOW_AUTOSIZE); 
    namedWindow(result_window, CV_WINDOW_AUTOSIZE); 

    imshow(image_window, Img_Template_1); 
    imshow(image_window, Img_Template_2); 
    imshow(result_window, Img_Result); 

    waitKey(0); 
    return 0; 
} 
+0

你可以寫Java代碼模板匹配爲單個模板? – Micka

+0

如果單個模板匹配適用於您,只需將其擴展爲第二個模板(爲第二個模板執行相同的代碼),我就可以在 –

+0

之上編寫單個模板的鏈接。這與您要翻譯的代碼所做的相同。 – Micka

回答

0

我試着寫這樣一個劇本,但我在計算過程中感到困惑,並把計算結果

public void matching() { 
String img_eq = Environment.getExternalStorageDirectory().getAbsolutePath() + "/img_eq/img_eq.jpg"; 
String template = Environment.getExternalStorageDirectory().getAbsolutePath() + "/img_template/img_template.jpg"; 
String template2 = Environment.getExternalStorageDirectory().getAbsolutePath() + "/img_template/img_template2.jpg"; 

Mat img = Highgui.imread(img_eq, CvType.CV_8SC3); 
Mat templ = Highgui.imread(template, CvType.CV_8SC3); 
Mat templ_2 = Highgui.imread(template2, CvType.CV_8SC3); 


      int match_method = Imgproc.TM_SQDIFF_NORMED; 
      int result_cols = img.cols() - templ.cols() + 1; 
      int result_rows = img.rows() - templ.rows() + 1; 
      Mat result = new Mat(result_rows, result_cols, CvType.CV_8SC3); 

      Imgproc.matchTemplate(img, templ, result, match_method); 
      Core.normalize(result, result, 0, 1, Core.NORM_MINMAX, -1, new Mat()); 

      ///Localizing the best match with minMaxLoc 
      MinMaxLocResult mmr = Core.minMaxLoc(result); 


      Point matchLoc = new Point(); 
      //for (int i = 0; i < List_template_image.size(); i++) 
      { 
       if (match_method == Imgproc.TM_SQDIFF || match_method == Imgproc.TM_SQDIFF_NORMED) { 
       matchLoc = mmr.minLoc; 
       } else { 
       matchLoc = mmr.maxLoc; 
       } 
       ///Show me what you got 
       Core.rectangle(img, matchLoc, new Point(matchLoc.x + templ.cols(), 
        matchLoc.y + templ.rows()), new Scalar(0, 255, 0)); 
       // Save the visualized detection. 
       Highgui.imwrite("/mnt/sdcard/img_result/img_result.jpg", img); 
       Mat image = Highgui.imread("/mnt/sdcard/img_result/img_result.jpg"); 
       Mat android_image = Mat.zeros(image.cols(), image.rows(), CvType.CV_8SC3); 

       Imgproc.cvtColor(image, android_image, Imgproc.COLOR_BGR2RGB); 

       Bitmap bm = Bitmap.createBitmap(android_image.cols(),android_image.rows(), Bitmap.Config.ARGB_8888); 
       Utils.matToBitmap(android_image, bm); 

       ImageView iv = (ImageView) findViewById(R.id.image); 
       iv.setImageBitmap(bm); 
      }