mirror of https://github.com/opencv/opencv.git
parent
1187a7fa34
commit
afa5b0cc93
13 changed files with 560 additions and 103 deletions
Before Width: | Height: | Size: 75 KiB After Width: | Height: | Size: 75 KiB |
@ -0,0 +1,98 @@ |
||||
import java.util.List; |
||||
|
||||
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.core.Size; |
||||
import org.opencv.highgui.HighGui; |
||||
import org.opencv.imgproc.Imgproc; |
||||
import org.opencv.objdetect.CascadeClassifier; |
||||
import org.opencv.videoio.VideoCapture; |
||||
|
||||
class ObjectDetection { |
||||
public void detectAndDisplay(Mat frame, CascadeClassifier faceCascade, CascadeClassifier eyesCascade) { |
||||
Mat frameGray = new Mat(); |
||||
Imgproc.cvtColor(frame, frameGray, Imgproc.COLOR_BGR2GRAY); |
||||
Imgproc.equalizeHist(frameGray, frameGray); |
||||
|
||||
// -- Detect faces
|
||||
MatOfRect faces = new MatOfRect(); |
||||
faceCascade.detectMultiScale(frameGray, faces); |
||||
|
||||
List<Rect> listOfFaces = faces.toList(); |
||||
for (Rect face : listOfFaces) { |
||||
Point center = new Point(face.x + face.width / 2, face.y + face.height / 2); |
||||
Imgproc.ellipse(frame, center, new Size(face.width / 2, face.height / 2), 0, 0, 360, |
||||
new Scalar(255, 0, 255)); |
||||
|
||||
Mat faceROI = frameGray.submat(face); |
||||
|
||||
// -- In each face, detect eyes
|
||||
MatOfRect eyes = new MatOfRect(); |
||||
eyesCascade.detectMultiScale(faceROI, eyes); |
||||
|
||||
List<Rect> listOfEyes = eyes.toList(); |
||||
for (Rect eye : listOfEyes) { |
||||
Point eyeCenter = new Point(face.x + eye.x + eye.width / 2, face.y + eye.y + eye.height / 2); |
||||
int radius = (int) Math.round((eye.width + eye.height) * 0.25); |
||||
Imgproc.circle(frame, eyeCenter, radius, new Scalar(255, 0, 0), 4); |
||||
} |
||||
} |
||||
|
||||
//-- Show what you got
|
||||
HighGui.imshow("Capture - Face detection", frame ); |
||||
} |
||||
|
||||
public void run(String[] args) { |
||||
String filenameFaceCascade = args.length > 2 ? args[0] : "../../data/haarcascades/haarcascade_frontalface_alt.xml"; |
||||
String filenameEyesCascade = args.length > 2 ? args[1] : "../../data/haarcascades/haarcascade_eye_tree_eyeglasses.xml"; |
||||
int cameraDevice = args.length > 2 ? Integer.parseInt(args[2]) : 0; |
||||
|
||||
CascadeClassifier faceCascade = new CascadeClassifier(); |
||||
CascadeClassifier eyesCascade = new CascadeClassifier(); |
||||
|
||||
if (!faceCascade.load(filenameFaceCascade)) { |
||||
System.err.println("--(!)Error loading face cascade: " + filenameFaceCascade); |
||||
System.exit(0); |
||||
} |
||||
if (!eyesCascade.load(filenameEyesCascade)) { |
||||
System.err.println("--(!)Error loading eyes cascade: " + filenameEyesCascade); |
||||
System.exit(0); |
||||
} |
||||
|
||||
VideoCapture capture = new VideoCapture(cameraDevice); |
||||
if (!capture.isOpened()) { |
||||
System.err.println("--(!)Error opening video capture"); |
||||
System.exit(0); |
||||
} |
||||
|
||||
Mat frame = new Mat(); |
||||
while (capture.read(frame)) { |
||||
if (frame.empty()) { |
||||
System.err.println("--(!) No captured frame -- Break!"); |
||||
break; |
||||
} |
||||
|
||||
//-- 3. Apply the classifier to the frame
|
||||
detectAndDisplay(frame, faceCascade, eyesCascade); |
||||
|
||||
if (HighGui.waitKey(10) == 27) { |
||||
break;// escape
|
||||
} |
||||
} |
||||
|
||||
System.exit(0); |
||||
} |
||||
} |
||||
|
||||
public class ObjectDetectionDemo { |
||||
public static void main(String[] args) { |
||||
// Load the native OpenCV library
|
||||
System.loadLibrary(Core.NATIVE_LIBRARY_NAME); |
||||
|
||||
new ObjectDetection().run(args); |
||||
} |
||||
} |
@ -0,0 +1,102 @@ |
||||
import java.io.IOException; |
||||
import java.nio.file.Files; |
||||
import java.nio.file.Paths; |
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
import org.opencv.core.Core; |
||||
import org.opencv.core.CvType; |
||||
import org.opencv.core.Mat; |
||||
import org.opencv.imgcodecs.Imgcodecs; |
||||
import org.opencv.photo.CalibrateDebevec; |
||||
import org.opencv.photo.MergeDebevec; |
||||
import org.opencv.photo.MergeMertens; |
||||
import org.opencv.photo.Photo; |
||||
import org.opencv.photo.TonemapDurand; |
||||
|
||||
class HDRImaging { |
||||
public void loadExposureSeq(String path, List<Mat> images, List<Float> times) { |
||||
path += "/"; |
||||
|
||||
List<String> lines; |
||||
try { |
||||
lines = Files.readAllLines(Paths.get(path + "list.txt")); |
||||
|
||||
for (String line : lines) { |
||||
String[] splitStr = line.split("\\s+"); |
||||
if (splitStr.length == 2) { |
||||
String name = splitStr[0]; |
||||
Mat img = Imgcodecs.imread(path + name); |
||||
images.add(img); |
||||
float val = Float.parseFloat(splitStr[1]); |
||||
times.add(1/ val); |
||||
} |
||||
} |
||||
} catch (IOException e) { |
||||
e.printStackTrace(); |
||||
} |
||||
} |
||||
|
||||
public void run(String[] args) { |
||||
String path = args.length > 0 ? args[0] : ""; |
||||
if (path.isEmpty()) { |
||||
System.out.println("Path is empty. Use the directory that contains images and exposure times."); |
||||
System.exit(0); |
||||
} |
||||
|
||||
//! [Load images and exposure times]
|
||||
List<Mat> images = new ArrayList<>(); |
||||
List<Float> times = new ArrayList<>(); |
||||
loadExposureSeq(path, images, times); |
||||
//! [Load images and exposure times]
|
||||
|
||||
//! [Estimate camera response]
|
||||
Mat response = new Mat(); |
||||
CalibrateDebevec calibrate = Photo.createCalibrateDebevec(); |
||||
Mat matTimes = new Mat(times.size(), 1, CvType.CV_32F); |
||||
float[] arrayTimes = new float[(int) (matTimes.total()*matTimes.channels())]; |
||||
for (int i = 0; i < times.size(); i++) { |
||||
arrayTimes[i] = times.get(i); |
||||
} |
||||
matTimes.put(0, 0, arrayTimes); |
||||
calibrate.process(images, response, matTimes); |
||||
//! [Estimate camera response]
|
||||
|
||||
//! [Make HDR image]
|
||||
Mat hdr = new Mat(); |
||||
MergeDebevec mergeDebevec = Photo.createMergeDebevec(); |
||||
mergeDebevec.process(images, hdr, matTimes); |
||||
//! [Make HDR image]
|
||||
|
||||
//! [Tonemap HDR image]
|
||||
Mat ldr = new Mat(); |
||||
TonemapDurand tonemap = Photo.createTonemapDurand(); |
||||
tonemap.process(hdr, ldr); |
||||
//! [Tonemap HDR image]
|
||||
|
||||
//! [Perform exposure fusion]
|
||||
Mat fusion = new Mat(); |
||||
MergeMertens mergeMertens = Photo.createMergeMertens(); |
||||
mergeMertens.process(images, fusion); |
||||
//! [Perform exposure fusion]
|
||||
|
||||
//! [Write results]
|
||||
fusion = fusion.mul(fusion, 255); |
||||
ldr = ldr.mul(ldr, 255); |
||||
Imgcodecs.imwrite("fusion.png", fusion); |
||||
Imgcodecs.imwrite("ldr.png", ldr); |
||||
Imgcodecs.imwrite("hdr.hdr", hdr); |
||||
//! [Write results]
|
||||
|
||||
System.exit(0); |
||||
} |
||||
} |
||||
|
||||
public class HDRImagingDemo { |
||||
public static void main(String[] args) { |
||||
// Load the native OpenCV library
|
||||
System.loadLibrary(Core.NATIVE_LIBRARY_NAME); |
||||
|
||||
new HDRImaging().run(args); |
||||
} |
||||
} |
@ -0,0 +1,61 @@ |
||||
from __future__ import print_function |
||||
import cv2 as cv |
||||
import argparse |
||||
|
||||
def detectAndDisplay(frame): |
||||
frame_gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY) |
||||
frame_gray = cv.equalizeHist(frame_gray) |
||||
|
||||
#-- Detect faces |
||||
faces = face_cascade.detectMultiScale(frame_gray) |
||||
for (x,y,w,h) in faces: |
||||
center = (x + w//2, y + h//2) |
||||
frame = cv.ellipse(frame, center, (w//2, h//2), 0, 0, 360, (255, 0, 255), 4) |
||||
|
||||
faceROI = frame_gray[y:y+h,x:x+w] |
||||
#-- In each face, detect eyes |
||||
eyes = eyes_cascade.detectMultiScale(faceROI) |
||||
for (x2,y2,w2,h2) in eyes: |
||||
eye_center = (x + x2 + w2//2, y + y2 + h2//2) |
||||
radius = int(round((w2 + h2)*0.25)) |
||||
frame = cv.circle(frame, eye_center, radius, (255, 0, 0 ), 4) |
||||
|
||||
cv.imshow('Capture - Face detection', frame) |
||||
|
||||
parser = argparse.ArgumentParser(description='Code for Cascade Classifier tutorial.') |
||||
parser.add_argument('--face_cascade', help='Path to face cascade.', default='../../data/haarcascades/haarcascade_frontalface_alt.xml') |
||||
parser.add_argument('--eyes_cascade', help='Path to eyes cascade.', default='../../data/haarcascades/haarcascade_eye_tree_eyeglasses.xml') |
||||
parser.add_argument('--camera', help='Camera devide number.', type=int, default=0) |
||||
args = parser.parse_args() |
||||
|
||||
face_cascade_name = args.face_cascade |
||||
eyes_cascade_name = args.eyes_cascade |
||||
|
||||
face_cascade = cv.CascadeClassifier() |
||||
eyes_cascade = cv.CascadeClassifier() |
||||
|
||||
#-- 1. Load the cascades |
||||
if not face_cascade.load(face_cascade_name): |
||||
print('--(!)Error loading face cascade') |
||||
exit(0) |
||||
if not eyes_cascade.load(eyes_cascade_name): |
||||
print('--(!)Error loading eyes cascade') |
||||
exit(0) |
||||
|
||||
camera_device = args.camera |
||||
#-- 2. Read the video stream |
||||
cap = cv.VideoCapture(camera_device) |
||||
if not cap.isOpened: |
||||
print('--(!)Error opening video capture') |
||||
exit(0) |
||||
|
||||
while True: |
||||
ret, frame = cap.read() |
||||
if frame is None: |
||||
print('--(!) No captured frame -- Break!') |
||||
break |
||||
|
||||
detectAndDisplay(frame) |
||||
|
||||
if cv.waitKey(10) == 27: |
||||
break |
@ -0,0 +1,56 @@ |
||||
from __future__ import print_function |
||||
from __future__ import division |
||||
import cv2 as cv |
||||
import numpy as np |
||||
import argparse |
||||
import os |
||||
|
||||
def loadExposureSeq(path): |
||||
images = [] |
||||
times = [] |
||||
with open(os.path.join(path, 'list.txt')) as f: |
||||
content = f.readlines() |
||||
for line in content: |
||||
tokens = line.split() |
||||
images.append(cv.imread(os.path.join(path, tokens[0]))) |
||||
times.append(1 / float(tokens[1])) |
||||
|
||||
return images, np.asarray(times, dtype=np.float32) |
||||
|
||||
parser = argparse.ArgumentParser(description='Code for High Dynamic Range Imaging tutorial.') |
||||
parser.add_argument('--input', type=str, help='Path to the directory that contains images and exposure times.') |
||||
args = parser.parse_args() |
||||
|
||||
if not args.input: |
||||
parser.print_help() |
||||
exit(0) |
||||
|
||||
## [Load images and exposure times] |
||||
images, times = loadExposureSeq(args.input) |
||||
## [Load images and exposure times] |
||||
|
||||
## [Estimate camera response] |
||||
calibrate = cv.createCalibrateDebevec() |
||||
response = calibrate.process(images, times) |
||||
## [Estimate camera response] |
||||
|
||||
## [Make HDR image] |
||||
merge_debevec = cv.createMergeDebevec() |
||||
hdr = merge_debevec.process(images, times, response) |
||||
## [Make HDR image] |
||||
|
||||
## [Tonemap HDR image] |
||||
tonemap = cv.createTonemapDurand(2.2) |
||||
ldr = tonemap.process(hdr) |
||||
## [Tonemap HDR image] |
||||
|
||||
## [Perform exposure fusion] |
||||
merge_mertens = cv.createMergeMertens() |
||||
fusion = merge_mertens.process(images) |
||||
## [Perform exposure fusion] |
||||
|
||||
## [Write results] |
||||
cv.imwrite('fusion.png', fusion * 255) |
||||
cv.imwrite('ldr.png', ldr * 255) |
||||
cv.imwrite('hdr.hdr', hdr) |
||||
## [Write results] |
Loading…
Reference in new issue