编写DetectFaceDemo.java,代码如下:
[java] view
plaincopyprint?
package com.njupt.zhb.test
import org.opencv.core.Core
import org.opencv.core.Mat
import org.opencv.core.MatOfRect
import org.opencv.core.Point
import org.opencv.core.Rect
import org.opencv.core.Scalar
import org.opencv.highgui.Highgui
import org.opencv.objdetect.CascadeClassifier
//
//燃竖 Detects faces in an image, draws boxes around them, and writes the results
// to "faceDetection.png".
//
public class DetectFaceDemo {
public void run() {
System.out.println("森段并\nRunning DetectFaceDemo")
System.out.println(getClass().getResource("lbpcascade_frontalface.xml").getPath())
// Create a face detector from the cascade file in the resources
//此迹 directory.
//CascadeClassifier faceDetector = new CascadeClassifier(getClass().getResource("lbpcascade_frontalface.xml").getPath())
//Mat image = Highgui.imread(getClass().getResource("lena.png").getPath())
//注意:源程序的路径会多打印一个‘/’,因此总是出现如下错误
/*
* Detected 0 faces Writing faceDetection.png libpng warning: Image
* width is zero in IHDR libpng warning: Image height is zero in IHDR
* libpng error: Invalid IHDR data
*/
//因此,我们将第一个字符去掉
String xmlfilePath=getClass().getResource("lbpcascade_frontalface.xml").getPath().substring(1)
CascadeClassifier faceDetector = new CascadeClassifier(xmlfilePath)
Mat image = Highgui.imread(getClass().getResource("we.jpg").getPath().substring(1))
// Detect faces in the image.
// MatOfRect is a special container class for Rect.
MatOfRect faceDetections = new MatOfRect()
faceDetector.detectMultiScale(image, faceDetections)
System.out.println(String.format("Detected %s faces", faceDetections.toArray().length))
// Draw a bounding box around each face.
for (Rect rect : faceDetections.toArray()) {
Core.rectangle(image, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 255, 0))
}
// Save the visualized detection.
String filename = "faceDetection.png"
System.out.println(String.format("Writing %s", filename))
Highgui.imwrite(filename, image)
}
}
package com.njupt.zhb.test
import org.opencv.core.Core
import org.opencv.core.Mat
import org.opencv.core.MatOfRect
import org.opencv.core.Point
import org.opencv.core.Rect
import org.opencv.core.Scalar
import org.opencv.highgui.Highgui
import org.opencv.objdetect.CascadeClassifier
//
// Detects faces in an image, draws boxes around them, and writes the results
// to "faceDetection.png".
//
public class DetectFaceDemo {
public void run() {
System.out.println("\nRunning DetectFaceDemo")
System.out.println(getClass().getResource("lbpcascade_frontalface.xml").getPath())
// Create a face detector from the cascade file in the resources
// directory.
//CascadeClassifier faceDetector = new CascadeClassifier(getClass().getResource("lbpcascade_frontalface.xml").getPath())
//Mat image = Highgui.imread(getClass().getResource("lena.png").getPath())
//注意:源程序的路径会多打印一个‘/’,因此总是出现如下错误
/*
* Detected 0 faces Writing faceDetection.png libpng warning: Image
* width is zero in IHDR libpng warning: Image height is zero in IHDR
* libpng error: Invalid IHDR data
*/
//因此,我们将第一个字符去掉
String xmlfilePath=getClass().getResource("lbpcascade_frontalface.xml").getPath().substring(1)
CascadeClassifier faceDetector = new CascadeClassifier(xmlfilePath)
Mat image = Highgui.imread(getClass().getResource("we.jpg").getPath().substring(1))
// Detect faces in the image.
// MatOfRect is a special container class for Rect.
MatOfRect faceDetections = new MatOfRect()
faceDetector.detectMultiScale(image, faceDetections)
System.out.println(String.format("Detected %s faces", faceDetections.toArray().length))
// Draw a bounding box around each face.
for (Rect rect : faceDetections.toArray()) {
Core.rectangle(image, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 255, 0))
}
// Save the visualized detection.
String filename = "faceDetection.png"
System.out.println(String.format("Writing %s", filename))
Highgui.imwrite(filename, image)
}
}
3.编写测试类:
[java] view
plaincopyprint?
package com.njupt.zhb.test
public class TestMain {
public static void main(String[] args) {
System.out.println("Hello, OpenCV")
// Load the native library.
System.loadLibrary("opencv_java246")
new DetectFaceDemo().run()
}
}
//运行结果:
//Hello, OpenCV
//
//Running DetectFaceDemo
///E:/eclipse_Jee/workspace/JavaOpenCV246/bin/com/njupt/zhb/test/lbpcascade_frontalface.xml
//Detected 8 faces
//Writing faceDetection.png
package com.njupt.zhb.test
public class TestMain {
public static void main(String[] args) {
System.out.println("Hello, OpenCV")
// Load the native library.
System.loadLibrary("opencv_java246")
new DetectFaceDemo().run()
}
}
//运行结果:
//Hello, OpenCV
//
//Running DetectFaceDemo
///E:/eclipse_Jee/workspace/JavaOpenCV246/bin/com/njupt/zhb/test/lbpcascade_frontalface.xml
//Detected 8 faces
//Writing faceDetection.png
/*function prototype (declaration).*// *函数原型(声明).* /
/*create a global variable named stepFlg use to note the step.*/
/ *创建一个叫stepFlg的全局变量记录步骤.* /
/*out of size.*/ /* 过大 */
/*the pionted location is not empty. 指定的位置不为空 */
/*okay, put down the chessman. OK,落子*/
/*user input.用户输入 */
/*the risk level status of the chessboard.棋盘的风险等级*/
/*the attack level status of the chessboard. 棋盘的进攻力度等级*/
/*obviate the special satus of the first chessman 忽略先手的特殊位置*/
/*now, flag the first chessman had down. 现在标记第一个棋子 */
/*note the number of the step. 记录步数*/
/*evaluate the risk level and attack level of every row, column and slash. 评估每行每列以及斜线方向的风险和攻击等级 */
/*attck! 进攻!*/
/*attck a row. 横向进攻*/
/*attck a column. 纵向进攻*/
/*attack slash(\). 斜线进攻(\)*/
/*attack slash(/).斜线进攻(/)态清*/
/*locate the risk grid and put down one chessman to resolve it.定位有风险的格子并落子解除风险*/
/*resolve the risk of a Row. 解除横向风险*/
/*resolve the risk of a column.解除纵向风险*/
/*resolve the risk of a slash(\). 解除斜线风险(\)*/
/*resolve the risk of a slash(/). 解除斜线风险(/)*/
/*if there is no risk exist, put down the chessman in a blank(is not the best blank, may be).如果没有风险存在,雹磨在空位上落子(可能不是最好的空位) */
/*display the current chessman board. 显示当前局面*/
/*print the head. 打印head*/
/*print the contect.打印contect*/
/*print the floor. 打印floor*/
/*check whether win this game. 检测是否应得本盘棋局*/
后面打印部分的注释,怎么翻译要源闭斗具体看程序是怎么定义的head,contect和floor啊~
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)