Last active
August 29, 2015 14:10
-
-
Save waldemarnt/894167c80e62424c449f to your computer and use it in GitHub Desktop.
Example of template javacv (opencv) template matching using the last java build
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package javacv; | |
import java.awt.Graphics; | |
import java.awt.Image; | |
import java.awt.image.BufferedImage; | |
import java.awt.image.CropImageFilter; | |
import java.awt.image.FilteredImageSource; | |
import java.io.File; | |
import java.net.URL; | |
import java.rmi.server.RemoteObjectInvocationHandler; | |
import javax.naming.Context; | |
import javax.swing.ImageIcon; | |
import org.bytedeco.javacv.*; | |
import org.bytedeco.javacpp.*; | |
import static org.bytedeco.javacpp.opencv_core.*; | |
import static org.bytedeco.javacpp.opencv_imgproc.*; | |
import static org.bytedeco.javacpp.opencv_calib3d.*; | |
import static org.bytedeco.javacpp.opencv_objdetect.*; | |
import static org.bytedeco.javacpp.opencv_core.*; | |
import static org.bytedeco.javacpp.opencv_highgui.*; | |
import static org.bytedeco.javacpp.opencv_imgproc.*; | |
import static org.bytedeco.javacpp.opencv_core.cvSetImageROI; | |
import static org.bytedeco.javacpp.opencv_core.cvMinMaxLoc; | |
import org.bytedeco.javacpp.opencv_core.CvRect; | |
public class Test { | |
static Image image; | |
public static void main(String[] args) throws Exception { | |
int width = Integer.parseInt(args[3]); | |
int height = Integer.parseInt(args[4]); | |
IplImage src = cvLoadImage( | |
args[0], 0); | |
IplImage tmp = cvLoadImage( | |
args[1], 0); | |
IplImage result = cvCreateImage( | |
cvSize(src.width() - tmp.width() + 1, | |
src.height() - tmp.height() + 1), IPL_DEPTH_32F, src.nChannels()); | |
cvZero(result); | |
// Match Template Function from OpenCV | |
cvMatchTemplate(src, tmp, result, CV_TM_CCORR_NORMED); | |
// double[] min_val = new double[2]; | |
// double[] max_val = new double[2]; | |
DoublePointer min_val = new DoublePointer(); | |
DoublePointer max_val = new DoublePointer(); | |
CvPoint minLoc = new CvPoint(); | |
CvPoint maxLoc = new CvPoint(); | |
cvMinMaxLoc(result, min_val, max_val, minLoc, maxLoc, null); | |
// Get the Max or Min Correlation Value | |
// System.out.println(Arrays.toString(min_val)); | |
// System.out.println(Arrays.toString(max_val)); | |
CvPoint point = new CvPoint(); | |
point.x(maxLoc.x() + tmp.width()); | |
point.y(maxLoc.y() + tmp.height()); | |
// cvMinMaxLoc(src, min_val, max_val,0,0,result); | |
cvRectangle(src, maxLoc, point, CvScalar.RED, 2, 8, 0);// Draw a | |
// Rectangle for | |
// Matched | |
// Region | |
CvRect rect = new CvRect(); | |
rect.x(maxLoc.x()); | |
rect.y(maxLoc.y()); | |
rect.width(tmp.width() + width); | |
rect.height(tmp.width() + height); | |
cvSetImageROI(src, rect); | |
IplImage imageNew = cvCreateImage(cvGetSize(src), src.depth(), | |
src.nChannels()); | |
cvCopy(src, imageNew); | |
cvSaveImage(args[2], imageNew); | |
cvShowImage("Lena Image", src); | |
cvWaitKey(0); | |
cvReleaseImage(src); | |
cvReleaseImage(tmp); | |
cvReleaseImage(result); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
we need 4 default parameters like this
"C:\Users\Waldema\Desktop\bg.jpg" "C:\Users\Waldema\Desktop\logosiemens.jpg" "C:\Users\Waldema\Desktop\imageToFind.jpg" 100 200
configure in the Run configurations