简体   繁体   English

使用 cv2 和 python 打印图像

[英]Using cv2 with python to print an image

When I use cv2.imshow , it works and the picture shows.当我使用 cv2.imshow 时,它可以工作并且图片显示。 but when I want to save it in a folder through using cv2.imwrite:但是当我想通过使用 cv2.imwrite 将它保存在一个文件夹中时:

test_image = cv2.imread("/Users/Capstone-Project-1/test/000002806137.jpg")
test_image = cv2.resize(test_image, (512,256))/255.0
test_image = np.rollaxis(test_image, axis=2, start=0)
_, _, ti = test(lane_agent, np.array([test_image])) 

cv2.imshow("test", ti[0])
cv2.waitKey(0) 
cv2.imwrite('/Users/Capstone-Project-1/Deep Neural 
Networks/save_test/image_result.png',test_image)

it gives me the error below:它给了我以下错误:

error: (-215:Assertion failed) image.channels() == 1 || image.channels() == 3 || 
image.channels() == 4 in function 'imwrite_'

Any suggestions how to fix the issue?有什么建议可以解决这个问题吗?

Maybe you have to do it like these:也许你必须这样做:

path = 'D:/OpenCV/Scripts/Images'
cv2.imwrite(os.path.join(path , 'filename.png'), img)

why you use test_image and not ti[0] , the same frame you want to show?为什么你使用test_image而不是ti[0] ,你想显示同一个框架?

have you tried this?你试过这个吗?

cv2.imwrite('/Users/Capstone-Project-1/Deep Neural Networks/save_test/image_result.png', ti[0])

do you have the same error?你有同样的错误吗?

test_image.shape[-1] needs to be 1 (monchrome), 3 (RGB) or 4 (RGBA) test_image.shape[-1]需要为 1(单色)、3(RGB)或 4(RGBA)

Since you start as jpg , the data is probably RGB, so your shape after cv2.resize is (512, 256, 3)由于您从jpg开始,因此数据可能是 RGB,因此您在cv2.resize之后的形状是(512, 256, 3)

When you do:当你这样做时:

 test_image = np.rollaxis(test_image, axis=2, start=0)

your axes change and your shape becomes (3, 512, 256) .你的轴会改变,你的形状变成(3, 512, 256) Now it isn't recognized as an image anymore, and imwrite fails.现在它不再被识别为图像,并且imwrite失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM