- PNG-Dateien in C++
- Gaußscher Glättungs Kernel
- WebCam mit OpenCV
Diese kleine Demonstration zeigt, wie man in C++ eine Webcam ansprechen kann. Folgende Punkte werden gezeigt:
- Kamera starten
- Bild vertikal spiegeln (damit die rechte Hand rechts erscheint)
- Text Anzeigen
- Zeit für den Aufbau eines Frames berechnen und ausgeben
#include <opencv2/opencv.hpp>
#include <chrono>
int main(int argc, char** argv)
{
cv::VideoCapture cap(0);
int i = 0;
char buffer[1024];
int fontFace = cv::FONT_HERSHEY_SCRIPT_SIMPLEX;
int fontScale = 2;
int thickness = 2;
int baseline = 0;
while(true)
{
auto start = std::chrono::high_resolution_clock::now();
cv::Mat frame;
cap >> frame;
cv::flip(frame, frame, 1);
i++;
snprintf(buffer, sizeof(buffer), "Funny %d\n", i);
cv::Size textSize = cv::getTextSize(buffer, fontFace, fontScale, thickness, &baseline);
baseline += thickness;
cv::Point textOrg((frame.cols - textSize.width)/2, (frame.rows + textSize.height)/2);
cv::putText(frame, buffer, textOrg, fontFace, fontScale, cv::Scalar::all(255), thickness, 8);
if( frame.empty())
break;
cv::imshow("frame", frame);
char c = (char)cv::waitKey(30); // 30ms
if( c == 'q')
{
break;
}
auto ende = std::chrono::high_resolution_clock::now();
int milli = std::chrono::duration_cast<std::chrono::nanoseconds>(ende-start).count()/1000000;
fprintf(stderr, "%dms\n", milli);
}
cap.release();
cv::destroyAllWindows();
return 0;
}