cv2一个python - 16位适合cv2中的文件

时间:2015-04-03 09:43:52

标签: python opencv

在某段时间之前我写了一些脚本,它找到了太阳的中心(带有Canny和瞬间)和图像的中心。这是python opencv-finding circle (Sun) , coordinates of center the circle from picture

但现在我遇到了这个问题。当我在fit文件上使用这个脚本时,它不起作用。我有16位太阳的单色图片http://files.uloziste.com/d16feb4de5aeda18/

我知道我方法的图片必须是8位(CV_8U)

如何将此图片转换为8位并获取图像深度信息?当我使用高度,宽度,深度= im.shape时,我得到错误:

高度,宽度,深度= im.shape ValueError:解包需要2个以上的值

以下是open fit文件的代码

import numpy as np
import cv2
import pyfits


hdul = pyfits.open('11_18_46_640_syn_snap_obs_1533.fits')
im=hdul[0].data


#height, width, depth = im.shape
print im.shape
thresh = 123
imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)

blur = cv2.GaussianBlur(imgray,(5,5),0)
edges = cv2.Canny(blur,thresh,thresh*2)
contours,hierarchy=cv2.findContours(edges,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
c=len(contours) - 1

cnt = contours[c]
cv2.drawContours(im,contours,-1,(0,255,0),-1)

#centroid_x = M10/M00 and centroid_y = M01/M00
M = cv2.moments(cnt)
x = int(M['m10']/M['m00'])
y = int(M['m01']/M['m00'])
print x,y

cv2.circle(im,(x,y),1,(0,0,255),2)
cv2.putText(im,"x[px],y[px]",(10,50), cv2.FONT_HERSHEY_SIMPLEX, 1,  (255,0,255))
cv2.putText(im,"center of Sun",(x,y), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,0,255))
cv2.putText(im,str(x)+","+str(y),(10,100), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,0,255))
cv2.circle(im,(width/2,height/2),1,(255,0,0),2)
cv2.putText(im,"center of image",(width/2,height/2),    cv2.FONT_HERSHEY_SIMPLEX, 1, (255,0,0))
cv2.putText(im,str(width/2)+","+str(height/2), (10,150),    cv2.FONT_HERSHEY_SIMPLEX, 1, (255,0,0))
cv2.putText(im,"difference:"+str(width/2-x)+","+str(height/2-y),(400,50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,255,255))

cv2.imshow('contour',im)
cv2.waitKey(0) code here

好的是编辑:

此代码将我的图片转换为8位。但是当我运行脚本时,我得到错误:cnt = contours [0] IndexError:列表索引超出范围

为什么?有什么建议吗?

代码

import pyfits
import numpy as np
import cv2

hdul = pyfits.open('11_18_46_640_syn_snap_obs_1533.fits')

hdu=hdul[0].data
ma=hdu.max()
mi=hdu.min()
image = np.array(hdu, copy=True)
image.clip(mi,ma, out=image)
image -=mi
image //= (ma - mi + 1) / 255.
im=image.astype(np.uint8)

#height, width, depth = im.shape
#print im.shape


thresh = 123
imgray = cv2.cvtColor(im,cv2.COLOR_GRAY2RGB)

blur = cv2.GaussianBlur(im,(5,5),0)

edges = cv2.Canny(blur,thresh,thresh*2)

contours, hierarchy =cv2.findContours(edges,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

c=len(contours) - 1

cnt = contours[0]
cv2.drawContours(im,contours,-1,(0,255,0),-1)

#centroid_x = M10/M00 and centroid_y = M01/M00
M = cv2.moments(cnt)
x = int(M['m10']/M['m00'])
y = int(M['m01']/M['m00'])

print x,y

cv2.circle(im,(x,y),1,(0,0,255),2)
cv2.putText(im,"x[px],y[px]",(10,50), cv2.FONT_HERSHEY_SIMPLEX, 1,    (255,0,255))
cv2.putText(im,"center of Sun",(x,y), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,0,255))
cv2.putText(im,str(x)+","+str(y),(10,100), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,0,255))
cv2.circle(im,(width/2,height/2),1,(255,0,0),2)
cv2.putText(im,"center of image",(width/2,height/2),       cv2.FONT_HERSHEY_SIMPLEX, 1, (255,0,0))
cv2.putText(im,str(width/2)+","+str(height/2), (10,150), cv2.FONT_HERSHEY_SIMPLEX, 1, (255,0,0))
cv2.putText(im,"difference:"+str(width/2-x)+","+str(height/2-y),(400,50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,255,255))

cv2.imshow('im',im)
cv2.waitKey(0)

1 个答案:

答案 0 :(得分:0)

好吧,您最初的问题是理解16 位文件格式
常规灰度用无符号 8 位编码阴影,因此范围从 0 到 255。16 位图像只是扩展了这个范围,最多允许 2^16 个阴影。因此,使用 ndarray.shape 获取图像的深度是没有意义的 - 它只有一个颜色通道(即每个像素具有单个 UINT16 值的二维矩阵)。

至于第二个索引问题
ndarray.astype('uint8') 获取最低有效位,因此,假设 1460 (0101_1011_0100) 将变为 180 (1011_0100)。我的猜测是,您然后尝试在与您期望的不同的图片中找到 Canny 的轮廓,但没有成功。那么你只是在索引一个空数组。

解决方案
使用 cv2.convertScaleAbs(image, alpha=(255/65535)) 将 16 位转换为 8 位