Ipython cv2.imwrite()不保存图像

时间:2016-10-19 15:44:23

标签: python opencv

我在python opencv中编写了一个代码。我试图将处理后的图像写回磁盘,但图像没有保存,并且没有显示任何错误(运行时和编译)代码是

"""
Created on Wed Oct 19 18:07:34 2016

@author: Niladri
"""

import numpy as np  
import cv2  

if __name__ == '__main__':  
import sys  

img = cv2.imread('C:\Users\Niladri\Desktop\TexturesCom_LandscapeTropical0080_2_S.jpg')
if img is None:  
    print 'Failed to load image file:'
    sys.exit(1)  

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)  
h, w = img.shape[:2]  

eigen = cv2.cornerEigenValsAndVecs(gray, 15, 3)  
eigen = eigen.reshape(h, w, 3, 2)  # [[e1, e2], v1, v2]  
#flow = eigen[:,:,2] 
iter_n = 10
sigma = 5
str_sigma = 3*sigma
blend = 0.5
img2 = img
for i in xrange(iter_n):  
    print i,  

    gray = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)  
    eigen = cv2.cornerEigenValsAndVecs(gray, str_sigma, 3)  
    eigen = eigen.reshape(h, w, 3, 2)  # [[e1, e2], v1, v2]  
    x, y = eigen[:,:,1,0], eigen[:,:,1,1] 
    print eigen

    gxx = cv2.Sobel(gray, cv2.CV_32F, 2, 0, ksize=sigma)  
    gxy = cv2.Sobel(gray, cv2.CV_32F, 1, 1, ksize=sigma)  
    gyy = cv2.Sobel(gray, cv2.CV_32F, 0, 2, ksize=sigma)  
    gvv = x*x*gxx + 2*x*y*gxy + y*y*gyy  
    m = gvv < 0  

    ero = cv2.erode(img, None)  
    dil = cv2.dilate(img, None)  
    img1 = ero  
    img1[m] = dil[m]  
    img2 = np.uint8(img2*(1.0 - blend) + img1*blend)  
#print 'done'
cv2.imshow('dst_rt', img2)
cv2.waitKey(0)
cv2.destroyAllWindows()

#cv2.imwrite('C:\Users\Niladri\Desktop\leaf_image_shock_filtered.jpg', img2)    



for i in xrange(iter_n):  
    print i,  

    gray = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)  
    eigen = cv2.cornerEigenValsAndVecs(gray, str_sigma, 3)  
    eigen = eigen.reshape(h, w, 3, 2)  # [[e1, e2], v1, v2]  
    x, y = eigen[:,:,1,0], eigen[:,:,1,1] 
    print eigen

    gxx = cv2.Sobel(gray, cv2.CV_32F, 2, 0, ksize=sigma)  
    gxy = cv2.Sobel(gray, cv2.CV_32F, 1, 1, ksize=sigma)  
    gyy = cv2.Sobel(gray, cv2.CV_32F, 0, 2, ksize=sigma)  
    gvv = x*x*gxx + 2*x*y*gxy + y*y*gyy  
    m = gvv < 0  

    ero = cv2.erode(img, None)  
    dil = cv2.dilate(img, None)  
    img1 = dil  
    img1[m] = ero[m]  
    img2 = np.uint8(img2*(1.0 - blend) + img1*blend)  
print 'done'

#cv2.imwrite('D:\IP\tropical_image_sig5.bmp', img2)    


cv2.imshow('dst_rt', img2)
cv2.waitKey(0)
cv2.destroyAllWindows()

#cv2.imshow('dst_rt', img2)    

cv2.imwrite('C:\Users\Niladri\Desktop\tropical_image_sig5.bmp', img2)

任何人都可以告诉我它为什么不起作用。 cv2.imshow工作正常(因为它显示正确的图像)。 感谢致敬 Niladri

3 个答案:

答案 0 :(得分:9)

作为一般的绝对规则, 以保护带有r前缀的Windows路径字符串(包含反斜杠)或解释某些字符(例如:\n,\b,\v,\x aaaaand \t,完整列表here):

所以这样做:

cv2.imwrite('C:\Users\Niladri\Desktop\tropical_image_sig5.bmp', img2)

您正试图保存到C:\Users\Niladri\Desktop<TAB>ropical_image_sig5.bmp

(我真的不知道它的作用:))

这样做:

cv2.imwrite(r'C:\Users\Niladri\Desktop\tropical_image_sig5.bmp', img2)

注意:读取工作正常,因为&#34;转义&#34;大写字母在python 2中没有特别的意义(\U\N在python 3中有意义所以它不会有效)

答案 1 :(得分:2)

正如Jean所说,错误是由于\被解释为转义序列。因此,使用os.path.join()总是更安全,因为它更加跨平台,您无需担心转义序列问题。例如,在您的情况下,您还需要担心前几个参数,因为这是您的主目录

import os
cv2.imwrite(os.path.join(os.path.expanduser('~'),'Desktop','tropical_image_sig5.bmp'), img2)

os.path.expanduser('~')将直接返回您的主目录。

答案 2 :(得分:0)

如果您想使代码中的路径具有可移植性,请查看pathlib。

https://docs.python.org/3/library/pathlib.html