如何在OpenCV中处理JPEG二进制数据? [英] How to process a JPEG binary data in OpenCV?

查看:367
本文介绍了如何在OpenCV中处理JPEG二进制数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在OpenCV中处理JPEG二进制数据.当我这样做时,会出现分段错误(核心已转储).

I am trying to process a JPEG Binary data in OpenCV. When I do that I get Segmentation fault (core dumped).

我通过fread命令读取JPEG文件并存储在缓冲区中.

I read JPEG file through fread command and stored in a buffer.

读取后,我将缓冲区数据复制到了Mat变量中, 当我尝试使用cvtColor OpenCV函数对复制的数据进行灰度转换时.我遇到了细分错误.

After reading, I copied the buffer data to a Mat variable, When I tried to do grayscale conversion on copied data using cvtColor OpenCV function. I get Segmentation Fault.

int main( int argc, char** argv )
{

    Mat threshold_output;
    Mat gray_image;

    unsigned char *pre_image;
    FILE *read_image;
    FILE *write_image;
    int filesize;
    size_t data, write;

    read_image = fopen(argv[1] , "rb"); //Read Jpeg as Binary
    write_image = fopen("output11.jpg", "wb"); //Write JPEG

    if(read_image == NULL)
    {
        printf("Image Not Found\r\n");
    }
    fseek(read_image, 0, SEEK_END);
    int fileLen = ftell(read_image);
    fseek(read_image, 0, SEEK_SET);

    pre_image = (unsigned char *)malloc(fileLen);
    data = fread(pre_image, 1, fileLen, read_image);
    write = fwrite(pre_image, 1, fileLen, write_image);

    // Printed and verify the values
    printf("File Size %d\r\n", fileLen);
    printf("Read bytes %zu\r\n", data);
    printf("Write bytes %zu\r\n", data);

    fclose(read_image);
    fclose(write_image);

    /* Copy the Jpeg Binary buffer to a MAt Variable*/  
    cv::Mat image(Size(640, 480), CV_8UC3, pre_image); //Seg Fault comes here
    /* Convert Grayscale */
    cvtColor( image, gray_image, CV_BGR2GRAY);
    /* Threshold conversion */
    threshold( gray_image, threshold_output, 80, 255, THRESH_BINARY );

    namedWindow( "Thresholded", CV_WINDOW_AUTOSIZE );
    imshow( "Thresholded", image );

    waitKey(0);
    return 0;
}

我已附上代码以供参考.我已经验证freadfwrite都可以正常工作. 但是当我只执行cvtColor时,我得到了错误.

I have attached the code for reference. I have verified that both fread and fwrite works properly. But when I do the cvtColor only I got error.

推荐答案

@Micka已经指出,您应该使用

As @Micka already pointed out, you should use cv::imdecode

您可以将其与FILE*一起使用.如果您使用的是C ++,则可能要使用fstreams.您还可以直接依靠OpenCV功能来读取文件.

You can use it with your FILE*. You probably may want to use fstreams if you're using C++. You can also rely directly on OpenCV capabilities to read files.

下面的代码将向您显示这些用于读取文件的选项.编写代码类似(如果需要,可以添加).

The code below will show you these options for reading files. Code for writing is similar (I can add it if you need it).

请记住,如果要编写二进制流,则应使用 imencode

Remember that if you want to write the binary stream, you should use imencode

#include <opencv2\opencv.hpp>
#include <fstream>
#include <stdio.h>

using namespace std;
using namespace cv;

int main()
{

    ////////////////////////////////
    // Method 1: using FILE*
    ////////////////////////////////

    FILE* read_image = fopen("path_to_image", "rb");
    if (read_image == NULL)
    {
        printf("Image Not Found\n");
    }

    fseek(read_image, 0, SEEK_END);
    int fileLen = ftell(read_image);
    fseek(read_image, 0, SEEK_SET);

    unsigned char* pre_image = (unsigned char *)malloc(fileLen);
    size_t data = fread(pre_image, 1, fileLen, read_image);

    // Printed and verify the values
    printf("File Size %d\n", fileLen);
    printf("Read bytes %d\n", data);

    fclose(read_image);

    vector<unsigned char> buffer(pre_image, pre_image + data);
    Mat img = imdecode(buffer, IMREAD_ANYCOLOR);

    ////////////////////////////////
    //// Method 2: using fstreams
    ////////////////////////////////

    //ifstream ifs("path_to_image", iostream::binary);
    //filebuf* pbuf = ifs.rdbuf();
    //size_t size = pbuf->pubseekoff(0, ifs.end, ifs.in);
    //pbuf->pubseekpos(0, ifs.in);
    //vector<char> buffer(size);
    //pbuf->sgetn(buffer.data(), size);
    //ifs.close();
    //Mat img = imdecode(buffer, IMREAD_ANYCOLOR);


    ////////////////////////////////
    //// Method 3: using imread
    ////////////////////////////////

    //Mat img = imread("path_to_image", IMREAD_ANYCOLOR);


    // Work with img as you want

    imshow("img", img);
    waitKey();


    return 0;
}

这篇关于如何在OpenCV中处理JPEG二进制数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆