使用树莓派原装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++)
		{
			double s = img_gray.at<uchar>(j, i); 
			if (s < T)
				s = 0; 
			x += i * s / sumval;
			y += j * s / sumval;
		}
	}
	Center.x = cvRound(x);
	Center.y = cvRound(y);
	return Center;
}

//摄像头图像处理
void cam_stand_by()
{
	VideoCapture capture; 
	if (!capture.isOpened())
	{
		cout << "fail to open camera!" << endl; 
		exit(-1); 
	}
	while (1)
	{
		capture >> FRAME; 
		PCENTER = gray_center(FRAME); 
		if (waitKey(30) >= 0)
			break; 
	}
}

int main()
{
//开启一个线程,并将其分离,使不阻塞主程序
	thread cam_th(cam_stand_by); 
	cam_th.detach(); 
//启动服务端
	int serverSock = socket(AF_INET, SOCK_STREAM, 0); 
	if (serverSock < 0)
	{
		cout << "socket creation failed" << endl; 
		exit(-1); 
	}
	cout << "socket creation successfully" << endl; 

	struct sockaddr_in serverAddr; 
	memset(&serverAddr, 0, sizeof(serverAddr)); 
	serverAddr.sin_family = AF_INET; 
	serverAddr.sin_port = htons(USEPORT); 
	serverAddr.sin_addr.s_addr = htonl(INADDR_ANY); 

	if (bind(serverSock, 
		(struct sockaddr*)&serverAddr, 
		sizeof(struct sockaddr)) == -1)
	{
		cout << "Bind error, Port["<< USEPORT << "]" << endl; 
	       exit(-1); 
    }
	cout << "Bind successfully" << endl; 

	if (listen(serverSock, 10) == -1)
	{
		cout << "Listen error!" << endl; 
	}
	cout << "Listening on port[" << USEPORT << "]" << endl; 

	while (1)
	{
		struct sockaddr clientAddr; 
		int size = sizeof(struct sockaddr); 
		int clientSock = accept(serverSock, (struct sockaddr*)&clientAddr, (socklen_t*)&size); 
		cout << "\n****NEW client touched****" << endl; 
		while (1)
		{
			if (send(clientSock, FRAME.data, FRAME.total()*FRAME.elemSize(), 0) < 0)
				break;  
			send(clientSock, &PCENTER, sizeof(Point), 0); 
		}
		cout << "\n==== CLIENT BREAK ====" << endl; 
		close(clientSock); 
	}
	close(serverSock); 
	return 0; 
}

PC-客户端

#include <iostream>
#include <string>
#include <opencv2/opencv.hpp> //包含opencv
#include <WinSock2.h> //包含WinSock2.h头文件
using namespace std;
using namespace cv; 
#pragma comment(lib, "ws2_32.lib") //加载 ws2_32.dll 
#pragma warning(disable:4996)
#define imgSize 640*480*3 
//图像大小,由于传输三通道彩色图,所以*3
//有尝试过每次由树莓派先传输图像大小到客户端,但出现了一些问题,所以直接在这里定义宏
constexpr auto RASPI_IP = "192.168.1.119";

int main()
{
	//****初始化
	WSADATA wsaData;
	WSAStartup(MAKEWORD(2, 2), &wsaData);

	//****创建套接字
	SOCKET sock = socket(PF_INET, SOCK_STREAM, 0);

	//****创建sockAddr结构体
	sockaddr_in sockAddr;
	memset(&sockAddr, 0, sizeof(sockAddr));
	sockAddr.sin_family = PF_INET;
	sockAddr.sin_port = htons(1234);
	sockAddr.sin_addr.s_addr = inet_addr(RASPI_IP); //树莓派的局域网IP

//****建立连接
	connect(sock, (SOCKADDR*)& sockAddr, sizeof(SOCKADDR));
	cout << "客户端发送链接请求" << endl;
	int bytes = 0;
	//****接收服务器传回的数据 
	while (1)
	{
		cout << "等待服务端发送信息.." << endl;

		char* imgdata = new char[imgSize];
		char* pointdata = new char[MAXBYTE];
		//recv(sock, imgdata, imgSize, NULL);
		//若直接使用recv接收,会有丢包情况出现,图片不完整
		for (int i = 0; i < imgSize; i += bytes) //循环接收图片信息,防止没接收完
		{
			if ((bytes = recv(sock, imgdata + i, imgSize - i, 0)) == -1)
			{
				cout << "Fail to recive" << endl;
				exit(-1);
			}
		}
		
		recv(sock, pointdata, sizeof(Point), NULL); //接收重心Point信息
		Point* tack = (Point*)pointdata; 
		Point center(tack->x, tack->y);
		cout << tack->x << " " << tack->y << endl; 

		Mat img(Size(640, 480), CV_8UC3, imgdata); 
		circle(img, center, 6, Scalar(0, 255, 0), -1);

		imshow("Gray_Center", img);
		waitKey(1);
	}

	//关闭套接字、终止使用 DLL
	closesocket(sock);
	WSACleanup();

	return 0;
}

最终效果