☁It will print the banner and enter the shell:
15.6.16.3 A Simple Example
In this example, an image is loaded. A simple processing is performed, and the result is written to a new image. 15.6.16.3.1 Loading an image
The image was downloaded from USC standard database: http://sipi.usc.edu/database/database.php?volume=misc&image=9 15.6.16.3.2 Displaying the image
total = 0 for arg in args:
total += float(arg.strip()) print(total)
def help_add(self): print('\n'.join([ 'add [number,]',
'Add the arguments together and display the total.'
]))
def do_subtract(self, line): args = line.split() total = 0 if len(args) > 0: total = float(args[0]) for arg in args[1:]: total -= float(arg.strip()) print(total)
def help_subtract(self): print('\n'.join([ 'subtract [number,]',
'Subtract all following arguments from the first argument.'
]))
def do_EOF(self, line): print('bye, bye') return True
if __name__ == '__main__': Calculator().cmdloop() $ python calculator.py
Simple calculator that can do addition, subtraction, multiplication and division. calc >>> help
Documented commands (type help <topic>): ======================================== add help subtract
Undocumented commands: ====================== EOF
calc >>> help add add [number,]
Add the arguments together and display the total. calc >>> help subtract
subtract [number,]
Subtract all following arguments from the first argument. calc >>> bye, bye
$ pip install opencv-python
import cv2
%matplotlib inline import cv2
img = cv2.imread('images/opencv/4.2.01.tiff')
The image is saved in a numpy array. Each pixel is represented with 3 values (R,G,B). This provides you with access to manipulate the image at the level of single pixels. You can display the image using imshow function as well as Matplotlib’s imshow function.
You can display the image using imshow function:
or you can use Matplotlib. If you have not installed Matplotlib before, install it using:
Now you can use:
which results in
Figure: Image display 15.6.16.3.3 Scaling and Rotation
Scaling (resizing) the image relative to different axis
which results in
Figure: Scaling and rotation Rotation of the image for an angle of t
which results in
image 15.6.16.3.4 Gray-scaling cv2.imshow('Original',img) cv2.waitKey(0) cv2.destroyAllWindows()
$ pip install matplotlib
import matplotlib.pyplot as plt plt.imshow(img) res = cv2.resize(img, None, fx=1.2, fy=0.7, interpolation=cv2.INTER_CUBIC) plt.imshow(res) rows,cols,_ = img.shape t = 45 M = cv2.getRotationMatrix2D((cols/2,rows/2),t,1) dst = cv2.warpAffine(img,M,(cols,rows)) plt.imshow(dst)
img2 = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) plt.imshow(img2, cmap='gray')
☁
which results inFigure: Gray sacling 15.6.16.3.5 Image Thresholding
which results in
Figure: Image Thresholding 15.6.16.3.6 Edge Detection
Edge detection using Canny edge detection algorithm
which results in
Figure: Edge detection 15.6.16.4 Additional Features
OpenCV has implementations of many machine learning techniques such as KMeans and Support Vector Machines, that can be put into use with only a few lines of code. It also has functions especially for video analysis, feature detection, object recognition and many more. You can find out more about them in their website
[OpenCV](https://docs.opencv.org/3.0-beta/index.html was initially developed for C++ and still has a focus on that language, but it is still one of the most valuable image processing libraries in Python.
15.6.17 S
ECCHID
ISKWe are developing an autonomous robot boat that you can be part of developing within this class. The robot bot is actually measuring turbidity or water clarity. Traditionally this has been done with a Secchi disk. The use of the Secchi disk is as follows:
1. Lower the Secchi disk into the water. 2. Measure the point when you can no longer see it
3. Record the depth at various levels and plot in a geographical 3D map
One of the things we can do is take a video of the measurement instead of a human recording them. Than we can analyse the video automatically to see how deep a disk was lowered. This is a classical image analysis program. You are encouraged to identify algorithms that can identify the depth. The most simplest seems to be to do a histogram at a variety of depth steps, and measure when the histogram no longer changes significantly. The depth at that image will be the measurement we look for.
Thus if we analyse the images we need to look at the image and identify the numbers on the measuring tape, as well as the visibility of the disk.
To show case how such a disk looks like we refer to the image showcasing different Secchi disks. For our purpose the black-white contrast Secchi disk works well. ret,thresh = cv2.threshold(img2,127,255,cv2.THRESH_BINARY)
plt.subplot(1,2,1), plt.imshow(img2, cmap='gray') plt.subplot(1,2,2), plt.imshow(thresh, cmap='gray')
edges = cv2.Canny(img2,100,200)
plt.subplot(121),plt.imshow(img2,cmap = 'gray') plt.subplot(122),plt.imshow(edges,cmap = 'gray')
Figure: Secchi disk types. A marine style on the left and the freshwater version on the right wikipedia. More information about Secchi Disk can be found at:
https://en.wikipedia.org/wiki/Secchi/_disk
We have included next a couple of examples while using some obviously useful OpenCV methods. Surprisingly, the use of the edge detection that comes in mind first to identify if we still can see the disk, seems to complicated to use for analysis. We at this time believe the histogram will be sufficient.
Please inspect our examples. 15.6.17.1 Setup for OSX
First lest setup the OpenCV environment for OSX. Naturally you will have to update the versions based on your versions of python. When we tried the install of OpenCV on MacOS, the setup was slightly more complex than other packages. This may have changed by now and if you have improved instructions, pleas elt us know. However we do not want to install it via Anaconda out of the obvious reason that anaconda installs to many other things.
15.6.17.2 Step 1: Record the video