Skip to content

Instantly share code, notes, and snippets.

View garybradski's full-sized avatar

Gary Bradski garybradski

View GitHub Profile
@garybradski
garybradski / fork_imshow.cc
Created July 15, 2017 02:43
imshow in multi-thread environment
if (!fork()) {
cv::imshow("debug", frame_image);
cv::waitKey(0);
exit(0);
}
@garybradski
garybradski / gist:539209d0332ecff747891e0677736419
Created September 9, 2017 00:48
python_indexing_slices.py
my_tuple = ('p','r','o','g','r','a','m','i','z')
print("og forward:")
print("og: ",my_tuple[2:4])
print("og: ",my_tuple[-7:4])
print("og: ",my_tuple[-7:-5])
print("og: ",my_tuple[2:-5])
print("og backwards (go):")
print("go: ",my_tuple[3:1:-1])
@garybradski
garybradski / copy_image_using_mask.py
Created September 21, 2017 00:16
Python: Methods of copying a mask indicated region of one image into another
#Method 1, pythonic
img_res = np.zeros(image.shape, np.uint8) #create the image
img_res[:, :] = (0, 0, 200) #color it red
idx = (mask != 0)
img_res[idx] = image[idx]
#Method 2 OpenCV
img_red = np.zeros(image.shape, np.uint8) #create the image
img_red[:, :] = (0, 0, 200) #color it red
fg = cv2.bitwise_and(image, image, mask=mask) #isolate the segmented region
@garybradski
garybradski / read_movie.py
Created September 22, 2017 21:56
Just reading a movie with opencv in python, jumping to a frame and displaying
cap = cv2.VideoCapture('/opt/home/garybradski/data/matte_this/shot02/Tornado.mp4')
length = int(cap.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT))
print "len Tornado = ", length
cv2.namedWindow('Tornado', cv2.WINDOW_NORMAL)
cv2.resizeWindow('Tornado',960,540)
# seek
assert cap.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, 8000)
n = 0
wait_key = 30
while True:
@garybradski
garybradski / use_logger.py
Created September 23, 2017 01:50
Python code showing how to use logging.py
import argparse
import logging
parser.add_argument('--loglevel', help='Logging level: NONE, DEBUG 10, INFO 20, WARNING 30, ERROR 40, CRITICAL 50. Default: OFF 100', type=str, default='WARNING' )
args = parser.parse_args()
logging.basicConfig()
logger = logging.getLogger()
if(args.loglevel == 'OFF'): logger.setLevel(100)
elif(args.loglevel == 'DEBUG'): logger.setLevel(logging.DEBUG)
elif(args.loglevel == 'INFO'): logger.setLevel(logging.INFO)
@garybradski
garybradski / alpha_mask_blend.py
Created September 23, 2017 05:28
Blend one image with another using an alpha mask in python
import cv2
# Read the images
foreground = cv2.imread("puppets.png")
background = cv2.imread("ocean.png")
alpha = cv2.imread("puppets_alpha.png")
# Convert uint8 to float
foreground = foreground.astype(float)
background = background.astype(float)
@garybradski
garybradski / alpha_mask_blend.cc
Created September 23, 2017 05:29
blend one image with another using an alpha mask in c++ w/opencv
void alphaBlend(Mat& foreground, Mat& background, Mat& alpha, Mat& outImage)
{
// Find number of pixels.
int numberOfPixels = foreground.rows * foreground.cols * foreground.channels();
// Get floating point pointers to the data matrices
float* fptr = reinterpret_cast<float*>(foreground.data);
float* bptr = reinterpret_cast<float*>(background.data);
float* aptr = reinterpret_cast<float*>(alpha.data);
float* outImagePtr = reinterpret_cast<float*>(outImage.data);
@garybradski
garybradski / read_video.py
Last active October 4, 2017 01:34
Just the throw together code for running a vdieo in python together with logging etc
import cv2
import argparse
import numpy as np
import os
import logging
#GLOBAL
logging.basicConfig() #Fire up the logger
logger = logging.getLogger()
@garybradski
garybradski / matplotlib_AND_opencv_imshow.py
Created October 4, 2017 23:09
Using python matplotlib and opencv cv2 imshow in the same loop using the keyboard to trigger pause or run
import cv2
from matplotlib import pyplot as plt
. . .
#DO PYTHON MATPLOTLIB IMAGE DISPLAY
plt.imshow(image_seg)
plt.draw()
plt.show() # , plt.draw(), plt.show()
plt.pause(0.01)
@garybradski
garybradski / text_2_floatlist.py
Last active October 6, 2017 03:07
Just a function that reads a line of a text file, strips out [ ] and converts to list of floats
def text_to_floatlist(pathfile):
f = open(pathfile, 'r')
line = f.readline().strip('[]')
f.close()
return [float(x) for x in line.split()]
#Use
rect = text_to_floatlist(seg_bb[frame_count])
if(len(rect) < 4):
logger.critical("Length of rectangle not long enough %d",int(len(rect)) )