changing layout, adding ant and eclipse sections, more pictures
This commit is contained in:
44
samples/java/sbt/src/main/java/DetectFaceDemo.java
Normal file
44
samples/java/sbt/src/main/java/DetectFaceDemo.java
Normal file
@@ -0,0 +1,44 @@
|
||||
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");
|
||||
|
||||
// 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(
|
||||
"/AverageMaleFace.jpg").getPath());
|
||||
|
||||
// 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);
|
||||
}
|
||||
}
|
BIN
samples/java/sbt/src/main/resources/AverageMaleFace.jpg
Normal file
BIN
samples/java/sbt/src/main/resources/AverageMaleFace.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 15 KiB |
BIN
samples/java/sbt/src/main/resources/img1.bmp
Normal file
BIN
samples/java/sbt/src/main/resources/img1.bmp
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.5 MiB |
BIN
samples/java/sbt/src/main/resources/img2.bmp
Normal file
BIN
samples/java/sbt/src/main/resources/img2.bmp
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.5 MiB |
1505
samples/java/sbt/src/main/resources/lbpcascade_frontalface.xml
Normal file
1505
samples/java/sbt/src/main/resources/lbpcascade_frontalface.xml
Normal file
File diff suppressed because it is too large
Load Diff
20
samples/java/sbt/src/main/scala/Main.scala
Normal file
20
samples/java/sbt/src/main/scala/Main.scala
Normal file
@@ -0,0 +1,20 @@
|
||||
/*
|
||||
* The main runner for the Java demos.
|
||||
* Demos whose name begins with "Scala" are written in the Scala language,
|
||||
* demonstrating the generic nature of the interface.
|
||||
* The other demos are in Java.
|
||||
* Currently, all demos are run, sequentially.
|
||||
*
|
||||
* You're invited to submit your own examples, in any JVM language of
|
||||
* your choosing so long as you can get them to build.
|
||||
*/
|
||||
object Main extends App {
|
||||
// We must load the native library before using any OpenCV functions.
|
||||
// You must load this library _exactly once_ per Java invocation.
|
||||
// If you load it more than once, you will get a java.lang.UnsatisfiedLinkError.
|
||||
System.loadLibrary("opencv_java")
|
||||
|
||||
ScalaCorrespondenceMatchingDemo.run()
|
||||
ScalaDetectFaceDemo.run()
|
||||
new DetectFaceDemo().run()
|
||||
}
|
@@ -0,0 +1,69 @@
|
||||
import org.opencv.highgui.Highgui
|
||||
import org.opencv.features2d.DescriptorExtractor
|
||||
import org.opencv.features2d.Features2d
|
||||
import org.opencv.core.MatOfKeyPoint
|
||||
import org.opencv.core.Mat
|
||||
import org.opencv.features2d.FeatureDetector
|
||||
import org.opencv.features2d.DescriptorMatcher
|
||||
import org.opencv.core.MatOfDMatch
|
||||
import reflect._
|
||||
|
||||
/*
|
||||
* Finds corresponding points between a pair of images using local descriptors.
|
||||
* The correspondences are visualized in the image "scalaCorrespondences.png",
|
||||
* which is written to disk.
|
||||
*/
|
||||
object ScalaCorrespondenceMatchingDemo {
|
||||
def run() {
|
||||
println(s"\nRunning ${classTag[this.type].toString.replace("$", "")}")
|
||||
|
||||
// Detects keypoints and extracts descriptors in a given image of type Mat.
|
||||
def detectAndExtract(mat: Mat) = {
|
||||
// A special container class for KeyPoint.
|
||||
val keyPoints = new MatOfKeyPoint
|
||||
// We're using the SURF detector.
|
||||
val detector = FeatureDetector.create(FeatureDetector.SURF)
|
||||
detector.detect(mat, keyPoints)
|
||||
|
||||
println(s"There were ${keyPoints.toArray.size} KeyPoints detected")
|
||||
|
||||
// Let's just use the best KeyPoints.
|
||||
val sorted = keyPoints.toArray.sortBy(_.response).reverse.take(50)
|
||||
// There isn't a constructor that takes Array[KeyPoint], so we unpack
|
||||
// the array and use the constructor that can take any number of
|
||||
// arguments.
|
||||
val bestKeyPoints: MatOfKeyPoint = new MatOfKeyPoint(sorted: _*)
|
||||
|
||||
// We're using the SURF descriptor.
|
||||
val extractor = DescriptorExtractor.create(DescriptorExtractor.SURF)
|
||||
val descriptors = new Mat
|
||||
extractor.compute(mat, bestKeyPoints, descriptors)
|
||||
|
||||
println(s"${descriptors.rows} descriptors were extracted, each with dimension ${descriptors.cols}")
|
||||
|
||||
(bestKeyPoints, descriptors)
|
||||
}
|
||||
|
||||
// Load the images from the |resources| directory.
|
||||
val leftImage = Highgui.imread(getClass.getResource("/img1.bmp").getPath)
|
||||
val rightImage = Highgui.imread(getClass.getResource("/img2.bmp").getPath)
|
||||
|
||||
// Detect KeyPoints and extract descriptors.
|
||||
val (leftKeyPoints, leftDescriptors) = detectAndExtract(leftImage)
|
||||
val (rightKeyPoints, rightDescriptors) = detectAndExtract(rightImage)
|
||||
|
||||
// Match the descriptors.
|
||||
val matcher = DescriptorMatcher.create(DescriptorMatcher.BRUTEFORCE)
|
||||
// A special container class for DMatch.
|
||||
val dmatches = new MatOfDMatch
|
||||
// The backticks are because "match" is a keyword in Scala.
|
||||
matcher.`match`(leftDescriptors, rightDescriptors, dmatches)
|
||||
|
||||
// Visualize the matches and save the visualization.
|
||||
val correspondenceImage = new Mat
|
||||
Features2d.drawMatches(leftImage, leftKeyPoints, rightImage, rightKeyPoints, dmatches, correspondenceImage)
|
||||
val filename = "scalaCorrespondences.png"
|
||||
println(s"Writing ${filename}")
|
||||
assert(Highgui.imwrite(filename, correspondenceImage))
|
||||
}
|
||||
}
|
43
samples/java/sbt/src/main/scala/ScalaDetectFaceDemo.scala
Normal file
43
samples/java/sbt/src/main/scala/ScalaDetectFaceDemo.scala
Normal file
@@ -0,0 +1,43 @@
|
||||
import org.opencv.core.Core
|
||||
import org.opencv.core.MatOfRect
|
||||
import org.opencv.core.Point
|
||||
import org.opencv.core.Scalar
|
||||
import org.opencv.highgui.Highgui
|
||||
import org.opencv.objdetect.CascadeClassifier
|
||||
import reflect._
|
||||
|
||||
/*
|
||||
* Detects faces in an image, draws boxes around them, and writes the results
|
||||
* to "scalaFaceDetection.png".
|
||||
*/
|
||||
object ScalaDetectFaceDemo {
|
||||
def run() {
|
||||
println(s"\nRunning ${classTag[this.type].toString.replace("$", "")}")
|
||||
|
||||
// Create a face detector from the cascade file in the resources directory.
|
||||
val faceDetector = new CascadeClassifier(getClass.getResource("/lbpcascade_frontalface.xml").getPath)
|
||||
val image = Highgui.imread(getClass.getResource("/AverageMaleFace.jpg").getPath)
|
||||
|
||||
// Detect faces in the image.
|
||||
// MatOfRect is a special container class for Rect.
|
||||
val faceDetections = new MatOfRect
|
||||
faceDetector.detectMultiScale(image, faceDetections)
|
||||
|
||||
println(s"Detected ${faceDetections.toArray.size} faces")
|
||||
|
||||
// Draw a bounding box around each face.
|
||||
for (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.
|
||||
val filename = "scalaFaceDetection.png"
|
||||
println(s"Writing ${filename}")
|
||||
assert(Highgui.imwrite(filename, image))
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user