由于QT上位机只能接收BGR565的图像格式,且只能显示灰度或者RGB888,所以PL2PS的数据必须要变换位置,并使用cvtColor函数进行转换
1 void MainWindow::recieve_dis(int udp_index) 2 { 3 Mat recv_img_2(img_h_size, img_w_size, CV_8UC2); 4 Mat recv_img_3(img_h_size, img_w_size, CV_8UC3); 5 6 recv_img_2.data = (uchar*)receive->img_buf + udp_index * pkg_size * pkg_num; 7 8 cvtColor(recv_img_2, recv_img_3, CV_BGR5652RGB);
}
在发送彩色图像的时候,由于以太网速率变大,正在显示img_buf的时候,下一帧图像已经在更新了,那么就会出现图像撕裂的情况,写入和读取不能够在同一帧,所以在上位机做一个乒乓操作,通过传入的udp_index作为帧索引
1 void udpthread::run() 2 { 3 while (!stopped) 4 { 5 int recv = recvfrom(hServer, udp_buf, sizeof(udp_buf), 0, (SOCKADDR*)&addrClient, (int*)&nlen); 6 if (recv>0) 7 { 8 data_head *packageHead = (data_head *)udp_buf; 9 if (packageHead->pkg_cnt < 1440) 10 { 11 memcpy(img_buf + udp_index*pkg_size*pkg_num + packageHead->pkg_cnt*pkg_size, udp_buf + sizeof(data_head), pkg_size); 12 } 13 14 if (packageHead->pkg_cnt == pkg_num - 1) 15 { 16 emit recivok(udp_index); 17 18 if (udp_index == 0) 19 { 20 udp_index = 1; 21 } 22 else 23 { 24 udp_index = 0; 25 } 26 } 27 } 28 } 29 }
1 void MainWindow::recieve_dis(int udp_index) 2 { 3 Mat recv_img_2(img_h_size, img_w_size, CV_8UC2); 4 Mat recv_img_3(img_h_size, img_w_size, CV_8UC3); 5 6 recv_img_2.data = (uchar*)receive->img_buf + udp_index * pkg_size * pkg_num; 7 8 cvtColor(recv_img_2, recv_img_3, CV_BGR5652RGB); //BGR565转RGB888 9 10 bmp = recv_img_3; 11 12 image = MatImageToQt(recv_img_3); 13 pixmap = QPixmap::fromImage(image); 14 ui.img_map->setPixmap(pixmap); 15 16 recvImageNum++; 17 18 ui.recv_cnt->setText(QString::number(recvImageNum)); 19 20 if (recordvideo == 1) 21 { 22 cap_save.write(recv_img_3); 23 } 24 }
标签:index,udp,彩色图像,img,编程,pkg,recv,size From: https://www.cnblogs.com/tuzki9611/p/17596382.html