mirror of
https://github.com/opencv/opencv.git
synced 2025-01-06 02:08:12 +08:00
3dcc8c38b4
Removed obsolete python samples #25268 Clean Samples #25006 This PR removes 36 obsolete python samples from the project, as part of an effort to keep the codebase clean and focused on current best practices. Some of these samples will be updated with latest algorithms or will be combined with other existing samples. Removed Samples: > browse.py camshift.py coherence.py color_histogram.py contours.py deconvolution.py dft.py dis_opt_flow.py distrans.py edge.py feature_homography.py find_obj.py fitline.py gabor_threads.py hist.py houghcircles.py houghlines.py inpaint.py kalman.py kmeans.py laplace.py lk_homography.py lk_track.py logpolar.py mosse.py mser.py opt_flow.py plane_ar.py squares.py stitching.py text_skewness_correction.py texture_flow.py turing.py video_threaded.py video_v4l2.py watershed.py These changes aim to improve the repository's clarity and usability by removing examples that are no longer relevant or have been superseded by more up-to-date techniques.
56 lines
1.7 KiB
Python
Executable File
56 lines
1.7 KiB
Python
Executable File
#!/usr/bin/env python
|
|
|
|
'''
|
|
Simple "Square Detector" program.
|
|
|
|
Loads several images sequentially and tries to find squares in each image.
|
|
'''
|
|
|
|
import numpy as np
|
|
import cv2 as cv
|
|
|
|
|
|
def angle_cos(p0, p1, p2):
|
|
d1, d2 = (p0-p1).astype('float'), (p2-p1).astype('float')
|
|
return abs( np.dot(d1, d2) / np.sqrt( np.dot(d1, d1)*np.dot(d2, d2) ) )
|
|
|
|
def find_squares(img):
|
|
img = cv.GaussianBlur(img, (5, 5), 0)
|
|
squares = []
|
|
for gray in cv.split(img):
|
|
for thrs in range(0, 255, 26):
|
|
if thrs == 0:
|
|
bin = cv.Canny(gray, 0, 50, apertureSize=5)
|
|
bin = cv.dilate(bin, None)
|
|
else:
|
|
_retval, bin = cv.threshold(gray, thrs, 255, cv.THRESH_BINARY)
|
|
contours, _hierarchy = cv.findContours(bin, cv.RETR_LIST, cv.CHAIN_APPROX_SIMPLE)
|
|
for cnt in contours:
|
|
cnt_len = cv.arcLength(cnt, True)
|
|
cnt = cv.approxPolyDP(cnt, 0.02*cnt_len, True)
|
|
if len(cnt) == 4 and cv.contourArea(cnt) > 1000 and cv.isContourConvex(cnt):
|
|
cnt = cnt.reshape(-1, 2)
|
|
max_cos = np.max([angle_cos( cnt[i], cnt[(i+1) % 4], cnt[(i+2) % 4] ) for i in range(4)])
|
|
if max_cos < 0.1:
|
|
squares.append(cnt)
|
|
return squares
|
|
|
|
def main():
|
|
from glob import glob
|
|
for fn in glob('../data/pic*.png'):
|
|
img = cv.imread(fn)
|
|
squares = find_squares(img)
|
|
cv.drawContours( img, squares, -1, (0, 255, 0), 3 )
|
|
cv.imshow('squares', img)
|
|
ch = cv.waitKey()
|
|
if ch == 27:
|
|
break
|
|
|
|
print('Done')
|
|
|
|
|
|
if __name__ == '__main__':
|
|
print(__doc__)
|
|
main()
|
|
cv.destroyAllWindows()
|