本文介绍了为什么我的输出为空白图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我编写的一些用于显示图像幅度谱的代码:

orig_imdata = imread('Original_Image.png');
spec_orig = fft2(double(orig_imdata));
spec_orig2 = abs(spec_orig);
spec_img = fftshift(spec_orig2);
imshow(spec_img);

当我注释掉abs的使用而只是fftshift图像时,我得到了一个图像,尽管有相位和幅值.如果我在使用fftshift之后立即应用了abs函数,则会从imshow中得到空白结果.我需要一张图像,该图像具有光谱分析的幅度.

有人知道这是怎么回事吗?

解决方案

图像根本不是空白". imshow被设计为使得任何小于0的double精度值都显示为黑色,而任何大于1的值都显示为白色.在计算图像的幅度分量时,您将在代码中生成double精度图像.

因此,我非常怀疑,因为您的大多数分量(如果不是 all )都大于1,这使您可以看到全白的图像,因此是空白"的图像.现在,我们已经找到了问题所在,但是您还没有走出困境.简单地缩放组件以使其适合[0,1]的范围也无济于事.如果执行此操作,则幅度谱的DC分量可能会很大,以致于使图像中的其余幅度分量不堪重负.因此,您只会在中间看到一个白点,其余图像将变成黑色.

通常的做法是对幅度谱应用log操作以进行显示,然后重新缩放值以使其适合[0,1]范围:

%// Your code
orig_imdata = imread('Original_Image.png');
spec_orig = fft2(double(orig_imdata));
spec_orig2 = abs(spec_orig);
spec_img = fftshift(spec_orig2);

%// New code
spec_img_log = log(1 + spec_img);
imshow(spec_img_log,[]);

log操作中,值的巨大动态范围被压缩到较小的范围,尤其是当值变大时,因此自然地缩放到此压缩范围的[0,1]将为您提供更好的视觉效果. /p>

向每个组件加1然后采用log的原因是为了避免log(0)操作.如果任何幅度分量为零,则将得出log(1)的值,该值将变为0.执行此操作后,可以使用imshow(...,[])重新缩放显示,以使最小幅度分量变为0,而最大幅度分量变为0.到 log 频谱中的1个.请注意,我没有修改原始频谱,因此您可以对此进行处理.同样,由imshow完成的重新缩放完全在调用内部完成.无论如何,它不会重新缩放数据-它仅用于显示,仅此而已.


让它去看看它是怎么回事.

Here's some code I wrote to display the magnitude spectrum of an image:

orig_imdata = imread('Original_Image.png');
spec_orig = fft2(double(orig_imdata));
spec_orig2 = abs(spec_orig);
spec_img = fftshift(spec_orig2);
imshow(spec_img);

When I comment out the use of abs and just fftshift the image, I get an image, albeit with the phase and the magnitude. If I apply the abs function right after using fftshift, I get a blank result from imshow. I need an image of the magnitude of the spectral analysis of my image.

Does anyone know what's going wrong here?

解决方案

The image isn't "blank" at all. imshow is designed so that any double precision values that are less than 0 are displayed as black and any values larger than 1 are displayed as white. When computing the magnitude components of the image, you are producing double precision images in your code.

Therefore, I highly suspect that because most of your components (if not all) are greater than 1, this gives you a visualization of an entirely white and therefore "blank" image. Now that we've found the problem, however you're not out of the woods yet. Simply scaling your components so that they fit in the range of [0,1] won't help either. If you do this, the magnitude spectrum's DC component will probably be so large that it's overwhelming the rest of the magnitude components in your image. Therefore, you'll only see one white dot in the middle and the rest of the image will be black.

A common practice is to apply a log operation to the magnitude spectrum for display, then rescaling the values so that they fit in the range of [0,1]:

%// Your code
orig_imdata = imread('Original_Image.png');
spec_orig = fft2(double(orig_imdata));
spec_orig2 = abs(spec_orig);
spec_img = fftshift(spec_orig2);

%// New code
spec_img_log = log(1 + spec_img);
imshow(spec_img_log,[]);

In the log operation, the huge dynamic range of values gets compressed to a smaller range, especially when the values get larger, and so naturally scaling to [0,1] of this compressed range will give you better visual results.

The reason why you add 1 to every component then take the log is to avoid the log(0) operation. Should any magnitude components be zero, this will evaluate to log(1), which will become 0. Once you do this, you can use imshow(...,[]) to rescale the display so that the smallest magnitude component goes to 0 while the largest magnitude component goes to 1 of the log spectrum. Take note that I did not modify the original spectrum so you can do your processing on that. Also, the rescaling done by imshow is completely done internally to the call. It does not rescale the data in anyway - it does this only for display and that's that.


Give that a go and see how it goes.

这篇关于为什么我的输出为空白图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 00:36