C++ OpenCV摄像机UDP套接字流
我正在尝试编写一个客户端,该客户端从相机捕获原始帧,并通过udp套接字将这些帧发送到服务器 以下是我客户的代码:C++ OpenCV摄像机UDP套接字流,c++,windows,sockets,camera,C++,Windows,Sockets,Camera,我正在尝试编写一个客户端,该客户端从相机捕获原始帧,并通过udp套接字将这些帧发送到服务器 以下是我客户的代码: cv::VideoCapture cap(0); // open the video camera no. 0 if (!cap.isOpened()){ puts("Cannot open the video cam"); return -1; } double dWidth = cap.get(CV_CAP_PROP_FRAME_WIDTH); //get th
cv::VideoCapture cap(0); // open the video camera no. 0
if (!cap.isOpened()){
puts("Cannot open the video cam");
return -1;
}
double dWidth = cap.get(CV_CAP_PROP_FRAME_WIDTH); //get the width of frames of the video
double dHeight = cap.get(CV_CAP_PROP_FRAME_HEIGHT); //get the height of frames of the video
cout << "Frame size : " << dWidth << " x " << dHeight << endl;
while (TRUE){
cv::Mat frame;
bool bSuccess = cap.read(frame); // read a new frame from video
if (!bSuccess){
puts("Cannot read a frame from video stream");
break;
}
frame = (frame.reshape(0, 1)); // to make it continuous
int imgSize = frame.total()*frame.elemSize();
if (send(s, (const char*)frame.data, imgSize, 0) < 0){
printf("Send failed %d\n", GetLastError());
return -1;
}
}
}
这是服务器:
DWORD bytes = 0, imgSize = 0;
while (TRUE){
recv(new_socket, (char*)buffer, 5999, 0);
cv::Mat img = cv::Mat::zeros(690, 690, CV_8UC3);
imgSize = img.total()*img.elemSize();
//Receive data here
for (int i = 0; i < imgSize; i += bytes) {
if ((bytes = recv(new_socket, (char*)sockData + i, imgSize - i, 0)) == -1) {
puts("recv failed");
break;
}
}
// Assign pixel value to img
int ptr = 0;
for (int i = 0; i < img.rows; i++) {
for (int j = 0; j < img.cols; j++) {
// following line crashes with Access violation
img.at<cv::Vec3b>(i, j) = cv::Vec3b(sockData[ptr + 0], sockData[ptr + 1], sockData[ptr + 2]); ptr = ptr + 3;
}
}
cv::imwrite("prova.jpg", img);
cv::namedWindow("Server", CV_WINDOW_AUTOSIZE); //Create a Window for display
cv::imshow("Server", img);
}
使用此代码,我在服务器上获得访问冲突。
我做错了什么
有人能提供一个有效的例子吗?问题是什么?Idk为什么但R_Kapp删除了我的问题???????