试图将numpy数组读入opencv - cv2.imdecode返回空参数

时间:2016-07-04 18:45:41

标签: python arrays opencv matplotlib fits

几年后再回到编码状态。尝试编写可以通过.fits文件的文件夹编写一段代码,读出图像数据,将它们转换为数组,将它们读入opencv,然后对它们执行边缘检测。我可以使用.png文件使其工作正常,因为我不必使用数组。我已经做了很多阅读,这使我能够达到这一点。问题是,img目前被退回空洞,导致其他一切都变得怪异。有什么想法吗?感谢任何帮助!

#import packages

import cv2
from matplotlim import pyplot as plt
import os
from astropy.io import fits
from skimage import img_as_uint
import numpy as np

#create array with filenames

data = []
for root, dirs, files in os.walk(r'/Users/hannah/Desktop/firefountain-testset'):
    for file in files:
        if file.endswith('.fits'):
            data.append(file)

#start my loop through the folder
for i in data:
    fn = i
#read fits image data
    hdulist = fits.open(fn)
    img_data = hdulist[1].data
#put fits data into array with dtype set as original
    imgraw=np.array(img_data, dtype = np.uint8)
#convert to uint16 
    img = img_as_uint(imgraw)
#crop to area of interest then add into array - possibly don't need second line
    imgcrop = img[210:255,227:277]
    imgcroparr = np.array(imgcrop)
#attempt to read into cv2 - this is where it loses the data 
#all fine before this point I believe
    imgfinal = cv2.imdecode(imgcroparr, 0,)
    plt.imshow(imgfinal) 
#imgfinal returns blank.

google drive with .fits files I'm using, plus .png to show what they should look like

更新了我的代码

for i in data:
    fn = i
    imgraw = fits.getdata(fn)
    imgcrop = imgraw[210:255,227:277]
    img = cv2.imdecode(imgcrop, 0,)
    plt.imshow(img) 

这打开了适合文件没有问题,imgcrop按预期工作。但是,cv2.imdecode(imgcrop,0)引发了以下内容:

错误:/Users/jhelmus/anaconda/conda-bld/work/opencv-2.4.8/modules/highgui/src/loadsave.cpp:307:错误:(-215)buf.data&&功能imdecode _

中的buf.isContinuous()

在此上下文中无法在此错误中找到任何内容,变得越来越困惑。这可能是我忘记做的非常愚蠢和基本的事情,但我看不到它。

0 个答案:

没有答案