使用树莓派原装CSI摄像头录制视频并利用灰度重心法获取重心,将图像和重心数据通过Socket实时传输到电脑上
因为需要实现程序一启动便打开摄像头计算数据,同时启动Socket服务器等待客户端连接,所以利用C++11中的thread库通过多线程实现程序
树莓派-服务端
#include <iostream>
#include <unistd.h>
#include <cstring>
#include <string>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <thread>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
#define USEPORT 1234
#define T 20
Mat FRAME;
Point PCENTER;
//灰度重心法函数
Point gray_center(Mat& img)
{
Mat img_gray;
cvtColor(img, img_gray, COLOR_BGR2GRAY, 0);
Point Center;
double sumval = 0;
MatIterator_<uchar> it, end;
for (int i = 0; i < img_gray.cols; i++)
{
for (int j = 0; j < img_gray.rows; j++)
{
double s = img_gray.at<uchar>(j, i);
if (s < T)
s = 0;
sumval += s;
}
}
Center.x = Center.y = 0;
double x = 0, y = 0;
for (int i = 0; i < img_gray.cols; i++)
{
for (int j = 0; j < img_gray.rows; j++