I just posted so recently but I'm new to rust and keep hitting walls with what I imagine is elementary for someone more documentation-literate than I am.
I have been recreating my python code listed here
while True:
cv2.namedWindow("HSV")
cv2.resizeWindow("HSV",640,480)
cv2.createTrackbar("HUE MIN","HSV",0,179,empty)
cv2.createTrackbar("HUE MAX","HSV",0,179,empty)
cv2.createTrackbar("SAT MIN","HSV",0,255,empty)
cv2.createTrackbar("SAT MAX","HSV",0,255,empty)
cv2.createTrackbar("VAL MIN","HSV",0,255,empty)
cv2.createTrackbar("VAL MAX","HSV",0,255,empty)
while(cap.isOpened()):
ret, frame = cap.read()
if ret==True:
hsvframe = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
h_min = cv2.getTrackbarPos("HUE MIN","HSV")
h_max = cv2.getTrackbarPos("HUE MAX","HSV")
s_min = cv2.getTrackbarPos("SAT MIN","HSV")
s_max = cv2.getTrackbarPos("SAT MAX","HSV")
v_min = cv2.getTrackbarPos("VAL MIN","HSV")
v_max = cv2.getTrackbarPos("VAL MAX","HSV")
lower = np.array([h_min,s_min,v_min])
upper = np.array([h_max,s_max,v_max])
mask = cv2.inRange(hsvframe,lower,upper)
result = cv2.bitwise_and(frame,frame,mask=mask)
print(lower,upper)
out.write(mask)
cv2.imshow('mask',mask)
pass
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
I have been trying to recreate this while loop in rust and cant seem to understand how to call the position with opencv::highgui::get_trackbar_pos like cv2.getTrackbarPos
I also dont necessarily understand loop {}
and its nuances so im not sure what the equivalent ret, frame = cap.read, if ret==True:
would be because it seems like rust's cvt_color
handles that so I dont know if it affects where my get_trackbar_pos
should be
Lastly I'm really not sure how I should be writing to a lower and upper set of arrays so any advice there would be helpful.
use std::f32::MAX;
use opencv::{ highgui::{self, WINDOW_AUTOSIZE}, imgproc, prelude::*, videoio, Result};
fn run() -> Result<()> {
let window = "video capture";
let hsv = "HSV";
highgui::named_window(window, 1)?;
highgui::named_window(hsv, 1)?;
highgui::named_window(hsv, WINDOW_AUTOSIZE)?;
highgui::create_trackbar("HUE MAX", hsv, None, 179, None)?;
highgui::create_trackbar("HUE MIN", hsv, None, 179, None)?;
highgui::create_trackbar("SAT MAX", hsv, None, 179, None)?;
highgui::create_trackbar("SAT MIN", hsv, None, 179, None)?;
highgui::create_trackbar("VAL MAX", hsv, None, 179, None)?;
highgui::create_trackbar("VAL MIN", hsv, None, 179, None)?;
opencv::opencv_branch_32! {
let mut cam = videoio::VideoCapture::new_default(0)?; // 0 is the default camera
}
opencv::not_opencv_branch_32! {
let mut cam = videoio::VideoCapture::new(0, videoio::CAP_ANY)?; // 0 is the default camera
}
let opened = videoio::VideoCapture::is_opened(&cam)?;
if !opened {
panic!("Unable to open default camera!");
}
loop {
let mut frame = Mat::default();
cam.read(&mut frame)?;
if frame.size()?.width > 0 {
let mut gray = Mat::default();
let h_max = opencv::highgui::get_trackbar_pos("HUE MAX", hsv)?;
let h_min = opencv::highgui::get_trackbar_pos("HUE MIN", hsv)?;
let s_max = opencv::highgui::get_trackbar_pos("SAT MAX", hsv)?;
let s_min = opencv::highgui::get_trackbar_pos("SAT MIN", hsv)?;
let v_max = opencv::highgui::get_trackbar_pos("VAL MAX", hsv)?;
let v_min = opencv::highgui::get_trackbar_pos("VAL MIN", hsv)?;
let _lower = [h_min, s_min, v_min];
let _upper = [h_max, s_max, v_max];
imgproc::cvt_color(&frame, &mut gray, imgproc::COLOR_BGR2HSV, 0)?;
highgui::imshow(window, &gray)?;
}
if highgui::wait_key(10)? > 0 {
break;
}
}
Ok(())
}
fn main() {
run().unwrap()
}
Here is my main.rs
You can see at the bottom my loop and arrays.
The Rust OpenCV Documentation seems really simple and clear, I'm just fairly rust illiterate and even reading the rust book I'm having trouble.