首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将图像加载到GSL矩阵

将图像加载到GSL矩阵
EN

Stack Overflow用户
提问于 2014-12-17 15:39:05
回答 1查看 148关注 0票数 0

有人知道有什么函数可以将灰度图像加载到GSL矩阵中吗?

类似于:

代码语言:javascript
复制
gsl_matrix *M;
load_image("./image.jpg", M); // any image extension would also be fine
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-12-17 16:15:39

这样的东西可以在我的电脑上工作:允许你使用libjpeg加载一个灰度JPG图像。

代码语言:javascript
复制
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <jpeglib.h>
#include <gsl/gsl_matrix.h>

gsl_matrix *load_jpg_image(const char *pFileName)
{
    FILE *pFile;
    long jpegSize;
    unsigned char *pJpegBytes, *pPixels;
    struct jpeg_decompress_struct cinfo;
    struct jpeg_error_mgr jerr;
    int status, w, h, numComponents, stride, x, y;
    gsl_matrix *pMatrix;

    pFile = fopen(pFileName, "rb");
    if (!pFile)
    {
        fprintf(stderr, "Can't open file\n");
        return NULL;
    }

    // Get the size of the file
    fseek(pFile, 0, SEEK_END);
    jpegSize = ftell(pFile);
    rewind(pFile);

    if (jpegSize == 0)
    {
        fclose(pFile);
        fprintf(stderr, "Empty file\n");
        return NULL;
    }

    // Read it into memory and close file
    pJpegBytes = (unsigned char *)malloc(jpegSize);
    fread(pJpegBytes, 1, jpegSize, pFile);
    fclose(pFile);

    // Jpeg decompression starts here
    memset(&cinfo, 0, sizeof(struct jpeg_decompress_struct));
    cinfo.err = jpeg_std_error(&jerr);
    jpeg_create_decompress(&cinfo);

    jpeg_mem_src(&cinfo, pJpegBytes, jpegSize);
    status = jpeg_read_header(&cinfo, TRUE);

    if (status != 1)
    {
        free(pJpegBytes);
        fprintf(stderr, "Invalid JPEG header\n");
        return NULL;
    }

    jpeg_start_decompress(&cinfo);

    w = cinfo.output_width;
    h = cinfo.output_height;
    numComponents = cinfo.output_components;

    if (numComponents != 1)
    {
        free(pJpegBytes);
        fprintf(stderr, "Can only handle 1 color component\n");
        return NULL;
    }

    pPixels = (unsigned char *)malloc(w*h);
    stride = w*numComponents;

    // perhaps this can de done much faster by processing
    // multiple lines at once
    while (cinfo.output_scanline < cinfo.output_height) 
    {
        unsigned char *buffer_array[1];
        buffer_array[0] = pPixels + cinfo.output_scanline * stride;

        jpeg_read_scanlines(&cinfo, buffer_array, 1);
    }

    jpeg_finish_decompress(&cinfo);
    jpeg_destroy_decompress(&cinfo);

    free(pJpegBytes);

    // Now, create and fill in the matrix
    pMatrix = gsl_matrix_alloc(h, w);    
    for (y = 0 ; y < h ; y++)
        for (x = 0 ; x < w ; x++)
            gsl_matrix_set(pMatrix, y, x, pPixels[x+y*stride]);

    return pMatrix;
}

int main(void)
{
    gsl_matrix *pMatrix;
    int rows, cols;
    int i, j;

    pMatrix = load_jpg_image("test.jpg");
    if (pMatrix == NULL)
    {
        fprintf(stderr, "Can't load matrix\n");
        return -1;
    }

    //
    // Use the matrix
    // 

    gsl_matrix_free(pMatrix);
    return 0;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27529272

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档