Skip to content

Commit

Permalink
Feature/overallfix (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pzixel authored Feb 19, 2018
1 parent a3df1df commit 25bce2a
Show file tree
Hide file tree
Showing 27 changed files with 267 additions and 240 deletions.
4 changes: 1 addition & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cv"
version = "0.2.0"
version = "0.2.1"
authors = ["Ben Zhang <[email protected]>"]
build = "build.rs"
repository = "https://github.com/nebgnahz/cv-rs.git"
Expand All @@ -13,8 +13,6 @@ description = "This library primarily provides a binding and API for OpenCV 3.x.
[dependencies]
bytes = "0.4"
failure = "0.1"
num = "0.1"
num-derive = "0.1.44"

[dev-dependencies]
getopts = "0"
Expand Down
8 changes: 4 additions & 4 deletions examples/calc_hist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ extern crate cv;

use cv::*;
use cv::highgui::*;
use cv::imgcodecs::ImreadModes;
use cv::imgcodecs::ImageReadMode;

fn main() {
////////////////////////////////
Expand All @@ -17,7 +17,7 @@ fn main() {
std::process::exit(-1);
}

let mat = Mat::from_path(&args[1], ImreadModes::ImreadGrayscale).expect("Failed to read from path");
let mat = Mat::from_path(&args[1], ImageReadMode::Grayscale).expect("Failed to read from path");

if !mat.is_valid() {
println!("Could not open or find the image");
Expand Down Expand Up @@ -49,7 +49,7 @@ fn main() {
let hist_image = Mat::with_size(hist_h, hist_w, CvType::Cv8UC3 as i32);

// Normalize the histogram to the height of the histogram window
let b_hist = hist.normalize(0.0, hist_h as f64, NormTypes::NormMinMax);
let b_hist = hist.normalize(0.0, hist_h as f64, NormType::MinMax);

// Plot each segment as a line element
for i in 1..hsize {
Expand All @@ -59,6 +59,6 @@ fn main() {
}

// Show the histogram
highgui_named_window("Display window", WindowFlags::WindowNormal);
highgui_named_window("Display window", WindowFlag::Normal).unwrap();
hist_image.show("Histogram", 0).unwrap();
}
15 changes: 7 additions & 8 deletions examples/camshift.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,15 @@ struct SelectionStatus {
status: bool,
}

fn on_mouse(e: i32, x: i32, y: i32, _: i32, data: MouseCallbackData) {
let event: MouseEventTypes = unsafe { std::mem::transmute(e as u8) };
fn on_mouse(event: MouseEventType, x: i32, y: i32, _: i32, data: MouseCallbackData) {
match event {
MouseEventTypes::LButtonDown => {
MouseEventType::LButtonDown => {
let ss = data as *mut SelectionStatus;
let mut selection = unsafe { &mut (*ss).selection };
selection.x = x;
selection.y = y;
}
MouseEventTypes::LButtonUp => {
MouseEventType::LButtonUp => {
let ss = data as *mut SelectionStatus;
let mut selection = unsafe { &mut (*ss).selection };
let mut status = unsafe { &mut (*ss).status };
Expand All @@ -44,8 +43,8 @@ fn main() {
let cap = VideoCapture::new(0);
assert!(cap.is_open());

highgui_named_window("Window", WindowFlags::WindowAutosize);
highgui_set_mouse_callback("Window", on_mouse, ss_ptr as MouseCallbackData);
highgui_named_window("Window", WindowFlag::Autosize).unwrap();
highgui_set_mouse_callback("Window", on_mouse, ss_ptr as MouseCallbackData).unwrap();

let mut is_tracking = false;

Expand All @@ -58,7 +57,7 @@ fn main() {
while let Some(mut m) = cap.read() {
m.flip(FlipCode::YAxis);

let hsv = m.cvt_color(ColorConversionCodes::BGR2HSV);
let hsv = m.cvt_color(ColorConversion::BGR2HSV);

let ch = [0, 0];
let hue = hsv.mix_channels(1, 1, &ch[0] as *const i32, 1);
Expand All @@ -77,7 +76,7 @@ fn main() {
&hsize,
&phranges[0] as *const *const f32,
);
hist = raw_hist.normalize(0.0, 255.0, NormTypes::NormMinMax);
hist = raw_hist.normalize(0.0, 255.0, NormType::MinMax);

track_window = selection;
m.rectangle(selection);
Expand Down
6 changes: 3 additions & 3 deletions examples/display_image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
extern crate cv;
use cv::*;
use cv::highgui::*;
use cv::imgcodecs::ImreadModes;
use cv::imgcodecs::ImageReadMode;

fn main() {
let args: Vec<_> = std::env::args().collect();
Expand All @@ -12,13 +12,13 @@ fn main() {
std::process::exit(-1);
}

let mat = Mat::from_path(&args[1], ImreadModes::ImreadColor).expect("Failed to read from path");
let mat = Mat::from_path(&args[1], ImageReadMode::Color).expect("Failed to read from path");

if !mat.is_valid() {
println!("Could not open or find the image");
std::process::exit(-1);
}

highgui_named_window("Display window", WindowFlags::WindowNormal);
highgui_named_window("Display window", WindowFlag::Normal).unwrap();
mat.show("Display window", 0).unwrap();
}
6 changes: 3 additions & 3 deletions examples/face_detect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ fn main() {

let mut buf = Vec::new();
File::open(d).unwrap().read_to_end(&mut buf).unwrap();
let mat = Mat::imdecode(&buf, ImreadModes::ImreadGrayscale);
let mat = Mat::image_decode(&buf, ImageReadMode::Grayscale);

let mut d = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
d.push("assets/haarcascade_frontalface_default.xml");
let cascade = CascadeClassifier::from_path(d).unwrap();

highgui_named_window("window", WindowFlags::WindowNormal);
highgui_named_window("window", WindowFlag::Normal).unwrap();

// result is a vector of rectangles
let result = cascade.detect_with_params(&mat, 1.1, 15, Size2i::new(80, 80), Size2i::default());
Expand All @@ -34,7 +34,7 @@ fn main() {
r.scale(1.2),
Scalar::new(255, 255, 0, 255),
10,
LineTypes::Line8,
LineType::Line8,
)
})
.count();
Expand Down
4 changes: 2 additions & 2 deletions examples/hog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ fn run() -> Result<()> {
.expect("You need to provide the directory");

if show {
highgui_named_window("window", WindowFlags::WindowAutosize);
highgui_named_window("window", WindowFlag::Autosize).unwrap();
}

let mut param = HogParams::default();
Expand All @@ -77,7 +77,7 @@ fn run_detect_for_image<P: AsRef<Path>, OD: ObjectDetect>(detector: &mut OD, pat
.into_owned();
let frame_num = filename.parse::<usize>().unwrap();
File::open(path).unwrap().read_to_end(&mut buf).unwrap();
let mat = Mat::imdecode(&buf, ImreadModes::ImreadGrayscale);
let mat = Mat::image_decode(&buf, ImageReadMode::Grayscale);

let start = ::std::time::Instant::now();
let results = detector.detect(&mat);
Expand Down
14 changes: 7 additions & 7 deletions examples/hs_hist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ extern crate cv;

use cv::*;
use cv::highgui::*;
use cv::imgcodecs::ImreadModes;
use cv::imgproc::ColorConversionCodes;
use cv::imgcodecs::ImageReadMode;
use cv::imgproc::ColorConversion;

fn main() {
////////////////////////////////
Expand All @@ -18,14 +18,14 @@ fn main() {
std::process::exit(-1);
}

let mat = Mat::from_path(&args[1], ImreadModes::ImreadColor).expect("Failed to read from path");
let mat = Mat::from_path(&args[1], ImageReadMode::Color).expect("Failed to read from path");

if !mat.is_valid() {
println!("Could not open or find the image");
std::process::exit(-1);
}

let hsv = mat.cvt_color(ColorConversionCodes::BGR2HSV);
let hsv = mat.cvt_color(ColorConversion::BGR2HSV);

////////////////////////////////
//
Expand Down Expand Up @@ -73,12 +73,12 @@ fn main() {
hist_image.rectangle_custom(
rect,
Scalar::all(intensity),
LineTypes::Filled as i32,
LineTypes::Line8,
LineType::Filled as i32,
LineType::Line8,
);
}
}

highgui_named_window("Display window", WindowFlags::WindowNormal);
highgui_named_window("Display window", WindowFlag::Normal).unwrap();
hist_image.show("Histogram", 0).unwrap();
}
2 changes: 1 addition & 1 deletion examples/video_capture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fn main() {
let cap = VideoCapture::new(0);
assert!(cap.is_open());

highgui_named_window("Window", WindowFlags::WindowAutosize);
highgui_named_window("Window", WindowFlag::Autosize).unwrap();
while let Some(image) = cap.read() {
image.show("Window", 30).unwrap();
}
Expand Down
7 changes: 1 addition & 6 deletions native/opencv-wrapper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ int cv_wait_key(int delay) {
return cv::waitKey(delay);
}

void cv_set_mouse_callback(const char* const winname, MouseCallback on_mouse, void* userdata) {
void cv_set_mouse_callback(const char* const winname, cv::MouseCallback on_mouse, void* userdata) {
cv::setMouseCallback(winname, on_mouse, userdata);
}

Expand Down Expand Up @@ -357,11 +357,6 @@ double cv_videocapture_get(cv::VideoCapture* cap, int property) {
// =============================================================================
// VideoWriter
// =============================================================================
/// http://www.fourcc.org/codecs.php
int cv_fourcc(char c1, char c2, char c3, char c4) {
return (((c1) &255) + (((c2) &255) << 8) + (((c3) &255) << 16) + (((c4) &255) << 24));
}

void* cv_videowriter_default() {
return new cv::VideoWriter();
}
Expand Down
5 changes: 1 addition & 4 deletions native/opencv-wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,7 @@ void cv_destroy_window(const char* const winname);
void cv_imshow(const char* const winname, cv::Mat* mat);
int cv_wait_key(int delay_in_millis);

typedef void (*MouseCallback)(int e, int x, int y, int flags, void* data);
void cv_set_mouse_callback(const char* const winname, MouseCallback onMouse, void* userdata);
void cv_set_mouse_callback(const char* const winname, cv::MouseCallback onMouse, void* userdata);

// =============================================================================
// VideoIO
Expand All @@ -134,8 +133,6 @@ void cv_videocapture_drop(cv::VideoCapture* cap);
bool cv_videocapture_set(cv::VideoCapture* cap, int property, double value);
double cv_videocapture_get(cv::VideoCapture* cap, int property);

int cv_fourcc(char c1, char c2, char c3, char c4);

void* cv_videowriter_default();
void* cv_videowriter_new(const char* const path, int fourcc, double fps, Size2i frame_size, bool is_color);
void cv_videowriter_drop(cv::VideoWriter* writer);
Expand Down
49 changes: 23 additions & 26 deletions src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
use bytes::{self, ByteOrder};
use errors::*;
use failure::Error;
use num;
use std::ffi::CString;
use std::mem;
use std::os::raw::{c_char, c_double, c_int, c_uchar};
Expand Down Expand Up @@ -164,7 +163,7 @@ pub struct Size2f {
}

/// The `Rect` defines a rectangle in integer.
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq)]
#[derive(Default, Debug, Clone, Copy, Eq, PartialEq)]
#[repr(C)]
pub struct Rect {
/// x coordinate of the left-top corner
Expand Down Expand Up @@ -237,17 +236,15 @@ impl Rect2f {
}

/// Line type
#[derive(Debug, PartialEq, Clone, Copy)]
pub enum LineTypes {
#[repr(C)]
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
pub enum LineType {
/// Default type
Filled = -1,

/// 4-connected line
Line4 = 4,

/// 8-connected line
Line8 = 8,

/// antialiased line
LineAA = 16,
}
Expand All @@ -268,7 +265,7 @@ extern "C" {
fn cv_mat_step1(cmat: *const CMat, i: c_int) -> usize;
fn cv_mat_elem_size(cmat: *const CMat) -> usize;
fn cv_mat_elem_size1(cmat: *const CMat) -> usize;
fn cv_mat_type(cmat: *const CMat) -> c_int;
fn cv_mat_type(cmat: *const CMat) -> CvType;
fn cv_mat_roi(cmat: *const CMat, rect: Rect) -> *mut CMat;
fn cv_mat_logic_and(cimage: *mut CMat, cmask: *const CMat);
fn cv_mat_flip(src: *mut CMat, code: c_int);
Expand All @@ -278,7 +275,7 @@ extern "C" {

/// A flag to specify how to flip the image. see
/// [Mat::flip](struct.Mat.html#method.flip)
#[derive(Debug, Clone, Copy)]
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
pub enum FlipCode {
/// Along x-axis: dst[i, j] = src[src.rows - i - 1, j]
XAxis,
Expand Down Expand Up @@ -453,9 +450,8 @@ impl Mat {

/// Returns the images type. For supported types, please see
/// [CvType](enum.CvType).
pub fn cv_type(&self) -> Result<CvType, Error> {
let t: i32 = unsafe { cv_mat_type(self.inner) };
num::FromPrimitive::from_i32(t).ok_or(CvError::EnumFromPrimitiveConversionError { value: t }.into())
pub fn cv_type(&self) -> CvType {
unsafe { cv_mat_type(self.inner) }
}

/// Returns an identity matrix of the specified size and type.
Expand Down Expand Up @@ -634,8 +630,8 @@ impl Drop for Mat {
/// | CV_32S | 4 | 12 | 20 | 28 | 36 | 44 | 52 | 60 |
/// | CV_32F | 5 | 13 | 21 | 29 | 37 | 45 | 53 | 61 |
/// | CV_64F | 6 | 14 | 22 | 30 | 38 | 46 | 54 | 62 |
#[derive(Debug, PartialEq, Clone, Copy, FromPrimitive)]
#[repr(C)]
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
pub enum CvType {
/// 8 bit unsigned (like `uchar`), single channel (grey image)
Cv8UC1 = 0,
Expand Down Expand Up @@ -748,7 +744,7 @@ extern "C" {
from_to: *const c_int,
npairs: isize,
);
fn cv_normalize(csrc: *const CMat, cdst: *mut CMat, alpha: c_double, beta: c_double, norm_type: c_int);
fn cv_normalize(csrc: *const CMat, cdst: *mut CMat, alpha: c_double, beta: c_double, norm_type: NormType);

fn cv_bitwise_and(src1: *const CMat, src2: *const CMat, dst: *mut CMat);
fn cv_bitwise_not(src: *const CMat, dst: *mut CMat);
Expand All @@ -759,24 +755,25 @@ extern "C" {

/// Normalization type. Please refer to [OpenCV's
/// documentation](http://docs.cv.org/trunk/d2/de8/group__core__array.html).
#[derive(Debug, PartialEq, Clone, Copy, FromPrimitive)]
pub enum NormTypes {
#[repr(C)]
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
pub enum NormType {
/// Normalized using `max`
NormInf = 1,
Inf = 1,
/// Normalized using L1 distance
NormL1 = 2,
L1 = 2,
/// Normalized using L2 distance
NormL2 = 4,
L2 = 4,
/// Normalized using L2 sqr distance
NormL2Sqr = 5,
L2Sqr = 5,
/// Normalized using hamming distance
NormHamming = 6,
Hamming = 6,
/// Normalized using hamming2 distance
NormHamming2 = 7,
Hamming2 = 7,
/// Normalized using relative distance
NormRelative = 8,
Relative = 8,
/// Normalized using minmax distance
NormMinMax = 32,
MinMax = 32,
}

impl Mat {
Expand Down Expand Up @@ -831,9 +828,9 @@ impl Mat {
}

/// Normalize the Mat according to the normalization type.
pub fn normalize(&self, alpha: f64, beta: f64, t: NormTypes) -> Mat {
pub fn normalize(&self, alpha: f64, beta: f64, t: NormType) -> Mat {
let m = CMat::new();
unsafe { cv_normalize(self.inner, m, alpha, beta, t as c_int) }
unsafe { cv_normalize(self.inner, m, alpha, beta, t) }
Mat::from_raw(m)
}

Expand Down
Loading

0 comments on commit 25bce2a

Please sign in to comment.