mirror of
https://github.com/opencv/opencv.git
synced 2024-11-27 20:50:25 +08:00
Update python samples
This commit is contained in:
parent
0a7a54f312
commit
9b399f3960
@ -30,6 +30,7 @@ def main():
|
|||||||
circles = cv.HoughCircles(img, cv.HOUGH_GRADIENT, 1, 10, np.array([]), 100, 30, 1, 30)
|
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
|
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
|
_a, b, _c = circles.shape
|
||||||
for i in range(b):
|
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)
|
cv.circle(cimg, (circles[0][i][0], circles[0][i][1]), circles[0][i][2], (0, 0, 255), 3, cv.LINE_AA)
|
||||||
|
@ -38,10 +38,10 @@ def Hist_and_Backproj(val):
|
|||||||
|
|
||||||
## [Read the image]
|
## [Read the image]
|
||||||
parser = argparse.ArgumentParser(description='Code for Back Projection tutorial.')
|
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()
|
args = parser.parse_args()
|
||||||
|
|
||||||
src = cv.imread(args.input)
|
src = cv.imread(cv.samples.findFile(args.input))
|
||||||
if src is None:
|
if src is None:
|
||||||
print('Could not open or find the image:', args.input)
|
print('Could not open or find the image:', args.input)
|
||||||
exit(0)
|
exit(0)
|
||||||
|
@ -54,10 +54,10 @@ def Hist_and_Backproj(mask):
|
|||||||
|
|
||||||
# Read the image
|
# Read the image
|
||||||
parser = argparse.ArgumentParser(description='Code for Back Projection tutorial.')
|
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()
|
args = parser.parse_args()
|
||||||
|
|
||||||
src = cv.imread(args.input)
|
src = cv.imread(cv.samples.findFile(args.input))
|
||||||
if src is None:
|
if src is None:
|
||||||
print('Could not open or find the image:', args.input)
|
print('Could not open or find the image:', args.input)
|
||||||
exit(0)
|
exit(0)
|
||||||
|
@ -53,14 +53,14 @@ cv.normalize(r_hist, r_hist, alpha=0, beta=hist_h, norm_type=cv.NORM_MINMAX)
|
|||||||
|
|
||||||
## [Draw for each channel]
|
## [Draw for each channel]
|
||||||
for i in range(1, histSize):
|
for i in range(1, histSize):
|
||||||
cv.line(histImage, ( bin_w*(i-1), hist_h - int(round(b_hist[i-1])) ),
|
cv.line(histImage, ( bin_w*(i-1), hist_h - int(b_hist[i-1]) ),
|
||||||
( bin_w*(i), hist_h - int(round(b_hist[i])) ),
|
( bin_w*(i), hist_h - int(b_hist[i]) ),
|
||||||
( 255, 0, 0), thickness=2)
|
( 255, 0, 0), thickness=2)
|
||||||
cv.line(histImage, ( bin_w*(i-1), hist_h - int(round(g_hist[i-1])) ),
|
cv.line(histImage, ( bin_w*(i-1), hist_h - int(g_hist[i-1]) ),
|
||||||
( bin_w*(i), hist_h - int(round(g_hist[i])) ),
|
( bin_w*(i), hist_h - int(g_hist[i]) ),
|
||||||
( 0, 255, 0), thickness=2)
|
( 0, 255, 0), thickness=2)
|
||||||
cv.line(histImage, ( bin_w*(i-1), hist_h - int(round(r_hist[i-1])) ),
|
cv.line(histImage, ( bin_w*(i-1), hist_h - int(r_hist[i-1]) ),
|
||||||
( bin_w*(i), hist_h - int(round(r_hist[i])) ),
|
( bin_w*(i), hist_h - int(r_hist[i]) ),
|
||||||
( 0, 0, 255), thickness=2)
|
( 0, 0, 255), thickness=2)
|
||||||
## [Draw for each channel]
|
## [Draw for each channel]
|
||||||
|
|
||||||
|
@ -30,7 +30,7 @@ def goodFeaturesToTrack_Demo(val):
|
|||||||
print('** Number of corners detected:', corners.shape[0])
|
print('** Number of corners detected:', corners.shape[0])
|
||||||
radius = 4
|
radius = 4
|
||||||
for i in range(corners.shape[0]):
|
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
|
# Show what you got
|
||||||
cv.namedWindow(source_window)
|
cv.namedWindow(source_window)
|
||||||
|
@ -30,7 +30,7 @@ def goodFeaturesToTrack_Demo(val):
|
|||||||
print('** Number of corners detected:', corners.shape[0])
|
print('** Number of corners detected:', corners.shape[0])
|
||||||
radius = 4
|
radius = 4
|
||||||
for i in range(corners.shape[0]):
|
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
|
# Show what you got
|
||||||
cv.namedWindow(source_window)
|
cv.namedWindow(source_window)
|
||||||
|
@ -53,7 +53,7 @@ thickness = 2
|
|||||||
sv = svm.getUncompressedSupportVectors()
|
sv = svm.getUncompressedSupportVectors()
|
||||||
|
|
||||||
for i in range(sv.shape[0]):
|
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]
|
## [show_vectors]
|
||||||
|
|
||||||
cv.imwrite('result.png', image) # save the image
|
cv.imwrite('result.png', image) # save the image
|
||||||
|
@ -94,13 +94,13 @@ thick = -1
|
|||||||
for i in range(NTRAINING_SAMPLES):
|
for i in range(NTRAINING_SAMPLES):
|
||||||
px = trainData[i,0]
|
px = trainData[i,0]
|
||||||
py = trainData[i,1]
|
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
|
# Class 2
|
||||||
for i in range(NTRAINING_SAMPLES, 2*NTRAINING_SAMPLES):
|
for i in range(NTRAINING_SAMPLES, 2*NTRAINING_SAMPLES):
|
||||||
px = trainData[i,0]
|
px = trainData[i,0]
|
||||||
py = trainData[i,1]
|
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]
|
## [show_data]
|
||||||
|
|
||||||
#------------------------- 6. Show support vectors --------------------------------------------
|
#------------------------- 6. Show support vectors --------------------------------------------
|
||||||
@ -109,7 +109,7 @@ thick = 2
|
|||||||
sv = svm.getUncompressedSupportVectors()
|
sv = svm.getUncompressedSupportVectors()
|
||||||
|
|
||||||
for i in range(sv.shape[0]):
|
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]
|
## [show_vectors]
|
||||||
|
|
||||||
cv.imwrite('result.png', I) # save the Image
|
cv.imwrite('result.png', I) # save the Image
|
||||||
|
@ -33,7 +33,7 @@ def hog(img):
|
|||||||
return hist
|
return hist
|
||||||
## [hog]
|
## [hog]
|
||||||
|
|
||||||
img = cv.imread('digits.png',0)
|
img = cv.imread(cv.samples.findFile('digits.png'),0)
|
||||||
if img is None:
|
if img is None:
|
||||||
raise Exception("we need the digits.png image from samples/data here !")
|
raise Exception("we need the digits.png image from samples/data here !")
|
||||||
|
|
||||||
|
@ -40,15 +40,16 @@ while(1):
|
|||||||
p1, st, err = cv.calcOpticalFlowPyrLK(old_gray, frame_gray, p0, None, **lk_params)
|
p1, st, err = cv.calcOpticalFlowPyrLK(old_gray, frame_gray, p0, None, **lk_params)
|
||||||
|
|
||||||
# Select good points
|
# Select good points
|
||||||
good_new = p1[st==1]
|
if p1 is not None:
|
||||||
good_old = p0[st==1]
|
good_new = p1[st==1]
|
||||||
|
good_old = p0[st==1]
|
||||||
|
|
||||||
# draw the tracks
|
# draw the tracks
|
||||||
for i,(new,old) in enumerate(zip(good_new, good_old)):
|
for i,(new,old) in enumerate(zip(good_new, good_old)):
|
||||||
a,b = new.ravel()
|
a,b = new.ravel()
|
||||||
c,d = old.ravel()
|
c,d = old.ravel()
|
||||||
mask = cv.line(mask, (a,b),(c,d), color[i].tolist(), 2)
|
mask = cv.line(mask, (int(a),int(b)),(int(c),int(d)), color[i].tolist(), 2)
|
||||||
frame = cv.circle(frame,(a,b),5,color[i].tolist(),-1)
|
frame = cv.circle(frame,(int(a),int(b)),5,color[i].tolist(),-1)
|
||||||
img = cv.add(frame,mask)
|
img = cv.add(frame,mask)
|
||||||
|
|
||||||
cv.imshow('frame',img)
|
cv.imshow('frame',img)
|
||||||
|
@ -86,8 +86,8 @@ def main():
|
|||||||
|
|
||||||
framenum = -1 # Frame counter
|
framenum = -1 # Frame counter
|
||||||
|
|
||||||
captRefrnc = cv.VideoCapture(sourceReference)
|
captRefrnc = cv.VideoCapture(cv.samples.findFileOrKeep(sourceReference))
|
||||||
captUndTst = cv.VideoCapture(sourceCompareWith)
|
captUndTst = cv.VideoCapture(cv.samples.findFileOrKeep(sourceCompareWith))
|
||||||
|
|
||||||
if not captRefrnc.isOpened():
|
if not captRefrnc.isOpened():
|
||||||
print("Could not open the reference " + sourceReference)
|
print("Could not open the reference " + sourceReference)
|
||||||
|
Loading…
Reference in New Issue
Block a user