Atitti 模板匹配 List matchTemplate(

Atitti 模板匹配 List<java.awt.Point> matchTemplate(

 

 

关键词 opencv java

 

 

 

public static List<java.awt.Point> matchTemplate(String srcImgFile, String desImgFile, String dbgDir, int matchMode,

int count) {

List<java.awt.Point> li = Lists.newArrayList();

BufferedImage src;

BufferedImage dest;

BufferedImage resultRect4dbg_img;

src = imgx.toImg(srcImgFile);

dest = imgx.toImg(desImgFile);

resultRect4dbg_img = imgx.toImg(srcImgFile);

Point tmpPt = null;

for (int i = count; i > 0; i--) {

java.awt.Point javaPoint = null;

if (i == count) // init

{

 

} else {

// Imgproc.drawContours(src, contours,-1, new

// Scalar(255,255,255),-1);

Rectangle drawRect = getDrawRect(tmpPt, dest.getWidth(), dest.getHeight());

src = drawContours(src, drawRect, Color.black);

imgx.save_overwrite(src, dbgDir + "\\" + filex.getUUidName() + " drawConted.jpg");

}

Point matchTemplatePt = null;

try {

// dbgDir + "\\" + filex.getUUidName() + " drawConted.jpg"

matchTemplatePt = matchTemplate(src, dest, resultRect4dbg_img, matchMode);

javaPoint = toJavaPoint(matchTemplatePt);

java.awt.Point downPnt=new java.awt.Point(javaPoint.x+dest.getWidth(), javaPoint.y+dest.getHeight());

imgx.rectangle(resultRect4dbg_img, javaPoint, downPnt, Color.red);

imgx.save_overwrite(resultRect4dbg_img, dbgDir + "\\" + filex.getUUidName() + " rected.jpg");

} catch (CantFindMatch | IOException e) {

System.out.println(e.getMessage() + "");

}

li.add(javaPoint);

tmpPt = matchTemplatePt;

}

 

return li;

}

 

 

 

 

public static Point matchTemplate(BufferedImage srcImg, BufferedImage desImg, BufferedImage resultRect4dbg,

int matchMode) throws CantFindMatch, IOException {

// 将文件读入为OpenCV的Mat格式

 

Mat source = bufImg2mat(srcImg);

// Highgui.imread(sourcePath);

// System.out.println( AtiJson.toJson(source));

// if(source.cols()==0)

// throw new IOException(sourcePath);

 

Mat dst = bufImg2mat(desImg);

// if(dst.cols()==0)

// throw new IOException(dstPath);

// 创建于原图相同的大小,储存匹配度

Mat result = Mat.zeros(source.rows(), source.cols(), CvType.CV_32FC1);

// 调用模板匹配方法

 

try {

Imgproc.matchTemplate(source, dst, result, matchMode); // TM_CCOEFF_NORMED

 

} catch (Exception e) {

 

throw new RuntimeException("Imgproc.matchTemplate ex:" + e.getMessage(), e);

 

}

 

// 规格化 rst= Mat [ 1005*1236*CV_32FC1, isCont=true, isSubmat=false,

// nativeObj=0x57b3fca0, dataAddr=0x59e80050 ]

Core.normalize(result, result, 0, 1, Core.NORM_MINMAX, -1);

// 获得最可能点,MinMaxLocResult是其数据格式,包括了最大、最小点的位置x、y

// System.out.println(AtiJson.toJson(result));

MinMaxLocResult mlr = Core.minMaxLoc(result);

Point matchLoc;

if (matchMode == Imgproc.TM_SQDIFF || matchMode == Imgproc.TM_SQDIFF_NORMED) {

matchLoc = mlr.minLoc;

} else {

matchLoc = mlr.maxLoc;

 

}

 

// 在原图上的对应模板可能位置画一个绿色矩形

Core.rectangle(source, matchLoc, new Point(matchLoc.x + dst.width(), matchLoc.y + dst.height()),

new Scalar(0, 255, 0));

// String outpic=filex.addSuffix(sourcePath,"matched");

// 将结果输出到对应位置

// Highgui.imwrite(outpic, source);

resultRect4dbg = mat2bufImg(source);

 

if (matchLoc != null)

return matchLoc;

throw new CantFindMatch(" cant find match");

 

}

:: 绰号:老哇的爪子claw of Eagle 偶像破坏者Iconoclast image-smasher

捕鸟王"Bird Catcher  kok  虔诚者Pious 宗教信仰捍卫者 Defender Of the Faith. 卡拉卡拉红斗篷 Caracalla red cloak 万兽之王

简称:: Emir Attilax Akbar 埃米尔 阿提拉克斯 阿克巴

全名::Emir Attilax Akbar bin Mahmud bin  attila bin Solomon bin adam Al Rapanui 埃米尔 阿提拉克斯 阿克巴 马哈茂德 阿提拉 所罗门 本亚当  阿尔 拉帕努伊

常用名:艾提拉(艾龙),  EMAIL:1466519819@qq.com

 

 

头衔:uke总部o2o负责人,全球网格化项目创始人,

uke交友协会会长  uke捕猎协会会长 Emir Uke部落首席大酋长,

 

uke宗教与文化融合事务部部长, uke宗教改革委员会副主席

uke制度与重大会议委员会委员长,uke保安部首席大队长,uke制度检查委员会副会长,

 

uke 首席cto   软件部门总监 技术部副总监  研发部门总监主管  产品部副经理 项目部副经理   uke科技研究院院长uke软件培训大师

 

uke波利尼西亚区大区连锁负责人 汤加王国区域负责人 uke克尔格伦群岛区连锁负责人,莱恩群岛区连锁负责人,uke布维岛和南乔治亚和南桑威奇群岛大区连锁负责人

 Uke软件标准化协会理事长理事长 Uke 数据库与存储标准化协会副会长

 

uke终身教育学校副校长   Uke医院 与医学院方面的创始人

 uec学院校长, uecip图像处理机器视觉专业系主任   uke文档检索专业系主任

Uke图像处理与机器视觉学院首席院长

Uke 户外运动协会理事长  度假村首席大村长   uke出版社编辑总编

 

转载请注明来源:attilax的专栏  ?http://blog.csdn.net/attilax

--Atiend  v8

 

 

posted @ 2017-02-25 15:51  attilaxAti  阅读(24)  评论(0编辑  收藏  举报