A collection of commonly used computer vision functions based on OpenCV and Python.
pip install git+https://github.com/prasunroy/cvutils.git
git clone https://github.com/prasunroy/cvutils.git
cd cvutils
python setup.py install
imread(path, flag=-1)
Reads an image file.
Supported image file formats:
Bitmap : *.bmp, *.dib
JPEG : *.jpe, *.jpeg, *.jpg
JPEG 2000 : *.jp2
PNG : *.png
Portable : *.pbm, *.pgm, *.ppm
Raster : *.ras, *.sr
TIFF : *.tif, *.tiff
WebP : *.webp
Args:
path : Path to an image file or an image url.
flag : Read flag. Defaults to -1.
>0 -- read as color image (ignores alpha channel)
=0 -- read as grayscale image
<0 -- read as original image (keeps alpha channel)
Returns:
An image as a numpy array if read is successful None otherwise.
The order of channels is BGR(A) for color image.
from cvutils.io import imread
from cvutils.io import imshow
# read an image file
image = imread('ArcticFox.jpg')
imshow(image, title='Read from image file')
# read an image url
image = imread('https://github.com/prasunroy/cvutils/raw/master/assets/Kingfisher.jpg')
imshow(image, title='Read from image url')
imwrite(path, image)
Writes an image file.
Supported image file formats:
Bitmap : *.bmp, *.dib
JPEG : *.jpe, *.jpeg, *.jpg
JPEG 2000 : *.jp2
PNG : *.png
Portable : *.pbm, *.pgm, *.ppm
Raster : *.ras, *.sr
TIFF : *.tif, *.tiff
WebP : *.webp
Args:
path : Path to the image file to be written. If the file already
exists it will be overwritten.
image : A numpy array. The order of channels is BGR(A) for color image.
Returns:
True if write is successful False otherwise.
from cvutils.io import imread
from cvutils.io import imwrite
# read a JPEG image file as grayscale and write to a PNG image file
image = imread('Kingfisher.jpg', flag=0)
imwrite('Kingfisher.png', image)
imshow(image, title='')
Shows an image in a window.
Args:
image : Image source. This can be either a numpy array, a path to an
image file or an image url.
title : Window title. Defaults to an empty string.
Returns:
None
from cvutils.io import imread
from cvutils.io import imshow
# show an image file
imshow('ArcticFox.jpg', title='Color image')
# show an image url
imshow('https://github.com/prasunroy/cvutils/raw/master/assets/Kingfisher.jpg', title='Color image')
# read an image file as grayscale and show
image = imread('ArcticFox.jpg', flag=0)
imshow(image, title='Grayscale image')
# read an image url as grayscale and show
image = imread('https://github.com/prasunroy/cvutils/raw/master/assets/Kingfisher.jpg', flag=0)
imshow(image, title='Grayscale image')
imnoise(image, model, mu=0, sigma=0, density=0)
Applies a noise model to an image.
Args:
image : An image as a numpy array.
model : A noise model as a string.
='Gaussian' -- applies Gaussian noise model
='Salt-and-Pepper' -- applies Salt and Pepper noise model
mu : Mean of Gaussian distribution. Only applicable for Gaussian
noise model. Defaults to 0.
sigma : Standard deviation of Gaussian distribution. Only applicable
for Gaussian noise model. Defaults to 0.
density : Fraction of image pixels affected by noise. Only applicable
for Salt and Pepper noise model. Defaults to 0. Should be a
value between 0 and 1.
Returns:
A noisy image as a numpy array if the input is a valid image
None otherwise.
from cvutils.io import imread
from cvutils.io import imshow
from cvutils.noise import imnoise
# read images
image1 = imread('ArcticFox.jpg')
image2 = imread('Kingfisher.jpg')
# apply additive white Gaussian noise
noisy1_awgn = imnoise(image1, model='Gaussian', mu=0, sigma=25)
noisy2_awgn = imnoise(image2, model='Gaussian', mu=0, sigma=50)
# apply salt and pepper noise
noisy1_snpn = imnoise(image1, model='Salt-and-Pepper', density=0.05)
noisy2_snpn = imnoise(image2, model='Salt-and-Pepper', density=0.10)
# show results
imshow(image1, title='Original image')
imshow(noisy1_awgn, title='Gaussian noise')
imshow(noisy1_snpn, title='Salt and Pepper noise')
imshow(image2, title='Original image')
imshow(noisy2_awgn, title='Gaussian noise')
imshow(noisy2_snpn, title='Salt and Pepper noise')
ArcticFox.jpg and Kingfisher.jpg are obtained from Pixabay made available under Creative Commons CC0 License.
This work is inspired by Adrian Rosebrock's work on imutils package.
MIT License
Copyright (c) 2018 Prasun Roy
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Made with ❤️ and GitHub