-
-
Save kjunichi/356ee9c7150f97c6d162 to your computer and use it in GitHub Desktop.
This file contains 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
#[allow(dead_code)] | |
pub mod cv { | |
extern crate libc; | |
use self::libc::{c_char, c_double, c_int, c_schar, c_void}; | |
pub struct HaarClassifierCascade; | |
pub struct MemStorage; | |
pub struct Mat { | |
pub type_: c_int, | |
pub step: c_int, | |
pub refcount: *mut c_int, | |
pub hdr_refcount: c_int, | |
pub data: *mut c_void, | |
pub rows: c_int, | |
pub cols: c_int, | |
} | |
pub struct Point { | |
pub x: c_int, | |
pub y: c_int, | |
} | |
pub struct Rect { | |
pub x: c_int, | |
pub y: c_int, | |
pub width: c_int, | |
pub height: c_int, | |
} | |
pub type Scalar = [c_double, ..4]; | |
pub struct Seq { | |
tree_node_fields: [c_int, ..10], | |
pub total: c_int, | |
} | |
pub struct Size { | |
pub width: c_int, | |
pub height: c_int, | |
} | |
#[link(name = "opencv_core.2.4.9")] | |
extern "C" { | |
fn cvCreateMat(rows: c_int, cols: c_int, type_: c_int) -> *mut Mat; | |
fn cvCreateMemStorage(block_size: c_int) -> *mut MemStorage; | |
fn cvGetSeqElem(seq: *const Seq, index: c_int) -> *mut c_schar; | |
fn cvLoad(filename: *const c_char) -> *mut c_void; | |
fn cvRectangle(image: *mut Mat, | |
pt1: Point, | |
pt2: Point, | |
color: Scalar, | |
thickness: c_int, | |
line_type: c_int, | |
shift: c_int) -> c_void; | |
fn cvReleaseMemStorage(storage: &*mut MemStorage) -> c_void; | |
} | |
pub fn create_mat(rows: c_int, cols: c_int, type_: c_int) -> *mut Mat { | |
unsafe { cvCreateMat(rows, cols, type_) } | |
} | |
pub fn create_mem_storage(block_size: int) -> *mut MemStorage { | |
unsafe { cvCreateMemStorage(block_size.to_i32().unwrap()) } | |
} | |
pub fn get_seq_elem_as_rect(seq: *const Seq, index: int) -> *mut Rect { | |
unsafe { cvGetSeqElem(seq, index.to_i32().unwrap()) as *mut Rect } | |
} | |
pub fn load_haar_classifier_cascade(filename: &str) -> *mut HaarClassifierCascade { | |
unsafe { cvLoad(filename.to_string().to_c_str().unwrap()) as *mut HaarClassifierCascade } // ?? | |
} | |
pub fn rectangle(image: *mut Mat, pt1: Point, pt2: Point, color: Scalar, thickness: int) -> () { | |
unsafe { cvRectangle(image, pt1, pt2, color, thickness.to_i32().unwrap(), 16i32, 0i32); } | |
} | |
pub fn release_mem_storage(storage: &*mut MemStorage) -> () { | |
unsafe { cvReleaseMemStorage(storage); } | |
} | |
#[link(name = "opencv_highgui.2.4.9")] | |
extern "C" { | |
fn cvDestroyAllWindows() -> c_void; | |
fn cvLoadImageM(filename: *const c_char, iscolor: c_int) -> *mut Mat; | |
fn cvNamedWindow(title: *const c_char) -> c_int; | |
fn cvShowImage(name: *const c_char, image: *const Mat) -> c_void; | |
fn cvWaitKey(delay: c_int) -> c_int; | |
} | |
pub fn destroy_all_windows() -> () { | |
unsafe { cvDestroyAllWindows(); } | |
} | |
pub fn load_image(filename: &str) -> *mut Mat { | |
filename.with_c_str(|filename| unsafe { | |
cvLoadImageM(filename, 1i32) | |
}) | |
} | |
pub fn named_window(title: &str) -> () { | |
title.with_c_str(|title| unsafe { | |
cvNamedWindow(title); | |
}); | |
} | |
pub fn show_image(name: &str, image: *const Mat) -> () { | |
name.with_c_str(|name| unsafe { | |
cvShowImage(name, image); | |
}) | |
} | |
pub fn wait_key(delay: int) -> int { | |
unsafe { cvWaitKey(delay.to_i32().unwrap()).to_int().unwrap() } | |
} | |
#[link(name = "opencv_imgproc.2.4.9")] | |
extern "C" { | |
fn cvCvtColor(src: *mut Mat, dst: *mut Mat, code: c_int) -> c_void; | |
fn cvEqualizeHist(src: *mut Mat, dst: *mut Mat) -> c_void; | |
} | |
pub fn cvt_color(src: *mut Mat, dst: *mut Mat, code: int) -> () { | |
unsafe { cvCvtColor(src, dst, code.to_i32().unwrap()); } | |
} | |
pub fn equalize_hist(src: *mut Mat, dst: *mut Mat) -> () { | |
unsafe { cvEqualizeHist(src, dst); } | |
} | |
#[link(name = "opencv_objdetect.2.4.9")] | |
extern "C" { | |
fn cvHaarDetectObjects(image: *const Mat, | |
cascade: *mut HaarClassifierCascade, | |
storage: *mut MemStorage, | |
scale_factor: c_double, | |
min_neighbors: c_int, | |
flags: c_int, | |
min_size: Size, | |
max_size: Size) -> *Seq; | |
fn cvReleaseHaarClassifierCascade(cascade: &*mut HaarClassifierCascade) -> c_void; | |
} | |
pub fn haar_detect_objects(image: *Mat, cascade: *mut HaarClassifierCascade, storage: *mut MemStorage) -> *const Seq { | |
unsafe { | |
let zero = Size { width: 0i32, height: 0i32 }; | |
cvHaarDetectObjects(image, cascade, storage, 1.1f64, 3, 0, zero, zero) | |
} | |
} | |
pub fn release_haar_classifier_cascade(cascade: &*mut HaarClassifierCascade) -> () { | |
unsafe { cvReleaseHaarClassifierCascade(cascade); } | |
} | |
} | |
fn main() { | |
let name = "Face detection"; | |
cv::named_window(name); | |
let image0 = cv::load_image(std::os::args().get(1).as_slice()); | |
let image1 = unsafe { cv::create_mat((*image0).rows, (*image0).cols, 0i32) as *mut cv::Mat }; // CV_8UC1 = 0 | |
let cascade = cv::load_haar_classifier_cascade("haarcascade_frontalface_alt.xml"); | |
let storage = cv::create_mem_storage(0i); | |
cv::cvt_color(image0, image1, 6i); // CV_BGR2GRAY = 6 | |
cv::equalize_hist(image1, image1); | |
let seq = cv::haar_detect_objects(image1, cascade, storage); | |
unsafe { | |
for n in range(0i, (*seq).total.to_int().unwrap()) { | |
let rect = *cv::get_seq_elem_as_rect(seq, n); | |
let src = cv::Point { x: rect.x, y: rect.y }; | |
let dst = cv::Point { x: rect.x + rect.width, y: rect.y + rect.height }; | |
cv::rectangle(image0, src, dst, [144.0f64, 48.0f64, 255.0f64, 0.0f64], 1i); | |
} | |
} | |
cv::show_image(name, image0); | |
cv::wait_key(0); | |
cv::release_mem_storage(&storage); | |
cv::release_haar_classifier_cascade(&cascade); | |
cv::destroy_all_windows(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment