Two computers transmit files through serial port and call opencv library to program and display pictures and text

1, Two computers use serial port for file transmission

(1) Experiment content

Connect the two laptops with serial port by means of usb to rs232 module and DuPont line. Then use tools and software such as serial port assistant (with file transfer function) to transfer a large file (picture, video and compressed package software) on one laptop to another computer, budget the relationship between file size, baud rate and transmission time, and compare the actual transmission time.

(2) . configuration environment

1. Prepare two computers, two usb to serial ports and four DuPont cables.
2. Connect as shown in the figure (connection mode: 3V3-3V3, GND-GND, TXD-RXD, RXD-TXD):

Note: after my actual operation, I found that (GND-GND, TXD-RXD, RXD-TXD) must be connected, but (3V3-3V3) can not be connected


3. Open the serial port assistant (I use sscom5.13.1, which you can choose by yourself), select the port number and baud rate, and configure it as shown in the figure (it is not necessary to configure it completely according to the actual situation):

(3) . experimental process

1. First click open serial port, then click open file, then select the file to send, and click send file (here I send a picture jpg file):

2. Wait patiently, record the transmission time, and the receiver receives successfully:

3. Open the folder, find the transfer file, restore the file type, and you can see that the transfer is successful:


(4) . result analysis

The relationship among budget file size, baud rate and transmission time is:
Theoretical time = (file size * 8) / baud rate
The experimental results show that the actual transmission time is greater than the estimated transmission time

2, Under Ubuntu, call opencv library to program and display pictures and text based on Linux environment

(1) . experimental requirements

Under Ubuntu, use C/C + + (or python) to call opencv library to program and display a picture, open a text file named "logo.txt" (there is only one line of text file, including your own name and student number), and read the font data of the corresponding characters in the Chinese character 24 * 24 dot matrix font library (file HZKf2424.hz in the compressed package) according to the name and student number, Overlay the name and student number to the lower right of this picture.

(2) . experimental preparation

1. Open Ubuntu to enter the Linux system, create a new folder, and put 24 dot matrix HZ file, ASCL code zf file and a picture in this folder.

2. Create a new text file named logo, and then enter your name and student number in it (for privacy and security, I made it up here (ノ) ω<. ) ノ))☆.. )

Note: when inputting the text you want to display in the logo.txt file, you need to write it in ANSI code, otherwise Chinese will be garbled


Open the text document - > file - > Save as - > select ANSI code and save.

(3) . code writing

1. Open the terminal in the folder, create a new file named main.cpp, and then enter the code:

#include<iostream>
#include<opencv/cv.h>
#include"opencv2/opencv.hpp"
#include<opencv/cxcore.h>
#include<opencv/highgui.h>
#include<math.h>

using namespace cv;
using namespace std;

void paint_chinese(Mat& image,int x_offset,int y_offset,unsigned long offset);
void paint_ascii(Mat& image,int x_offset,int y_offset,unsigned long offset);
void put_text_to_image(int x_offset,int y_offset,String image_path,char* logo_path);

int main(){
    String image_path="fei.jpg";//Name of picture
    char* logo_path="logo.txt";//Name of Chinese character file
    put_text_to_image(400,600,image_path,logo_path);//change txt place
    return 0;
}

void paint_ascii(Mat& image,int x_offset,int y_offset,unsigned long offset){
    //Coordinates of the starting point of the drawing
	Point p;
	p.x = x_offset;
	p.y = y_offset;
	 //Storing ascii word film
	char buff[16];           
	//Open ascii font file
	FILE *ASCII;

	if ((ASCII = fopen("Asci0816.zf", "rb")) == NULL){
		printf("Can't open ascii.zf,Please check the path!");
		//getch();
		exit(0);
	}

	fseek(ASCII, offset, SEEK_SET);
	fread(buff, 16, 1, ASCII);

	int i, j;
	Point p1 = p;
	for (i = 0; i<16; i++)                  //Sixteen char s
	{
		p.x = x_offset;
		for (j = 0; j < 8; j++)              //One char and eight bit s
		{
			p1 = p;
			if (buff[i] & (0x80 >> j))    /*Test whether the current bit is 1*/
			{
				/*
					Because the original ascii word film was 8 * 16, it was not large enough,
					So the original pixel is replaced by four pixels,
					After replacement, there are 16 * 32 pixels
					ps: I think it's unnecessary to write code like this, but I only think of this method for the time being
				*/
				circle(image, p1, 0, Scalar(255, 144, 163), -1);
				p1.x++;
				circle(image, p1, 0, Scalar(255, 144, 163), -1);
				p1.y++;
				circle(image, p1, 0, Scalar(255, 144, 163), -1);
				p1.x--;
			        circle(image, p1, 0, Scalar(255, 144, 163), -1);
			}						
            p.x+=2;            //The original one pixel is changed to four pixels, so both x and y should be + 2, and Scalar(255, 144, 163) is the font color of the name
		}
		p.y+=2;
	}
}
void paint_chinese(Mat& image,int x_offset,int y_offset,unsigned long offset){//Draw Chinese characters on the picture
    Point p;
    p.x=x_offset;
    p.y=y_offset;
    FILE *HZK;
    char buff[72];//72 bytes for storing Chinese characters

    if((HZK=fopen("HZKf2424.hz","rb"))==NULL){
        printf("Can't open HZKf2424.hz,Please check the path!");
        exit(0);//sign out
    }
    fseek(HZK, offset, SEEK_SET);/*Move the file pointer to the offset position*/
    fread(buff, 72, 1, HZK);/*Read 72 bytes from the offset position, and each Chinese character occupies 72 bytes*/
    bool mat[24][24];//Define a new matrix to store the transposed text film
    int i,j,k;
    for (i = 0; i<24; i++)                 /*24x24 Dot matrix Chinese characters, a total of 24 lines*/
	{
        	for (j = 0; j<3; j++)                /*There are 3 bytes in the horizontal direction, and the value of each byte is determined by cycle*/
			for (k = 0; k<8; k++)              /*Each byte has 8 bits, and the loop judges whether each byte is 1*/
				if (buff[i * 3 + j] & (0x80 >> k))    /*Test whether the current bit is 1*/
				{
					mat[j * 8 + k][i] = true;          /*1 is stored in a new word film*/
				}
				else {
					mat[j * 8 + k][i] = false;
				}
	}
	
    for (i = 0; i < 24; i++)
	{
		p.x = x_offset;
		for (j = 0; j < 24; j++)
		{		
			if (mat[i][j])
				circle(image, p, 1, Scalar(138, 67, 128), -1);		  //Write (replace) pixel and student number color
			p.x++;                                                //Shift right one pixel
		}
		p.y++;                                                    //Move down one pixel
	}
}

void put_text_to_image(int x_offset,int y_offset,String image_path,char* logo_path){//Put Chinese characters on the picture
//x and y are the starting coordinates of the first word on the picture
    //Get pictures through picture path
    Mat image=imread(image_path);
    int length=17;//The length of characters to be printed (the length can be as many bytes as you print, which can be adjusted according to your own situation)
    unsigned char qh,wh;//Define area code and tag number
    unsigned long offset;//Offset
    unsigned char hexcode[30];//Hexadecimal used to store Notepad reading. Remember to use unsigned
    FILE* file_logo;

    if ((file_logo = fopen(logo_path, "rb")) == NULL){
		printf("Can't open txtfile,Please check the path!");
		//getch();
		exit(0);
	}

    fseek(file_logo, 0, SEEK_SET);
    fread(hexcode, length, 1, file_logo);
    int x =x_offset,y = y_offset;//x. Y: the starting coordinate of the text drawn on the picture

    for(int m=0;m<length;){
        if(hexcode[m]==0x23){
            break;//It ends when the # number is read
        }
        else if(hexcode[m]>0xaf){
            qh=hexcode[m]-0xaf;//The font used starts with Chinese characters, not Chinese symbols
            wh=hexcode[m+1] - 0xa0;//Calculation bit code
            offset=(94*(qh-1)+(wh-1))*72L;
            paint_chinese(image,x,y,offset);
            /*
            Calculate the offset in the Chinese character library
            Each Chinese character is represented by a 24 * 24 dot matrix
            A line has three bytes, a total of 24 lines, so 72 bytes are required
            */

            m=m+2;//The internal code of a Chinese character occupies two bytes,
            x+=24;//A Chinese character has 24 * 24 pixels. Because it is placed horizontally, it moves 24 pixels to the right
        }

        else{
        //When the read character is ASCII
        wh=hexcode[m];
        offset=wh*16l;//Calculate the offset of English characters
        paint_ascii(image,x,y,offset);
        m++;//English characters only occupy one byte in the file, so just move back one bit
        x+=16;
        }

    }

    cv::imshow("image", image);
    cv::waitKey();
}

Note:
1. Modify the picture name in line 16 and the name student number file name in line 17 according to the actual situation
2.18 line code is the text display position
3.115 line code is the length of characters to be printed
4. Please check the code annotation for color modification and other details

(4) . compilation and operation

1. Save the main.cpp file and compile the instructions in the terminal

g++ main.cpp -o test `pkg-config --cflags --libs opencv`


2. Get the following file named test

3. Enter the following code to run the program:

./test


4. Operation results

3, Summary

Through this experiment, I learned how to use the serial port for file transmission between two computers, understood and learned the principle of Chinese character dot matrix, understood the relationship between the conversion of internal code, national standard code and location code, and called opencv library to program and display pictures and texts under Ubuntu based on Linux Environment.

4, References

https://blog.csdn.net/yesking_new/article/details/6694032
https://blog.csdn.net/junseven164/article/details/121130735
https://blog.csdn.net/qq_46467126/article/details/121313820

Keywords: Linux OpenCV Ubuntu

Added by shai1 on Sat, 20 Nov 2021 05:00:16 +0200