Skip to content

Commit

Permalink
Convert examples in chapter 8 to JavaCV 0.8.
Browse files Browse the repository at this point in the history
  • Loading branch information
jpsacha committed May 9, 2014
1 parent 51a1293 commit da53014
Show file tree
Hide file tree
Showing 9 changed files with 290 additions and 254 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -237,12 +237,16 @@ object OpenCVUtils {
bi
}


/** Draw circles at key point locations on an image. Circle radius is proportional to key point size. */
def drawOnImage(image: IplImage, points: KeyPoint): Image = {
drawOnImage(image, toArray(points))
}

/** Draw circles at key point locations on an image. Circle radius is proportional to key point size. */
def drawOnImage(image: Mat, points: KeyPoint): Image = {
drawOnImage(image.asIplImage(), toArray(points))
}


/** Draw a shape on an image.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
/*
* Copyright (c) 2011-2013 Jarek Sacha. All Rights Reserved.
* Copyright (c) 2011-2014 Jarek Sacha. All Rights Reserved.
*
* Author's e-mail: jpsacha at gmail.com
*/

package opencv2_cookbook.chapter08

import com.googlecode.javacv.cpp.opencv_core._
import com.googlecode.javacv.cpp.opencv_imgproc._
import java.io.File
import opencv2_cookbook.OpenCVUtils._
import org.bytedeco.javacpp.opencv_core._
import org.bytedeco.javacpp.opencv_imgproc._


/**
Expand All @@ -20,20 +20,20 @@ import opencv2_cookbook.OpenCVUtils._
*/
object Ex1HarrisCornerMap extends App {

// Read input image
val image = loadAndShowOrExit(new File("data/church01.jpg"))
// Read input image
val image = loadAndShowOrExit(new File("data/church01.jpg"))

// Image to store the Harris detector responses.
val cornerStrength = IplImage.create(cvGetSize(image), IPL_DEPTH_32F, 1)
// Detect Harris Corners
cvCornerHarris(image, cornerStrength,
3 /* neighborhood size */ ,
3 /* aperture size */ ,
0.01 /* Harris parameter */)
// Image to store the Harris detector responses.
val cornerStrength = cvCreateImage(cvGetSize(image), IPL_DEPTH_32F, 1)
// Detect Harris Corners
cvCornerHarris(image, cornerStrength,
3 /* neighborhood size */ ,
3 /* aperture size */ ,
0.01 /* Harris parameter */)

// Threshold to retain only locations of strongest corners
val harrisCorners = IplImage.create(cvGetSize(image), IPL_DEPTH_8U, 1)
val threshold = 0.0001
cvThreshold(cornerStrength, harrisCorners, threshold, 255, CV_THRESH_BINARY_INV)
show(harrisCorners, "Harris Corner Map")
// Threshold to retain only locations of strongest corners
val harrisCorners = cvCreateImage(cvGetSize(image), IPL_DEPTH_8U, 1)
val threshold = 0.0001
cvThreshold(cornerStrength, harrisCorners, threshold, 255, CV_THRESH_BINARY_INV)
show(harrisCorners, "Harris Corner Map")
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
/*
* Copyright (c) 2011-2013 Jarek Sacha. All Rights Reserved.
* Copyright (c) 2011-2014 Jarek Sacha. All Rights Reserved.
*
* Author's e-mail: jpsacha at gmail.com
*/

package opencv2_cookbook.chapter08

import com.googlecode.javacv.cpp.opencv_features2d.{KeyPoint, GFTTDetector}
import java.io.File
import opencv2_cookbook.OpenCVUtils._
import org.bytedeco.javacpp.opencv_features2d.{KeyPoint, GFTTDetector}


/**
Expand All @@ -18,21 +18,21 @@ import opencv2_cookbook.OpenCVUtils._
*/
object Ex3GoodFeaturesToTrack extends App {

// Read input image
val image = loadAndShowOrExit(new File("data/church01.jpg"))
// Read input image
val image = loadMatAndShowOrExit(new File("data/church01.jpg"))

// Compute good features to track
val gftt = new GFTTDetector(
500 /* maximum number of corners to be returned */ ,
0.01 /* quality level*/ ,
10.0 /* minimum allowed distance between points*/ ,
3 /* block size*/ ,
false /* use Harris detector*/ ,
0.04 /* Harris parameter */
)
val keyPoints = new KeyPoint()
gftt.detect(image, keyPoints, null)
// Compute good features to track
val gftt = new GFTTDetector(
500 /* maximum number of corners to be returned */ ,
0.01 /* quality level*/ ,
10.0 /* minimum allowed distance between points*/ ,
3 /* block size*/ ,
false /* use Harris detector*/ ,
0.04 /* Harris parameter */
)
val keyPoints = new KeyPoint()
gftt.detect(image, keyPoints)

// Draw keyPoints
show(drawOnImage(image, keyPoints), "Good Features to Track Detector")
// Draw keyPoints
show(drawOnImage(image, keyPoints), "Good Features to Track Detector")
}
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
/*
* Copyright (c) 2011-2013 Jarek Sacha. All Rights Reserved.
* Copyright (c) 2011-2014 Jarek Sacha. All Rights Reserved.
*
* Author's e-mail: jpsacha at gmail.com
*/

package opencv2_cookbook.chapter08

import com.googlecode.javacv.cpp.opencv_features2d.{KeyPoint, FastFeatureDetector}
import java.io.File
import opencv2_cookbook.OpenCVUtils._
import org.bytedeco.javacpp.opencv_features2d.{KeyPoint, FastFeatureDetector}

/**
* The example for section "Detecting FAST features" in Chapter 8, page 203.
*/
object Ex4FAST extends App {

// Read input image
val image = loadAndShowOrExit(new File("data/church01.jpg"))
// Read input image
val image = loadMatAndShowOrExit(new File("data/church01.jpg"))

// Detect FAST features
val ffd = new FastFeatureDetector(
40 /* threshold for detection */ ,
true /* non-max suppression */)
val keyPoints = new KeyPoint()
ffd.detect(image, keyPoints, null)
// Detect FAST features
val ffd = new FastFeatureDetector(
40 /* threshold for detection */ ,
true /* non-max suppression */)
val keyPoints = new KeyPoint()
ffd.detect(image, keyPoints)

// Draw keyPoints
show(drawOnImage(image, keyPoints), "FAST Features")
// Draw keyPoints
show(drawOnImage(image, keyPoints), "FAST Features")
}
Original file line number Diff line number Diff line change
@@ -1,38 +1,39 @@
/*
* Copyright (c) 2011-2013 Jarek Sacha. All Rights Reserved.
* Copyright (c) 2011-2014 Jarek Sacha. All Rights Reserved.
*
* Author's e-mail: jpsacha at gmail.com
*/

package opencv2_cookbook.chapter08

import com.googlecode.javacv.cpp.opencv_core._
import com.googlecode.javacv.cpp.opencv_features2d._
import com.googlecode.javacv.cpp.opencv_nonfree._
import java.io.File
import opencv2_cookbook.OpenCVUtils._
import org.bytedeco.javacpp.opencv_core._
import org.bytedeco.javacpp.opencv_features2d._
import org.bytedeco.javacpp.opencv_nonfree._


/**
* Example of extracting SURF features from section "Detecting the scale-invariant SURF features" in chapter 8.
*/
object Ex5SURF extends App {

// Read input image
val image = loadAndShowOrExit(new File("data/church01.jpg"))
// Read input image
val image = loadMatAndShowOrExit(new File("data/church01.jpg"))

// Detect SURF features.
val keyPoints = new KeyPoint()
val hessianThreshold = 2500d
val nOctaves = 4
val nOctaveLayers = 2
val extended = true
val upright = false
val surf = new SURF(hessianThreshold, nOctaves, nOctaveLayers, extended, upright)
surf.detect(image, null, keyPoints)
// Detect SURF features.
val keyPoints = new KeyPoint()
val hessianThreshold = 2500d
val nOctaves = 4
val nOctaveLayers = 2
val extended = true
val upright = false
val surf = new SURF(hessianThreshold, nOctaves, nOctaveLayers, extended, upright)
surf.detect(image, keyPoints)

// Draw keyPoints
val featureImage = IplImage.create(cvGetSize(image), image.depth(), 3)
drawKeypoints(image, keyPoints, featureImage, CvScalar.WHITE, DrawMatchesFlags.DRAW_RICH_KEYPOINTS)
show(featureImage, "SURF Features")
// Draw keyPoints
// val featureImage = cvCreateImage(cvGetSize(image), image.depth(), 3)
val featureImage = new Mat()
drawKeypoints(image, keyPoints, featureImage, new Scalar(255, 255, 255, 0), DrawMatchesFlags.DRAW_RICH_KEYPOINTS)
show(featureImage, "SURF Features")
}
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
/*
* Copyright (c) 2011-2013 Jarek Sacha. All Rights Reserved.
* Copyright (c) 2011-2014 Jarek Sacha. All Rights Reserved.
*
* Author's e-mail: jpsacha at gmail.com
*/

package opencv2_cookbook.chapter08

import com.googlecode.javacv.cpp.opencv_core._
import com.googlecode.javacv.cpp.opencv_features2d._
import com.googlecode.javacv.cpp.opencv_nonfree._
import java.io.File
import opencv2_cookbook.OpenCVUtils._
import org.bytedeco.javacpp.opencv_core._
import org.bytedeco.javacpp.opencv_features2d._
import org.bytedeco.javacpp.opencv_nonfree._


/**
* Example of extracting SIFT features from section "Detecting the scale-invariant SURF features" in chapter 8.
*/
object Ex6SIFT extends App {

// Read input image
val image = loadAndShowOrExit(new File("data/church01.jpg"))
// Read input image
val image = loadMatAndShowOrExit(new File("data/church01.jpg"))

// Detect SIFT features.
val keyPoints = new KeyPoint()
val nFeatures = 0
val nOctaveLayers = 3
val contrastThreshold = 0.03
val edgeThreshold = 10
val sigma = 1.6
val sift = new SIFT(nFeatures, nOctaveLayers, contrastThreshold, edgeThreshold, sigma)
sift.detect(image, null, keyPoints)
// Detect SIFT features.
val keyPoints = new KeyPoint()
val nFeatures = 0
val nOctaveLayers = 3
val contrastThreshold = 0.03
val edgeThreshold = 10
val sigma = 1.6
val sift = new SIFT(nFeatures, nOctaveLayers, contrastThreshold, edgeThreshold, sigma)
sift.detect(image, keyPoints)

// Draw keyPoints
val featureImage = IplImage.create(cvGetSize(image), image.depth(), 3)
drawKeypoints(image, keyPoints, featureImage, CvScalar.WHITE, DrawMatchesFlags.DRAW_RICH_KEYPOINTS)
show(featureImage, "SIFT Features")
// Draw keyPoints
val featureImage = new Mat()
drawKeypoints(image, keyPoints, featureImage, new Scalar(255, 255, 255, 0), DrawMatchesFlags.DRAW_RICH_KEYPOINTS)
show(featureImage, "SIFT Features")
}
Loading

0 comments on commit da53014

Please sign in to comment.