Added new features
This commit is contained in:
@@ -8,8 +8,11 @@ Camera::Camera(int port)
|
||||
Camera::port = port;
|
||||
capture = VideoCapture(port);
|
||||
|
||||
cameraAvailable = true;
|
||||
|
||||
if (!capture.isOpened())
|
||||
{
|
||||
cameraAvailable = false;
|
||||
cout << "Failed to open camera on port " << port << endl;
|
||||
}
|
||||
|
||||
@@ -31,6 +34,9 @@ Camera::~Camera()
|
||||
|
||||
bool Camera::Calibrate()
|
||||
{
|
||||
cout << "Hold a 9x7 chessboard below the camera" << endl;
|
||||
cout << "Press space bar to take a picture" << endl;
|
||||
|
||||
// The number of boards you want to capture, the number of internal corners horizontally
|
||||
// and the number of internal corners vertically (That's just how the algorithm works).
|
||||
int numBoards = 10;
|
||||
@@ -156,23 +162,43 @@ bool Camera::Calibrate()
|
||||
fs.release();
|
||||
destroyAllWindows();
|
||||
|
||||
cout << "Calibration finished, press enter to exit" << endl;
|
||||
cin.ignore();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
Mat Camera::getImage()
|
||||
{
|
||||
Mat imageUndistorted;
|
||||
Mat image;
|
||||
Mat RGB_img;
|
||||
|
||||
capture >> image;
|
||||
if (!cameraAvailable)
|
||||
{
|
||||
vector<string> files;
|
||||
string dir = "training/";
|
||||
read_directory(dir, files);
|
||||
|
||||
undistort(image, imageUndistorted, intrinsic, distCoeffs);
|
||||
|
||||
Rect region_of_interest = Rect(10, 10, image.cols - 20, image.rows - 20);
|
||||
Mat image_roi = imageUndistorted(region_of_interest);
|
||||
string file = files[rand() % files.size()];
|
||||
|
||||
return image_roi;
|
||||
cout << file << endl;
|
||||
|
||||
image = imread("training/" + file, CV_LOAD_IMAGE_COLOR);
|
||||
return image;
|
||||
}
|
||||
else
|
||||
{
|
||||
Mat imageUndistorted;
|
||||
Mat RGB_img;
|
||||
|
||||
capture >> image;
|
||||
|
||||
undistort(image, imageUndistorted, intrinsic, distCoeffs);
|
||||
|
||||
Rect region_of_interest = Rect(10, 10, image.cols - 20, image.rows - 20);
|
||||
Mat image_roi = imageUndistorted(region_of_interest);
|
||||
|
||||
return image_roi;
|
||||
}
|
||||
}
|
||||
|
||||
Mat Camera::takeImage()
|
||||
@@ -181,9 +207,13 @@ Mat Camera::takeImage()
|
||||
|
||||
bool finished = false;
|
||||
|
||||
if (!cameraAvailable)
|
||||
image = getImage();
|
||||
|
||||
while (!finished)
|
||||
{
|
||||
image = getImage();
|
||||
if(cameraAvailable)
|
||||
image = getImage();
|
||||
|
||||
imshow("Live feed", image);
|
||||
|
||||
@@ -196,3 +226,18 @@ Mat Camera::takeImage()
|
||||
|
||||
return image;
|
||||
}
|
||||
|
||||
|
||||
void Camera::read_directory(const string& name, vector<string> &v)
|
||||
{
|
||||
string pattern = name;
|
||||
pattern.append("\\*");
|
||||
WIN32_FIND_DATA data;
|
||||
HANDLE hFind;
|
||||
if ((hFind = FindFirstFile(pattern.c_str(), &data)) != INVALID_HANDLE_VALUE) {
|
||||
do {
|
||||
v.push_back(data.cFileName);
|
||||
} while (FindNextFile(hFind, &data) != 0);
|
||||
FindClose(hFind);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user