我想使用“ libtiff”库从“ .tiff”图像中读取u8位像素强度值。我遇到了此代码,并对其进行了修改,以根据需要读取8位值,并且仅返回一列的正确值。
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <inttypes.h>
#include "tiffio.h"
#define imsize 286628
int count;
int count2;
uint8* im;
uint32 imagelength;
uint32 width;
int main(){
im = (uint8*)malloc(imsize*sizeof(uint8));
TIFF* tif = TIFFOpen("image1.tif", "r");
if (tif) {
tsize_t scanline;
tdata_t buf;
uint32 row;
uint32 col;
uint16 nsamples;
TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &nsamples);
TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &imagelength);
TIFFGetField(tif,TIFFTAG_IMAGEWIDTH,&width);
scanline = TIFFScanlineSize(tif);
buf = _TIFFmalloc(scanline);
uint8* data;
for (row = 0; row < imagelength; row++)
{
TIFFReadScanline(tif, buf, row,1);
count2++;
for (col = 0; col < scanline; col++)
data = (uint8*)buf;
//printf("%d\n",col); remains the same not incrementing
printf("%d ", *data);//printing for testing need only to copy to an array to access by index
im[count] = *data;
count++;
printf("\n");
}
printf("im[1]= %d\n im[2] = %d \n im[3] = %d \n im[286628] = %d\n",im[0],im[1],im[2],im[286627]);
_TIFFfree(buf);
TIFFClose(tif);
free(im);
}
printf("num of cols= %d\n",count);
printf("num of rows = %d\n",count2);//both counts print col size
printf("width = %d\n",width); //prints row size
return 0;
}
在嵌套的forloop中,如果添加方括号,则循环将迭代正确的像素数 (对于本示例来说,#of-pixels = 286628,547x524图像),但是值不正确。 如果我将括号移开,我会得到正确的值,但第一列(只有547个值)。
需要进行哪些更改才能正确遍历所有像素?
注释: 我正在尝试获取矩阵值为“ imread()”的矩阵
答案 0 :(得分:0)
在col
循环中,每一列都使用data = (uint8*)buf;
做完全相同的事情,而{
似乎是 first 列的数据。循环也缺少}
大括号data = (uint8*)buf;
。
移动行
for (row = 0; row < imagelength; row++)
{
TIFFReadScanline(tif, buf, row,1);
count2++;
data = (uint8*)buf; // move up
for (col = 0; col < scanline; col++)
{ // add braces
printf("%d ", *data);
im[count] = *data;
count++;
data++; // increment buffer pointer
}
printf("\n");
}
在列循环外并在循环内递增。
{{1}}