ETree_F011_Board FPGA开发板摄像头以太网通信上位机研究

ETree_F011_Board FPGA开发板配有 MT9V011 30W摄像头模组的下位机代码,但是原有资料中摄像头以太网通信的上位机是可执行文件没有源代码,本文基于OpenCV3.0实现了通过UDP读取摄像头采集的图片。

#include <opencv2\imgproc\imgproc.hpp>
#include <opencv2\highgui\highgui.hpp>
#include <WinSock2.h>
#include <stdio.h>    
#include <stdlib.h>    
#include <string.h> 
#include <time.h>

#pragma comment(lib,"ws2_32.lib")
using namespace cv;

int main(int argc, char* argv[])
{
	WSADATA wsaData;
	SOCKET sockListener;
	SOCKADDR_IN RecvAddr, SenderAddr;
	char cRecvBuff[1280];
	int nSize, nbSize;
	nSize = sizeof (SOCKADDR_IN);
	if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0)
	{
		printf("Can't initiates windows socket!Program stop.\n");
		return -1;
	}
	sockListener = socket(AF_INET, SOCK_DGRAM, 0);
	RecvAddr.sin_family = AF_INET;
	RecvAddr.sin_port = htons(32768);
	RecvAddr.sin_addr.s_addr = inet_addr("192.168.3.3");
	if (bind(sockListener, (SOCKADDR FAR *)&RecvAddr, sizeof(RecvAddr)) != 0)
	{
		printf("Can't bind socket to local port!Program stop.\n");
		return -1;
	}
	printf("Local port: 192.168.3.3:32768\n");

	int Width = 640;
	int Height = 480;
	unsigned char* imageArray = (unsigned char *)malloc(sizeof(unsigned char)*Width*Height);
	Mat image, imageRGB;

	double atime, btime;
	while (waitKey(1) != 27)
	{
		atime = clock();
		int k = 0;
		while (k < Height / 2)
		{
			int firstLine = 0;
			if ((nbSize = recvfrom(sockListener, cRecvBuff, 1280, 0, (SOCKADDR FAR *) &SenderAddr, &nSize)) == SOCKET_ERROR)
			{
				printf("Recive Error\n");
				break;
			}

			for (size_t i = 0; i < nbSize; i++)
			{
				if (cRecvBuff[i] == 0)
					firstLine++;
				else
					break;
			}
			memcpy(imageArray + k * nbSize, cRecvBuff, nbSize);
			k++;
			if (firstLine == Width - 1)
				k = 1;
		}

		image = cv::Mat(Height, Width, CV_8UC1, imageArray);
		cvtColor(image, imageRGB, COLOR_BayerBG2RGB);

		btime = clock();
		char text[16];
		sprintf(text, "fps=%.3f", 1000.0 / (btime - atime));
		putText(imageRGB, text, Point(10, 20), FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(0, 255, 255), 1, 8, 0);
		imshow("show", imageRGB);
	}

	free(imageArray);
	closesocket(sockListener);
	WSACleanup();
	return 0;
}

可以实现16帧/秒的稳定传输。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值