Call opencv library programming under Ubuntu to display a picture and display characters

?

1. Internal code, location code encoding rules and glyph data storage format of Chinese characters

The in-machine code, location code encoding rules and glyph data storage format of Chinese characters are concepts related to Chinese character information processing. Here is a brief description:

1. Internal code:

Internal code is the binary encoding used internally by the computer to represent characters.

There are many kinds of in-machine codes for Chinese characters, common ones include Unicode, GB2312, GBK, UTF-8, etc.

Unicode is an international standard that assigns unique binary codes to all characters in the world, including almost all language characters. It is a universal character set.

2. Location code:

Location code is a concept in GB2312 encoding, which is used to indicate the position of a Chinese character in the character set.

GB2312 divides all commonly used Chinese characters into 94 areas, each area contains 94 bits, totaling 8836 characters.

The area code and bit number are used to indicate the position of a character in the character set. These two numbers can determine the only Chinese character.

3. Encoding rules:

GB2312 encoding rules stipulate the arrangement of Chinese characters and the corresponding internal codes.

The location code of Chinese characters is mapped to the machine code through certain rules. Different encoding systems have different mapping rules.

The UTF-8 encoding rule is a variable-length encoding that uses 1 to 4 bytes to represent a character and is compatible with ASCII.

4. Glyph data storage format:

The glyph data storage format of Chinese characters usually refers to the data representation method in the font library.

The font library contains glyph information of Chinese characters, including strokes, structure, etc.

Glyph data can be stored in the form of dot matrix, outline, etc. to facilitate computer display and processing.

It should be noted that with the development of computer technology, more advanced character encoding and glyph storage methods continue to emerge, and Unicode has become a widely adopted character encoding standard internationally. Different encoding methods and storage formats have their own advantages and disadvantages in different application scenarios.

2. Call the opencv library to program and display a picture and display characters under Ubuntu

1. Edit the txt file of the displayed characters in advance.

2. Select Save as and select ANSI encoding

3. Import pictures into the ubuntu system, 24-dot matrix HZKs2424.hz file, Asci0816.zf file

4. Create the test.cpp file and write the program

#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 & amp; image,int x_offset,int y_offset,unsigned long offset);
void paint_ascii(Mat & amp; 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="pic.jpg";//The name of the picture
    char* logo_path="logo.txt";//The name of the Chinese character file
    put_text_to_image(200,350,image_path,logo_path);//change txt place
    return 0;
}

void paint_ascii(Mat & amp; image,int x_offset,int y_offset,unsigned long offset){<!-- -->
    //The starting point coordinates of drawing
Point p;
p.x = x_offset;
p.y = y_offset;
//Storage ascii character mask
char buff[16];
//Open the 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
{<!-- -->
p.x = x_offset;
for (j = 0; j < 8; j + + ) //A char with eight bits
{<!-- -->
p1 = p;
if (buff[i] & amp; (0x80 >> j)) /*Test whether the current bit is 1*/
{<!-- -->
/*
Since the original ASCII character film is 8*16, it is not big enough.
So the original one pixel is replaced with 4 pixels,
After replacement, there will be 16*32 pixels
ps: I feel like writing code like this is redundant, but for now I only think of this method.
*/
circle(image, p1, 0, Scalar(0, 0, 255), -1);
p1.x + + ;
circle(image, p1, 0, Scalar(0, 0, 255), -1);
p1.y + + ;
circle(image, p1, 0, Scalar(0, 0, 255), -1);
p1.x--;
circle(image, p1, 0, Scalar(0, 0, 255), -1);
}
            p.x + =2; //The original one pixel becomes four pixels, so both x and y should be + 2
}
p.y + =2;
}
}
void paint_chinese(Mat & amp; 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, used to store Chinese characters

    if((HZK=fopen("HZKf2424.hz","rb"))==NULL){<!-- -->
        printf("Can't open HZKf2424.hz,Please check the path!");
        exit(0);//Exit
    }
    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, each Chinese character occupies 72 bytes*/
    bool mat[24][24];//Define a new matrix to store the transposed text mask
    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 horizontally, and the loop determines the size of each byte*/
for (k = 0; k<8; k + + ) /*Each byte has 8 bits, loop to determine whether each bit is 1*/
if (buff[i * 3 + j] & amp; (0x80 >> k)) /*Test whether the current bit is 1*/
{<!-- -->
mat[j * 8 + k][i] = true; /*The value of 1 is stored in the new word mask*/
}
else {<!-- -->
mat[j * 8 + k][i] = false;
}
}
\t
    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(255, 0, 0), -1); //Write (replace) pixels
p.x + + ; //Move one pixel to the right
}
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 the picture through the picture path
    Mat image=imread(image_path);
    int length=18;//The length of characters to be printed (the length is as long as the number of bytes printed, adjust according to your own situation)
    unsigned char qh,wh;//define area code, bit number
    unsigned long offset;//Offset
    unsigned char hexcode[30];//Used to store the hexadecimal read by Notepad, 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: starting coordinates of drawing text on the picture

    for(int m=0;m<length;){<!-- -->
        if(hexcode[m]==0x23){<!-- -->
            break;//End when reading the # sign
        }
        else if(hexcode[m]>0xaf){<!-- -->
            qh=hexcode[m]-0xaf;//The font used starts with the Chinese character ah, not the Chinese character symbol
            wh=hexcode[m + 1] - 0xa0;//Calculate bit code
            offset=(94*(qh-1) + (wh-1))*72L;
            paint_chinese(image,x,y,offset);
            /*
            Calculate the offset in the Chinese character library
            For each Chinese character, it is represented by a 24*24 dot matrix.
            There are three bytes in one line, and there are 24 lines in total, so 72 bytes are needed to represent it.
            */

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

        else{<!-- -->
        //When the characters read are ASCII codes
        wh=hexcode[m];`Insert code snippet here`
        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 them back one byte.
        x + =16;
        }

    }

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

5. Use the following command to compile

6. Use command output

7.Output results

3. Summary

The content of this experiment is to call the opencv library programming to display a picture and display characters under Ubuntu.

Through this experiment, I learned how to use the OpenCV library to perform basic image processing operations under Ubuntu. OpenCV provides powerful tools and functions for processing images, including loading, saving, modifying and rendering text. This is useful for image processing and computer vision applications. In addition, this experiment also highlights some important concepts in programming, such as image data structures, coordinate system conversion, and image rendering.

4. Reference

[Embedded 14] Font reading and display of dot matrix Chinese characters

?