ipython cv2.imwrite()不保存图像 [英] Ipython cv2.imwrite() not saving image

查看:1406
本文介绍了ipython cv2.imwrite()不保存图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在python opencv中编写了代码.我正在尝试将已处理的图像写回到磁盘,但是该图像未保存,并且未显示任何错误(运行时和编译),代码为

I have written a code in python opencv. I am trying to write the processed image back to disk but the image is not getting saved and it is not showing any error(runtime and compilation) The code is

"""
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)

Can anyone please tell me why it is not working. cv2.imshow is working properly(as it is showing the correct image). Thanks and Regards Niladri

推荐答案

作为一般且绝对的规则,您必须r前缀或某些名称保护Windows路径字符串(包含反斜杠)解释字符(例如:\n,\b,\v,\x aaaa和\t,完整列表这里):

As a general and absolute rule, you have to protect your windows path strings (containing backslashes) with r prefix or some characters are interpreted (ex: \n,\b,\v,\x aaaaand \t, full list here):

因此,当执行此操作时:

so when doing this:

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

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

imreadimwrite的烦人之处在于,这些函数不会在错误时引发异常,而会以静默方式失败. imwrite返回False

And the annoying thing with imread and imwrite is that those functions don't throw exceptions on errors, but fail silently. imwrite returns False

>>> cv2.imread("D:/nonexisting.jpg")  # this returns None, no error
>>> s = cv2.imread("D:/sloth_book.jpg")  # this works
>>> s
array([[[250, 250, 250],
        [246, 246, 246],
        [255, 255, 255],
        ...,
>>> cv2.imwrite("inexistent_dir/file.jpg",s)  # dir doesn't exist, write fails
False

因此,您必须检查这些函数的返回值.

So you have to check return value of those functions.

执行此操作:

if not cv2.imwrite(r'C:\Users\Niladri\Desktop\tropical_image_sig5.bmp', img2):
     raise Exception("Could not write image")

注意:读取工作正常,因为转义的"大写字母在python 2中没有特殊含义(\U\N在python 3中具有含义,因此它不起作用)

Note: the read works fine because "escaped" uppercase letters have no particular meaning in python 2 (\U and \N have a meaning in python 3 so it wouldn't have worked)

如果有错误,程序现在会大声抱怨.

And if there's an error, the program now complains loudly.

这篇关于ipython cv2.imwrite()不保存图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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