Is there any possible reason to have some difficulties with jpg files in opencv while using imread function to read them?

拈花ヽ惹草 提交于 2019-12-24 00:45:57

问题


Recently, I use opencv in python.

As I noticed, when I want to import cv2 module in python, I need to add cv2.so file path manually to the system paths using:

sys.path.append('/path/to/cv.so')

Howewer, when I want to read jpg files in the ipython notebooks, it fails:

import sys
import numpy as np
import os
sys.path.append("/usr/local/lib1/python2.7/site-packages")
import cv2
im1=cv2.imread('pic1.png')
print im1.shape
#output: (512, 512, 3)
im2=cv2.imread('pic1.jpg')
print im2.shape
#output:
-------------------------------------------------------------------------
AttributeError                         Traceback (most recent call last)
<ipython-input-8-2d36ac00eca0> in <module>()
----> 1 print im2.shape
AttributeError: 'NoneType' object has no attribute 'shape'

Based on my previous question, I rebuilt opencv several times.

I read somewhere that it might be the result of some dependency problems. But I have the both packages (libjpeg and libjaspe) on my system:

print cv2.getBuildInformation()

Media I/O: 
ZLib:                        /lib64/libz.so (ver 1.2.8)
JPEG:                        /lib64/libjpeg.so (ver 80)
WEBP:                        /lib64/libwebp.so (ver encoder: 0x0202)
PNG:                         /lib64/libpng.so (ver 1.6.17)
TIFF:                        /lib64/libtiff.so (ver 42 - 4.0.2)
JPEG 2000:                   /lib64/libjasper.so (ver 1.900.1)

Any idea?


回答1:


After lots of efforts, I realized that the solution is to add:

PYTHONPATH=""
export PYTHONPATH
PATH=/usr/bin:/usr/local/bin
export PATH

at the end of /home/.bashrc file (to make them permanent), before running python or ipython notebook from terminal.

Note: use only opencv-python.x86_64 (based on your machine architecture) from yum (dnf) repository (for fedora users of course!) and python 2.7.

DO NOT download opencv from its website. That makes some dependency troubles, I guess.



来源:https://stackoverflow.com/questions/36870234/is-there-any-possible-reason-to-have-some-difficulties-with-jpg-files-in-opencv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!