package detection;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.awt.Point;
import java.io.*;
import org.jdom.*;
import org.jdom.input.*;
import org.jdom.filter.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Iterator;
import java.util.Scanner;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
public class Detector {
/** The list of classifiers that the test image should pass to be considered as an image.*/
List<Stage> stages;
Point size;
/**Factory method. Builds a detector from an XML file.
* @param filename The XML file (generated by OpenCV) describing the Haar Cascade.
* @return The corresponding detector.
*/
public static Detector create(String filename) {
/* Read XML file */
org.jdom.Document document=null;
SAXBuilder sxb = new SAXBuilder();
try
{
document = sxb.build(new File(filename));
}
catch(Exception e){e.printStackTrace();}
return new Detector(document);
}
/** Detector constructor.
* Builds, from a XML document (i.e. the result of parsing an XML file, the corresponding Haar cascade.
* @param document The XML document (parsing of file generated by OpenCV) describing the Haar cascade.
*/
public Detector(org.jdom.Document document)
{
/* The detector is constituted by stages, each of them telling whether the considered zone represents the object
* with probability a bit greater than 0.5. If a zone passes all stages, it is considered as representing the object.*/
stages=new LinkedList<Stage>();
/* Read the size (in pixels) of the detector. */
Element racine = (Element) document.getRootElement().getChildren().get(0);
Scanner scanner = new Scanner(racine.getChild("size").getText());
size = new Point(scanner.nextInt(),scanner.nextInt());
/* Iterate over the stages nodes to read the stages. */
Iterator it=racine.getChild("stages").getChildren("_").iterator();
while(it.hasNext())
{
Element stage=(Element)it.next();
/* Read the stage threshold. */
float thres=Float.parseFloat(stage.getChild("stage_threshold").getText());
/*Read all trees of the stage. */
Iterator it2=stage.getChild("trees").getChildren("_").iterator();
Stage st=new Stage(thres);
while(it2.hasNext())
{
Element tree = ((Element)it2.next());
Tree t = new Tree();
Iterator it4 = tree.getChildren("_").iterator();
while(it4.hasNext())
{
Element feature=(Element) it4.next();
float thres2=Float.parseFloat(feature.getChild("threshold").getText());
int left_node=-1;
float left_val = 0;
boolean has_left_val =false;
int right_node=-1;
float right_val = 0;
boolean has_right_val =false;
Element e;
if((e=feature.getChild("left_val"))!=null)
{
left_val = Float.parseFloat(e.getText());
has_left_val=true;
}
else
{
left_node = Integer.parseInt(feature.getChild("left_node").getText());
has_left_val=false;
}
if((e=feature.getChild("right_val"))!=null)
{
right_val = Float.parseFloat(e.getText());
has_right_val=true;
}
else
{
right_node = Integer.parseInt(feature.getChild("right_node").getText());
has_right_val=false;
}
Feature f = new Feature(thres2,left_val,left_node,has_left_val,right_val,right_node,has_right_val,size);
Iterator it3=feature.getChild("feature").getChild("rects").getChildren("_").iterator();
while(it3.hasNext())
{
String s = ((Element) it3.next()).getText().trim();
Rect r = Rect.fromString(s);
f.add(r);
}
t.addFeature(f);
}
st.addTree(t);
}
stages.add(st);
}
}
/** Returns the list of detected objects in an image applying the Viola-Jones algorithm.
*
* The algorithm tests, from sliding windows on the image, of variable size, which regions should be considered as searched objects.
* Please see Wikipedia for a description of the algorithm.
* @param file The image file to scan.
* @param baseScale The initial ratio between the window size and the Haar classifier size (default 2).
* @param scale_inc The scale increment of the window size, at each step (default 1.25).
* @param increment The shift of the window at each sub-step, in terms of percentage of the window size.
* @return the list of rectangles containing searched objects, expressed in pixels.
*/
public List<java.awt.Rectangle> getFaces(String file,float baseScale, float scale_inc,float increment, int min_neighbors,boolean doCannyPruning)
{
try {
BufferedImage image = ImageIO.read(new File(file));
return getFaces(image, baseScale, scale_inc, increment, min_neighbors, doCannyPruning);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public List<java.awt.Rectangle> getFaces(BufferedImage image,float baseScale, float scale_inc,float increment, int min_neighbors,boolean doCannyPruning)
{
//StopWatch sw = new StopWatch();
//sw.start();
List<Rectangle> ret=new ArrayList<Rectangle>();
int width=image.getWidth();
int height=image.getHeight();
/* Compute the max scale of the detector, i.e. the size of the image divided by the size of the detector. */
float maxScale = (Math.min((width+0.f)/size.x,(height+0.0f)/size.y));
/* Compute the grayscale image, the integral image and the squared integral image.*/
int[][] grayImage=new int[width][height];
int[][] img = new int[width][height];
int[][] squares=new int[width][height];
for(int i=0;i<width;i++)
{
int col=0;
int col2=0;
for(int j=0;j<height;j++)
{
int c = image.getRGB(i,j);
int red = (c & 0x00ff0000) >> 16;
int green = (c & 0x0000ff00) >> 8;
int blue = c & 0x000000ff;
int value=(30*red +59*green +11*blue)/100;
img[i][j]=value;
grayImage[i][j]=(i>0?grayImage[i-1][j]:0)+col+value;
squares[i][j]=(i>0?squares[i-1][j]:0)+col2+value*value;
col+=value;
col2+=value*value;
}
}
/* Eventually compute the gradient of the image, if option is on. */
int[][] canny = null;
if(doCannyPruning)
canny = CannyPruner.getIntegralCanny(img);
/*Heart of the algorithm : detection */
/*For each scale of the detection window */
for(float scale=baseScale;scale<maxScale;scale*=scale_inc)
{
/*Compute the sliding step of the window*/
int step=(int) (scale*size.x*increment);
int size=(int) (scale*this.size.x);
/*For each position of the window on the image, check whether the object is detected there.*/
for(int i=0;i<width-size;i+=step)
{
for(int j=0;j<height-size;j+=step)
{
/* If Canny pruning is on, compute the edge density of the zone.
* If it is too low, the object should not be there so skip the region.*/
if(doCannyPruning)
{
int edges_density = canny[i+size][j+size]+canny[i][j]-canny[i][j+size]-canny[i+size][j];
int d = edges_density/size/size;
if(d<20||d>100)
continue;
}
boolean pass=true;
/* Perform each stage of the detector on the window. If one stage fails, the zone is rejected.*/
for(Stage s:stages)
{
if(!s.pass(grayImage,squares,i,j,scale))
{pass=false;
break;}
}
/* If the window passed all stages, add it to the results. */
if(pass) ret.add(new Rectangle(i,j,size,size));
}
}
}
//sw.print("Single threaded :
没有合适的资源?快使用搜索试试~ 我知道了~
小程序 jviolajones(人脸检测算法)(源码).rar
共29个文件
svn-base:13个
java:8个
xml:3个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 26 浏览量
2023-02-03
13:45:08
上传
评论
收藏 1002KB RAR 举报
温馨提示
免责声明:资料部分来源于合法的互联网渠道收集和整理,部分自己学习积累成果,供大家学习参考与交流。收取的费用仅用于收集和整理资料耗费时间的酬劳。 本人尊重原创作者或出版方,资料版权归原作者或出版方所有,本人不对所涉及的版权问题或内容负法律责任。如有侵权,请举报或通知本人删除。
资源推荐
资源详情
资源评论
收起资源包目录
小程序 jviolajones(人脸检测算法)(源码).rar (29个子文件)
jviolajones(人脸检测算法)
lena.jpg 90KB
src
detection
Rect.java 597B
CannyPruner.java 3KB
Tree.java 1KB
Detector.java 10KB
Feature.java 3KB
MultiThreadedDetector.java 5KB
Test.java 3KB
Stage.java 1010B
.svn
pristine
be
be7ed570cf5cb6b47c5e9e66d5d089f8c2f41fff.svn-base 597B
61
619346163507ec96074acf4f30b9e4f967e30a2f.svn-base 1KB
e3
e302327f77d880817192b9114c7c0bc458439be9.svn-base 3KB
48
48b9eb0c707b7ac4a1f651e200a973184f600a0f.svn-base 3KB
34
3444d453367af67e18dd20f99cdb4d90397a1fa9.svn-base 90KB
82
8209d377bb71684a5827adc9819091c4472ad94b.svn-base 5KB
99
9986f2db44c7d183a861e72d3003eea1fcfe7a99.svn-base 149KB
a6
a6ba0bfac8bfdb49518ec10043f1bd2ef4cb8a05.svn-base 507KB
b2
b239971aca1937f3d159c5291885c566c4076829.svn-base 1010B
4e
4e3b1fb708ee172afadcf36f1667ddc7c23aeace.svn-base 10KB
e0
e0e5b4fe0ac4260bf95cfd8d32e230b9c88ed69e.svn-base 841KB
1c
1ca41ff097ef0361f064b8723c5dd60044870079.svn-base 1.23MB
64
6495222db095850e96a6ee81aa3c719d94efcd3d.svn-base 3KB
tmp
format 3B
entries 3B
wc.db 41KB
haarcascade_eye.xml 507KB
haarcascade_frontalface_default.xml 1.23MB
jdom.jar 149KB
haarcascade_frontalface_alt2.xml 841KB
共 29 条
- 1
资源评论
大富大贵7
- 粉丝: 313
- 资源: 8869
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功