Doxygen tutorials: python final edits

This commit is contained in:
Maksim Shabunin
2014-12-01 15:46:05 +03:00
parent 875f922332
commit 812ce48c36
49 changed files with 426 additions and 353 deletions

View File

@@ -21,29 +21,30 @@ Accessing and Modifying pixel values
Let's load a color image first:
@code{.py}
import cv2
import numpy as np
>>> import cv2
>>> import numpy as np
img = cv2.imread('messi5.jpg')
>>> img = cv2.imread('messi5.jpg')
@endcode
You can access a pixel value by its row and column coordinates. For BGR image, it returns an array
of Blue, Green, Red values. For grayscale image, just corresponding intensity is returned.
@code{.py}
px = img[100,100]
print px
>>> px = img[100,100]
>>> print px
[157 166 200]
# accessing only blue pixel
blue = img[100,100,0]
print blue
>>> blue = img[100,100,0]
>>> print blue
157
@endcode
You can modify the pixel values the same way.
@code{.py}
img[100,100] = [255,255,255]
print img[100,100]
>>> img[100,100] = [255,255,255]
>>> print img[100,100]
[255 255 255]
@endcode
**warning**
Numpy is a optimized library for fast array calculations. So simply accessing each and every pixel
@@ -52,18 +53,20 @@ values and modifying it will be very slow and it is discouraged.
@note Above mentioned method is normally used for selecting a region of array, say first 5 rows and
last 3 columns like that. For individual pixel access, Numpy array methods, array.item() and
array.itemset() is considered to be better. But it always returns a scalar. So if you want to access
all B,G,R values, you need to call array.item() separately for all. Better pixel accessing and
editing method :
@code{.python}
all B,G,R values, you need to call array.item() separately for all.
Better pixel accessing and editing method :
@code{.py}
# accessing RED value
img.item(10,10,2)
>>> img.item(10,10,2)
59
# modifying RED value
img.itemset((10,10,2),100)
img.item(10,10,2)
>>> img.itemset((10,10,2),100)
>>> img.item(10,10,2)
100
@endcode
Accessing Image Properties
--------------------------
@@ -73,23 +76,29 @@ etc.
Shape of image is accessed by img.shape. It returns a tuple of number of rows, columns and channels
(if image is color):
@code{.py}
print img.shape
>>> print img.shape
(342, 548, 3)
@endcode
@note If image is grayscale, tuple returned contains only number of rows and columns. So it is a
good method to check if loaded image is grayscale or color image. Total number of pixels is accessed
by \`img.size\`:
good method to check if loaded image is grayscale or color image.
Total number of pixels is accessed by `img.size`:
@code{.py}
print img.size
>>> print img.size
562248
@endcode
Image datatype is obtained by \`img.dtype\`:
@code{.py}
print img.dtype
>>> print img.dtype
uint8
@endcode
@note img.dtype is very important while debugging because a large number of errors in OpenCV-Python
code is caused by invalid datatype. Image ROI ===========
code is caused by invalid datatype.
Image ROI
---------
Sometimes, you will have to play with certain region of images. For eye detection in images, first
face detection is done all over the image and when face is obtained, we select the face region alone
@@ -99,8 +108,8 @@ are always on faces :D ) and performance (because we search for a small area)
ROI is again obtained using Numpy indexing. Here I am selecting the ball and copying it to another
region in the image:
@code{.py}
ball = img[280:340, 330:390]
img[273:333, 100:160] = ball
>>> ball = img[280:340, 330:390]
>>> img[273:333, 100:160] = ball
@endcode
Check the results below:
@@ -113,18 +122,19 @@ Sometimes you will need to work separately on B,G,R channels of image. Then you
BGR images to single planes. Or another time, you may need to join these individual channels to BGR
image. You can do it simply by:
@code{.py}
b,g,r = cv2.split(img)
img = cv2.merge((b,g,r))
>>> b,g,r = cv2.split(img)
>>> img = cv2.merge((b,g,r))
@endcode
Or
\>\>\> b = img[:,:,0]
@code
>>> b = img[:,:,0]
@endcode
Suppose, you want to make all the red pixels to zero, you need not split like this and put it equal
to zero. You can simply use Numpy indexing, and that is more faster.
@code{.py}
img[:,:,2] = 0
>>> img[:,:,2] = 0
@endcode
**warning**
cv2.split() is a costly operation (in terms of time). So do it only if you need it. Otherwise go
@@ -140,10 +150,9 @@ padding etc. This function takes following arguments:
- **src** - input image
- **top**, **bottom**, **left**, **right** - border width in number of pixels in corresponding
directions
-
**borderType** - Flag defining what kind of border to be added. It can be following types:
- **cv2.BORDER_CONSTANT** - Adds a constant colored border. The value should be given
- **borderType** - Flag defining what kind of border to be added. It can be following types:
- **cv2.BORDER_CONSTANT** - Adds a constant colored border. The value should be given
as next argument.
- **cv2.BORDER_REFLECT** - Border will be mirror reflection of the border elements,
like this : *fedcba|abcdefgh|hgfedcb*

View File

@@ -16,15 +16,17 @@ res = img1 + img2. Both images should be of same depth and type, or second image
scalar value.
@note There is a difference between OpenCV addition and Numpy addition. OpenCV addition is a
saturated operation while Numpy addition is a modulo operation. For example, consider below sample:
@code{.py}
x = np.uint8([250])
y = np.uint8([10])
saturated operation while Numpy addition is a modulo operation.
print cv2.add(x,y) # 250+10 = 260 => 255
For example, consider below sample:
@code{.py}
>>> x = np.uint8([250])
>>> y = np.uint8([10])
>>> print cv2.add(x,y) # 250+10 = 260 => 255
[[255]]
print x+y # 250+10 = 260 % 256 = 4
>>> print x+y # 250+10 = 260 % 256 = 4
[4]
@endcode
It will be more visible when you add two images. OpenCV function will provide a better result. So
@@ -114,4 +116,3 @@ Exercises
-# Create a slide show of images in a folder with smooth transition between images using
cv2.addWeighted function

View File

@@ -113,8 +113,11 @@ working on this issue)*
@note Python scalar operations are faster than Numpy scalar operations. So for operations including
one or two elements, Python scalar is better than Numpy arrays. Numpy takes advantage when size of
array is a little bit bigger. We will try one more example. This time, we will compare the
performance of **cv2.countNonZero()** and **np.count_nonzero()** for same image.
array is a little bit bigger.
We will try one more example. This time, we will compare the performance of **cv2.countNonZero()**
and **np.count_nonzero()** for same image.
@code{.py}
In [35]: %timeit z = cv2.countNonZero(img)
100000 loops, best of 3: 15.8 us per loop