Difference between cv2, scipy.misc and skimage - python

What is the main difference between
cv2.imread / resize/ imwrite
scipy.misc.imread / imresize/ imsave
skimage.io.imread / skimage.transform.resize / skimage.io.imsave
and how to decide which one to use?
I know cv2 and skimage have different encoder, and cv2 use 'BGR' not 'RGB' in default. But sometimes a script might use them together, for example main.py, where it uses scipy.misc.imread, cv2.imresize and cv2.imwrite. I am wondering the reason to do so.

The scipy.misc module exists historically as a place to gather functions that do not fit easily into the other SciPy submodules. It is slated for deprecation and should not be used.
In the Python ecosystem, I'd recommend imageio for reading images (or matplotlib.pyplot.imread, if you already are using matplotlib).
Scikit-image provides a convenient wrapper around all of these I/O libraries as skimage.io (it should pick up whatever is installed on your system already). It also ensures that images are converted to the correct data type and range formats for use with other skimage functions (see http://scikit-image.org/docs/dev/user_guide/data_types.html).
cv2.imread et al. operate on OpenCV image objects and, as you've already observed, those images are typically stored in BGR memory layout. But for loading PNGs and JPGs, most of these libraries listed above all wrap the same underlying C libraries, perhaps with slightly different parameters for compression etc.
I'd recommend that you use whichever functions minimize the dependency footprint of your script / package.

Related

Remove OpenCV image size limitation

I am loading in a very large image (60,000 x 80,000 pixels) and am exceeding the max pixels I can load:
cv2.error: OpenCV(4.2.0) /Users/travis/build/skvark/opencv-python/opencv/modules/imgcodecs/src/loadsave.cpp:75:
error: (-215:Assertion failed) pixels <= CV_IO_MAX_IMAGE_PIXELS in function 'validateInputImageSize'
From what I have found this is referring to the limitation imposed on line 65
Ideally I'd change that to deal with at least 5 gigapixel images
#define CV_IO_MAX_IMAGE_PIXELS (1<<33)
I have seen some workarounds for this (OpenCV image size limit) but those don't seem to address the problem which is an arbitrary definition (I'm working off a high performance server with 700gb ram so compute not an issue).
My issue is that I have no idea where this file is. The error points me towards this "travis" directory which doesn't exist locally for me and in my local environment the c++ files aren't available.
Any idea on where to look to find the c++ library?
You have to modify the openCV source files and then compile it your own.
EDIT: You can also modify environment variables
export CV_IO_MAX_IMAGE_PIXELS=1099511627776
For my problem I should have specified it was a .tif file (NOTE most large images will be in this file format anyway). In which case a very easy way to load it in to a numpy array (so it can then work with OpenCV) is with the package tifffile.
pip install tifffile as tifi
This will install it in your python environment.
import tifffile as tifi
img = tifi.imread("VeryLargeFile.tif")
From here you can use it as you would with any numpy array and it is fully compatible with OpenCV etc.
Adding the following to your program should fix the issue in python opencv.
import os
os.environ["OPENCV_IO_MAX_IMAGE_PIXELS"] = str(pow(2,40))
import cv2

Data types in OpenCV

I'm working with OpenCV for the first time and I'm a little bit confused with data types. I am working with Python.
I see that I can store an image as:
Numpy array
CvMat
Iplimage
The problem I'm having is that different parts of OpenCV seem to require different types and I keep having to try and convert back and forth- it's very confusing, and I'm certain it can't have been designed this way on purpose. I'm also a bit confused about when something should be in the cv module vs cv2.cv:
import cv
import cv2.cv
Can someone explain the logic? It would really help.
Thanks!
cv (or, cv2.cv)
is the old opencv python api, using IplImage and CvMat.
you should not use that anymore. it's being phased out, and won't be available in the next version.
cv2 is the new python api, using numpy arrays for almost anything instead,
so easy to combine with scipy, matplotlib and what not. (and , btw, much closer the the current c++ api)

importing python modules - ImageChops

I'm looking for a good way to analyze image similarity, using python.
I'm NOT looking for a way to establish whether two images are identical. I'm just looking for a way to establish the similarity between two images (e.g., if two images are very similar they could be given a "grade" of 9/10; if they are completely unalike, they'll be given a very low index, such as 2/10).
From some reading that I've done, the module ImageChops has been suggested - however, I haven't been able to find a way to download it.
If anyone knows how to download it, or has a suggestion for other effective solutions, I'd greatly appreciate their advice!
Thanks in advance!
ImageChops is a module from PIL(Pillow). To use the ImageChops function you need to pip install Pillow OR easy_install Pillow OR download the src & extract the src then from CMD CD to the extracted folder & run python setup.py install.
To use the ImageChops you can do this from PIL import ImageChops
you can read the document section
some basic usage example http://effbot.org/imagingbook/imagechops.htm
To check the difference between 2 images:
import Image
from PIL import ImageChops
im1 = Image.open("splash.png")
im2 = Image.open("splash2.png")
diff = ImageChops.difference(im2, im1)
there's a compare images script, but its not a PIL; its on scipy module
You may also check this script here
ImageChops is an module belong to Python Image Library(PIL). Just note that there is no image similarity algorithm (except pixel-wise) built-in in ImageChops, instead, it is a tool you used to write your own algorithm. There is a greate article here:
How can I quantify difference between two images?
The ImageChops module contains a number of arithmetical image operations, called channel operations (“chops”). These can be used for various purposes, including special effects, image compositions, algorithmic painting, and more.
http://effbot.org/imagingbook/imagechops.htm
you can download the Python Image Library here.
http://www.pythonware.com/products/pil/
There are precompiled package for windows user too.
http://www.lfd.uci.edu/~gohlke/pythonlibs/

Picklable image in python

Does anyone know of a method to make images picklable? I am trying to pass OpenCV images between processes to separate display from real-time processing of an OpenCV video. I have tried PIL Image.tostring(), but to no avail. Is there a standard technique?
This is what you are looking for: pickleable image object
Making it a dict and then using the information in the dict to recreate the image again. Hopefully this helps
OpenCV's newer cv2 module uses NumPy arrays, which are inherently pickleable. See my answer here: https://stackoverflow.com/a/17054961/1510289

What is the difference between a PIL Image from PIL.Image.open and a NumPy Image matplotlib.image.imread ?

I would like to know why I should or shouldn't use a matplotlib image over a PIL image. I know that matplotlib uses PIL to load any image that is not a PNG, but what is the advantage in having it in a numpy array over the PIL backend representation?
The PIL API includes function for performing a wide range of image manipulations. But of course, it does not provide a function for all conceivable operations. Numpy is useful when you have a mathematical operation to perform on the image which is not built into the PIL API. PIL has a way of altering pixels one-by-one but because if its reliance on Python loops it can be a very slow way to manipulate a large image (or many images).
Numpy math is relatively fast and it has an expressive syntax which
can make coding new image manipulations easier. Moreover, scipy has many additional image manipulating functions which can be applied to numpy arrays.
Here are a few examples:
emboss
converting rgb to hsv
replacing a color

Categories