基于NCC模板匹配识别



一:基本原理

NCC是一种基于统计学计算两组样本数据相关性的算法,其取值范围为[-1, 1]之间,而对图像来说,每个像素点都可以看出是RGB数值,这样整幅图像就可以看成是一个样本数据的集合,如果它有一个子集与另外一个样本数据相互匹配则它的ncc值为1,表示相关性很高,如果是-1则表示完全不相关,基于这个原理,实现图像基于模板匹配识别算法,其中第一步就是要归一化数据,数学公式如下:


二:实现步骤

(1)      获取模板像素并计算均值与标准方差、像素与均值diff数据样本

(2)      根据模板大小,在目标图像上从左到右,从上到下移动窗口,计

算每移动一个像素之后窗口内像素与模板像素的ncc值,与阈值比较,大于

阈值则记录位置

(3)      根据得到位置信息,使用红色矩形标记出模板匹配识别结果。

(4)      UI显示结果

 

三:编程实现

基于JAVA语言完成了整个算法编程实现与演示,其中第一步的代码如下:

  1. int tw = template.getWidth();  
  2. int th = template.getHeight();  
  3. int[] tpixels = new int[tw * th];  
  4. getRGB(template, 00, tw, th, tpixels);  
  5. for(int i=0; i<tpixels.length; i++)  
  6. {  
  7.     tpixels[i] = (tpixels[i] >> 16) & 0xff;  
  8. }  
  9. double[] meansdev = getPixelsMeansAndDev(tpixels);  
  10. double[] tDiff = calculateDiff(tpixels, meansdev[0]);  
  11. int raidus_width = tw / 2;  
  12. int raidus_height = th / 2;  
		int tw = template.getWidth();
		int th = template.getHeight();
		int[] tpixels = new int[tw * th];
		getRGB(template, 0, 0, tw, th, tpixels);
		for(int i=0; i<tpixels.length; i++)
		{
			tpixels[i] = (tpixels[i] >> 16) & 0xff;
		}
		double[] meansdev = getPixelsMeansAndDev(tpixels);
		double[] tDiff = calculateDiff(tpixels, meansdev[0]);
		int raidus_width = tw / 2;
		int raidus_height = th / 2;

第二步的实现代码如下:

  1. int[] windowPixels = new int[tw * th];  
  2. Arrays.fill(windowPixels, 0);  
  3. for (int row = 0; row < height; row++) {  
  4.     for (int col = 0; col < width; col++) {  
  5.         // calculate the means and dev for each window  
  6.         if(row <  raidus_height || (row + raidus_height) >= height)  
  7.             continue;  
  8.         if(col < raidus_width || (col + raidus_width) >= width)   
  9.             continue;  
  10.         int wrow = 0;  
  11.         Arrays.fill(windowPixels, 0);  
  12.         for(int subrow = -raidus_height; subrow <= raidus_height; subrow++ )  
  13.         {  
  14.             int wcol = 0;  
  15.             for(int subcol = -raidus_width; subcol <= raidus_width; subcol++ )  
  16.             {  
  17.                 if(wrow >= th || wcol >= tw)  
  18.                 {  
  19.                     continue;  
  20.                 }  
  21.                 windowPixels[wrow * tw + wcol] = getPixelValue(width, col + subcol, row + subrow, inPixels);  
  22.                 wcol++;  
  23.             }  
  24.             wrow++;  
  25.         }  
  26.         // calculate the ncc  
  27.         double[] _meansDev = getPixelsMeansAndDev(windowPixels);  
  28.         double[] diff = calculateDiff(windowPixels, _meansDev[0]);  
  29.         double ncc = calculateNcc(tDiff, diff, _meansDev[1], meansdev[1]);  
  30.         if(ncc > threhold) {  
  31.             Point mpoint = new Point();  
  32.             mpoint.x = col;  
  33.             mpoint.y  = row;  
  34.             points.add(mpoint);  
  35.         }  
  36.     }  
  37. }  
		int[] windowPixels = new int[tw * th];
		Arrays.fill(windowPixels, 0);
		for (int row = 0; row < height; row++) {
			for (int col = 0; col < width; col++) {
				// calculate the means and dev for each window
				if(row <  raidus_height || (row + raidus_height) >= height)
					continue;
				if(col < raidus_width || (col + raidus_width) >= width) 
					continue;
				int wrow = 0;
				Arrays.fill(windowPixels, 0);
				for(int subrow = -raidus_height; subrow <= raidus_height; subrow++ )
				{
					int wcol = 0;
					for(int subcol = -raidus_width; subcol <= raidus_width; subcol++ )
					{
						if(wrow >= th || wcol >= tw)
						{
							continue;
						}
						windowPixels[wrow * tw + wcol] = getPixelValue(width, col + subcol, row + subrow, inPixels);
						wcol++;
					}
					wrow++;
				}
				// calculate the ncc
				double[] _meansDev = getPixelsMeansAndDev(windowPixels);
				double[] diff = calculateDiff(windowPixels, _meansDev[0]);
				double ncc = calculateNcc(tDiff, diff, _meansDev[1], meansdev[1]);
				if(ncc > threhold) {
					Point mpoint = new Point();
					mpoint.x = col;
					mpoint.y  = row;
					points.add(mpoint);
				}
			}
		}

第三步的实现代码如下:

  1. // draw matched template on target image according position  
  2. setRGB( dest, 00, width, height, inPixels );  
  3. Graphics2D g2d = dest.createGraphics();  
  4. g2d.setPaint(Color.RED);  
  5. g2d.setStroke(new BasicStroke(4));  
  6. for(Point p : points)  
  7. {  
  8.     g2d.drawRect(p.x - raidus_width, p.y - raidus_height, tw, th);  
  9. }  
		// draw matched template on target image according position
		setRGB( dest, 0, 0, width, height, inPixels );
		Graphics2D g2d = dest.createGraphics();
		g2d.setPaint(Color.RED);
		g2d.setStroke(new BasicStroke(4));
		for(Point p : points)
		{
			g2d.drawRect(p.x - raidus_width, p.y - raidus_height, tw, th);
		}

其中第二步用到的计算NCC的方法实现如下:

  1. private double calculateNcc(double[] tDiff, double[] diff, double dev1, double dev2) {  
  2.     // TODO Auto-generated method stub  
  3.     double sum = 0.0d;  
  4.     double count = diff.length;  
  5.     for(int i=0; i<diff.length; i++)  
  6.     {  
  7.         sum += ((tDiff[i] * diff[i])/(dev1 * dev2));  
  8.     }  
  9.     return (sum / count);  
  10. }  
	private double calculateNcc(double[] tDiff, double[] diff, double dev1, double dev2) {
		// TODO Auto-generated method stub
		double sum = 0.0d;
		double count = diff.length;
		for(int i=0; i<diff.length; i++)
		{
			sum += ((tDiff[i] * diff[i])/(dev1 * dev2));
		}
		return (sum / count);
	}

UI部分完整源代码如下:

  1. package com.gloomyfish.image.templae.match;  
  2.   
  3. import java.awt.BorderLayout;  
  4. import java.awt.FlowLayout;  
  5. import java.awt.Graphics;  
  6. import java.awt.Graphics2D;  
  7. import java.awt.event.ActionEvent;  
  8. import java.awt.event.ActionListener;  
  9. import java.awt.image.BufferedImage;  
  10. import java.io.IOException;  
  11.   
  12. import javax.imageio.ImageIO;  
  13. import javax.swing.JButton;  
  14. import javax.swing.JComponent;  
  15. import javax.swing.JFrame;  
  16. import javax.swing.JPanel;  
  17.   
  18. public class DemoUI extends JComponent {  
  19.       
  20.     /** 
  21.      *  
  22.      */  
  23.     private static final long serialVersionUID = 1L;  
  24.     private BufferedImage targetImage;  
  25.     private BufferedImage template;  
  26.       
  27.     public DemoUI()  
  28.     {  
  29.         super();  
  30.         java.net.URL imageURL = this.getClass().getResource("words.png");  
  31.         java.net.URL templateURL = this.getClass().getResource("template.png");  
  32.           
  33.         try {  
  34.             template = ImageIO.read(templateURL);  
  35.             targetImage = ImageIO.read(imageURL);  
  36.         } catch (IOException e) {  
  37.             e.printStackTrace();  
  38.         }  
  39.     }  
  40.       
  41.     public void setTarget(BufferedImage target) {  
  42.         this.targetImage = target;  
  43.     }  
  44.   
  45.     @Override  
  46.     protected void paintComponent(Graphics g) {  
  47.         Graphics2D g2 = (Graphics2D) g;  
  48.         if(targetImage != null) {  
  49.             g2.drawImage(targetImage, 1010, targetImage.getWidth(), targetImage.getHeight(), null);  
  50.         }  
  51.         if(template != null) {  
  52.             g2.drawImage(template, 20+targetImage.getWidth(), 10, template.getWidth(), template.getHeight(), null);  
  53.         }  
  54.     }  
  55.       
  56.     public static void main(String[] args) {  
  57.         JFrame f = new JFrame("模板匹配与识别");  
  58.         JButton okBtn = new JButton("匹配");  
  59.         final DemoUI ui = new DemoUI();  
  60.         okBtn.addActionListener(new ActionListener() {  
  61.   
  62.             @Override  
  63.             public void actionPerformed(ActionEvent e) {  
  64.                   
  65.                 ui.process();  
  66.             }  
  67.         });  
  68.           
  69.         JPanel btnPanel = new JPanel();  
  70.         btnPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));  
  71.         btnPanel.add(okBtn);  
  72.           
  73.         f.getContentPane().add(btnPanel, BorderLayout.SOUTH);  
  74.         f.getContentPane().add(ui, BorderLayout.CENTER);  
  75.         f.setSize(500500);  
  76.         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  77.         f.setVisible(true);  
  78.     }  
  79.   
  80.     protected void process() {  
  81.         NccTemplateMatchAlg algo = new NccTemplateMatchAlg(template);  
  82.         targetImage = algo.filter(targetImage, null);  
  83.         this.repaint();  
  84.     }  
  85.   
  86. }  
package com.gloomyfish.image.templae.match;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class DemoUI extends JComponent {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private BufferedImage targetImage;
	private BufferedImage template;
	
	public DemoUI()
	{
		super();
		java.net.URL imageURL = this.getClass().getResource("words.png");
		java.net.URL templateURL = this.getClass().getResource("template.png");
		
		try {
			template = ImageIO.read(templateURL);
			targetImage = ImageIO.read(imageURL);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void setTarget(BufferedImage target) {
		this.targetImage = target;
	}

	@Override
	protected void paintComponent(Graphics g) {
		Graphics2D g2 = (Graphics2D) g;
		if(targetImage != null) {
			g2.drawImage(targetImage, 10, 10, targetImage.getWidth(), targetImage.getHeight(), null);
		}
		if(template != null) {
			g2.drawImage(template, 20+targetImage.getWidth(), 10, template.getWidth(), template.getHeight(), null);
		}
	}
	
	public static void main(String[] args) {
		JFrame f = new JFrame("模板匹配与识别");
		JButton okBtn = new JButton("匹配");
		final DemoUI ui = new DemoUI();
		okBtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				
				ui.process();
			}
		});
		
		JPanel btnPanel = new JPanel();
		btnPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
		btnPanel.add(okBtn);
		
		f.getContentPane().add(btnPanel, BorderLayout.SOUTH);
		f.getContentPane().add(ui, BorderLayout.CENTER);
		f.setSize(500, 500);
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.setVisible(true);
	}

	protected void process() {
		NccTemplateMatchAlg algo = new NccTemplateMatchAlg(template);
		targetImage = algo.filter(targetImage, null);
		this.repaint();
	}

}

四:程序运行效果如下


其中左边是目标图像、右边为模板图像

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值