void USBCamera::open() { LINFO << "Capture: Trying to open USB camera."; LINFO << "Capture: (Warning) You can change the capture device with the configuration files."; try { if(!isOpened()) { m_camera->release(); m_camera->open(getDeviceNumber()); setImageSize(m_frameWidth, m_frameHeight); if(!isOpened()) { throw OpenCVException("can't open usb camera"); } } } catch(cv::Exception & ex) { throw OpenCVException(ex.msg.c_str()); } LINFO << "Capture: Succesfully opened USB camera."; }
void Image::rotate(int angle) { try { switch (angle % 360) { case 0: break; case 90: cv::flip(m_image.t(), m_image, 1); break; case 180: cv::flip(m_image, m_image, -1); break; case 270: cv::flip(m_image.t(), m_image, 0); break; default: break; } } catch(cv::Exception & ex) { throw OpenCVException(ex.msg.c_str()); } }
Image * USBCamera::takeImage() { // Update camera, call executor's functor. tryToUpdateCapture(); // Take image try { // Delay camera for some time.. usleep(m_delay*1000); // Get image from camera Image * image = new Image(); pthread_mutex_lock(&m_lock); m_camera->grab(); m_camera->retrieve(image->getImage()); pthread_mutex_unlock(&m_lock); // Check if need to rotate the image image->rotate(m_angle); return image; } catch(cv::Exception & ex) { pthread_mutex_unlock(&m_lock); throw OpenCVException(ex.msg.c_str()); } }
Image * USBCamera::takeImage() { // Update camera, call executor's functor. tryToUpdateCapture(); // Take image try { // Delay camera for some time.. usleep(m_delay*1000); // Get image from camera Image * image = new Image(); cvGrabFrame(m_camera); cvGrabFrame(m_camera); cvGrabFrame(m_camera); cvGrabFrame(m_camera); cvGrabFrame(m_camera); // workaround for buffering image->setImage(cvRetrieveFrame(m_camera)); // Check if need to rotate the image image->rotate(m_angle); return image; } catch(cv::Exception & ex) { throw OpenCVException(ex.msg.c_str()); } }
Image * VideoCapture::takeImage() { // Take image try { // Delay camera for some time.. usleep(m_delay*1000); cv::waitKey(10); // this is needed for video files. // Get image from camera Image * image = new Image(); pthread_mutex_lock(&m_lock); m_video->grab(); m_video->retrieve(image->getImage()); pthread_mutex_unlock(&m_lock); // Check if need to rotate the image image->rotate(m_angle); return image; } catch(cv::Exception & ex) { pthread_mutex_unlock(&m_lock); throw OpenCVException(ex.msg.c_str()); } }
void Image::convert2Gray() { try { cvtColor(m_image, m_image, CV_RGB2GRAY); } catch(cv::Exception & ex) { throw OpenCVException(ex.msg.c_str()); } }
void Image::erode(const Image & kernel) { try { cv::erode(m_image, m_image, kernel.m_image); } catch(cv::Exception & ex) { throw OpenCVException(ex.msg.c_str()); } }
void Image::threshold(const int threshold) { try { cv::threshold(m_image, m_image, threshold, 255, CV_THRESH_BINARY); } catch(cv::Exception & ex) { throw OpenCVException(ex.msg.c_str()); } }
void Image::bitwiseAnd(const Image & image, Image & result) { try { cv::bitwise_and(m_image, image.m_image, result.m_image); } catch(cv::Exception & ex) { throw OpenCVException(ex.msg.c_str()); } }
void Image::difference(const Image & image, Image & result) { try { cv::absdiff(m_image, image.m_image, result.m_image); } catch(cv::Exception & ex) { throw OpenCVException(ex.msg.c_str()); } }
bool Image::save(const std::string pathToFile) { try { return imwrite(pathToFile, m_image); } catch(cv::Exception & ex) { throw OpenCVException(ex.msg.c_str()); } }
void USBCamera::close() { try { cvReleaseCapture(&m_camera); } catch(cv::Exception & ex) { throw OpenCVException(ex.msg.c_str()); } }
int Image::brightness() { try { cv::Scalar mean = cv::mean(m_image); return mean[0]; } catch(cv::Exception & ex) { throw OpenCVException(ex.msg.c_str()); } }
Image Image::scaleToSmall() { try { Image scaled; return scaled; } catch(cv::Exception & ex) { throw OpenCVException(ex.msg.c_str()); } }
VideoCapture::VideoCapture(int width, int height) { try { m_video = new cv::VideoCapture(); setImageSize(width, height); } catch(cv::Exception & ex) { throw OpenCVException(ex.msg.c_str()); } };
USBCamera::USBCamera(int width, int height) { try { m_camera = cvCaptureFromCAM(CV_CAP_ANY); setImageSize(width, height); } catch(cv::Exception & ex) { throw OpenCVException(ex.msg.c_str()); } };
void USBCamera::setImageSize(int width, int height) { Capture::setImageSize(width, height); try { cvSetCaptureProperty(m_camera, CV_CAP_PROP_FRAME_WIDTH, m_frameWidth); cvSetCaptureProperty(m_camera, CV_CAP_PROP_FRAME_HEIGHT, m_frameHeight); } catch(cv::Exception & ex) { throw OpenCVException(ex.msg.c_str()); } }
void VideoCapture::close() { try { pthread_mutex_unlock(&m_lock); pthread_mutex_destroy(&m_lock); m_video->release(); } catch(cv::Exception & ex) { throw OpenCVException(ex.msg.c_str()); } }
void VideoCapture::setImageSize(int width, int height) { Capture::setImageSize(width, height); try { m_video->set(CV_CAP_PROP_FRAME_WIDTH, m_frameWidth); m_video->set(CV_CAP_PROP_FRAME_HEIGHT, m_frameHeight); } catch(cv::Exception & ex) { throw OpenCVException(ex.msg.c_str()); } }
void USBCamera::close() { try { pthread_mutex_unlock(&m_lock); pthread_mutex_destroy(&m_lock); m_camera->release(); } catch(cv::Exception & ex) { throw OpenCVException(ex.msg.c_str()); } }
void USBCamera::setImageSize(int width, int height) { Capture::setImageSize(width, height); try { m_camera->set(CV_CAP_PROP_FRAME_WIDTH, m_frameWidth); m_camera->set(CV_CAP_PROP_FRAME_HEIGHT, m_frameHeight); m_camera->set(CV_CAP_PROP_FOURCC, CV_FOURCC(m_fourcc[0], m_fourcc[1], m_fourcc[2], m_fourcc[3])); } catch(cv::Exception & ex) { throw OpenCVException(ex.msg.c_str()); } }
void VideoCapture::grab() { try { pthread_mutex_lock(&m_lock); // A video doesn't need a grabber. // m_video->grab(); pthread_mutex_unlock(&m_lock); } catch(cv::Exception & ex) { pthread_mutex_unlock(&m_lock); pthread_mutex_destroy(&m_lock); throw OpenCVException(ex.msg.c_str()); } }
void USBCamera::grab() { try { pthread_mutex_lock(&m_lock); incrementHealth(); m_camera->grab(); pthread_mutex_unlock(&m_lock); } catch(cv::Exception & ex) { pthread_mutex_unlock(&m_lock); pthread_mutex_destroy(&m_lock); throw OpenCVException(ex.msg.c_str()); } }
Image VideoCapture::retrieve() { try { Image image; pthread_mutex_lock(&m_lock); m_video->retrieve(image.getImage()); pthread_mutex_unlock(&m_lock); return image; } catch(cv::Exception & ex) { pthread_mutex_unlock(&m_lock); pthread_mutex_destroy(&m_lock); throw OpenCVException(ex.msg.c_str()); } }
void VideoCapture::open() { try { if(!isOpened()) { m_video->release(); m_video->open(getPath()); //m_video->set(CV_CAP_PROP_POS_FRAMES, 1850); setImageSize(m_frameWidth, m_frameHeight); } } catch(cv::Exception & ex) { throw OpenCVException(ex.msg.c_str()); } }
void Image::drawRectangle(JSONValue & region, int color[3]) { try { cv::Scalar _color(color[0],color[1],color[2]); cv::Rect rectangle; rectangle.x = region[0].GetInt(); rectangle.y = region[1].GetInt(); rectangle.width = region[2].GetInt() - region[0].GetInt(); rectangle.height = region[3].GetInt() - region[1].GetInt(); cv::rectangle(m_image, rectangle, _color, 1); } catch(cv::Exception & ex) { throw OpenCVException(ex.msg.c_str()); } }
Image Image::crop(int x1, int y1, int x2, int y2) { try { cv::Point tl(x1,y1); cv::Point br(x2,y2); cv::Rect rectangle(tl,br); Image cropped; cropped.setImage(m_image(rectangle)); return cropped; } catch(cv::Exception & ex) { throw OpenCVException(ex.msg.c_str()); } }