real_time/software/raspberry/superviseur-robot/lib/camera.cpp

118 lines
2.6 KiB
C++
Raw Normal View History

/*
2018-12-14 17:04:42 +01:00
* Copyright (C) 2018 dimercur
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "camera.h"
#include "img.h"
#include <unistd.h>
2018-12-14 17:04:42 +01:00
using namespace cv;
Camera::Camera(int size, int fps) {
this->SetSize(size);
#ifndef __FOR_PC__
this->cap.set(CV_CAP_PROP_FORMAT, CV_8UC3);
this->cap.set(CV_CAP_PROP_FRAME_WIDTH,width);
this->cap.set(CV_CAP_PROP_FRAME_HEIGHT,height);
this->cap.set(CV_CAP_PROP_FPS, fps);
#endif /* __FOR_PC__ */
}
bool Camera::Open() {
bool status = false;
#ifdef __FOR_PC__
if (this->cap.open(0)) {
//this->cap.set(CV_CAP_PROP_FORMAT, CV_8UC3);
this->cap.set(CV_CAP_PROP_FRAME_WIDTH,width);
this->cap.set(CV_CAP_PROP_FRAME_HEIGHT,height);
status =true;
}
#else
if (this->cap.open()) {
cout<<"Camera warmup 2sec"<<endl<<flush;
sleep(2);
cout<<"Start capture"<<endl<<flush;
status =true;
}
#endif /* __FOR_PC__ */
return status;
}
void Camera::Close() {
this->cap.release();
}
void Camera::SetSize(int size) {
2018-12-14 17:04:42 +01:00
this->size = size;
2018-12-14 17:04:42 +01:00
switch (size){
case xs:
this->width = 480;
this->height = 360;
break;
case sm:
this->width = 640;
this->height = 480;
break;
case md:
this->width = 1024;
this->height = 768;
break;
case lg:
this->width = 1280;
this->height = 960;
break;
default:
this->width = 480;
this->height = 360;
}
}
Img Camera::Grab() {
2018-12-14 17:04:42 +01:00
ImageMat frame;
#ifdef __FOR_PC__
2018-12-14 17:04:42 +01:00
cap >> frame;
Img capture = Img(frame);
#else
cap.grab();
cap.retrieve (frame);
cvtColor(frame,frame,CV_BGR2RGB);
Img capture = Img(frame);
#endif /* __FOR_PC__ */
2018-12-14 17:04:42 +01:00
return capture;
}
bool Camera::IsOpen() {
2018-12-14 17:04:42 +01:00
return cap.isOpened();
}
int Camera::GetWidth() const {
2018-12-14 17:04:42 +01:00
return width;
}
int Camera::GetHeight() const {
2018-12-14 17:04:42 +01:00
return height;
}