简体   繁体   English

在Matlab中改变图像?

[英]imread altering image in Matlab?

I am having an issue reading a single image of a stacked tiff in using imread. 我在使用imread读取堆叠tiff的单个图像时遇到问题。 The tiff is 128-by-126. tiff是128×126。 It reads in just fine with ImageJ, but I try reading it into Matlab for some processing and it creates an odd streak in the center of the image. 它使用ImageJ读取得很好,但我尝试将其读入Matlab进行一些处理,并在图像中心创建一条奇数条纹。 With the origin of the image in the top left, rows 63 and 64 are repeated as rows 65 and 66, and the last two rows of the image, 125 and 126 are cut off. 利用左上方的图像的原点,行63和64作为行65和66重复,并且图像的最后两行125和126被切掉。 I can tell this is happening by visual comparison of the image displayed in matlab to the image displayed in ImageJ. 我可以通过将matlab中显示的图像与ImageJ中显示的图像进行视觉比较来判断是否发生了这种情况。

If I take the same tiff stack, and save the first frame in ImageJ, I don't have this issue. 如果我采用相同的tiff堆栈,并保存ImageJ中的第一帧,我没有这个问题。 Even when displaying the outputted matlab image using ImageJ, I see the same issue. 即使使用ImageJ显示输出的matlab图像,我也会看到同样的问题。 However, I would like to automate the process to save images from several tiff stacks as single tiff files, which I can't do in ImageJ, so I turned to Matlab and ran into this issue. 但是,我想自动化将来自几个tiff堆栈的图像保存为单个tiff文件的过程,这是我在ImageJ中无法做到的,所以我转向Matlab并遇到了这个问题。 I have included my code below. 我在下面提供了我的代码。 I tried reading the tiff in two different ways and got the same error. 我尝试用两种不同的方式阅读tiff并得到了同样的错误。 It seems to be related to the tiff stack and how matlab reads in the tiffs. 它似乎与tiff堆栈以及matlab在tiff中的读取方式有关。 I am using Matlab R2012b. 我正在使用Matlab R2012b。

I have included links below to the static ImageJ image I am seeing and the static matlab image I am seeing. 我已将下面的链接包含在我看到的静态ImageJ图像和我看到的静态matlab图像中。 I have also included a link for loading the stacked tiff file that is generating these issues for me. 我还提供了一个链接,用于加载堆叠的tiff文件,这个文件正在为我生成这些问题。

Note: When I have ImageJ output each frame as an individual tiff and I open the first frame from that output in matlab using the same code below, the image is correctly displayed. 注意:当我将ImageJ作为单独的tiff输出时,我使用下面相同的代码在matlab中从该输出打开第一帧,图像被正确显示。 The error only occurs when reading in the first frame from the image stack in Matlab. 只有在Matlab中从图像堆栈读取第一帧时才会出现错误。

StackOverflow doesn't support embedding TIFF files, but you can view and download them from these links: StackOverflow不支持嵌入TIFF文件,但您可以从这些链接查看和下载它们:

Code Used to Generate the Image 用于生成图像的代码

fname='C:\FileLocation\pcd144_012.tif';
im1=imread(fname,1)
imagesc(im1);
axis image; colormap gray;

I tried reading in the image as a tiff object to see if it solved the problem and this didn't work either. 我尝试在图像中读取tiff对象,看看它是否解决了问题,但这也无效。 The image has two strips, and the last two lines of the first strip are the same as the first two lines of the last strip, which is why the middle lines seem to be repeated. 图像有两个条带,第一个条带的最后两行与最后一个条带的前两行相同,这就是为什么中间线条似乎重复的原因。 It seems matlab is indexing reading my image in wrong, likely because it is not a square image. 似乎matlab正在索引读错我的图像,可能是因为它不是方形图像。 Am I just doing something wrong, or does matlab have a bug with respect to reading in non-square tiffs? 我只是做错了什么,或者matlab有关于读取非方形tiff的错误? Any ideas or suggestions for improvement? 有任何改进的想法或建议吗?

First of, I kinda agree with horchler, that is, there is something wrong in your header. 首先,我有点同意horchler,也就是说,标题中有错误。

We can easily observe that the StripByteCounts (15872) does not match width*height (128*126) . 我们可以很容易地发现StripByteCounts (15872)width*height (128*126)不匹配。 This could be the reason you see the repetition in row 63 - 64 and 65 - 66 . 这可能是你看到的重复行的原因63 - 6465 - 66

Since the RowPerStrip = 64 and StripOffsets = [8,15880] may indicate that you have a 128*124 graph, Matlab perhaps uses last two rows in the first 64 rows to pad the missing rows at the beginning of the rest of the rows. 由于RowPerStrip = 64StripOffsets = [8,15880]可能表示您有一个128*124图形,因此Matlab可能会使用前64行中的最后两行来填充其余行开头的缺失行。 So the total row can be filled up to 126. Well, this is just my guess for how Matlab handles the disagreement between dimension and ByteCounts. 所以总行可以填充到126.嗯,这只是我猜测Matlab如何处理维度和ByteCounts之间的分歧。

After all, to your question, imread indeed alters image in Matlab when reading TIFF without issuing any warning. 毕竟,对于你的问题, imread在读取TIFF时确实改变了Matlab中的图像而没有发出任何警告。 Bad job in imread reading TIFF, Matlab. 在恶劣的工作imread读TIFF,MATLAB。

After observing your TIFF frames in one of your links, the TIFF seems to actually have image data with dimension 128*126 . 在您的某个链接中观察到TIFF帧后,TIFF实际上看起来实际上具有尺寸为128*126图像数据。 So if you trust the dimension indicating in the header, you would probably use fread to read the frames in your TIFF instead of using shaky imfread . 因此,如果您信任标题中指示的维度,您可能会使用fread来读取TIFF中的帧而不是使用抖动的imfread

fname='pcd144_012.tif';
tiffInfo = imfinfo(fname);
framIndex = 1;
tiffWidth = tiffInfo(framIndex).Width; % image width
tiffHeight = tiffInfo(framIndex).Height; % image height
tiffStartOffset = tiffInfo(framIndex).StripOffsets(1); % Image data offset start point
tiffEndOffset = tiffInfo(framIndex).StripOffsets(2); % Image data offset end point
fid = fopen(fname);
fseek(fid,tiffStartOffset,'bof');
im1 = fread(fid,tiffWidth*tiffHeight,'*uint16'); % We knew this from BitsPerSample 
fclose(fid);
im1 = reshape(im1,tiffWidth,tiffHeight); % Reshape the image data array
figure
imagesc(im1);
colormap gray;
axis xy;
axis image; 

Now, while this may solve the weird Matlab imread behavior, however, the above result still does not match the picture you showed in your second link. 现在,虽然这可能解决了奇怪的Matlab imread行为,但是,上面的结果仍然与您在第二个链接中显示的图片不匹配。 According to the picture in the second link, it has 300 frames but the one you attached in your first link only has 30 frames. 根据第二个链接中的图片,它有300帧但你在第一个链接中附加的帧只有30帧。 Maybe we are all looking at the wrong picture? 也许我们都在看错了?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM