Skip to content

Commit 97c27a7

Browse files
committed
add: CameraCaptureクラスの実装
1 parent 6835837 commit 97c27a7

File tree

4 files changed

+201
-30
lines changed

4 files changed

+201
-30
lines changed

.vscode/settings.json

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
{
2+
"files.associations": {
3+
"any": "cpp",
4+
"array": "cpp",
5+
"atomic": "cpp",
6+
"bit": "cpp",
7+
"cctype": "cpp",
8+
"charconv": "cpp",
9+
"chrono": "cpp",
10+
"clocale": "cpp",
11+
"cmath": "cpp",
12+
"compare": "cpp",
13+
"complex": "cpp",
14+
"concepts": "cpp",
15+
"condition_variable": "cpp",
16+
"csignal": "cpp",
17+
"cstdarg": "cpp",
18+
"cstddef": "cpp",
19+
"cstdint": "cpp",
20+
"cstdio": "cpp",
21+
"cstdlib": "cpp",
22+
"cstring": "cpp",
23+
"ctime": "cpp",
24+
"cwchar": "cpp",
25+
"cwctype": "cpp",
26+
"deque": "cpp",
27+
"forward_list": "cpp",
28+
"list": "cpp",
29+
"map": "cpp",
30+
"set": "cpp",
31+
"string": "cpp",
32+
"unordered_map": "cpp",
33+
"unordered_set": "cpp",
34+
"vector": "cpp",
35+
"exception": "cpp",
36+
"algorithm": "cpp",
37+
"functional": "cpp",
38+
"iterator": "cpp",
39+
"memory": "cpp",
40+
"memory_resource": "cpp",
41+
"numeric": "cpp",
42+
"optional": "cpp",
43+
"random": "cpp",
44+
"ratio": "cpp",
45+
"string_view": "cpp",
46+
"system_error": "cpp",
47+
"tuple": "cpp",
48+
"type_traits": "cpp",
49+
"utility": "cpp",
50+
"format": "cpp",
51+
"fstream": "cpp",
52+
"initializer_list": "cpp",
53+
"iomanip": "cpp",
54+
"iosfwd": "cpp",
55+
"iostream": "cpp",
56+
"istream": "cpp",
57+
"limits": "cpp",
58+
"mutex": "cpp",
59+
"new": "cpp",
60+
"numbers": "cpp",
61+
"ostream": "cpp",
62+
"semaphore": "cpp",
63+
"span": "cpp",
64+
"sstream": "cpp",
65+
"stdexcept": "cpp",
66+
"stop_token": "cpp",
67+
"streambuf": "cpp",
68+
"thread": "cpp",
69+
"cinttypes": "cpp",
70+
"typeinfo": "cpp",
71+
"variant": "cpp"
72+
}
73+
}

tests/CameraCaptureTest.cpp

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,35 @@
1010

1111
using namespace std;
1212

13-
namespace CameraCapure_test {
14-
// start関数が最終的に標準出力に"Hello KATLAB"を出力することを確認するテスト
15-
TEST(CameraCapureTest, Start)
13+
namespace etrobocon2025_test {
14+
// CameraCaptureクラスがインスタンス化ができるか確認するテスト
15+
TEST(CameraCapureTest, CameraCapureInit)
1616
{
17-
string expected = "Camera started.\n";
17+
CameraCapture cap;
18+
int expected = 0;
1819

19-
// 標準出力をキャプチャ
20-
OStreamCapture capture(cout);
20+
EXPECT_EQ(expected, cap.getCameraID());
21+
}
2122

23+
// 有効なcameraIDを指定した場合のテスト
24+
TEST(CameraCapureTest, SetCameraIDTrue)
25+
{
26+
CameraCapture cap;
27+
int expected = 1;
28+
int cameraID = 1;
2229

23-
// start関数を実行
24-
CameraCapture::start();
30+
ASSERT_TRUE(cap.setCameraID(cameraID));
31+
EXPECT_EQ(expected, cap.getCameraID());
32+
}
33+
34+
// 無効なcameraIDを指定した場合のテスト
35+
TEST(CameraCapureTest, SetCameraIDFalse)
36+
{
37+
CameraCapture cap;
38+
int expected = 0;
39+
int cameraID = -1;
2540

26-
EXPECT_EQ(expected, capture.getOutput());
41+
ASSERT_FALSE(cap.setCameraID(cameraID));
42+
EXPECT_EQ(expected, cap.getCameraID());
2743
}
2844
} // namespace etrobocon2025_test

usb_camera/CameraCapure.cpp

Lines changed: 84 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,94 @@
44
* @author Haranaruki
55
*/
66

7-
#include "CameraCapure.h"
7+
#include "CameraCapure.h"
88

9-
void CameraCapture::start() {
10-
std::cout << "Camera started." << std::endl;
9+
CameraCapture::CameraCapture() : cameraID(0) {}
1110

12-
cv::VideoCapture cap(0); // デバイスID 0 を指定
13-
14-
if (!cap.isOpened()) {
15-
std::cerr << "カメラを開けませんでした。" << std::endl;
16-
return ;
11+
int CameraCapture::getAvailableCameraID(int maxTested)
12+
{
13+
for(int i = 0; i < maxTested; ++i) {
14+
cv::VideoCapture cap(i);
15+
if(cap.isOpened()) {
16+
cap.release();
17+
return i;
1718
}
19+
}
20+
return -1;
21+
}
1822

19-
cv::Mat frame;
20-
while (true) {
21-
cap >> frame;
22-
if (frame.empty()) break;
23+
int CameraCapture::getCameraID()
24+
{
25+
return cameraID;
26+
}
2327

24-
cv::imshow("USB Camera", frame);
25-
if (cv::waitKey(1) == 'q') break;
26-
}
28+
bool CameraCapture::setCameraID(int id)
29+
{
30+
if(id < 0) {
31+
return false;
32+
}
33+
cameraID = id;
34+
return true;
35+
}
36+
37+
bool CameraCapture::openCamera()
38+
{
39+
cap.open(cameraID);
40+
if(!cap.isOpened()) {
41+
std::cerr << "カメラを開くことができませんでした。" << std::endl;
42+
return false;
43+
}
44+
45+
return true;
46+
}
2747

28-
cap.release();
29-
cv::destroyAllWindows();
30-
return ;
48+
void CameraCapture::setCapProps(double width, double height)
49+
{
50+
cap.set(cv::CAP_PROP_FRAME_WIDTH, width);
51+
cap.set(cv::CAP_PROP_FRAME_HEIGHT, height);
3152
}
53+
54+
bool CameraCapture::getFrame(cv::Mat& outFrame)
55+
{
56+
if(!cap.isOpened()) {
57+
std::cerr << "カメラを開いて開いてください。" << std::endl;
58+
return false;
59+
}
60+
cap >> outFrame;
61+
if(outFrame.empty()) {
62+
std::cerr << "フレームの取得に失敗しました。" << std::endl;
63+
return false;
64+
}
65+
return true;
66+
}
67+
68+
bool CameraCapture::getFrames(cv::Mat* frames, int numFrames, double seconds)
69+
{
70+
if(frames == nullptr || numFrames <= 0 || seconds <= 0) return false;
71+
int intervalMs = static_cast<int>((seconds * 1000) / numFrames);
72+
bool allSuccess = true;
73+
for(int i = 0; i < numFrames; ++i) {
74+
if(!getFrame(frames[i])) {
75+
std::cerr << "フレーム " << i << " の取得に失敗しました。" << std::endl;
76+
allSuccess = false;
77+
}
78+
if(i < numFrames - 1) {
79+
std::this_thread::sleep_for(std::chrono::milliseconds(intervalMs));
80+
}
81+
}
82+
return allSuccess;
83+
}
84+
85+
bool CameraCapture::saveLatestFrame(std::string filepath, std::string filename)
86+
{
87+
if(latestFrame.empty()) {
88+
std::cerr << "保存するフレームがありません。" << std::endl;
89+
return false;
90+
}
91+
std::string fullpath = filepath + "/" + filename + ".JPEG";
92+
if(!cv::imwrite(fullpath, latestFrame)) {
93+
std::cerr << "画像の保存に失敗しました: " << fullpath << std::endl;
94+
return false;
95+
}
96+
return true;
97+
}

usb_camera/CameraCapure.h

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,27 @@
99

1010
#include <iostream>
1111
#include <opencv2/opencv.hpp>
12+
#include <vector>
13+
#include <string>
14+
#include <thread>
15+
#include <chrono>
1216

1317
class CameraCapture {
14-
public:
15-
static void start();
16-
};
18+
private:
19+
cv::VideoCapture cap;
20+
int cameraID;
21+
cv::Mat latestFrame;
1722

23+
public:
24+
CameraCapture();
25+
int getAvailableCameraID(int maxTested = 10);
26+
int getCameraID();
27+
bool setCameraID(int id);
28+
bool openCamera();
29+
void setCapProps(double width, double height);
30+
bool getFrame(cv::Mat& outFrame);
31+
bool getFrames(cv::Mat* frames, int numFrames, double seconds);
32+
bool saveLatestFrame(std::string filepath, std::string filename);
33+
};
1834

1935
#endif

0 commit comments

Comments
 (0)