Commit 0f51155e authored by berak's avatar berak

py_tutorials: add print() braces for python3

parent 17b89b2a
...@@ -218,7 +218,7 @@ for i in xrange(len(objpoints)): ...@@ -218,7 +218,7 @@ for i in xrange(len(objpoints)):
error = cv2.norm(imgpoints[i], imgpoints2, cv2.NORM_L2)/len(imgpoints2) error = cv2.norm(imgpoints[i], imgpoints2, cv2.NORM_L2)/len(imgpoints2)
mean_error += error mean_error += error
print "total error: ", mean_error/len(objpoints) print( "total error: {}".format(mean_error/len(objpoints)) )
@endcode @endcode
Additional Resources Additional Resources
-------------------- --------------------
......
...@@ -30,18 +30,18 @@ You can access a pixel value by its row and column coordinates. For BGR image, i ...@@ -30,18 +30,18 @@ You can access a pixel value by its row and column coordinates. For BGR image, i
of Blue, Green, Red values. For grayscale image, just corresponding intensity is returned. of Blue, Green, Red values. For grayscale image, just corresponding intensity is returned.
@code{.py} @code{.py}
>>> px = img[100,100] >>> px = img[100,100]
>>> print px >>> print( px )
[157 166 200] [157 166 200]
# accessing only blue pixel # accessing only blue pixel
>>> blue = img[100,100,0] >>> blue = img[100,100,0]
>>> print blue >>> print( blue )
157 157
@endcode @endcode
You can modify the pixel values the same way. You can modify the pixel values the same way.
@code{.py} @code{.py}
>>> img[100,100] = [255,255,255] >>> img[100,100] = [255,255,255]
>>> print img[100,100] >>> print( img[100,100] )
[255 255 255] [255 255 255]
@endcode @endcode
...@@ -76,7 +76,7 @@ etc. ...@@ -76,7 +76,7 @@ etc.
Shape of image is accessed by img.shape. It returns a tuple of number of rows, columns and channels Shape of image is accessed by img.shape. It returns a tuple of number of rows, columns and channels
(if image is color): (if image is color):
@code{.py} @code{.py}
>>> print img.shape >>> print( img.shape )
(342, 548, 3) (342, 548, 3)
@endcode @endcode
...@@ -85,12 +85,12 @@ good method to check if loaded image is grayscale or color image. ...@@ -85,12 +85,12 @@ good method to check if loaded image is grayscale or color image.
Total number of pixels is accessed by `img.size`: Total number of pixels is accessed by `img.size`:
@code{.py} @code{.py}
>>> print img.size >>> print( img.size )
562248 562248
@endcode @endcode
Image datatype is obtained by \`img.dtype\`: Image datatype is obtained by \`img.dtype\`:
@code{.py} @code{.py}
>>> print img.dtype >>> print( img.dtype )
uint8 uint8
@endcode @endcode
......
...@@ -23,10 +23,10 @@ For example, consider below sample: ...@@ -23,10 +23,10 @@ For example, consider below sample:
>>> x = np.uint8([250]) >>> x = np.uint8([250])
>>> y = np.uint8([10]) >>> y = np.uint8([10])
>>> print cv2.add(x,y) # 250+10 = 260 => 255 >>> print( cv2.add(x,y) ) # 250+10 = 260 => 255
[[255]] [[255]]
>>> print x+y # 250+10 = 260 % 256 = 4 >>> print( x+y ) # 250+10 = 260 % 256 = 4
[4] [4]
@endcode @endcode
It will be more visible when you add two images. OpenCV function will provide a better result. So It will be more visible when you add two images. OpenCV function will provide a better result. So
......
...@@ -44,7 +44,7 @@ for i in xrange(5,49,2): ...@@ -44,7 +44,7 @@ for i in xrange(5,49,2):
img1 = cv2.medianBlur(img1,i) img1 = cv2.medianBlur(img1,i)
e2 = cv2.getTickCount() e2 = cv2.getTickCount()
t = (e2 - e1)/cv2.getTickFrequency() t = (e2 - e1)/cv2.getTickFrequency()
print t print( t )
# Result I got is 0.521107655 seconds # Result I got is 0.521107655 seconds
@endcode @endcode
......
...@@ -69,8 +69,8 @@ kp = star.detect(img,None) ...@@ -69,8 +69,8 @@ kp = star.detect(img,None)
# compute the descriptors with BRIEF # compute the descriptors with BRIEF
kp, des = brief.compute(img, kp) kp, des = brief.compute(img, kp)
print brief.descriptorSize() print( brief.descriptorSize() )
print des.shape print( des.shape )
@endcode @endcode
The function brief.getDescriptorSize() gives the \f$n_d\f$ size used in bytes. By default it is 32. Next one The function brief.getDescriptorSize() gives the \f$n_d\f$ size used in bytes. By default it is 32. Next one
is matching, which will be done in another chapter. is matching, which will be done in another chapter.
......
...@@ -108,10 +108,10 @@ kp = fast.detect(img,None) ...@@ -108,10 +108,10 @@ kp = fast.detect(img,None)
img2 = cv2.drawKeypoints(img, kp, None, color=(255,0,0)) img2 = cv2.drawKeypoints(img, kp, None, color=(255,0,0))
# Print all default params # Print all default params
print "Threshold: ", fast.getThreshold() print( "Threshold: {}".format(fast.getThreshold()) )
print "nonmaxSuppression: ", fast.getNonmaxSuppression() print( "nonmaxSuppression:{}".format(fast.getNonmaxSuppression()) )
print "neighborhood: ", fast.getType() print( "neighborhood: {}".format(fast.getType()) )
print "Total Keypoints with nonmaxSuppression: ", len(kp) print( "Total Keypoints with nonmaxSuppression: {}".format(len(kp)) )
cv2.imwrite('fast_true.png',img2) cv2.imwrite('fast_true.png',img2)
...@@ -119,7 +119,7 @@ cv2.imwrite('fast_true.png',img2) ...@@ -119,7 +119,7 @@ cv2.imwrite('fast_true.png',img2)
fast.setNonmaxSuppression(0) fast.setNonmaxSuppression(0)
kp = fast.detect(img,None) kp = fast.detect(img,None)
print "Total Keypoints without nonmaxSuppression: ", len(kp) print( "Total Keypoints without nonmaxSuppression: {}".format(len(kp)) )
img3 = cv2.drawKeypoints(img, kp, None, color=(255,0,0)) img3 = cv2.drawKeypoints(img, kp, None, color=(255,0,0))
......
...@@ -85,7 +85,7 @@ if len(good)>MIN_MATCH_COUNT: ...@@ -85,7 +85,7 @@ if len(good)>MIN_MATCH_COUNT:
img2 = cv2.polylines(img2,[np.int32(dst)],True,255,3, cv2.LINE_AA) img2 = cv2.polylines(img2,[np.int32(dst)],True,255,3, cv2.LINE_AA)
else: else:
print "Not enough matches are found - %d/%d" % (len(good),MIN_MATCH_COUNT) print( "Not enough matches are found - {}/{}".format(len(good), MIN_MATCH_COUNT) )
matchesMask = None matchesMask = None
@endcode @endcode
Finally we draw our inliers (if successfully found the object) or matching keypoints (if failed). Finally we draw our inliers (if successfully found the object) or matching keypoints (if failed).
......
...@@ -92,7 +92,7 @@ examples are shown in Python terminal since it is just same as SIFT only. ...@@ -92,7 +92,7 @@ examples are shown in Python terminal since it is just same as SIFT only.
While matching, we may need all those features, but not now. So we increase the Hessian Threshold. While matching, we may need all those features, but not now. So we increase the Hessian Threshold.
@code{.py} @code{.py}
# Check present Hessian threshold # Check present Hessian threshold
>>> print surf.getHessianThreshold() >>> print( surf.getHessianThreshold() )
400.0 400.0
# We set it to some 50000. Remember, it is just for representing in picture. # We set it to some 50000. Remember, it is just for representing in picture.
...@@ -102,7 +102,7 @@ While matching, we may need all those features, but not now. So we increase the ...@@ -102,7 +102,7 @@ While matching, we may need all those features, but not now. So we increase the
# Again compute keypoints and check its number. # Again compute keypoints and check its number.
>>> kp, des = surf.detectAndCompute(img,None) >>> kp, des = surf.detectAndCompute(img,None)
>>> print len(kp) >>> print( len(kp) )
47 47
@endcode @endcode
It is less than 50. Let's draw it on the image. It is less than 50. Let's draw it on the image.
...@@ -119,7 +119,7 @@ on wings of butterfly. You can test it with other images. ...@@ -119,7 +119,7 @@ on wings of butterfly. You can test it with other images.
Now I want to apply U-SURF, so that it won't find the orientation. Now I want to apply U-SURF, so that it won't find the orientation.
@code{.py} @code{.py}
# Check upright flag, if it False, set it to True # Check upright flag, if it False, set it to True
>>> print surf.getUpright() >>> print( surf.getUpright() )
False False
>>> surf.setUpright(True) >>> surf.setUpright(True)
...@@ -139,7 +139,7 @@ etc, this is better. ...@@ -139,7 +139,7 @@ etc, this is better.
Finally we check the descriptor size and change it to 128 if it is only 64-dim. Finally we check the descriptor size and change it to 128 if it is only 64-dim.
@code{.py} @code{.py}
# Find size of descriptor # Find size of descriptor
>>> print surf.descriptorSize() >>> print( surf.descriptorSize() )
64 64
# That means flag, "extended" is False. # That means flag, "extended" is False.
...@@ -149,9 +149,9 @@ Finally we check the descriptor size and change it to 128 if it is only 64-dim. ...@@ -149,9 +149,9 @@ Finally we check the descriptor size and change it to 128 if it is only 64-dim.
# So we make it to True to get 128-dim descriptors. # So we make it to True to get 128-dim descriptors.
>>> surf.extended = True >>> surf.extended = True
>>> kp, des = surf.detectAndCompute(img,None) >>> kp, des = surf.detectAndCompute(img,None)
>>> print surf.descriptorSize() >>> print( surf.descriptorSize() )
128 128
>>> print des.shape >>> print( des.shape )
(47, 128) (47, 128)
@endcode @endcode
Remaining part is matching which we will do in another chapter. Remaining part is matching which we will do in another chapter.
......
...@@ -21,7 +21,7 @@ in Python terminal: ...@@ -21,7 +21,7 @@ in Python terminal:
@code{.py} @code{.py}
import cv2 import cv2
events = [i for i in dir(cv2) if 'EVENT' in i] events = [i for i in dir(cv2) if 'EVENT' in i]
print events print( events )
@endcode @endcode
Creating mouse callback function has a specific format which is same everywhere. It differs only in Creating mouse callback function has a specific format which is same everywhere. It differs only in
what the function does. So our mouse callback function does one thing, it draws a circle where we what the function does. So our mouse callback function does one thing, it draws a circle where we
......
...@@ -24,7 +24,7 @@ commands in your Python terminal : ...@@ -24,7 +24,7 @@ commands in your Python terminal :
@code{.py} @code{.py}
>>> import cv2 >>> import cv2
>>> flags = [i for i in dir(cv2) if i.startswith('COLOR_')] >>> flags = [i for i in dir(cv2) if i.startswith('COLOR_')]
>>> print flags >>> print( flags )
@endcode @endcode
@note For HSV, Hue range is [0,179], Saturation range is [0,255] and Value range is [0,255]. @note For HSV, Hue range is [0,179], Saturation range is [0,255] and Value range is [0,255].
Different softwares use different scales. So if you are comparing OpenCV values with them, you need Different softwares use different scales. So if you are comparing OpenCV values with them, you need
...@@ -96,7 +96,7 @@ terminal: ...@@ -96,7 +96,7 @@ terminal:
@code{.py} @code{.py}
>>> green = np.uint8([[[0,255,0 ]]]) >>> green = np.uint8([[[0,255,0 ]]])
>>> hsv_green = cv2.cvtColor(green,cv2.COLOR_BGR2HSV) >>> hsv_green = cv2.cvtColor(green,cv2.COLOR_BGR2HSV)
>>> print hsv_green >>> print( hsv_green )
[[[ 60 255 255]]] [[[ 60 255 255]]]
@endcode @endcode
Now you take [H-10, 100,100] and [H+10, 255, 255] as lower bound and upper bound respectively. Apart Now you take [H-10, 100,100] and [H+10, 255, 255] as lower bound and upper bound respectively. Apart
......
...@@ -27,7 +27,7 @@ im2,contours,hierarchy = cv2.findContours(thresh, 1, 2) ...@@ -27,7 +27,7 @@ im2,contours,hierarchy = cv2.findContours(thresh, 1, 2)
cnt = contours[0] cnt = contours[0]
M = cv2.moments(cnt) M = cv2.moments(cnt)
print M print( M )
@endcode @endcode
From this moments, you can extract useful data like area, centroid etc. Centroid is given by the From this moments, you can extract useful data like area, centroid etc. Centroid is given by the
relations, \f$C_x = \frac{M_{10}}{M_{00}}\f$ and \f$C_y = \frac{M_{01}}{M_{00}}\f$. This can be done as relations, \f$C_x = \frac{M_{10}}{M_{00}}\f$ and \f$C_y = \frac{M_{01}}{M_{00}}\f$. This can be done as
......
...@@ -99,7 +99,7 @@ im2,contours,hierarchy = cv2.findContours(thresh2,2,1) ...@@ -99,7 +99,7 @@ im2,contours,hierarchy = cv2.findContours(thresh2,2,1)
cnt2 = contours[0] cnt2 = contours[0]
ret = cv2.matchShapes(cnt1,cnt2,1,0.0) ret = cv2.matchShapes(cnt1,cnt2,1,0.0)
print ret print( ret )
@endcode @endcode
I tried matching shapes with different shapes given below: I tried matching shapes with different shapes given below:
......
...@@ -218,7 +218,7 @@ for i in xrange(1,256): ...@@ -218,7 +218,7 @@ for i in xrange(1,256):
# find otsu's threshold value with OpenCV function # find otsu's threshold value with OpenCV function
ret, otsu = cv2.threshold(blur,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU) ret, otsu = cv2.threshold(blur,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
print thresh,ret print( "{} {}".format(thresh,ret) )
@endcode @endcode
*(Some of the functions may be new here, but we will cover them in coming chapters)* *(Some of the functions may be new here, but we will cover them in coming chapters)*
......
...@@ -186,12 +186,12 @@ using IPython magic command %timeit. ...@@ -186,12 +186,12 @@ using IPython magic command %timeit.
@code{.py} @code{.py}
In [16]: img = cv2.imread('messi5.jpg',0) In [16]: img = cv2.imread('messi5.jpg',0)
In [17]: rows,cols = img.shape In [17]: rows,cols = img.shape
In [18]: print rows,cols In [18]: print("{} {}".format(rows,cols))
342 548 342 548
In [19]: nrows = cv2.getOptimalDFTSize(rows) In [19]: nrows = cv2.getOptimalDFTSize(rows)
In [20]: ncols = cv2.getOptimalDFTSize(cols) In [20]: ncols = cv2.getOptimalDFTSize(cols)
In [21]: print nrows, ncols In [21]: print("{} {}".format(nrows,ncols))
360 576 360 576
@endcode @endcode
See, the size (342,548) is modified to (360, 576). Now let's pad it with zeros (for OpenCV) and find See, the size (342,548) is modified to (360, 576). Now let's pad it with zeros (for OpenCV) and find
......
...@@ -51,7 +51,7 @@ ret,result,neighbours,dist = knn.findNearest(test,k=5) ...@@ -51,7 +51,7 @@ ret,result,neighbours,dist = knn.findNearest(test,k=5)
matches = result==test_labels matches = result==test_labels
correct = np.count_nonzero(matches) correct = np.count_nonzero(matches)
accuracy = correct*100.0/result.size accuracy = correct*100.0/result.size
print accuracy print( accuracy )
@endcode @endcode
So our basic OCR app is ready. This particular example gave me an accuracy of 91%. One option So our basic OCR app is ready. This particular example gave me an accuracy of 91%. One option
improve accuracy is to add more data for training, especially the wrong ones. So instead of finding improve accuracy is to add more data for training, especially the wrong ones. So instead of finding
...@@ -64,7 +64,7 @@ np.savez('knn_data.npz',train=train, train_labels=train_labels) ...@@ -64,7 +64,7 @@ np.savez('knn_data.npz',train=train, train_labels=train_labels)
# Now load the data # Now load the data
with np.load('knn_data.npz') as data: with np.load('knn_data.npz') as data:
print data.files print( data.files )
train = data['train'] train = data['train']
train_labels = data['train_labels'] train_labels = data['train_labels']
@endcode @endcode
...@@ -109,7 +109,7 @@ ret, result, neighbours, dist = knn.findNearest(testData, k=5) ...@@ -109,7 +109,7 @@ ret, result, neighbours, dist = knn.findNearest(testData, k=5)
correct = np.count_nonzero(result == labels) correct = np.count_nonzero(result == labels)
accuracy = correct*100.0/10000 accuracy = correct*100.0/10000
print accuracy print( accuracy )
@endcode @endcode
It gives me an accuracy of 93.22%. Again, if you want to increase accuracy, you can iteratively add It gives me an accuracy of 93.22%. Again, if you want to increase accuracy, you can iteratively add
error data in each level. error data in each level.
......
...@@ -118,9 +118,9 @@ knn = cv2.ml.KNearest_create() ...@@ -118,9 +118,9 @@ knn = cv2.ml.KNearest_create()
knn.train(trainData, cv2.ml.ROW_SAMPLE, responses) knn.train(trainData, cv2.ml.ROW_SAMPLE, responses)
ret, results, neighbours ,dist = knn.findNearest(newcomer, 3) ret, results, neighbours ,dist = knn.findNearest(newcomer, 3)
print "result: ", results,"\n" print( "result: {}\n".format(results) )
print "neighbours: ", neighbours,"\n" print( "neighbours: {}\n".format(neighbours) )
print "distance: ", dist print( "distance: {}\n".format(dist) )
plt.show() plt.show()
@endcode @endcode
......
...@@ -30,7 +30,7 @@ $ yum install numpy opencv* ...@@ -30,7 +30,7 @@ $ yum install numpy opencv*
Open Python IDLE (or IPython) and type following codes in Python terminal. Open Python IDLE (or IPython) and type following codes in Python terminal.
@code{.py} @code{.py}
>>> import cv2 >>> import cv2
>>> print cv2.__version__ >>> print( cv2.__version__ )
@endcode @endcode
If the results are printed out without any errors, congratulations !!! You have installed If the results are printed out without any errors, congratulations !!! You have installed
OpenCV-Python successfully. OpenCV-Python successfully.
...@@ -218,7 +218,7 @@ Installation is over. All files are installed in /usr/local/ folder. But to use ...@@ -218,7 +218,7 @@ Installation is over. All files are installed in /usr/local/ folder. But to use
should be able to find OpenCV module. You have two options for that. should be able to find OpenCV module. You have two options for that.
-# **Move the module to any folder in Python Path** : Python path can be found out by entering -# **Move the module to any folder in Python Path** : Python path can be found out by entering
import sys;print sys.path in Python terminal. It will print out many locations. Move `import sys; print(sys.path)` in Python terminal. It will print out many locations. Move
/usr/local/lib/python2.7/site-packages/cv2.so to any of this folder. For example, /usr/local/lib/python2.7/site-packages/cv2.so to any of this folder. For example,
@code{.sh} @code{.sh}
su mv /usr/local/lib/python2.7/site-packages/cv2.so /usr/lib/python2.7/site-packages su mv /usr/local/lib/python2.7/site-packages/cv2.so /usr/lib/python2.7/site-packages
......
...@@ -36,7 +36,7 @@ Installing OpenCV from prebuilt binaries ...@@ -36,7 +36,7 @@ Installing OpenCV from prebuilt binaries
-# Open Python IDLE and type following codes in Python terminal. -# Open Python IDLE and type following codes in Python terminal.
@code @code
>>> import cv2 >>> import cv2
>>> print cv2.__version__ >>> print( cv2.__version__ )
@endcode @endcode
If the results are printed out without any errors, congratulations !!! You have installed If the results are printed out without any errors, congratulations !!! You have installed
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment