简体   繁体   English

在Matlab中读取Parula图像而不会丢失分辨率

[英]To imread Parula image in Matlab without losing resolution

There is no bijection between RGB and Parula, discussed here . 有RGB和Parula之间没有一一对应,讨论在这里 I am thinking how to do well the image processing of files in Parula. 我在想如何做好Parula中文件的图像处理。 This challenge has been developed from this thread about removing black color from ECG images by extending the case to a generalized problem with Parula colors. 通过将案例扩展到Parula颜色的广义问题,从这个线索开发了关于从ECG图像中去除黑色的挑战。

Data: 数据:

在此输入图像描述

which is generated by 这是由...生成的

[X,Y,Z] = peaks(25);
imgParula = surf(X,Y,Z);
view(2);
axis off;

It is not the point of this thread to use this code in your solution to read the second image. 在您的解决方案中使用此代码来读取第二个图像不是此线程的重点。

Code: 码:

[imgParula, map, alpha] = imread('http://i.stack.imgur.com/tVMO2.png'); 

where map is [] and alpha is a completely white image. 其中map[]alpha是完全白色的图像。 Doing imshow(imgParula) gives imshow(imgParula)给出

在此输入图像描述

where you see a lot of interference and lost of resolution because Matlab reads images as RGB, although the actual colormap is Parula . 你看到很多干扰和丢失分辨率,因为Matlab将图像读成RGB,尽管实际的色图是Parula Resizing this picture does not improve resolution. 调整此图片大小不会提高分辨率。

How can you read image into corresponding colormap in Matlab? 如何在Matlab中将图像读入相应的色彩映射? I did not find any parameter to specify the colormap in reading. 我没有找到任何参数来指定阅读中的色彩映射。

The Problem 问题

There is a one-to-one mapping from indexed colors in the parula colormap to RGB triplets. 有一个一到一个映射从索引颜色parula颜色映射到RGB三元组。 However, no such one-to-one mapping exists to reverse this process to convert a parula indexed color back to RGB (indeed there are an infinite number ways to do so). 但是,没有这样的一对一映射来反转此过程以将parula索引颜色转换回RGB(实际上有无数种方法可以这样做)。 Thus, there is no one-to-one correspondence or bijection between the two spaces. 因此,两个空间之间没有一对一的对应关系或双向关系。 The plot below, which shows the R, G, and B values for each parula index, makes this clearer. 下面的图表显示了每个parula索引的R,G和B值,这使得这一点更加清晰。

Parula到RGB图

This is the case for most indexed colors. 大多数索引颜色都是这种情况。 Any solution to this problem will be non-unique. 这个问题的任何解决方案都是非唯一的。


A Built-in Solution 内置解决方案

I after playing around with this a bit, I realized that there's already a built-in function that may be sufficient: rgb2ind , which converts RGB image data to indexed image data. 我稍微玩了一下后,我意识到已经有一个内置函数可能就足够了: rgb2ind ,它将RGB图像数据转换为索引图像数据。 This function uses dither (which in turn calls the mex function ditherc ) to perform the inverse colormap transformation. 此函数使用dither (进而调用mex函数ditherc )来执行逆色图转换。

Here's a demonstration that uses JPEG compression to add noise and distort the colors in the original parula index data: 这是一个使用JPEG压缩来添加噪声并扭曲原始parula索引数据中颜色的parula

img0 = peaks(32);                     % Generate sample data
img0 = img0-min(img0(:));
img0 = floor(255*img0./max(img0(:))); % Convert to 0-255
fname = [tempname '.jpg'];            % Save file in temp directory
map = parula(256);                    % Parula colormap
imwrite(img0,map,fname,'Quality',50); % Write data to compressed JPEG
img1 = imread(fname);                 % Read RGB JPEG file data

img2 = rgb2ind(img1,map,'nodither');  % Convert RGB data to parula colormap

figure;
image(img0);                          % Original indexed data
colormap(map);
axis image;

figure;
image(img1);                          % RGB JPEG file data
axis image;

figure;
image(img2);                          % rgb2ind indexed image data
colormap(map);
axis image;

This should produce images similar to the first three below. 这应该产生类似于前三个的图像。

示例原始数据和转换后的图像


Alternative Solution: Color Difference 替代解决方案:色差

Another way to accomplish this task is by comparing the difference between the colors in the RGB image with the RGB values that correspond to each colormap index. 完成此任务的另一种方法是通过比较RGB图像中的颜色与对应于每个颜色图索引的RGB值之间的差异。 The standard way to do this is by calculating Δ E in the CIE L*a*b* color space. 执行此操作的标准方法是通过计算 CIE L * a * b *颜色空间中的ΔE I've implemented a form of this in a general function called rgb2map that can be downloaded from my GitHub . 我在一个名为rgb2map的通用函数中实现了这种形式,可以从我的GitHub下载 This code relies on makecform and applycform in the Image Processing Toolbox to convert from RGB to the 1976 CIE L*a*b* color space. 此代码依赖于图像处理工具箱中的makecformapplycform将RGB转换为1976 CIE L * a * b *颜色空间。

The following code will produce an image like the one on the right above: 以下代码将生成如右上图所示的图像:

img3 = rgb2map(img1,map);

figure;
image(img3);                          % rgb2map indexed image data
colormap(map);
axis image;

For each RGB pixel in an input image, rgb2map calculates the color difference between it and every RGB triplet in the input colormap using the CIE 1976 standard. 对于输入图像中的每个RGB像素, rgb2map使用CIE 1976标准计算输入色彩映射中的每个RGB三元组之间的色差。 The min function is used to find the index of the minimum Δ E (if more than one minimum value exists, the index of the first is returned). min函数用于查找最小ΔE的索引(如果存在多于一个最小值,则返回第一个的索引)。 More sophisticated means can be used to select the "best" color in the case of multiple Δ E minima, but they will be more costly. 在多个ΔE最小值的情况下,可以使用更复杂的手段来选择“最佳”颜色,但是它们将更昂贵。


Conclusions 结论

As a final example, I used an image of the namesake Parula bird to compare the two methods in the figure below. 作为最后一个例子,我使用同名Parula鸟的图像来比较下图中的两种方法。 The two results are quite different for this image. 这个图像的两个结果完全不同。 If you manually adjust rgb2map to use the more complex CIE 1994 color difference standard, you'll get yet another rendering. 如果您手动调整rgb2map以使用更复杂的CIE 1994色差标准,您将获得另一个渲染。 However, for images that more closely match the original parula colormap (as above) both should return more similar results. 但是,对于更接近原始parula (如上所述)的图像,两者都应返回更相似的结果。 Importantly, rgb2ind benefits from calling mex functions and is almost 100 times faster than rgb2map despite several optimizations in my code (if the CIE 1994 standard is used, it's about 700 times faster). 重要的是, rgb2ind受益于调用mex函数,并且几乎比rgb2map快100倍,尽管我的代码中进行了多次优化(如果使用CIE 1994标准,它的速度大约快700倍)。

使用两种方法转换的鸟的RGB图像

Lastly, those who want to learn more about colormaps in Matlab, should read this four-part MathWorks blog post by Steve Eddins on the new parula colormap. 最后,那些想要在Matlab中学习更多关于parula映射的人,应该阅读Steve parula关于新parula 四部分MathWorks博客文章

Update 6-20-2015: rgb2map code described above updated to use different color space transforms, which improves it's speed by almost a factor of two. 2015年6月 rgb2map 更新: rgb2map描述的rgb2map代码已更新为使用不同的颜色空间变换,这使其速度提高了近两倍。

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

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