配置:
Ubuntu16.04
opencv-3.4.12
Ubuntu安装opencv-亲测
cpp代码
#include <iostream>
#include <string>
#include <opencv2/opencv.hpp>
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/objdetect.hpp>
#include <opencv2/imgproc/types_c.h>
#include <opencv2/videoio.hpp>
//----------------socket网络编程所需头文件-------------------
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>//用于储存编码后的视屏数据
#include <sys/ioctl.h>
using namespace std;
using namespace cv;
static int src_sockfd;
string IP_ADDRES="192.168.1.9";
int PORT=8898;
int main( int argc, char** argv )
{
// 打开摄像头
// VideoCapture capture(0);
// 打开文件
VideoCapture capture;
capture.open("/mnt/hgfs/VWFile/1.mp4");
if (!capture.isOpened()) {
printf("could not read this video file...\n");
return -1;
}
//socket
struct sockaddr_in src_addr;
//创建socket
if ((src_sockfd = socket(AF_INET,SOCK_DGRAM,0)) == -1)
{
perror("Socket failed!\n");
exit(1);
}
printf("img Socket id = %d\n",src_sockfd);
//设置sockaddr_in 结构体中相关参数
memset(&src_addr,0, sizeof(src_addr));
src_addr.sin_family = AF_INET;
src_addr.sin_port = htons(PORT);
src_addr.sin_addr.s_addr = inet_addr(IP_ADDRES.c_str());
//inet_pton(AF_INET, servInetAddr, &serv_addr.sin_addr);
bzero(&(src_addr.sin_zero), 8);
//创建显示窗口
namedWindow("CSI Camera", WINDOW_AUTOSIZE);
Mat src_img;
//逐帧显示
while (capture.read(src_img))
{
resize(src_img, src_img, Size(640, 480));
imshow("CSI Camera",src_img);
vector<uchar> data_encode;
vector<int>quality =vector<int>(2);
quality[0]=IMWRITE_JPEG_QUALITY;//编码格式
quality[1]=50;//图片压缩比例:50为压缩为50%
imencode(".jpg", src_img, data_encode,quality);//将图像编码
int nSize=data_encode.size();
//cout<<"src图像的数据大小: "<<nSize<<endl;
// write(sockfd, reinterpret_cast<uchar*>(&nSize), sizeof(int));
sendto(src_sockfd,&data_encode[0],nSize,0,(struct sockaddr*)&src_addr, sizeof(src_addr));
int keycode = cv::waitKey(30) & 0xff ; //ESC键退出
if (keycode == 27) break ;
}
close(src_sockfd);//结束之后关闭这个网络套接字
destroyWindow("CSI Camera");
}
我使用Cmake进行编译
MakeList.txt文件
# cmake needs this line
cmake_minimum_required(VERSION 2.8)
# Define project name
project(opencv_socket _project)
# Find OpenCV, you may need to set OpenCV_DIR variable
# to the absolute path to the directory containing OpenCVConfig.cmake file
# via the command line or GUI
find_package(OpenCV REQUIRED)
# If the package has been found, several variables will
# be set, you can find the full list with descriptions
# in the OpenCVConfig.cmake file.
# Print some message showing some of them
message(STATUS "OpenCV library status:")
message(STATUS " version: ${OpenCV_VERSION}")
message(STATUS " libraries: ${OpenCV_LIBS}")
message(STATUS " include path: ${OpenCV_INCLUDE_DIRS}")
if(CMAKE_VERSION VERSION_LESS "2.8.11")
# Add OpenCV headers location to your include paths
include_directories(${OpenCV_INCLUDE_DIRS})
endif()
# Declare the executable target built from your sources
add_executable(opencv_socket example.cpp)
# Link your application with OpenCV libraries
target_link_libraries(opencv_socket PRIVATE ${OpenCV_LIBS})
编译/运行
cmake .
make
./opencv_socket
最终效果演示。
此项目Linux端+Window