简体   繁体   English

同一张图片使用不同的base64编码

[英]Different base64 encoding for same Image

I need to convert base64 to opencv::mat file. 我需要将base64 to opencv::mat转换base64 to opencv::mat文件。 So I took a set of 10 frames from a video and appended base64(A) (using base64.b64encode in python). 因此,我从视频中提取了一组10帧并附加了base64(A) (在python中使用base64.b64encode )。

After that, I used the code from here to convert that base64 to Mat. 之后,我使用此处的代码将base64转换为Mat。 Images looked fine. 图像看起来不错。

But they have larger file size than the original images, plus when I encoded these final images to base64(B) (using base64.b64encode in python), the encoded base64 is different from original base64(A) . 但是它们的文件大小比原始图像大,而且当我将这些最终图像编码为base64(B) (使用python中的base64.b64encode )时,编码的base64与原始base64(A) I can't understand why? 我不明白为什么? This is also affecting the output of my application that is using the cv::mat output. 这也会影响使用cv::mat输出的应用程序的输出。

For base64 to Mat I am using code from here (asposted above). 对于base64 to Mat我正在使用此处的代码(如上所述)。

Edited : Following is my python script to convert set of jpeg to base64 (.txt) 编辑 :以下是我的python脚本,用于将jpeg to base64集转换jpeg to base64 (.txt)

 def img2txt(file_directory):    
        imageFiles = glob.glob(file_directory+"/*.jpg")
        imageFiles.sort()
        fileWrite='base64encoding.txt'
        #print fileWrite
        for i in range(0,len(imageFiles)):
            image = open(imageFiles[i],'rb')
            image_read = image.read()
            image_64_encode = base64.b64encode(image_read)
            with open (fileWrite, 'a') as f: 
                f.writelines(image_64_encode+'\n')

base64decode function : from here base64decode函数 :从这里开始

static const std::string base64_chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/";


static inline bool is_base64(unsigned char c) {
    return (isalnum(c) || (c == '+') || (c == '/'));
}

std::string base64_decode(std::string const& encoded_string) {
    int in_len = encoded_string.size();
    int i = 0;
    int j = 0;
    int in_ = 0;
    unsigned char char_array_4[4], char_array_3[3];
    std::string ret;

    while (in_len-- && (encoded_string[in_] != '=') && is_base64(encoded_string[in_])) {
        char_array_4[i++] = encoded_string[in_]; in_++;
        if (i == 4) {
            for (i = 0; i < 4; i++)
                char_array_4[i] = base64_chars.find(char_array_4[i]);

            char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
            char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
            char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];

            for (i = 0; (i < 3); i++)
                ret += char_array_3[i];
            i = 0;
        }
    }

    if (i) {
        /*for (j = i; j < 4; j++)
            char_array_4[j] = 0;*/

        for (j = 0; j < i; j++)
            char_array_4[j] = base64_chars.find(char_array_4[j]);

        char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
        char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
        //char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];

        for (j = 0; (j < i - 1); j++) 
            ret += char_array_3[j];
    }

    return ret;
}

C++ Main function : C ++主要功能

int main()
{  
  ifstream in("TestBase64.txt");
  if(!in) {
    cout << "Cannot open input file.\n";
    return 1;
    }
    int i=0;
    string encoded_string;
    while (getline(in, encoded_string)) 
    {
        string decoded_string = base64_decode(encoded_string);
        vector<uchar> data(decoded_string.begin(), decoded_string.end());
        cv::imwrite("/Frames_from_B_to_Mat/Frames_from_B_to_Mat"+b+".jpg");
        i++; 
    }

    return 0;
}

On the line: 在线上:

vector<uchar> data(decoded_string.begin(), decoded_string.end());

you are presumably holding the JPEG-encoded representation of one image in data . 你大概抱着一个图像中的JPEG编码表示data So you may as well just write this to a binary file, rather than use cv::imwrite() which is for writing a Mat to a file. 因此,您最好将其写入二进制文件,而不要使用cv::imwrite()来将Mat写入文件。

If, for some inexplicable reason, you want to use cv::imwrite() , you need to pass it a Mat . 如果出于某种莫名其妙的原因要使用cv::imwrite() ,则需要将其传递给Mat So you would end up decoding the JPEG representation to a Mat and then encoding to JPEG and writing - which seems silly: 因此,您最终将JPEG表示解码为Mat,然后编码为JPEG并写入-这似乎很愚蠢:

cv::Mat img = cv::imdecode(data, cv::IMREAD_COLOR);
cv::imwrite('result.jpg',img);

TLDR; TLDR;

What I'm saying is that your data is already JPEG-encoded, you read it from a JPEG file. 我的意思是您的数据已经过JPEG编码,您可以从JPEG文件中读取数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM