- •Дослідження основних проблем предметної області
- •Роль інформаційних технологій у предметній області
- •Загальний опис проблем предметної області.
- •Актуальність даної теми.
- •Аналіз існуючих аналогів
- •Метод Віоли-Джонса
- •Метод surf
- •Метод sift
- •Метод orc
- •Порівняльна характеристика
- •Мета кваліфікаційної роботи
- •Постановка задачі
- •Об’єкт та методи дослідження
- •Програмна підтимка дослідження
- •Інформаційна модель системи
- •Детальний опис реалізації основних процедур та функцій додатку з прикладами.
- •Рекомендації по впровадженню та використанню
- •Порівняльний аналіз методів
- •Оцінка складності алгоритму контурного аналізу
- •Аналіз продуктивності методів
- •Рекомендаціі до використання методів
- •Охорона праці
- •Економічна частина диплому
- •Цивільний захист
- •Наукова новизна та практичне значення одержаних результатів
- •Перелік використаних джерел
- •Додаток a
- •Додаток b
Додаток a
Додаток b
package p;
import static com.googlecode.javacv.cpp.opencv_core.CV_AA;
import static com.googlecode.javacv.cpp.opencv_core.IPL_DEPTH_8U;
import static com.googlecode.javacv.cpp.opencv_core.cvClearMemStorage;
import static com.googlecode.javacv.cpp.opencv_core.cvDrawContours;
import static com.googlecode.javacv.cpp.opencv_core.cvGetSeqElem;
import static com.googlecode.javacv.cpp.opencv_core.cvLoad;
import static com.googlecode.javacv.cpp.opencv_core.cvPoint;
import static com.googlecode.javacv.cpp.opencv_core.cvRectangle;
import static com.googlecode.javacv.cpp.opencv_imgproc.*;
import static com.googlecode.javacv.cpp.opencv_objdetect.CV_HAAR_DO_CANNY_PRUNING;
import static com.googlecode.javacv.cpp.opencv_objdetect.cvHaarDetectObjects;
import java.awt.Frame;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Scanner;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
import com.googlecode.javacpp.Loader;
import com.googlecode.javacv.CanvasFrame;
import com.googlecode.javacv.OpenCVFrameGrabber;
import com.googlecode.javacv.cpp.opencv_objdetect;
import com.googlecode.javacv.cpp.opencv_core.CvContour;
import com.googlecode.javacv.cpp.opencv_core.CvMemStorage;
import com.googlecode.javacv.cpp.opencv_core.CvRect;
import com.googlecode.javacv.cpp.opencv_core.CvScalar;
import com.googlecode.javacv.cpp.opencv_core.CvSeq;
import com.googlecode.javacv.cpp.opencv_core.IplImage;
import com.googlecode.javacv.cpp.opencv_imgproc.CvHuMoments;
import com.googlecode.javacv.cpp.opencv_imgproc.CvMoments;
import com.googlecode.javacv.cpp.opencv_objdetect.CvHaarClassifierCascade;
public class Main {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new GridLayout(1, true));
shell.setText("Детектор");
Image image = new Image(display, "C:\\Eye-icon.ico");
shell.setImage(image);
//group box for radio buttons
GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
Group group1 = new Group(shell, SWT.NONE);
group1.setLayoutData(gd);
group1.setText("Выбор метода детектирования объектов: ");
group1.setLayout(new GridLayout(1, true));
//contours analis radio buuton
gd = new GridData(SWT.LEFT, SWT.CENTER, true, true);
final Button button1 = new Button(group1, SWT.RADIO);
button1.setLayoutData(gd);
button1.setText("Контурный анализ");
//haar radio buuton
gd = new GridData(SWT.LEFT, SWT.CENTER, true, true);
final Button button2 = new Button(group1, SWT.RADIO);
button2.setLayoutData(gd);
button2.setText("Каскад Хаара");
//group box for settings
gd = new GridData(SWT.FILL, SWT.FILL, true, true);
final Group group2 = new Group(shell, SWT.NONE);
gd.grabExcessHorizontalSpace = true;
group2.setLayoutData(gd);
group2.setText("Дополнительные настройки: ");
GridLayout gl = new GridLayout(3, false);
gl.makeColumnsEqualWidth = false;
group2.setLayout(gl);
//Gaussian
gd = new GridData(SWT.RIGHT, SWT.CENTER, true, true);
final Label label1 = new Label(group2, SWT.NONE);
label1.setText("Фильтр Гаусса:");
label1.setLayoutData(gd);
gd = new GridData(SWT.FILL, SWT.CENTER, true, false);
final Scale scale1 = new Scale(group2, SWT.NONE);
scale1.setMinimum(0);
scale1.setMaximum(31);
scale1.setIncrement(2);
scale1.setPageIncrement(2);
scale1.setSelection(9);
scale1.setLayoutData(gd);
gd = new GridData(SWT.LEFT, SWT.CENTER, true, true);
final Label label2 = new Label(group2, SWT.NONE);
label2.setText("9 ");
label2.setLayoutData(gd);
//canny1
gd = new GridData(SWT.RIGHT, SWT.CENTER, true, true);
final Label label3 = new Label(group2, SWT.NONE);
label3.setText("Верхний порог Canny:");
label3.setLayoutData(gd);
gd = new GridData(SWT.FILL, SWT.CENTER, true, false);
final Scale scale2 = new Scale(group2, SWT.NONE);
scale2.setMinimum(1);
scale2.setMaximum(255);
scale2.setPageIncrement(5);
scale2.setSelection(100);
scale2.setLayoutData(gd);
gd = new GridData(SWT.LEFT, SWT.CENTER, true, true);
final Label label4 = new Label(group2, SWT.NONE);
label4.setText("100 ");
label4.setLayoutData(gd);
//canny2
gd = new GridData(SWT.RIGHT, SWT.CENTER, true, true);
final Label label5 = new Label(group2, SWT.NONE);
label5.setText("Нижний порог Canny:");
label5.setLayoutData(gd);
gd = new GridData(SWT.FILL, SWT.CENTER, true, false);
final Scale scale3 = new Scale(group2, SWT.NONE);
scale3.setMinimum(1);
scale3.setMaximum(255);
scale3.setPageIncrement(5);
scale3.setSelection(100);
scale3.setLayoutData(gd);
gd = new GridData(SWT.LEFT, SWT.CENTER, true, true);
final Label label6 = new Label(group2, SWT.NONE);
label6.setText("100 ");
label6.setLayoutData(gd);
//check box buuton
gd = new GridData(SWT.LEFT, SWT.CENTER, true, true);
gd.horizontalSpan = 3;
final Button button5 = new Button(group2, SWT.CHECK);
button5.setLayoutData(gd);
button5.setText("Показать окно результатов оператора Canny");
//group box for selecting file or capture
gd = new GridData(SWT.FILL, SWT.FILL, true, true);
Group group4 = new Group(shell, SWT.NONE);
group4.setLayoutData(gd);
group4.setText("Выбор видеопотока: ");
group4.setLayout(new GridLayout(2, true));
//capture
gd = new GridData(SWT.LEFT, SWT.CENTER, true, true);
final Button button6 = new Button(group4, SWT.RADIO);
gd.horizontalSpan = 2;
button6.setLayoutData(gd);
button6.setText("Веб-камера");
button6.setSelection(true);
//file stream
gd = new GridData(SWT.LEFT, SWT.CENTER, true, true);
final Button button7 = new Button(group4, SWT.RADIO);
gd.horizontalSpan = 2;
button7.setLayoutData(gd);
button7.setText("Из файла");
//file path
gd = new GridData(SWT.FILL, SWT.CENTER, true, true);
final Text text1 = new Text(group4, SWT.BORDER);
text1.setLayoutData(gd);
//browse
gd = new GridData(SWT.LEFT, SWT.CENTER, true, true);
final Button button8 = new Button(group4, SWT.PUSH);
button8.setLayoutData(gd);
button8.setText("...");
//group box for two buttons: start and stop
gd = new GridData(SWT.FILL, SWT.FILL, true, true);
Group group3 = new Group(shell, SWT.NONE);
group3.setLayoutData(gd);
group3.setText("Управление:");
group3.setLayout(new GridLayout(2, true));
gd = new GridData(SWT.FILL, SWT.FILL, true, true);
final Button button3 = new Button(group3, SWT.PUSH);
button3.setLayoutData(gd);
button3.setText("Запустить");
gd = new GridData(SWT.FILL, SWT.FILL, true, true);
final Button button4 = new Button(group3, SWT.PUSH);
button4.setLayoutData(gd);
button4.setText("Остановить");
button4.setEnabled(false);
final Contours contours = new Contours();
final Thread contoursThread = new Thread(contours);
final Haar haar = new Haar();
final Thread haarThread = new Thread(haar);
button1.addListener(SWT.MouseDown, new Listener() {
@Override
public void handleEvent(Event event) {
label1.setEnabled(true);
label2.setEnabled(true);
label3.setEnabled(true);
label4.setEnabled(true);
label5.setEnabled(true);
label6.setEnabled(true);
scale1.setEnabled(true);
scale2.setEnabled(true);
scale3.setEnabled(true);
button5.setEnabled(true);
}
});
button2.addListener(SWT.MouseDown, new Listener() {
@Override
public void handleEvent(Event event) {
label1.setEnabled(false);
label2.setEnabled(false);
label3.setEnabled(false);
label4.setEnabled(false);
label5.setEnabled(false);
label6.setEnabled(false);
scale1.setEnabled(false);
scale2.setEnabled(false);
scale3.setEnabled(false);
button5.setEnabled(false);
}
});
button3.addListener(SWT.MouseDown, new Listener() {
@Override
public void handleEvent(Event arg0) {
if (button1.getSelection()) {
contoursThread.start();
} else {
haarThread.start();
}
button1.setEnabled(false);
button2.setEnabled(false);
button3.setEnabled(false);
button4.setEnabled(true);
}
});
button4.addListener(SWT.MouseDown, new Listener() {
@Override
public void handleEvent(Event arg0) {
if (button1.getSelection()) {
contours.stop();
} else {
haar.stop();
}
button1.setEnabled(true);
button2.setEnabled(true);
button3.setEnabled(true);
button4.setEnabled(false);
}
});
scale1.addListener(SWT.Selection, new Listener() {
@Override
public void handleEvent(Event event) {
contours.gaussianThr = scale1.getSelection();
//label2.setText(String.valueOf(scale1.getSelection()));
if ((contours.gaussianThr % 2) == 0) {
contours.gaussianThr++;
scale1.setSelection(contours.gaussianThr);
label2.setText(String.valueOf(scale1.getSelection()));
}
}
});
scale2.addListener(SWT.Selection, new Listener() {
@Override
public void handleEvent(Event event) {
contours.cannyThr1 = scale2.getSelection();
label4.setText(String.valueOf(scale2.getSelection()));
}
});
scale3.addListener(SWT.Selection, new Listener() {
@Override
public void handleEvent(Event event) {
contours.cannyThr2 = scale3.getSelection();
label6.setText(String.valueOf(scale3.getSelection()));
}
});
button5.addListener(SWT.MouseDown, new Listener() {
@Override
public void handleEvent(Event event) {
if (button5.getSelection() != true) {
contours.createFrame();
} else {
contours.disposeFrame();
}
}
});
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (display.readAndDispatch())
display.sleep();
}
}
}
class Contours implements Runnable {
CanvasFrame frame;
CanvasFrame frame2;
public int cannyThr1 = 100;
public int cannyThr2 = 100;
public int gaussianThr = 9;
boolean fr2 = false;
static boolean stoped;
public void createFrame() {
if (frame2 == null) {
frame2 = new CanvasFrame("Gray Frame");
frame2.setDefaultCloseOperation(CanvasFrame.EXIT_ON_CLOSE);
}
}
public void disposeFrame() {
if (frame2 != null) {
frame2.dispose();
}
}
public void run() {
frame = new CanvasFrame("Main Frame");
frame.setDefaultCloseOperation(CanvasFrame.EXIT_ON_CLOSE);
stoped = false;
try {
OpenCVFrameGrabber grabber = new OpenCVFrameGrabber("C:\\3test.avi");
grabber.start();
while (!stoped) {
IplImage Grab = grabber.grab();
if (Grab == null)
return;
Grab = Detect(Grab);
frame.showImage(Grab);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
frame.dispose();
if (frame2 != null)
frame2.dispose();
}
}
public void stop() {
stoped = true;
}
public IplImage Detect(IplImage Grab) {
CvMemStorage storage = CvMemStorage.create(0);
CvSeq contour = new CvSeq(null);
CvSeq seqhull = new CvSeq(null);
CvMoments moments = new CvMoments();
CvHuMoments hu_moments = new CvHuMoments();
CvHuMoments hu_templ = new CvHuMoments();
try {
Scanner fileScan = new Scanner(new BufferedReader(new FileReader(
"C:\\moments.txt")));
if (fileScan.hasNext())
hu_templ.hu1(fileScan.nextDouble());
if (fileScan.hasNext())
hu_templ.hu2(fileScan.nextDouble());
if (fileScan.hasNext())
hu_templ.hu3(fileScan.nextDouble());
if (fileScan.hasNext())
hu_templ.hu4(fileScan.nextDouble());
if (fileScan.hasNext())
hu_templ.hu5(fileScan.nextDouble());
if (fileScan.hasNext())
hu_templ.hu6(fileScan.nextDouble());
if (fileScan.hasNext())
hu_templ.hu7(fileScan.nextDouble());
} catch (Exception e) {
}
IplImage gray = IplImage.create(Grab.width(), Grab.height(),
IPL_DEPTH_8U, 1);
if (Grab == null) {
System.err.println("Невозможно загрузить изображение.");
} else {
cvCvtColor(Grab, gray, CV_BGR2GRAY); // Получаем серый цвет
//cvEqualizeHist(gray, gray);
cvSmooth(gray, gray, CV_GAUSSIAN, gaussianThr, 0, 0, 0);
//cvThreshold(gray, gray, 100, 110, CV_THRESH_BINARY);
// Необходимо удалить белый бордюр
// cvRectangle(gray, cvPoint(0,0),
// cvPoint(gray.width()-1,gray.height()-1),CvScalar.GRAY, 2, 8, 16);
// Утолщаем контуры
//cvDilate(gray, gray, null, 1);
// Нах. границы
cvCanny(gray, gray, cannyThr1, cannyThr2, 3);
// Нах. контуры
// frame.showImage(gray);
cvFindContours(gray, storage, contour,
Loader.sizeof(CvContour.class), CV_RETR_TREE,
CV_CHAIN_APPROX_SIMPLE, cvPoint(0, 0));
int NC = 0;
while (contour != null && !contour.isNull()) {
if (contour.elem_size() > 0) {
/*
* CvSeq points = cvApproxPoly(contour,
* Loader.sizeof(CvContour.class), storage,
* CV_POLY_APPROX_DP, cvContourPerimeter(contour)*0.002, 0);
*
* //seqhull = cvConvexHull2(
* points,null,CV_COUNTER_CLOCKWISE,1);
*/
cvMoments(contour, moments, 1);
cvGetHuMoments(moments, hu_moments);
if (MatchShapes(hu_templ, hu_moments) < 100) {
// if (NC == 0) {
cvDrawContours(Grab, contour, CvScalar.RED,
CvScalar.RED, -1, 4, CV_AA);
/* CvRect r = cvBoundingRect(contour, 1);
int x = r.x(), y = r.y(), w = r.width(), h = r.height();
cvRectangle(Grab, cvPoint(x, y), cvPoint(x + w, y + h), CvScalar.MAGENTA, 4, CV_AA, 0);*/
}
System.out.print("NC = " + NC + "\n");
System.out.print("hu1 = " + hu_moments.hu1() + "\n");
System.out.print("hu2 = " + hu_moments.hu2() + "\n");
System.out.print("hu3 = " + hu_moments.hu3() + "\n");
System.out.print("hu4 = " + hu_moments.hu4() + "\n");
System.out.print("hu5 = " + hu_moments.hu5() + "\n");
System.out.print("hu6 = " + hu_moments.hu6() + "\n");
System.out.print("hu7 = " + hu_moments.hu7() + "\n");
NC++;
}
contour = contour.h_next();
}
if (frame2 != null)
frame2.showImage(gray);
}
return Grab;
}
private double MatchShapes(CvHuMoments hu_m_templ, CvHuMoments hu_m) {
double mt = 0;
double[] ma = new double[7];
double[] mb = new double[7];
ma[0] = hu_m_templ.hu1();
ma[1] = hu_m_templ.hu2();
ma[2] = hu_m_templ.hu3();
ma[3] = hu_m_templ.hu4();
ma[4] = hu_m_templ.hu5();
ma[5] = hu_m_templ.hu6();
ma[6] = hu_m_templ.hu7();
mb[0] = hu_m.hu1();
mb[1] = hu_m.hu2();
mb[2] = hu_m.hu3();
mb[3] = hu_m.hu4();
mb[4] = hu_m.hu5();
mb[5] = hu_m.hu6();
mb[6] = hu_m.hu7();
for (int i = 0; i < 7; i++) {
mt += Math.abs((Math.signum(ma[i]) * Math.log(Math.abs(ma[i])) - Math.signum(mb[i]) * Math.log(Math.abs(mb[i]))) /
Math.signum(ma[i]) * Math.log(Math.abs(ma[i])));
}
System.out.print("\nMatchResult = " + mt + "\n");
return mt;
}
}
class Haar implements Runnable {
private static final int SCALE = 2;
// scaling factor to reduce size of input image
// cascade definition for face detection
private static final String CASCADE_FILE = "C:\\out_ok_25.02.2012.xml";
static boolean stoped;
public void run() {
stoped = false;
System.out.println("Starting OpenCV...");
Loader.load(opencv_objdetect.class);
CanvasFrame frame = new CanvasFrame("Main Frame");
frame.setDefaultCloseOperation(CanvasFrame.EXIT_ON_CLOSE);
try {
OpenCVFrameGrabber grabber = new OpenCVFrameGrabber("C:\\pasport.avi");
grabber.start();
while (!stoped) {
IplImage origImg = grabber.grab();
if (origImg == null)
return;
// convert to grayscale
IplImage grayImg = IplImage.create(origImg.width(),
origImg.height(), IPL_DEPTH_8U, 1);
cvCvtColor(origImg, grayImg, CV_BGR2GRAY);
// scale the grayscale (to speed up face detection)
IplImage smallImg = IplImage.create(grayImg.width() / SCALE,
grayImg.height() / SCALE, IPL_DEPTH_8U, 1);
cvResize(grayImg, smallImg, CV_INTER_LINEAR);
// equalize the small grayscale
IplImage equImg = IplImage.create(smallImg.width(),
smallImg.height(), IPL_DEPTH_8U, 1);
cvEqualizeHist(smallImg, equImg);
// create temp storage, used during object detection
CvMemStorage storage = CvMemStorage.create();
// instantiate a classifier cascade for face detection
CvHaarClassifierCascade cascade = new CvHaarClassifierCascade(
cvLoad(CASCADE_FILE));
System.out.println("Detecting faces...");
CvSeq faces = cvHaarDetectObjects(equImg, cascade, storage,
1.1, 3, CV_HAAR_DO_CANNY_PRUNING);
cvClearMemStorage(storage);
// iterate over the faces and draw yellow rectangles around them
int total = faces.total();
System.out.println("Found " + total + " obj");
for (int i = 0; i < total; i++) {
CvRect r = new CvRect(cvGetSeqElem(faces, i));
cvRectangle(
origImg,
cvPoint(r.x() * SCALE, r.y() * SCALE), // undo the
// scaling
cvPoint((r.x() + r.width()) * SCALE,
(r.y() + r.height()) * SCALE),
CvScalar.YELLOW, 6, CV_AA, 0);
}
frame.showImage(origImg);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
frame.dispose();
}
}
public void stop() {
stoped = true;
}
}
