基于树莓派qt和raspicam开发的红球识别程序
本例程在树莓派4上运行成功,基于arm-gcc-gnueabihf-8 。
1 首先在树莓派编译opencv 3.2.0 并 sudo make install
2 使用cmake 编译raspicam ,确保raspicam支持cv模块。
3 安装qt5-default 和qtcreator
4 运行qt-raspicam
#include "cameraworker.h" #include <fstream> #include <iostream> #include <QDebug> #include <QApplication> #include "unistd.h" #include <iostream> #include <stdio.h> #include <raspicam/raspicam_cv.h> #include <opencv2/opencv.hpp> #include <opencv2/imgproc/imgproc.hpp> #include<opencv2/highgui/highgui.hpp> using namespace std; using namespace cv; CameraWorker::CameraWorker() : cameraRunning(true) { qRegisterMetaType<QImage>("QImage&"); data = new unsigned char[camera.getImageTypeSize(RASPICAM_FORMAT_RGB)]; } CameraWorker::~CameraWorker() { } int CameraWorker::Hough_circle_track(Mat image) { Mat srcImage, dstImage; Mat channel[3]; //cmd &0x01 ==0x00?‰? cmd&0x01 =0x01??????cmd&0x02== cmd&0x02= //cmd & 0x10 ==0x10 valid ;cmd &0x10 =0x00,invalid //cmd &0x20 ==0x20 valid ;cmd &0x20 =0x00,invalid char cmd = 1; int g_nHm = 9; image.copyTo(srcImage); split(image, channel); channel[0] = channel[0].mul(.1*g_nHm); // B (mul: per-element matrix multiplication) channel[1] = channel[1].mul(.1*g_nHm); // G channel[2] = channel[2] - channel[0] - channel[1]; // R channel[2] = 3 * channel[2]; dstImage = channel[2]; GaussianBlur(dstImage, dstImage, Size(9, 9), 2, 2); vector<Vec3f> circles; // HoughCircles(dstImage, circles, CV_HOUGH_GRADIENT, 1, image.rows / 5, 200, 16, 50, 100); for (size_t i = 0; i < circles.size(); i++) { Point center(cvRound(circles[i][0]), cvRound(circles[i][1])); int radius = cvRound(circles[i][2]); circle(image, center, 3, Scalar(0, 255, 0), -1, 8, 0); circle(image, center, radius, Scalar(155, 50, 255), 3, 8, 0); printf("current circles position :" ); cout << circles[i][0] << "\t" << circles[i][1] << "\t" << circles[i][2] << endl; } return cmd; } void CameraWorker::doWork() { // Open the camera cv_camera.set( CV_CAP_PROP_CONVERT_RGB, CV_8UC1 ); if (!cv_camera.open()) { qDebug() << "Error opening camera"; cameraRunning = false; } else { cameraRunning = true; } // camera.setFormat(RASPICAM_FORMAT_BGR); // Wait for the camera sleep(1); Mat image; Mat rgbimage; // While the camera is on (the user has clicked the button), capture while (cameraRunning) { // Capture cv_camera.grab(); cv_camera.retrieve(image); // cv::imwrite("raspicam_cv_image.png",image); // Convert the data and send to the caller to handle // cv::resize(image,image,Size(320,240), 0.0,0.0,INTER_LINEAR); if( Hough_circle_track(image) > 0 ){ cv::cvtColor(image,rgbimage,COLOR_BGR2RGB); // qDebug() << "width is" << image.cols << "height is" << image.rows; QImage img = QImage(rgbimage.data, rgbimage.cols, rgbimage.rows, QImage::Format_RGB888); emit handleImage(img); } // Make the app process stopWork() if necessary qApp->processEvents(); usleep(200); } cv_camera.release(); } void CameraWorker::stopWork() { // Set the flag to false cameraRunning = false; emit finished(); }
[lv]链接:https://pan.baidu.com/s/14M-mx8k84-D9VQhyPpWrfA 提取码:1lba 复制这段内容后打开百度网盘手机App,操作更方便哦 [/lv]