Skip to content

Instantly share code, notes, and snippets.

View garybradski's full-sized avatar

Gary Bradski garybradski

View GitHub Profile
@garybradski
garybradski / ClockwiseAngle_SegmentIntersection
Created September 26, 2020 02:22
Clockwise angle between points, Intersection between segments
def clockwide_angle_between(p1, p2):
'''
Compute clockwise angle between 2 [x,y] points
:param p1: First point
:type p1: np.array([x1,y1])
:param p2: Second point
:type p2: np.array([x2,y3])
:return: clockwise angle between pt1 and pt2
:rtype: float64
'''
@garybradski
garybradski / gist:04e81b3d5869769efc63fe7e8b6e23c3
Last active September 5, 2020 00:31
Build OpenCV on a Mac with XCode
Building OpenCV on Mac with Xcode
1. make sure you have all the updates installed, latest Xcode, latest CMake.
2. run Xcode, if it suggest to install some developer tools on launch, do it. Then quit it.
3. in terminal run "sudo xcode-select --reset" 
4. wipe opencv build directory.
5. run CMake (or cmake-gui&), select opencv source directory and fresh build directory.
6. select Xcode project generator, I tried Unix, it may not work similarly well. Just use Xcode, you can still build it from command line.
7. add OPENCV_EXTRA_MODULES_PATH directory set to opencv_contrib/modules.
8. configure => generate => open project.
9. build all.
@garybradski
garybradski / no_derivs.py
Created May 11, 2020 04:15
Derivatives along a line in an image
def no_derivs(img, pt1,pt2,thresh=10):
'''
Test whether there are no image derivatives > thresh between pt1 and pt2
:param img: BGR opencv numpy image
:param pt1: first (x,y) point
:param pt2: second (x,y) point
:param thresh: no image differences > than this amount allowed
:return: True if no derivs, else False
'''
coords = np.around(np.linspace(pt1, pt2, np.linalg.norm(pt1 - pt2))).astype(int)
@garybradski
garybradski / Fast feature corner detector
Created May 3, 2020 03:11
Use of cv2.FastFeatureDetector
import numpy as np
import cv2
def fast_corners(img,border_buffer=0):
'''
Use the fast corner detector to find (x,y) integer corner locations
:param img_resized:
:param border_buffer: If >0, prune away kepoints w/in this distance of the boarder
:return: key point corner locations: list of int cv2.KeyPoints
'''
@garybradski
garybradski / Coordinates for circle around a point
Last active April 28, 2020 23:02
list of [x,y] coordinates for a circle offset that can then be offset to a given point in an image and samples
import numpy as np
import cv2
import copy
from copy import deepcopy
def offset_pts_for_circle(radius,db=False):
'''
Create offset coordinates for a circle around a point. The order of the resulting points will be
counter-clockwise
:param radius: Radius of circle
@garybradski
garybradski / floodfill.py
Last active April 28, 2020 00:06
Segment by floodfill, find contours, find polynomial contours, find quadrilaterals, find convex quadrilaterals, find the centroid of convex quadrilateras
#!/usr/bin/env python
'''
Floodfill sample improved by Gary Bradski to find the contour, convert to polygon, test for qaudrilateral
make sure the quadrilateral is convex and find the center of the convex quadrilateral. Also, read in a directory of
images, not just one.
Usage:
floodfill.py <Directory of images>
@garybradski
garybradski / BackGroundSegWithMask.py
Last active June 20, 2019 23:18
opencv python background subtraction with mask
import cv2
import numpy as np
class BackGroundSegmentor(object):
def __init__(self):
self.fgbg = cv2.createBackgroundSubtractorMOG2(
history=450, varThreshold=50, detectShadows=True)
self.fgbg.setNMixtures(3)
self.vbg = None
@garybradski
garybradski / git_log_all.sh
Created March 27, 2019 20:43
Show the git log in pretty colors
git log --graph --full-history --all --color \
--pretty=format:"%x1b[31m%h%x09%x1b[32m%d%x1b[0m%x20%s"
@garybradski
garybradski / corner_nearest_neighbor.py
Created January 9, 2019 05:13
Convolve to find axis aligned corners. Also, find nearest pixel
def convert_bin_img_to_corners(img):
'''
Mark corners with bias being pixels origin as (x,y): (0.5, 0.5)
:param img: Binary image of field
:return: Image of corners marked with 255, all else is 0
'''
kernel = np.ones(
(2, 2), np.float32) # Make convolution kernel 2x2 of 0.25s
ret, img_thresh = cv2.threshold(
img, 1, 1, cv2.THRESH_BINARY) # Make img into [0,1]
@garybradski
garybradski / rw_numpy_json.py
Created December 20, 2018 04:46
Read/write json of numpy array
import numpy as np
import codecs, json
#In order ot store an numpy array as a .json file
a = np.arange(10).reshape(2,5) # a 2 by 5 array
b = a.tolist() # nested lists with same data, indices
# Obviously, if you already have list, you don't/can't .tolist() it
file_path = "/path.json" ## your path variable
json.dump(b, codecs.open(file_path, 'w', encoding='utf-8'), separators=(',', ':'), sort_keys=True, indent=4) ### this saves the array in .json format