TCP/IP Live Video Transmission between QT in Windows and OpenCV in Linux/Raspberry Pi

Now capture or video stream is often used in projects of Raspberry Pi, such as video surveillance system and some small electric vehicle using Pi. In the most of time, we want to see the live video on our laptop or mobile phone, so I wrote this program to transmit live video between Raspi and Windows OS using TCP/IP.

Environment and Introduction

Client: Raspberry Pi 2 (Raspbian OS) with Logitech USB camera, installed OpenCV 3.1.0(OpenCV 2.4.x is also ok), using C.

Server: In Windows side, QT 5+ version is installed, using C++. Actually you also can install QT in Linux OS, then you can use the same code, because QT is a good cross-platform, so as to achieve Linux to Linux video transmission.

Pi 2 and Windows laptop are connected to the same router, so they are in same LAN.

setup

How does the program running?

Actually I can tunning the color value in QT(Windows, server) side, so that the program in Pi(Client) can do image processing, we can see the live image change in Windows GUI. It means that client transmit images to server, server and client transmit text information to each other at the same time.

qt-in-windows

At first, you click “Start” button in Windows GUI, the server program will start to listen port, once you execute the program in Pi/Client, you will see the live video in windows GUI and do real-time tunning.

linux-in-raspi

We can see from above picture, we can achieve 19.5 Frame Per Second while transmitting video, it’s quite fluent. It will be much faster if we use Pi camera.

Key Points

1.In client(Pi) program, a thread is created to receive text data(not image) from server, parallel with image transmitting.

	pthread_t thread;
	if(pthread_create(&thread, NULL, RcvData, &socket_fd)!=0)
	{
		printf("Thread create error \r\n");
	}

2.In client(Pi) side, image type is IPLImage of OpenCV; in server(QT) side, image type is QImage. We need to note that channle of IPLImage is order of BGR, but RGB for QImage, so we need to swap R and B channel before sending it. In OpenCV, cvConvertImage can achieve this function using parameter CV_CVTIMG_SWAP_RB, so you can see that the the image in Pi in above picture looks different. For gray image, no need to change anything.

3.I define a strcture for image, including image type(because original image and gray image need to be transmitted), length of image and raw data, you can see in the code.

Code

You can download the code from my Github. If you want to use Raspberry Pi 2/3 to run the client program, you may need this new makefile:

CXX ?= gcc

CXXFLAGS += -c -Wall $(shell pkg-config --cflags opencv)
LDFLAGS += $(shell pkg-config --libs --static opencv)

all: tcp

tcp: main.o; $(CXX) $< -o $@ $(LDFLAGS)

%.o: %.c; $(CXX) $< -o $@ $(CXXFLAGS)

clean: ; rm -f main.o tcp

Although the code is not so clean and nice, but I hope it’s helpful to you, especially to some beginners.

POST ANY SUGGESTIONS OR QUESTIONS TO ME.

Good Night!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s