2017-10-23 1 views
-1

다른 이미지 (주/큰 이미지) 안에 이미지 (템플릿/하위 이미지)를 찾는 자동화 안드로이드 응용 프로그램을 만들려고합니다.openCV를 사용하여 안드로이드에서 이미지 템플릿 매칭

OnePlus 3T menu image

Whatsapp icon image from MotG 3

메뉴 이미지는 원 플러스 3T에서입니다. Whatsapp 아이콘 이미지는 motoG 3에서 제작되었습니다.

oneplus 3T에서 whatsapp 이미지를 찾으려고 시도했으나 성공적으로 찾았습니다.

그러나 다른 화면 크기의 다른 장치에서 일부 하위 이미지를 찾으려고 할 때 작동하지 않습니다.

누군가 도와 드릴 수 있습니까? 아래는 내가 사용하고있는 코드입니다.

class MatchingDemo { 
public Mat run(Mat img, Mat templ, String outFile, int match_method) { 
    System.out.println("\nRunning Template Matching"); 

    ///Create the result matrix 
    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_32FC1); 

    ///Do the Matching and Normalize 
    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; 
    if (match_method == Imgproc.TM_SQDIFF || match_method == Imgproc.TM_SQDIFF_NORMED) { 
     matchLoc = mmr.minLoc; 
    } else { 
     matchLoc = mmr.maxLoc; 
    } 

    System.out.println("matchloc.x "+ matchLoc.x); 
    System.out.println("templ.cols "+ templ.cols()); 
    System.out.println("matchloc.y "+ matchLoc.y); 
    System.out.println("templ.rows "+ templ.rows()); 

    ///Show me what you got 
    Imgproc.rectangle(img, matchLoc, new Point(matchLoc.x + templ.cols(), 
      matchLoc.y + templ.rows()), new Scalar(0, 255, 0), 20); 

    // Save the visualized detection. 
    System.out.println("Writing "+ outFile); 
    Imgcodecs.imwrite(outFile, img); 



    return img; 
} 

}

+0

으로 시도 할 수 있습니다 더 규모 불변 결과를 얻을 수 있는가? 또한 다양한 장치에서 글꼴이 달라질 수 있으므로 템플릿 이미지에서 * WhatsApp * 텍스트를 제거하면 템플릿 일치가 더 정확 해집니다. – ZdaR

+0

안녕하세요 @ ZdaR .. 이미지 검색을위한 코드를 포함하여 질문을 편집했습니다. –

+0

템플릿 이미지에서 * WhatsApp * 텍스트를 제거하라는 제안을 시도해 보셨습니까? – ZdaR

답변

0

나는 당신의 주어진 스냅 샷에서 템플릿 이미지를 자른 모든 것이 잘 작동 :

  • 새로운 템플릿 이미지 :

enter image description here

코드 :

import cv2 
import numpy as np 


img_1 = cv2.imread("path/to/snapshot", 0) 
img_rgb = cv2.imread("path/to/snapshot") 
template_img = cv2.imread("path/to/template", 0) 

h, w = template_img.shape 

res = cv2.matchTemplate(img_1, template_img, cv2.TM_CCOEFF) 
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res) 

cv2.rectangle(img_rgb, max_loc, (max_loc[0]+w, max_loc[1]+h), np.array([0, 0, 255]), 3) 

cv2.imwrite("./debug.png", img_rgb) 

출력 :

enter image description here

참고 :matchTemplate 아주 기본적인 구현, 당신은 당신이 사용하는 코드를 보여주십시오 SIFT features

관련 문제