1

我正在 iOS 上创建一个应用程序,我在其中发送一个 keyPoints 列表(这有效)和一个MatOrbDescriptorExtractor. 图像的发送工作,接收到的base64与base64发送相同。所以我的猜测是它在编码和解码上出错了。

左边的图像是编码前的图像,右边的图像是服务器接收到的解码图像:

在base64之前 base64之后

这是用base64编码Mat(desc)图像的代码,我使用的base64函数来自这个站点

char sendFile[1000];
char temp[100];

std::sprintf(temp, "^^%d^^%d^^", desc.cols, desc.rows);
strcat(sendFile, temp);

const unsigned char* inBuffer = reinterpret_cast<const unsigned char*>(desc.data);

strcat(sendFile, base64_encode(inBuffer, strlen((char*)inBuffer)).c_str());
strcat(sendFile, "\0");

在此之后,文件通过 HTTP Post 保存在服务器上,然后exec()在 PHP 中打开 C++ 脚本,这样就可以了。

在此之后,图像以这种方式解码:

int processData(string input, int* width, int* height){
    int cur = 0, k = 0;
    for(unsigned int i = 0; i < input.length(); i++){
        if(input.substr(i, 2) == "^^"){
            if(cur == 0){
                k = i + 2;          
            }else if(cur == 1){
                *width = getIntFromString(input, k, i);         
                k = i + 2;
            }else{
                *height = getIntFromString(input, k, i);        
                break;
            }
            cur++;
        }
    }
    return 0;
}

int error, w, h;
string line, data;
ifstream file;

file.open(argv[1]);

if(file.is_open()){
    error = processData(line, &w, &h);
    if(error != 0){
        printf("Processing keypoints failed \n");
        return 1;
    } 
    getline(file, line);
    data = base64_decode(line);

    file.close();
}else{
    printf("Couldn't open file.\n");
    return 1;
}

Mat tex_des(Size(w, h), CV_8UC1, (void*)data.c_str());

如何以正确的方式发送 OpenCV 图像而不会丢失数据?

4

2 回答 2

3

您不得在二进制数据上使用任何 str... 函数!

strlen((char*)inBuffer) 在第一个零处停止,给出错误的结果

使用 desc.total() 代替缓冲区长度

于 2013-02-28T10:47:06.277 回答
0

我正在使用稍微不同的方法。我会写它,希望它会有所帮助:

//Consider we have the image saved in UIImage
UIImage * myImage;

//Get the data 
NSData imageData = UIImageJPEGRepresentation(myImage, 1.0);

//I am using this extension to encode to base 64 (https://gist.github.com/Abizern/1643491)
//Article from the save author http://www.cocoawithlove.com/2009/06/base64-encoding-options-on-mac-and.html
NSString *encodedImage = [imageData base64EncodedString];

我发送encodedImage到服务器。

于 2013-02-28T10:38:02.700 回答