gpt4 book ai didi

c - 将图像加载到 GSL 矩阵

转载 作者:行者123 更新时间:2023-11-30 15:26:03 28 4
gpt4 key购买 nike

有人知道一些将灰度图像加载到GSL矩阵中的函数吗?

类似于:

gsl_matrix *M;
load_image("./image.jpg", M); // any image extension would also be fine

最佳答案

类似的东西在我的电脑上有效:允许您使用 libjpeg 加载灰度 JPG 图像。

#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;
}

关于c - 将图像加载到 GSL 矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27529272/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com