Update python samples
authorSuleyman TURKMEN <sturkmen@hotmail.com>
Wed, 17 Feb 2021 09:04:49 +0000 (12:04 +0300)
committerSuleyman TURKMEN <sturkmen@hotmail.com>
Sun, 21 Feb 2021 20:02:16 +0000 (23:02 +0300)
samples/python/houghcircles.py
samples/python/tutorial_code/Histograms_Matching/back_projection/calcBackProject_Demo1.py
samples/python/tutorial_code/Histograms_Matching/back_projection/calcBackProject_Demo2.py
samples/python/tutorial_code/Histograms_Matching/histogram_calculation/calcHist_Demo.py
samples/python/tutorial_code/TrackingMotion/corner_subpixels/cornerSubPix_Demo.py
samples/python/tutorial_code/TrackingMotion/good_features_to_track/goodFeaturesToTrack_Demo.py
samples/python/tutorial_code/ml/introduction_to_svm/introduction_to_svm.py
samples/python/tutorial_code/ml/non_linear_svms/non_linear_svms.py
samples/python/tutorial_code/ml/py_svm_opencv/hogsvm.py
samples/python/tutorial_code/video/optical_flow/optical_flow.py
samples/python/tutorial_code/videoio/video-input-psnr-ssim.py

index 416309aab0fd0e4c60da80351914bed5978b26c9..60a36714fe37c5f691eafc8602a45e5edff3336f 100755 (executable)
@@ -30,6 +30,7 @@ def main():
     circles = cv.HoughCircles(img, cv.HOUGH_GRADIENT, 1, 10, np.array([]), 100, 30, 1, 30)
 
     if circles is not None: # Check if circles have been found and only then iterate over these and add them to the image
+        circles = np.uint16(np.around(circles))
         _a, b, _c = circles.shape
         for i in range(b):
             cv.circle(cimg, (circles[0][i][0], circles[0][i][1]), circles[0][i][2], (0, 0, 255), 3, cv.LINE_AA)
index fc85aca3399a9f325328a2e7c2e19af6d272d9ee..8086b9327e6c44ce3c08f451d32dd72b3b8cc7c2 100644 (file)
@@ -38,10 +38,10 @@ def Hist_and_Backproj(val):
 
 ## [Read the image]
 parser = argparse.ArgumentParser(description='Code for Back Projection tutorial.')
-parser.add_argument('--input', help='Path to input image.')
+parser.add_argument('--input', help='Path to input image.', default='home.jpg')
 args = parser.parse_args()
 
-src = cv.imread(args.input)
+src = cv.imread(cv.samples.findFile(args.input))
 if src is None:
     print('Could not open or find the image:', args.input)
     exit(0)
index 24255a2bb8ac5d89e69cdacdc182030fde47c9b5..42d5a9b81f324346822e1a568a94352ac0790067 100644 (file)
@@ -54,10 +54,10 @@ def Hist_and_Backproj(mask):
 
 # Read the image
 parser = argparse.ArgumentParser(description='Code for Back Projection tutorial.')
-parser.add_argument('--input', help='Path to input image.')
+parser.add_argument('--input', help='Path to input image.', default='home.jpg')
 args = parser.parse_args()
 
-src = cv.imread(args.input)
+src = cv.imread(cv.samples.findFile(args.input))
 if src is None:
     print('Could not open or find the image:', args.input)
     exit(0)
index 992ca90141fbbde8e4c0bcf6c6cef2c156457941..1789d4935835ce419d5f880605da46ff72713e4b 100644 (file)
@@ -53,14 +53,14 @@ cv.normalize(r_hist, r_hist, alpha=0, beta=hist_h, norm_type=cv.NORM_MINMAX)
 
 ## [Draw for each channel]
 for i in range(1, histSize):
-    cv.line(histImage, ( bin_w*(i-1), hist_h - int(round(b_hist[i-1])) ),
-            ( bin_w*(i), hist_h - int(round(b_hist[i])) ),
+    cv.line(histImage, ( bin_w*(i-1), hist_h - int(b_hist[i-1]) ),
+            ( bin_w*(i), hist_h - int(b_hist[i]) ),
             ( 255, 0, 0), thickness=2)
-    cv.line(histImage, ( bin_w*(i-1), hist_h - int(round(g_hist[i-1])) ),
-            ( bin_w*(i), hist_h - int(round(g_hist[i])) ),
+    cv.line(histImage, ( bin_w*(i-1), hist_h - int(g_hist[i-1]) ),
+            ( bin_w*(i), hist_h - int(g_hist[i]) ),
             ( 0, 255, 0), thickness=2)
-    cv.line(histImage, ( bin_w*(i-1), hist_h - int(round(r_hist[i-1])) ),
-            ( bin_w*(i), hist_h - int(round(r_hist[i])) ),
+    cv.line(histImage, ( bin_w*(i-1), hist_h - int(r_hist[i-1]) ),
+            ( bin_w*(i), hist_h - int(r_hist[i]) ),
             ( 0, 0, 255), thickness=2)
 ## [Draw for each channel]
 
index 6d8738b310da4daf4415b556314165fbdcbaaa50..37e38abd44405c28e964c221124ce2386d698517 100644 (file)
@@ -30,7 +30,7 @@ def goodFeaturesToTrack_Demo(val):
     print('** Number of corners detected:', corners.shape[0])
     radius = 4
     for i in range(corners.shape[0]):
-        cv.circle(copy, (corners[i,0,0], corners[i,0,1]), radius, (rng.randint(0,256), rng.randint(0,256), rng.randint(0,256)), cv.FILLED)
+        cv.circle(copy, (int(corners[i,0,0]), int(corners[i,0,1])), radius, (rng.randint(0,256), rng.randint(0,256), rng.randint(0,256)), cv.FILLED)
 
     # Show what you got
     cv.namedWindow(source_window)
index 3fb8441d92b2a27428ccfc182df642b0270d590a..ef082bddc13540910a4e1e10f7d73fe20a9444dd 100644 (file)
@@ -30,7 +30,7 @@ def goodFeaturesToTrack_Demo(val):
     print('** Number of corners detected:', corners.shape[0])
     radius = 4
     for i in range(corners.shape[0]):
-        cv.circle(copy, (corners[i,0,0], corners[i,0,1]), radius, (rng.randint(0,256), rng.randint(0,256), rng.randint(0,256)), cv.FILLED)
+        cv.circle(copy, (int(corners[i,0,0]), int(corners[i,0,1])), radius, (rng.randint(0,256), rng.randint(0,256), rng.randint(0,256)), cv.FILLED)
 
     # Show what you got
     cv.namedWindow(source_window)
index 1a5f20242069f86bb19822a31ec4d4ae80933197..eeb246bc3893e893121e49e95661742d502f737e 100644 (file)
@@ -53,7 +53,7 @@ thickness = 2
 sv = svm.getUncompressedSupportVectors()
 
 for i in range(sv.shape[0]):
-    cv.circle(image, (sv[i,0], sv[i,1]), 6, (128, 128, 128), thickness)
+    cv.circle(image, (int(sv[i,0]), int(sv[i,1])), 6, (128, 128, 128), thickness)
 ## [show_vectors]
 
 cv.imwrite('result.png', image) # save the image
index fc4b56c45443f421ebaba0ffce8fc6a94eea39b1..a88ac4bd1b1cfaad88b12cc65c61cd797aac2984 100644 (file)
@@ -94,13 +94,13 @@ thick = -1
 for i in range(NTRAINING_SAMPLES):
     px = trainData[i,0]
     py = trainData[i,1]
-    cv.circle(I, (px, py), 3, (0, 255, 0), thick)
+    cv.circle(I, (int(px), int(py)), 3, (0, 255, 0), thick)
 
 # Class 2
 for i in range(NTRAINING_SAMPLES, 2*NTRAINING_SAMPLES):
     px = trainData[i,0]
     py = trainData[i,1]
-    cv.circle(I, (px, py), 3, (255, 0, 0), thick)
+    cv.circle(I, (int(px), int(py)), 3, (255, 0, 0), thick)
 ## [show_data]
 
 #------------------------- 6. Show support vectors --------------------------------------------
@@ -109,7 +109,7 @@ thick = 2
 sv = svm.getUncompressedSupportVectors()
 
 for i in range(sv.shape[0]):
-    cv.circle(I, (sv[i,0], sv[i,1]), 6, (128, 128, 128), thick)
+    cv.circle(I, (int(sv[i,0]), int(sv[i,1])), 6, (128, 128, 128), thick)
 ## [show_vectors]
 
 cv.imwrite('result.png', I)                      # save the Image
index 79333a4f94a4db202298e03e8159f651535fd06c..898c7dc4d7c1ccfeb1daa6724be2ffa931ce4efb 100755 (executable)
@@ -33,7 +33,7 @@ def hog(img):
     return hist
 ## [hog]
 
-img = cv.imread('digits.png',0)
+img = cv.imread(cv.samples.findFile('digits.png'),0)
 if img is None:
     raise Exception("we need the digits.png image from samples/data here !")
 
index c367407e45d5f40eefff75bf4f54377a6e5e26d1..93bb2c421ecf5a21c450e983eb8913b68679f692 100644 (file)
@@ -40,15 +40,16 @@ while(1):
     p1, st, err = cv.calcOpticalFlowPyrLK(old_gray, frame_gray, p0, None, **lk_params)
 
     # Select good points
-    good_new = p1[st==1]
-    good_old = p0[st==1]
+    if p1 is not None:
+        good_new = p1[st==1]
+        good_old = p0[st==1]
 
     # draw the tracks
     for i,(new,old) in enumerate(zip(good_new, good_old)):
         a,b = new.ravel()
         c,d = old.ravel()
-        mask = cv.line(mask, (a,b),(c,d), color[i].tolist(), 2)
-        frame = cv.circle(frame,(a,b),5,color[i].tolist(),-1)
+        mask = cv.line(mask, (int(a),int(b)),(int(c),int(d)), color[i].tolist(), 2)
+        frame = cv.circle(frame,(int(a),int(b)),5,color[i].tolist(),-1)
     img = cv.add(frame,mask)
 
     cv.imshow('frame',img)
index 84610d476833985c3ed96ec8ffde93964416df52..973854feb6f486fb4a86161311f9246833c28f6b 100644 (file)
@@ -86,8 +86,8 @@ def main():
 
     framenum = -1 # Frame counter
 
-    captRefrnc = cv.VideoCapture(sourceReference)
-    captUndTst = cv.VideoCapture(sourceCompareWith)
+    captRefrnc = cv.VideoCapture(cv.samples.findFileOrKeep(sourceReference))
+    captUndTst = cv.VideoCapture(cv.samples.findFileOrKeep(sourceCompareWith))
 
     if not captRefrnc.isOpened():
         print("Could not open the reference " + sourceReference)