将具有图像数据的numpy数组转换为CvMat [英] Converting numpy array having image data to CvMat

查看:1157
本文介绍了将具有图像数据的numpy数组转换为CvMat的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在numpy数组中有一个图像,我使用savefig保存了该图像,然后使用opencv loadImage函数将图像加载到CvMat.但是我要删除此保存图像的步骤.

I have an image in a numpy array which I save using savefig and then use opencv loadImage function to load the image to a CvMat. But I want to remove this saving the image step.

我的Numpy图片大小为25x21,如果我使用

My Numpy Image size is 25x21, and if I use fromArray function like

im = cv.fromarray(asarray(img))

im = cv.fromarray(asarray(img))

我得到的CvMat大小为25x21,非常小.但是,当我将图像保存为png格式并使用LoadImage加载回去时,我得到了尺寸为429x509的全尺寸图像.

I get a CvMat of size 25x21 which is very small. But When I save the image to png format and load it back using LoadImage, I get the full sized image of size 429x509.

有人可以告诉我如何将这个完整尺寸的图像从numpy数组转换为CvMat吗?我可以在不使用savefig()保存的情况下将图像从numpy数组转换为png格式吗? 这就是我现在正在做的事情.

Can somebody please tell me how do I get this full sized image from numpy array to CvMat? Can I convert the image from numpy array to a png format in code without saving it using savefig()? This is what I am doing right now.

imgFigure = imshow(zeros((gridM,gridN)),cmap=cm.gray,vmin=VMIN,vmax=5,animated=True,interpolation='nearest',extent=[xmin,xmax,ymin,ymax])  
imgFigure.set_data(reshape(img,(gridM,gridN)))  
draw()  
fileName = '1p_'
fileName += str(counter)
fileName += ".png" 
savefig(fileName,bbox_inches='tight',pad_inches=0.01,facecolor='black')

上面的img的大小为525,gridM和gridN分别为25和21.然后我使用以下命令加载该图像:

The size of img above is 525 and gridM and gridN are 25 and 21.Then I load this image using:

img = cv.LoadImage(fileName, cv.CV_LOAD_IMAGE_GRAYSCALE)

现在img大小为429x509.

Now img size is 429x509.

推荐答案

您可以直接在numpy数组上使用cv.fromarray(),而无需在它们之间进行保存:

You can just use cv.fromarray() directly upon your numpy array with no need to save inbetween:

import cv
import numpy as np
a = np.arange(0,255,0.0255).reshape(50,200)
b = cv.fromarray(a)
cv.SaveImage('saved.png', b)
print b
#Output:
<cvmat(type=42424006 64FC1 rows=50 cols=200 step=1600 )>

numpy数组变为cvmat,并且大小不变.这是保存的图像:

The numpy array becomes a cvmat, and the size is unchanged. This is the saved image:

这篇关于将具有图像数据的numpy数组转换为CvMat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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