def main(): ap = argparse.ArgumentParser("Stitching a panorama from video input") ap.add_argument("-v", "--video", required = False, help = "Path to Video file (Defaults to camera input)") args = vars(ap.parse_args()) if not args.get("video", False): camera = cv2.VideoCapture(0) else: camera = cv2.VideoCapture(args["video"]) for i in range(10): #ignore first 10 frames to avoid black frames in camera video capture grabbed, frame = camera.read() if grabbed: imageStitcher = SurfStitcher(frame) # showImage(frame) # cv2.imwrite("Image1.jpg", frame) for i in range(3): for i in range(10): grabbed, frame = camera.read() if not grabbed: break imageStitcher.stitch(frame) # showImage(frame) # cv2.imwrite("Image2.jpg", frame) cv2.imshow("Video", utils.image_resize(frame, height = 500)) if cv2.waitKey(1000) & 0xFF == ord('q'): break imageStitcher.saveImage() cv2.destroyAllWindows() camera.release()
def find_eye_center(image): """ Find center of eye using Fabian's algorithm :param image: Gray scale image of eye :return: row, col identified as center """ # print image.shape global showImage scaled_image = utils.image_resize(image.copy(), width=EYE_ROI_WIDTH) gradient_energy_x = cv2.Sobel(scaled_image, cv2.CV_64F, 1, 0, ksize=3) gradient_energy_y = cv2.Sobel(scaled_image, cv2.CV_64F, 0, 1, ksize=3) gradient_magnitude = (gradient_energy_x**2 + gradient_energy_y**2)**0.5 threshold = np.mean(gradient_magnitude) + np.std(gradient_magnitude) * 3 gradient_energy_x /= gradient_magnitude gradient_energy_y /= gradient_magnitude mask = gradient_magnitude < threshold gradient_energy_x[mask] = 0 gradient_energy_y[mask] = 0 scaled_image = cv2.GaussianBlur(scaled_image, (5, 5), 0, 0) inverted_image = 255 * np.ones_like(scaled_image) - scaled_image if showImage: # cv2.HoughCircles(scaled_image, cv2.HOUGH_GRADIENT, 2, 12.0) cv2.imshow("EyeDebug", inverted_image) if (cv2.waitKey() & 0xFF) == ord('s'): showImage = False cv2.destroyWindow('EyeDebug') indices = np.indices(inverted_image.shape).astype(np.float32) indices += 1e-8 output_sum = np.zeros_like(inverted_image).astype(np.float32) for row in range(output_sum.shape[0]): for col in range(output_sum.shape[1]): val1 = (indices[0] - row) * gradient_energy_y val2 = (indices[1] - col) * gradient_energy_x val = (val1 + val2) output_sum += inverted_image * (val - val.mean()) / val.std() # compute_location_weight(row, col, inverted_image, gradient_energy_x, gradient_energy_y) index = np.unravel_index(np.argmax(output_sum), output_sum.shape) rescaled_index = (index[0] * image.shape[0] / scaled_image.shape[0], index[1] * image.shape[1] / scaled_image.shape[1]) return rescaled_index
def find_eye_center(image): """ Find center of eye using Fabian's algorithm :param image: Gray scale image of eye :return: row, col identified as center """ # print image.shape global showImage scaled_image = utils.image_resize(image.copy(), width=EYE_ROI_WIDTH) gradient_energy_x = cv2.Sobel(scaled_image, cv2.CV_64F, 1, 0, ksize=3) gradient_energy_y = cv2.Sobel(scaled_image, cv2.CV_64F, 0, 1, ksize=3) gradient_magnitude = (gradient_energy_x ** 2 + gradient_energy_y ** 2) ** 0.5 threshold = np.mean(gradient_magnitude) + np.std(gradient_magnitude) * 3 gradient_energy_x /= gradient_magnitude gradient_energy_y /= gradient_magnitude mask = gradient_magnitude < threshold gradient_energy_x[mask] = 0 gradient_energy_y[mask] = 0 scaled_image = cv2.GaussianBlur(scaled_image, (5, 5), 0, 0) inverted_image = 255 * np.ones_like(scaled_image) - scaled_image if showImage: # cv2.HoughCircles(scaled_image, cv2.HOUGH_GRADIENT, 2, 12.0) cv2.imshow("EyeDebug", inverted_image) if (cv2.waitKey() & 0xFF) == ord('s'): showImage = False cv2.destroyWindow('EyeDebug') indices = np.indices(inverted_image.shape).astype(np.float32) indices += 1e-8 output_sum = np.zeros_like(inverted_image).astype(np.float32) for row in range(output_sum.shape[0]): for col in range(output_sum.shape[1]): val1 = (indices[0] - row) * gradient_energy_y val2 = (indices[1] - col) * gradient_energy_x val = (val1 + val2) output_sum += inverted_image * (val - val.mean()) / val.std() # compute_location_weight(row, col, inverted_image, gradient_energy_x, gradient_energy_y) index = np.unravel_index(np.argmax(output_sum), output_sum.shape) rescaled_index = ( index[0] * image.shape[0] / scaled_image.shape[0], index[1] * image.shape[1] / scaled_image.shape[1]) return rescaled_index
def main(): ap = argparse.ArgumentParser("Stitching a panorama from video input") ap.add_argument("-v", "--video", required=False, help="Path to Video file (Defaults to camera input)") args = vars(ap.parse_args()) if not args.get("video", False): camera = cv2.VideoCapture(0) else: camera = cv2.VideoCapture(args["video"]) for i in range( 10 ): #ignore first 10 frames to avoid black frames in camera video capture grabbed, frame = camera.read() if grabbed: imageStitcher = SurfStitcher(frame) # showImage(frame) # cv2.imwrite("Image1.jpg", frame) for i in range(3): for i in range(10): grabbed, frame = camera.read() if not grabbed: break imageStitcher.stitch(frame) # showImage(frame) # cv2.imwrite("Image2.jpg", frame) cv2.imshow("Video", utils.image_resize(frame, height=500)) if cv2.waitKey(1000) & 0xFF == ord('q'): break imageStitcher.saveImage() cv2.destroyAllWindows() camera.release()
import image_utils as utils ap = argparse.ArgumentParser("Track and blur faces in video input") ap.add_argument("-v", "--video", help="Path to video file. Defaults to webcam video") args = vars(ap.parse_args()) camera = cv2.VideoCapture(0) calibrated = False grabbed, frame = camera.read() if not grabbed: raise ValueError("Camera read failed!") bg = utils.image_resize(frame, height=600).astype(np.float32) while True: grabbed, frame = camera.read() if not grabbed: print "Camera read failed" break frame = utils.image_resize(frame, height=600) height, width, channels = frame.shape if not calibrated: # Sample hand color utils.add_text(frame, "Press space after covering rectangle with hand. Hit SPACE when ready") x, y, w, h = width / 4, height / 2, 50, 50
termination = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1) lk_params = dict(winSize=(15, 15), maxLevel=2, criteria=termination) face_cascade = cv2.CascadeClassifier('Image_Lib/Face_Data/haarcascade_frontalface_default.xml') p0 = [] prev_frame = None while (True): grabbed, frame = camera.read() if not grabbed: print "Camera read failed!" break frame = utils.image_resize(frame) curr_frame_gray = cv2.equalizeHist(cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)) info = "" if prev_frame is None or len(p0) <= 3: info = "Detecting..." face = utils.detect_face(face_cascade, frame) if face is not None: prev_frame = curr_frame_gray x,y,w,h = face roi = np.zeros(prev_frame.shape, dtype=np.uint8) roi[y:y+h, x:x+w] = 255 p0 = cv2.goodFeaturesToTrack(prev_frame, mask=roi, **feature_params) else: p1,st,err = cv2.calcOpticalFlowPyrLK(prev_frame, curr_frame_gray, p0, None, **lk_params)
if cmd_subfolder not in sys.path: sys.path.insert(0, cmd_subfolder) import image_utils as utils ap = argparse.ArgumentParser(description="Visualize image channels separately") ap.add_argument("-i", "--image", required = True, help = "Path to image file") ap.add_argument("-m", "--mode", required = False, help = "Enter image channels mode. Default = BGR") args = vars(ap.parse_args()) image = cv2.imread(args["image"]) if(args["mode"] == None): (ch1, ch2, ch3) = cv2.split(image) print("BGR Color space") elif(args["mode"].upper() == "HSV"): imageCvt = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) (ch1, ch2, ch3) = cv2.split(imageCvt) print("HSV Color space") elif(args["mode"].upper() == "LAB"): imageCvt = cv2.cvtColor(image, cv2.COLOR_BGR2LAB) (ch1, ch2, ch3) = cv2.split(imageCvt) print("LAB space") cv2.imshow("Channel 1", utils.image_resize(ch1,height = 500)) cv2.imshow("Channel 2", utils.image_resize(ch2,height = 500)) cv2.imshow("Channel 3", utils.image_resize(ch3,height = 500)) cv2.waitKey(0) cv2.destroyAllWindows()
cmd_subfolder = os.path.realpath( os.path.abspath( os.path.join( os.path.split(inspect.getfile(inspect.currentframe()))[0], "..", "..", "Image_Lib"))) if cmd_subfolder not in sys.path: sys.path.insert(0, cmd_subfolder) import image_utils as utils ap = argparse.ArgumentParser( "Simple intensity measure to detect Day or Night picture") ap.add_argument("-i", "--image", required=True, help="Path to image file") args = vars(ap.parse_args()) image = cv2.imread(args["image"]) imageHSV = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) (h, s, v) = cv2.split(imageHSV) brightPixelCount = np.sum(v > 128) pixelCount = image.shape[0] * image.shape[1] if (brightPixelCount > pixelCount / 2): print "DAY" else: print "NIGHT" cv2.imshow("Image", utils.image_resize(image, height=600)) cv2.waitKey() cv2.destroyAllWindows()
dict = {0:[[0,0],[0,0]], 51:[[255,0],[0,0]], 102:[[0,255],[255,0]], 153:[[255,255],[255,0]], 204:[[255,255],[255,255]]} for row in range(height): for col in range(width): val = imageGray[row][col] if(val > 204): imageHalfTone[row*2:row*2+2, col*2:col*2+2] = dict[204] elif(val >153): imageHalfTone[row*2:row*2+2, col*2:col*2+2] = dict[153] elif(val > 102): imageHalfTone[row*2:row*2+2, col*2:col*2+2] = dict[102] elif(val > 51): imageHalfTone[row*2:row*2+2, col*2:col*2+2] = dict[51] else: imageHalfTone[row*2:row*2+2, col*2:col*2+2] = dict[0] return imageHalfTone ap = argparse.ArgumentParser("Classical Half Toning [2x2 Mask]") ap.add_argument('-i', '--image', required = True, help = 'Path to image file') args = vars(ap.parse_args()) image = utils.image_resize(cv2.imread(args["image"]), height = 300) imageHalfTone = cv2.merge([HalfToneImage(x) for x in cv2.split(image)]) cv2.imshow("HalfTone Image",imageHalfTone.astype(np.uint8)) cv2.waitKey() cv2.destroyAllWindows()
def saveImage(self): cv2.imshow("Image", utils.image_resize(self.leftImage, width = 900)) cv2.waitKey()
blockSize=7) termination = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1) lk_params = dict(winSize=(15, 15), maxLevel=2, criteria=termination) track_length = 15 track_interval = 0 tracks = [] prev_gray_frame = None print("Reading camera...") while True: grabbed, frame = camera.read() if not grabbed: raise EnvironmentError("Camera read failed!") frame = utils.image_resize(frame, height=600) output = frame.copy() curr_gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) if len(tracks) > 0: p0 = np.float32([tr[-1] for tr in tracks]).reshape(-1, 1, 2) p1, state, err = cv2.calcOpticalFlowPyrLK(prev_gray_frame, curr_gray_frame, p0, None, **lk_params) p0r, state, err = cv2.calcOpticalFlowPyrLK(curr_gray_frame, prev_gray_frame, p1, None, **lk_params) d = abs(p0 - p0r).reshape(-1, 2).max(-1) good = d < 1 new_tracks = [] for tr, (x, y), good_flag in zip(tracks, p1.reshape(-1, 2), good):
if cmd_subfolder not in sys.path: sys.path.insert(0, cmd_subfolder) import image_utils as utils ap = argparse.ArgumentParser(description="Solve orthogonal mazes") ap.add_argument("-i", "--image", required=True, help="Path to image file") args = vars(ap.parse_args()) image = cv2.imread(args["image"]) gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # cnts = utils.get_contours(gray_image, 200) # cnt = sorted(cnts, key=cv2.contourArea, reverse=True)[0] thresholded_image = utils.adaptive_threshold(gray_image, cv2.THRESH_BINARY_INV) cv2.imshow("Output", utils.image_resize(thresholded_image, height=600)) cv2.waitKey() _, cnts, _ = cv2.findContours(thresholded_image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) if len(cnts) != 2: print len(cnts) raise ValueError("Unable to solve maze - Failed at Contour finding!") solution_image = np.zeros(gray_image.shape, dtype=np.uint8) cv2.drawContours(solution_image, cnts, 0, (255, 255, 255), cv2.FILLED) cv2.imshow("Output", utils.image_resize(solution_image, height=600)) cv2.waitKey() kernel = np.ones((15, 15), dtype=np.uint8)
#subtract mean value of image h -= tarLMean # v -= tarVMean #scale std deviation based on source image h *= (tarLStd / srcLStd) # v *= (tarVStd/srcVStd) #add source image mean to target h += srcLMean # v += srcVMean # clip the pixel intensities to [0, 255] if they fall outside # this range h = np.clip(h, 0, 360) h *= 0.5 # v += 128 # v= np.clip(v, 0, 255) # merge the channels together and convert back to the RGB color # space, being sure to utilize the 8-bit unsigned integer data # type transfer = cv2.merge([h, s, v]) transfer = cv2.cvtColor(transfer.astype("uint8"), cv2.COLOR_HSV2BGR) cv2.imshow("Color Transform", utils.image_resize(transfer, height=500)) cv2.waitKey() cv2.destroyAllWindows() cv2.imwrite("result.jpg", transfer)
__author__ = 'Charlie' import numpy as np import cv2 import sys, inspect, os import argparse cmd_subfolder = os.path.realpath(os.path.abspath(os.path.join(os.path.split(inspect.getfile(inspect.currentframe()))[0],"..","..","Image_Lib"))) if cmd_subfolder not in sys.path: sys.path.insert(0, cmd_subfolder) import image_utils as utils ap = argparse.ArgumentParser() ap.add_argument("-i", "--image", required = True, help = "Path to image file") args = vars(ap.parse_args()) image = cv2.imread(args["image"]) cv2.imshow("Input", utils.image_resize(image)) # cv2.imshow("Output", utils.image_resize(utils.sharpenImage(image))) cv2.imwrite("image.jpg", utils.sharpenImage(image)) cv2.waitKey() cv2.destroyAllWindows()
for row in range(height): for col in range(width): val = imageGray[row][col] if (val > 204): imageHalfTone[row * 2:row * 2 + 2, col * 2:col * 2 + 2] = dict[204] elif (val > 153): imageHalfTone[row * 2:row * 2 + 2, col * 2:col * 2 + 2] = dict[153] elif (val > 102): imageHalfTone[row * 2:row * 2 + 2, col * 2:col * 2 + 2] = dict[102] elif (val > 51): imageHalfTone[row * 2:row * 2 + 2, col * 2:col * 2 + 2] = dict[51] else: imageHalfTone[row * 2:row * 2 + 2, col * 2:col * 2 + 2] = dict[0] return imageHalfTone ap = argparse.ArgumentParser("Classical Half Toning [2x2 Mask]") ap.add_argument('-i', '--image', required=True, help='Path to image file') args = vars(ap.parse_args()) image = utils.image_resize(cv2.imread(args["image"]), height=300) imageHalfTone = cv2.merge([HalfToneImage(x) for x in cv2.split(image)]) cv2.imshow("HalfTone Image", imageHalfTone.astype(np.uint8)) cv2.waitKey() cv2.destroyAllWindows()
elif args["feature"] == "ShiTomasiCorner": #Similar to Harris Corner except the value is considered as lambda greater than a threshold corners = cv2.goodFeaturesToTrack(gray, 100, 0.01, 10) for i in corners: x,y = i.ravel() cv2.circle(image,(x,y),2, [0,255,0], -1) # -1 thickness would fill the circle - any negative value for that case elif args["feature"] == "SIFT": siftClass = cv2.xfeatures2d.SIFT_create() #DoG to find kps, Histogram of gradients in neighborhood for features kps = siftClass.detect(gray, None) image = cv2.drawKeypoints(gray, kps,None) elif args["feature"] == "SURF": surfClass = cv2.xfeatures2d.SURF_create(500) #Difference of box filters (integral images fasten process), wavelets for features kps = surfClass.detect(gray, None) image = cv2.drawKeypoints(gray, kps, None) elif args["feature"]=="FAST": fastClass = cv2.FastFeatureDetector_create() #check a circular region around to figure if corner or not with threshold kps = fastClass.detect(gray, None) image = cv2.drawKeypoints(gray, kps, None) elif args["feature"]=="ORB": orb = cv2.ORB_create() kps = orb.detect(gray, None) image = cv2.drawKeypoints(gray, kps, None) cv2.imshow("Output", utils.image_resize(image, height = 500)) cv2.waitKey() cv2.destroyAllWindows()
import cv2 import numpy as np import argparse cmd_subfolder = os.path.realpath(os.path.abspath(os.path.join(os.path.split(inspect.getfile(inspect.currentframe()))[0],"..","..","Image_Lib"))) if cmd_subfolder not in sys.path: sys.path.insert(0, cmd_subfolder) import image_utils as utils ap = argparse.ArgumentParser("Find clusters in images and colors the image accordingly") ap.add_argument("-i", "--image", required = True, help ="Path to image") ap.add_argument("-k", "--clusters", required = False, type = int, help = "No of clusters to form in image (default = 10)") args = vars(ap.parse_args()) image = cv2.imread(args["image"]).astype("float32") data = image.reshape((-1,3)) K = 10 if(args["clusters"] != None): K = args["clusters"] termination = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1) ret, lbl, centers = cv2.kmeans(data, K, None, termination, 3, cv2.KMEANS_PP_CENTERS) centers = centers.astype("uint8") result = centers[lbl.flatten()] output = result.reshape(image.shape) cv2.imshow("Result", utils.image_resize(output, height = 500)) cv2.imwrite("result.jpg", output) cv2.waitKey() cv2.destroyAllWindows()
def showImage(image): cv2.imshow("Image", utils.image_resize(image, height = 400)) cv2.waitKey() cv2.destroyWindow("Image")
def parse_example_proto(self, example_serialized): # TBD # 1. define self.frame_per_clip # 2. well define feature's key words """Parses an Example proto containing a training example of an image. Args: example_serialized: scalar Tensor tf.string containing a serialized Example protocol buffer. Returns: image_buffer: Tensor tf.string containing the contents of a JPEG file. label: Tensor tf.int32 containing the label. bbox: 3-D float Tensor of bounding boxes arranged [1, num_boxes, coords] where each coordinate is [0, 1) and the coordinates are arranged as [ymin, xmin, ymax, xmax]. text: Tensor tf.string containing the human-readable label. """ # Dense features in Example proto. contexts, features = tf.parse_single_sequence_example( example_serialized, context_features={ "number_of_frames": tf.FixedLenFeature([], tf.int64) }, sequence_features={ 'images': tf.FixedLenSequenceFeature([], dtype=tf.string), }) # Truncate or pad images images = tf.cond( tf.shape( features['images'])[0] > self.num_seg * self.frame_per_seg, lambda: self.truncate(features['images']), lambda: self.padding(features['images'])) # sampling frames with specific fps if 24.0 % self.fps != 0: print("Warning, the fps is {} ".format(self.fps)) interval = int(24.0 / self.fps) images, flow_x, flow_y = images[:: interval], flow_x[:: interval], flow_y[:: interval] # sampling from segments images, flow_x, flow_y = tf.cond( tf.shape(features['images'])[0] > 0, lambda: self.sampling(images, flow_x, flow_y ), # if is not empty, do the sampling lambda: self.identity(images, flow_x, flow_y)) # else leave it # Decode jpg-string to TF-tensor output = tf.map_fn(image_utils.img_decode, images, dtype=tf.float32) # output shoud have shorter side as 256 or jittered scale, pixel values are range from [0, 1.0] if self.is_training is True: # scale jittering: random resize a short side range in [256, 480] if self.scale_jittering: output = image_utils.image_scale_jittering( output, self.scale_min, self.scale_max) # it works for multiple-channels else: output = image_utils.image_resize(output, self.scale_min) # horizontal filping random_float = tf.random_uniform([1], minval=0, maxval=1, dtype=tf.float32)[0] output = tf.cond(random_float > 0.5, lambda: tf.image.flip_left_right(output), lambda: output) # corner or center cropping random_int = tf.random_uniform([1], minval=0, maxval=6, dtype=tf.int32)[0] output = tf.case( { tf.equal(random_int, tf.constant(0, dtype=tf.int32)): lambda: self._crop_center(output), tf.equal(random_int, tf.constant(1, dtype=tf.int32)): lambda: self._crop_upper_left(output), tf.equal(random_int, tf.constant(2, dtype=tf.int32)): lambda: self._crop_upper_right(output), tf.equal(random_int, tf.constant(3, dtype=tf.int32)): lambda: self._crop_bottom_left(output), tf.equal(random_int, tf.constant(4, dtype=tf.int32)): lambda: self._crop_bottom_right(output), }, default=lambda: self._crop_center(output), exclusive=True) else: output = image_utils.image_resize( output, self.scale_min) # it works for multiple-channels output = self._crop_center(output) # center crop only output = ( output * 2 ) - 1.0 # range from [-1.0, 1.0] with shape (frames_per_clip, height, width, channels) # output = tf.transpose(output, [0, 3, 1, 2]) # from (frames_per_clip, height, width, channels) -> (frames_per_clip, channels, height, width) return output, label # , contexts, features
sys.path.insert(0, cmd_subfolder) from image_transform import four_point_transform import image_utils as utils ap = argparse.ArgumentParser() ap.add_argument("-i", "--image", required=True, help="Path to Image to be scanned") args = vars(ap.parse_args()) image = cv2.imread(args["image"]) ratio = image.shape[0] / 500.0 orig = image.copy() image = utils.image_resize(image, height=500) gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) gray = cv2.GaussianBlur(gray, (5, 5), 0) # clahe = cv2.createCLAHE() # gray = clahe.apply(gray) edged = cv2.Canny(gray, 25, 75) # show the original image and the edge detected image print "STEP 1: Edge Detection" # cv2.imshow("Image", image) # cv2.imshow("Edged", edged) # cv2.waitKey(0) # cv2.destroyAllWindows() # find the contours in the edged image, keeping only the
sys.path.insert(0, cmd_subfolder) import image_utils as utils ap = argparse.ArgumentParser() ap.add_argument("--image", "-i", required = True, help = "Path to input image") ap.add_argument("--template", "-t", required = True, help = "Path to template image") args = vars(ap.parse_args()) image = cv2.imread(args["image"]) template = cv2.imread(args["template"]) templateShape = template.shape[:2] result = cv2.matchTemplate(image, template, cv2.TM_CCOEFF) (_,_, minLoc, maxLoc) = cv2.minMaxLoc(result) # the puzzle image topLeft = maxLoc botRight = (topLeft[0] + templateShape[1], topLeft[1] + templateShape[0]) roi = image[topLeft[1]:botRight[1], topLeft[0]:botRight[0]] # construct a darkened transparent 'layer' to darken everything # in the puzzle except for waldo mask = np.zeros(image.shape, dtype = "uint8") image = cv2.addWeighted(image, 0.25, mask, 0.75, 0) image[topLeft[1]:botRight[1], topLeft[0]:botRight[0]] = roi # display the images cv2.imshow("Image", utils.image_resize(image, height = 650)) cv2.imshow("Template", template) cv2.waitKey(0)
ap = argparse.ArgumentParser("Track and blur faces in video input") ap.add_argument("-v", "--video", help="Path to video file. Defaults to webcam video") args = vars(ap.parse_args()) camera = cv2.VideoCapture(0) calibrated = False grabbed, frame = camera.read() if not grabbed: raise ValueError("Camera read failed!") bg = utils.image_resize(frame, height=600).astype(np.float32) while True: grabbed, frame = camera.read() if not grabbed: print "Camera read failed" break frame = utils.image_resize(frame, height=600) height, width, channels = frame.shape if not calibrated: # Sample hand color utils.add_text( frame, "Press space after covering rectangle with hand. Hit SPACE when ready"
cmd_subfolder = os.path.realpath( os.path.abspath(os.path.join(os.path.split(inspect.getfile(inspect.currentframe()))[0], "..", "..", "Image_Lib"))) if cmd_subfolder not in sys.path: sys.path.insert(0, cmd_subfolder) import image_utils as utils camera = cv2.VideoCapture(0) count = 1 while True: grabbed, frame = camera.read() if not grabbed: raise EnvironmentError("Camera read failed!") cv2.imshow("Output", utils.image_resize(frame, height=600)) key = cv2.waitKey() & 0xFF if key == ord('s'): cv2.imwrite("images/camera_calibration/chessboard%02d.jpg"%count, frame) print "Saved %02d" %count count +=1 elif key==ord('q'): break camera.release() cv2.destroyAllWindows()
#subtract mean value of image l -= tarLMean a -= tarAMean b -= tarBMean #scale std deviation based on source image l *= (tarLStd/srcLStd) a *= (tarAStd/srcAStd) b *= (tarBStd/srcBStd) #add source image mean to target l += srcLMean a += srcAMean b += srcBMean # clip the pixel intensities to [0, 255] if they fall outside # this range l = np.clip(l, 0, 255) a = np.clip(a, 0, 255) b = np.clip(b, 0, 255) # merge the channels together and convert back to the RGB color # space, being sure to utilize the 8-bit unsigned integer data # type transfer = cv2.merge([l, a, b]) transfer = cv2.cvtColor(transfer.astype("uint8"), cv2.COLOR_LAB2BGR) cv2.imshow("Color Transform", utils.image_resize(transfer, height = 500)) cv2.waitKey() cv2.destroyAllWindows()
cmd_subfolder = os.path.realpath( os.path.abspath(os.path.join(os.path.split(inspect.getfile(inspect.currentframe()))[0], "..", "Image_Lib"))) if cmd_subfolder not in sys.path: sys.path.insert(0, cmd_subfolder) from image_transform import four_point_transform import image_utils as utils ap = argparse.ArgumentParser() ap.add_argument("-i", "--image", required=True, help="Path to Image to be scanned") args = vars(ap.parse_args()) image = cv2.imread(args["image"]) ratio = image.shape[0] / 500.0 orig = image.copy() image = utils.image_resize(image, height=500) gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) gray = cv2.GaussianBlur(gray, (5, 5), 0) # clahe = cv2.createCLAHE() # gray = clahe.apply(gray) edged = cv2.Canny(gray, 25, 75) # show the original image and the edge detected image print "STEP 1: Edge Detection" # cv2.imshow("Image", image) # cv2.imshow("Edged", edged) # cv2.waitKey(0) # cv2.destroyAllWindows()
p = [pHit*p[i] if i==index else pMiss*p[i] for i in range(z_axis_length)] # print p, sum(p) p = [p[i]/sum(p) for i in range(z_axis_length)] return p camera = cv2.VideoCapture(0) face_cascade = cv2.CascadeClassifier('Image_Lib/Face_Data/haarcascade_frontalface_default.xml') calibrate = True calibration_rects = {} while True: face_box = None grabbed, frame = camera.read() frame = utils.image_resize(frame , height = 600) face_box = utils.detect_face(face_cascade, frame) if face_box is None: continue cv2.rectangle(frame, (face_box[0], face_box[1]), (face_box[0] + face_box[2], face_box[1] + face_box[3]), (255, 0, 0), 2) if calibrate: utils.add_text(frame, "Press: W - closest, S - farthest, C - neutral, Q - Done") no_points_either_side = z_axis_length/2 cv2.imshow("Calibrating...", frame) key = cv2.waitKey(1) & 0xFF if key == ord('w'):
imageDithered[row][col] = 128 elif val > 64: imageDithered[row][col] = 64 else: imageDithered[row][col] = 0 error = val - imageDithered[row][col] if (row + 2 < height) and (col - 1 > 0) and (col + 2 < width): imageDithered[row:row + 2, col - 1:col + 2] += np.multiply( multiplier, error) return imageDithered ap = argparse.ArgumentParser( "Dither Halftoning - Floyd Steinberg. Truncated to 4 levels") ap.add_argument("-i", "--image", required=True, help="Path to image file") args = vars(ap.parse_args()) image = cv2.imread(args["image"]) # imageGray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) imageDithered = cv2.merge([DitherImage(x) for x in cv2.split(image)]).astype(np.uint8) cv2.imshow("Grayed Image", utils.image_resize(image, height=600)) cv2.imshow("Dithered Image", utils.image_resize(imageDithered, height=600)) cv2.waitKey() cv2.destroyAllWindows() cv2.imwrite("results.jpg", imageDithered)
import image_utils as utils ap = argparse.ArgumentParser(description="Visualize image channels separately") ap.add_argument("-i", "--image", required=True, help="Path to image file") ap.add_argument("-m", "--mode", required=False, help="Enter image channels mode. Default = BGR") args = vars(ap.parse_args()) image = cv2.imread(args["image"]) if (args["mode"] == None): (ch1, ch2, ch3) = cv2.split(image) print("BGR Color space") elif (args["mode"].upper() == "HSV"): imageCvt = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) (ch1, ch2, ch3) = cv2.split(imageCvt) print("HSV Color space") elif (args["mode"].upper() == "LAB"): imageCvt = cv2.cvtColor(image, cv2.COLOR_BGR2LAB) (ch1, ch2, ch3) = cv2.split(imageCvt) print("LAB space") cv2.imshow("Channel 1", utils.image_resize(ch1, height=500)) cv2.imshow("Channel 2", utils.image_resize(ch2, height=500)) cv2.imshow("Channel 3", utils.image_resize(ch3, height=500)) cv2.waitKey(0) cv2.destroyAllWindows()
def showImage(image): cv2.imshow("Image", utils.image_resize(image, height=400)) cv2.waitKey() cv2.destroyWindow("Image")
ap.add_argument("-v", "--video", help="Path to video file. Defaults to camera if not provided") args = vars(ap.parse_args()) if not args.get("video", False): camera = cv2.VideoCapture(0) else: camera = cv2.VideoCapture(args["video"]) while True: grabbed, frame = camera.read() if not grabbed: break imageGray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) result = cv2.GaussianBlur(imageGray, (5, 5), 0) result = cv2.Canny(result, 50, 175) (_, cnts, _) = cv2.findContours(result, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) # cnts = sorted(cnts, key=cv2.contourArea) cv2.drawContours(frame, cnts, -1, (0, 0, 0), 1) cv2.imshow("Edge", utils.image_resize(frame, height=500)) if cv2.waitKey(1) & 0xFF == ord("q"): break camera.release() cv2.destroyAllWindows()
def saveImage(self): cv2.imshow("Image", utils.image_resize(self.leftImage, width=900)) cv2.waitKey()
if cmd_subfolder not in sys.path: sys.path.insert(0, cmd_subfolder) import image_utils as utils ap = argparse.ArgumentParser(description="Solve orthogonal mazes") ap.add_argument("-i", "--image", required = True, help = "Path to image file") args = vars(ap.parse_args()) image = cv2.imread(args["image"]) gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # cnts = utils.get_contours(gray_image, 200) # cnt = sorted(cnts, key=cv2.contourArea, reverse=True)[0] thresholded_image = utils.adaptive_threshold(gray_image, cv2.THRESH_BINARY_INV) cv2.imshow("Output", utils.image_resize(thresholded_image, height=600)) cv2.waitKey() _, cnts, _ = cv2.findContours(thresholded_image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) if len(cnts) != 2: print len(cnts) raise ValueError("Unable to solve maze - Failed at Contour finding!") solution_image = np.zeros(gray_image.shape, dtype=np.uint8) cv2.drawContours(solution_image, cnts, 0, (255,255,255),cv2.FILLED) cv2.imshow("Output", utils.image_resize(solution_image, height=600)) cv2.waitKey() kernel = np.ones((15, 15), dtype=np.uint8) solution_image = cv2.dilate(solution_image, kernel)
print "Video file read" camera = cv2.VideoCapture(args["video"]) while True: # grab the current frame (grabbed, frame) = camera.read() # if we are viewing a video and we did not grab a # frame, then we have reached the end of the video if args.get("video") and not grabbed: break # resize the frame, convert it to the HSV color space, # and determine the HSV pixel intensities that fall into # the speicifed upper and lower boundaries frame = utils.image_resize(frame, width=400) converted = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) skinMask = cv2.inRange(converted, lower, upper) # apply a series of erosions and dilations to the mask # using an elliptical kernel kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (11, 11)) skinMask = cv2.erode(skinMask, kernel, iterations=2) skinMask = cv2.dilate(skinMask, kernel, iterations=2) # blur the mask to help remove noise, then apply the # mask to the frame skinMask = cv2.GaussianBlur(skinMask, (3, 3), 0) skin = cv2.bitwise_and(frame, frame, mask=skinMask) # show the skin in the image along with the mask
sys.path.insert(0, cmd_subfolder) import image_utils as utils ap = argparse.ArgumentParser( "Find clusters in images and colors the image accordingly") ap.add_argument("-i", "--image", required=True, help="Path to image") ap.add_argument("-k", "--clusters", required=False, type=int, help="No of clusters to form in image (default = 10)") args = vars(ap.parse_args()) image = cv2.imread(args["image"]).astype("float32") data = image.reshape((-1, 3)) K = 10 if (args["clusters"] != None): K = args["clusters"] termination = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1) ret, lbl, centers = cv2.kmeans(data, K, None, termination, 3, cv2.KMEANS_PP_CENTERS) centers = centers.astype("uint8") result = centers[lbl.flatten()] output = result.reshape(image.shape) cv2.imshow("Result", utils.image_resize(output, height=500)) cv2.imwrite("result.jpg", output) cv2.waitKey() cv2.destroyAllWindows()
cv2.circle(image, (roi_x + col, roi_y + row), 10, (0, 255, 0), -1) else: eyes = eye_cascade.detectMultiScale(gray, 1.2, 3) if len(eyes) == 2: x, y, w, h = eyes[0] row, col = tracker.find_eye_center(gray[y:y + h, x:x + w]) print row, col cv2.circle(image, (x + col, y + row), 10, (255, 0, 0), -1) x, y, w, h = eyes[1] row, col = tracker.find_eye_center(gray[y:y + h, x:x + w]) print row, col cv2.circle(image, (x + col, y + row), 10, (255, 0, 0), -1) else: row, col = tracker.find_eye_center(gray[:, 0:image.shape[1] / 2]) print row, col cv2.circle(image, (col, row), 10, (0, 0, 255), -1) row, col = tracker.find_eye_center(gray[:, image.shape[1] / 2:]) print row, col cv2.circle(image, (image.shape[1] / 2 + col, row), 10, (0, 0, 255), -1) cv2.imshow("Output", utils.image_resize(image, height=600)) cv2.waitKey() cv2.destroyAllWindows()
while True: grabbed, frame = camera.read() if not grabbed: break # imageGray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # ret, lbl, centers = cv2.kmeans(frame.reshape((-1, 3)).astype(np.float32), n, None, termination, 3, cv2.KMEANS_PP_CENTERS) # # centers = centers.astype("uint8") # frame = centers[lbl.flatten()].reshape(frame.shape) # frame = 8*(frame/8) imageGray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) result = cv2.GaussianBlur(imageGray, (5, 5), 0) result = utils.autoCanny(result) (_, cnts, _) = cv2.findContours(result, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) # cnts = sorted(cnts, key=cv2.contourArea) # frame = cv2.pyrMeanShiftFiltering(frame,21, 51) frame = 8*(frame/8) cv2.drawContours(frame, cnts, -1, (0, 0, 0), 1) cv2.imshow("Video", utils.image_resize(frame, height=500)) if cv2.waitKey(1) & 0xFF == ord("q"): break camera.release() cv2.destroyAllWindows()
import image_utils as utils ap = argparse.ArgumentParser("Real time edge finding on video") ap.add_argument("-v", "--video", help="Path to video file. Defaults to camera if not provided") args = vars(ap.parse_args()) if not args.get("video", False): camera = cv2.VideoCapture(0) else: camera = cv2.VideoCapture(args["video"]) while True: grabbed, frame = camera.read() if not grabbed: break imageGray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) result = cv2.GaussianBlur(imageGray, (5, 5), 0) result = cv2.Canny(result, 50, 175) (_, cnts, _) = cv2.findContours(result, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) # cnts = sorted(cnts, key=cv2.contourArea) cv2.drawContours(frame, cnts, -1, (0, 0, 0), 1) cv2.imshow("Edge", utils.image_resize(frame, height=500)) if cv2.waitKey(1) & 0xFF == ord("q"): break camera.release() cv2.destroyAllWindows()