如何在matlab中从matplotlib(python)和imshow()使用imshow()函数生成相同的图像? [英] How to generate the same image with the function of imshow() from matplotlib(python) and imshow() in matlab?

查看:581
本文介绍了如何在matlab中从matplotlib(python)和imshow()使用imshow()函数生成相同的图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于同一矩阵,由函数imshow()从matplotlib和matlab生成的图像是不同的.如何在matplotlib中更改imshow()的某些参数可以在matlab中获得相同的结果

For the same matrix, the image generated by the function imshow() from matplotlib and matlab is different. how to change some parameters of imshow() in matplotlib can get same result in matlab

%matlab
img = 255*rand(101);
img(:,1:50)=3;
img(:,52:101)=1;
img(:,51)=2;
trans_img=imtranslate(img,[3*cos(pi/3),3*sin(pi/3)]);
imshow(trans_img)

这是由matlab生成的图像

#python
import numpy as np
import matplotlib.pyplot as plt
from mlab.releases import latest_release as mtl #call matlab function

img = 255 * np.random.uniform(0, 1, (101, 101))
img[:, 51:101] = 1 
img[:, 0:50] = 3
img[:, 50] = 2
trans_img = mtl.imtranslate(img, [[3*math.cos(math.pi/3),3*math.sin(math.pi/3)]] 
i = plt.imshow(trans_img, cmap=plt.cm.gray)
plt.show(i)

这是由matplotlib生成的图像

trans_img矩阵在两种情况下都相同,但是matlab和python中的图像不同

The trans_img matrix is the same in both cases, but the images in matlab and python are different

推荐答案

不幸的是,我没有具有imtranslate功能的最新版本的Matlab,但值得庆幸的是,Octave中的image程序包这样,我确定这是等效的.同样,我将使用oct2py模块而不是mlab,以便python从python的八度访问imtranslate函数.

Unfortunately I don't have an up-to-date enough version of Matlab that has the imtranslate function, but thankfully the image package in Octave does, which I'm sure is equivalent. Equally, I will be using the oct2py module instead of mlab as a result, for python to access the imtranslate function from octave within python.

八度代码:

img = 255*rand(101);
img(:,1:50)=3;
img(:,52:101)=1;
img(:,51)=2;
trans_img = imtranslate(img, 3*cos(pi/3),3*sin(pi/3));
imshow(trans_img, [min(trans_img(:)), max(trans_img(:))])

Python代码:

import numpy as np
import matplotlib.pyplot as plt
import math
from oct2py import octave
octave.pkg('load','image');  # load image pkg for access to 'imtranslate'
img = 255 * np.random.uniform(0, 1, (101, 101))
img[:, 51:101] = 1 
img[:, 0:50] = 3
img[:, 50] = 2
trans_img = octave.imtranslate(img, 3*math.cos(math.pi/3), 3*math.sin(math.pi/3))
i = plt.imshow(trans_img, cmap=plt.cm.gray)
plt.show(i)

两种情况下产生的图像(相同):

Resulting image (identical) in both cases:

对于为什么会出现差异,我的唯一评论是,我 did imshow中指定了minmax值,以确保适当的强度缩放.同样,您本可以使用imagesc(trans_img)代替(我实际上更喜欢这样做).我没有在python中为plt.imshow明确指定此类限制...可能默认情况下执行缩放.

My only comment on why you may have been seeing the discrepancy, is that I did specify the min and max values in imshow, to ensure appropriate intensity scaling. Equally you could have just used imagesc(trans_img) instead (I actually prefer this). I didn't specify such limits explicitly in python for plt.imshow ... perhaps it performs scaling by default.

此外,您的代码有一个小错误;至少在imtranslate的倍频程中,该函数采用3个参数,而不是2个. (另外,您的原始代码带有不平衡的括号).

Also, your code has a small bug; in the octave version of imtranslate at least, the function takes 3 arguments, not two. (Also, your original code has an unbalanced bracket).

这篇关于如何在matlab中从matplotlib(python)和imshow()使用imshow()函数生成相同的图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆