Matlab与python图像数据传递(或matlab与numpy array数据传递)

matlab中读入的图像数据是uint8数据类型的矩阵,而python中使用的图像数据格式为ndarray。中间涉及数据转换。

处理过程:matlab读入图像,在python语言中使用opencv做灰度化处理,再将灰度图像传递回matlab。

matlab中代码:(transferdata.m)

clear classes
close all
% 读入图像
image=imread('Default.jpg');
% 显示图像
figure
imshow(image);
% 1,3通道交换
image=changechannel(image);
% 转换成python图像数据格式
imagenparry=mat2nparray(image);
% 导入python包,test.py文件
obj = py.importlib.import_module('test'); 
% 重载obj
py.importlib.reload(obj);
% 灰度化处理
graynparry=obj.mytest(imagenparry);
% 转化成mat数据格式
gray=nparray2mat(graynparry);
% 更改数据格式
gray=uint8(gray);
% 显示图像
figure
imshow(gray);

python中代码:(test.py)

#encoding=utf-8

import cv2

def mytest(image):
    cv2.imshow('src', image)
    cv2.waitKey()
    imggray=cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    return imggray

if __name__ == "__main__":
    image = cv2.imread('Default.jpg')
    results=mytest(image)
    cv2.imshow('gray', results)
    cv2.waitKey()

几点重要的说明:

1)changechannel.m函数代码为:

function output = changechannel( input )
    %matlab image的图像通道顺序为R,G,B
    %opencv image的图像通道顺序为B,G,R
    %此函数进行1,3通道的交换
    output=input;
    output(:,:,1)=input(:,:,3);
    output(:,:,3)=input(:,:,1);
end

matlab读入的图像RGB通道的顺序,与opencv的读入图像的RGB通道不一致,所以要将1,3通道交换之后再传递图像数据。

2)使用py.importlib.reload(obj)重载test.py。

matlab中默认将test.py加入到工作空间,如果不重载的话,就算更改了test.py的代码,在matlab工作空间也不会生效。

3)mat2nparray.m将mat图像数据转化为numpy array图像数据,以便在Python中使用

function result = mat2nparray( matarray )
    %mat2nparray Convert a Matlab array into an nparray
    %   Convert an n-dimensional Matlab array into an equivalent nparray  
    data_size=size(matarray);
    if length(data_size)==1
        % 1-D vectors are trivial
        result=py.numpy.array(matarray);
    elseif length(data_size)==2
        % A transpose operation is required either in Matlab, or in Python due
        % to the difference between row major and column major ordering
        transpose=matarray';
        % Pass the array to Python as a vector, and then reshape to the correct
        % size
        result=py.numpy.reshape(transpose(:)', int32(data_size));
    else
        % For an n-dimensional array, transpose the first two dimensions to
        % sort the storage ordering issue
        transpose=permute(matarray,[length(data_size):-1:1]);
        % Pass it to python, and then reshape to the python style of matrix
        % sizing
        result=py.numpy.reshape(transpose(:)', int32(fliplr(size(transpose))));
    end
end

4)nparray2mat.m将python的图像数据转化为mat图像数据,以便在matlab中使用

function result = nparray2mat( nparray )
	%nparray2mat Convert an nparray from numpy to a Matlab array
	%   Convert an n-dimensional nparray into an equivalent Matlab array
	data_size = cellfun(@int64,cell(nparray.shape));
	if length(data_size)==1
        % This is a simple operation
        result=double(py.array.array('d', py.numpy.nditer(nparray)));
	elseif length(data_size)==2
        % order='F' is used to get data in column-major order (as in Fortran
        % 'F' and Matlab)
        result=reshape(double(py.array.array('d', ...
            py.numpy.nditer(nparray, pyargs('order', 'F')))), ...
            data_size);
    else
        % For multidimensional arrays more manipulation is required
        % First recover in python order (C contiguous order)
        result=double(py.array.array('d', ...
            py.numpy.nditer(nparray, pyargs('order', 'C'))));
        % Switch the order of the dimensions (as Python views this in the
        % opposite order to Matlab) and reshape to the corresponding C-like
        % array
        result=reshape(result,fliplr(data_size));
        % Now transpose rows and columns of the 2D sub-arrays to arrive at the
        % correct Matlab structuring
        result=permute(result,[length(data_size):-1:1]);
	end
end

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值