def PerformSegmentation(self, event): filepath = self.photo_text.GetValue() img = cv.LoadImage(filepath, cv.CV_LOAD_IMAGE_COLOR) img_hsv = cv.CreateImage(cv.GetSize(img), 8, 3) cv.CvtColor(img, img_hsv, cv.CV_BGR2HSV) frame_threshed = cv.CreateImage(cv.GetSize(img_hsv), 8, 1) print self.PINK_MIN cv.InRange( img_hsv, cv.fromarray(self.PINK_MIN, allowND=True), cv.fromarray(self.PINK_MAX, allowND=True), frame_threshed, ) cv.SaveImage("/home/saket/Desktop/threshed.jpg", frame_threshed) # col = frame_threshed.height # row=frame_threshed.width # img1 = wx.Image("/home/saket/Desktop/threshed.jpg", wx.BITMAP_TYPE_ANY)# img1 = cv2.imread("/home/saket/Desktop/threshed.jpg") img = cv2.imread(filepath) row, col, x = img1.shape # wxImage = wx.EmptyImage(row,col) # wxImage.SetData(frame_threshed.tostring()) # wxBmap = wxImage.ConvertToBitmap() # self.processed_image.SetBitmap(wx.BitmapFromBits(frame_threshed.tostring(),row,col)) dst = cv2.bitwise_and(img, img1) # self.processed_image.SetBitmap(wx.BitmapFromImage(img1)) self.processed_image.SetBitmap(wx.BitmapFromBuffer(col, row, dst)) # self.processed_image.SetBitmap(wx.BitmapFromImage(dst)) self.panel2.Refresh() print "d"
def compare(face_to_check,learn=False): import sys, getopt detected_time = 0 detected_time_max = 10 video_src = 0 cascade_fn = os.path.join('data','haarcascades','haarcascade_frontalface_alt2.xml') cascade = cv2.CascadeClassifier(cascade_fn) cam = create_capture(video_src, fallback='synth:bg=../cpp/lena.jpg:noise=0.05') while True: ret, img1 = cam.read() gray = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY) gray = cv2.equalizeHist(gray) t = clock() rects = detect(gray, cascade) if len(rects): if detected_time<detected_time_max: detected_time+=1 else: _found_size = (rects[0][0],rects[0][1],rects[0][2]-rects[0][0], rects[0][3]-rects[0][1]) _found_face = cv.GetImage(cv.fromarray(img1)) cv.SetImageROI(_found_face,_found_size) current_face = cv.CreateImage(cv.GetSize(_found_face), _found_face.depth, _found_face.nChannels) if learn: cv.Copy(_found_face, current_face, None) cv.SaveImage(os.path.join('data','images',face_to_check),current_face) cv.ResetImageROI(cv.GetImage(cv.fromarray(img1))) img2 = cv.LoadImage(os.path.join('data','images',face_to_check)) dest_face = cv.CreateImage(cv.GetSize(img2), img2.depth, img2.nChannels) cv.Resize(_found_face, dest_face) if cv.Norm(dest_face,img2)<=30000: return True else: return False sys,exit() else: detected_time = 0 dt = clock() - t
def genView(panopath, outpath, orientation, viewdir, detail): # panopath: location of raster, depth and plane_pano images # outpath: location to put generated view, depth, and plane images # orientation: [yaw, pitch, roll] of panorama (in degrees) # viewdir: clock-based view; in set [2,3,4,8,9,10] for database # detail: 0 = 768x512 with 60d fov, 1 = 2500x1200 with 90d fov # local constants start = time.time() pi = np.pi width, height, fov = (2500, 1200, 90) if detail else (768, 512, 60) # view details Rpano = geom.RfromYPR(orientation[0],orientation[1],orientation[2]) Yview = np.mod( orientation[0] + 30*viewdir, 360 ) Rview = geom.RfromYPR(Yview, 0, 0) Kview = geom.cameramat(width, height, fov*pi/180) Kinv = alg.inv(Kview) # Load image pano, depth pano, and plane pano images cvIP = cv.LoadImageM( os.path.join(panopath,'raster.jpg'), cv.CV_LOAD_IMAGE_UNCHANGED ) cvDP = cv.fromarray( np.asarray( Image.open( os.path.join(panopath,'depth.png') ) ) ) pp = np.asarray( Image.open( os.path.join(panopath,'plane_pano.png') ) ).copy() vals = set(list(pp.reshape(-1))) vals.remove(255) gnd = max(vals) pp[pp==gnd] = np.uint8(0) cvPP = cv.fromarray(pp) # load pixel map pix = np.append(np.array(np.meshgrid(range(width),range(height))).reshape(2,-1),np.ones([1,width*height]),0) midpoint = time.time() print 'Loading pano images took ' + str(midpoint-start) + ' seconds.' # Create output openCV matrices cvI = cv.CreateMat(height,width,cv.CV_8UC3) cvD = cv.CreateMat(height,width,cv.CV_32SC1) cvP = cv.CreateMat(height,width,cv.CV_8UC1) # compute mappings ray = np.dot( np.transpose(Rpano), np.dot( Rview, np.dot( Kinv, pix ) ) ) yaw, pitch = np.arctan2( ray[0,:] , ray[2,:] ) , np.arctan2( -ray[1,:] , np.sqrt((np.array([ray[0,:],ray[2,:]])**2).sum(0)) ) ix, iy = cv.fromarray(np.array(8192/(2*pi)*(pi+yaw),np.float32).reshape(height,width)), cv.fromarray(np.array(4096/pi*(pi/2-pitch),np.float32).reshape(height,width)) dx, dy = cv.fromarray(np.array(5000/(2*pi)*(pi+yaw),np.float32).reshape(height,width)), cv.fromarray(np.array(2500/pi*(pi/2-pitch),np.float32).reshape(height,width)) px, py = cv.fromarray(np.array(1000/(2*pi)*(pi+yaw),np.float32).reshape(height,width)), cv.fromarray(np.array( 500/pi*(pi/2-pitch),np.float32).reshape(height,width)) # call remap function cv.Remap(cvIP,cvI,ix,iy,cv.CV_INTER_CUBIC) cv.Remap(cvDP,cvD,dx,dy,cv.CV_INTER_NN) cv.Remap(cvPP,cvP,px,py,cv.CV_INTER_NN) # write images to file Image.fromarray(np.array(cvI)[:,:,[2,1,0]]).save(os.path.join(outpath,'view.jpg'),'jpeg') Image.fromarray(np.array(cvD)).save(os.path.join(outpath,'depth.png'),'png') Image.fromarray(np.array(cvP)).save(os.path.join(outpath,'plane.png'),'png') print 'Generating views from pano took ' + str(time.time()-midpoint) + ' seconds.'
def subimage(image, centre, theta, width, height): theta *= -1.0 output_image = cv.fromarray(np.zeros((height, width), np.float32)) mapping = np.array([[np.cos(theta), -np.sin(theta), centre[0]], [np.sin(theta), np.cos(theta), centre[1]]]) map_matrix_cv = cv.fromarray(mapping) cv.GetQuadrangleSubPix(image, output_image, map_matrix_cv) return output_image
def subimage(image, center, theta, width, height): output_image = cv.CreateImage((width, height), 8, 1) mapping = np.array([[np.cos(theta), -np.sin(theta), center[0]], [np.sin(theta), np.cos(theta), center[1]]]) map_matrix_cv = cv.fromarray(mapping) cv.GetQuadrangleSubPix(cv.fromarray(image), output_image, map_matrix_cv) # http://stackoverflow.com/questions/13104161/fast-conversion-of-iplimage-to-numpy-array return np.asarray(output_image[:,:])
def replace_color(self,col=None): print self.hue[0][0] self.hue_val=col #cv2.imshow("hue",self.hue) if col!=None: cv.Set(cv.fromarray(self.hue),(self.hue_val),cv.fromarray(self.mask)) self.scratch=cv2.merge([self.hue,self.sat,self.val]) self.scratch=cv2.cvtColor(self.scratch,cv2.cv.CV_HSV2BGR) print 'replaced' return self.scratch
def get_head_image(frame, slyce): try: hx = slyce.c_head_location_x hy = slyce.c_head_location_y centre = (int(hx), int(hy)) theta = np.radians(slyce.d_bodyAxis) patch = subimage(cv.fromarray(frame), centre, theta, width, height) except: patch = subimage(cv.fromarray(frame), (502,502), 0, width, height) data = np.array(patch) rat = data[75:95, 65:85].mean() / data[35:55, 65:85].mean() return data
def disparity(self): if self.left is None or self.right is None: print "Capture left and right images using 'l' and 'r' keys before running disparity" return None hl, wl = self.left.shape hr, wr = self.right.shape disp_left = cv2.cv.CreateMat(hl, wl, cv2.cv.CV_16S) disp_right = cv2.cv.CreateMat(hr, wr, cv2.cv.CV_16S) state = cv2.cv.CreateStereoGCState(16,2) # running the graph-cut algorithm from cv2.cv import fromarray cv2.cv.FindStereoCorrespondenceGC(fromarray(self.left), fromarray(self.right), disp_left, disp_right, state) cv2.cv.Save( "left.png", disp_left) # save the map cv2.cv.Save( "right.pgm", disp_right) # save the map
def tweetColor(color,frame): global capture global lastCheer try: print "========================Tweeting" + color + "===========================" with open("/home/root/CheerBot/stop.txt", "r+") as fo: fo.seek(0, 0) fo.write("1") fo.closed time.sleep(2) image_path = '/home/root/CheerBot/found.jpg' #fresh = cv.QueryFrame(capture) capture.set(3,1920) capture.set(4,1080) ret, fresh = capture.read() capture.set(3,320) capture.set(4,240) if not ret: print "Capture Failed" fresh = cv.fromarray(fresh) cv.SaveImage(image_path,fresh) if (3600 < (time.clock() - lastCheer)): salut = """@Cheerlights, """ lastCheer = time.clock() else: salut = '' api.update_status_with_media(status = (salut + "I spy something " + color + ": " + time.strftime('%m-%d-%y-%H%M', time.localtime())), media=image_path) with open("/home/root/CheerBot/stop.txt", "r+") as fo: fo.seek(0, 0) fo.write("0") fo.closed except tweetpony.APIError as err: print "Oops, something went wrong! Twitter returned error #%i and said: %s" % (err.code, err.description)
def get_head_image_looper(_frame, _vals): hx = _vals.c_head_location_x hy = _vals.c_head_location_y centre = (int(hx), int(hy)) theta = np.radians(_vals.d_bodyAxis) patch = subimage(cv.fromarray(_frame), centre, theta, width, height) return np.array(patch)
def run(self, frame): rects = [] found = list( cv.HOGDetectMultiScale( cv.fromarray(frame), self.storage, win_stride=self.w_s, padding=self.p, scale=self.s, group_threshold=self.g_t, ) ) found_filtered = [] for r in found: insidef = False for q in found: if self.inside(r, q): insidef = True break if not insidef: found_filtered.append(r) for r in found_filtered: (rx, ry), (rw, rh) = r # tl = (rx + int(rw*0.1), ry + int(rh*0.07)) # br = (rx + int(rw*0.9), ry + int(rh*0.87)) rects.append((rx + int(rw * 0.1), ry + int(rh * 0.1), rx + int(rw * 0.9), ry + int(rh * 1))) return rects
def __init__(self, parent=None): super(camWidget, self).__init__() self._capture = cv2.VideoCapture(0) # except: # print 'error opening camera: ' , sys.exc_info()[0] # # raise self.time = time.time() ret, imagearray = self._capture.read() frame=cv.fromarray(imagearray) self.setMinimumSize(frame.width,frame.height) self.setMaximumSize(self.minimumSize()) self.image_size = cv.GetSize(frame) self._image = IplQImage(frame) self._timer = QtCore.QTimer(self) self._timer.timeout.connect(self.getFrame) self.time = time.time() self._timer.start(1)
def write_frame(client, frame, boundary = '1337'): ret = cv.EncodeImage('.jpeg', cv.fromarray(frame)) image_bytes = bytearray(np.asarray(ret)) client.send("Content-type: image/jpeg\r\n") client.send("Content-Length: %d\r\n\r\n" % len(image_bytes)) client.send(image_bytes) client.send("\r\n--" + boundary + "\r\n")
def process_frames(self): while True: frame = cv.QueryFrame(self.capture) aframe = numpy.asarray(frame[:, :]) g = cv.fromarray(aframe) g = numpy.asarray(g) imgray = cv2.cvtColor(g, cv2.COLOR_BGR2GRAY) raw = str(imgray.data) scanner = zbar.ImageScanner() scanner.parse_config('enable') imagezbar = zbar.Image(frame.width, frame.height, 'Y800', raw) scanner.scan(imagezbar) # Process the frames for symbol in imagezbar: if not self.process_symbol(symbol): return # Update the preview window cv2.imshow(self.window_name, aframe) cv.WaitKey(5)
def scaleTo8U(bldIm, counts): w = bldIm.shape[1] h = bldIm.shape[0] mat = np.zeros((h, w, bldIm.shape[2]), dtype=np.int32) mat[:, :, 0] = np.where(counts == 0, 1, counts) mat[:, :, 1] = np.where(counts == 0, 1, counts) mat[:, :, 2] = np.where(counts == 0, 1, counts) mat = cv.fromarray(mat) cvbldIm = cv.fromarray(bldIm) bldIm8U = cv.CreateMat(h, w, cv.CV_8UC3) cv.Div(cvbldIm, mat, cvbldIm) cv.Scale(cvbldIm, bldIm8U, 1.0, 0) return np.asarray(bldIm8U)
def blend_views(bldIm, frame, mask, frec, max_rec): maskMat = cv.fromarray(mask) dispX = int(np.round(frec.left - max_rec.left)) dispY = int(np.round(frec.top - max_rec.top)) bldROI = cv.GetImage(bldIm) cv.SetImageROI(bldROI, (dispX, dispY, int(np.round(frec.width())), int(np.round(frec.height())))) cv.Add(bldROI, cv.fromarray(frame), bldROI, maskMat) cv.ResetImageROI(bldROI) cv.Copy(bldROI, bldIm) return bldIm
def _project_other_roi(self): warped_in = np.float32([np.array(self._other_roi_points)]) project_out = np.float32([[0, 0], [self._side_other_roi, 0], [self._side_other_roi, self._side_other_roi], [0, self._side_other_roi]]) M = cv2.getPerspectiveTransform(warped_in, project_out) self.subLock.acquire(True) local_image = deepcopy(self._np_image) self.subLock.release() self._other_projected = cv2.warpPerspective(local_image, M, (self._side_other_roi, self._side_other_roi)) # Finds red colors in HSV space hsv = cv2.cvtColor(self._other_projected, cv2.COLOR_BGR2HSV) self._inrange_colour = cv2.inRange(hsv, self._low_colour, self._high_colour) cv.ShowImage('Colour', cv.fromarray(self._inrange_colour)) # the following can probably be optimized red_cnt = 0 for x in range(self._side_other_roi): for y in range(self._side_other_roi): if red_cnt > self._inrange_colour_thresh: # Speed tweak return True else: if self._inrange_colour[x, y] == 255: red_cnt += 1 return False
def _filter_red(self): # Finds red colors in HSV space hsv = cv2.cvtColor(self._projected, cv2.COLOR_BGR2HSV) lower_red = np.array([165, 60, 60]) upper_red = np.array([180, 255, 255]) self._red = cv2.inRange(hsv, lower_red, upper_red) cv.ShowImage('Red', cv.fromarray(self._red))
def _show_image(self): self.subLock.acquire(True) local_image = deepcopy(self._np_image) self.subLock.release() # draw circles for idx, points in enumerate(self._roi_points): cv2.circle(local_image, (points[0], points[1]), 5, (255, 0, 0), 2) # draw green lines cv2.polylines(local_image, np.int32([np.array(self._roi_points)]), 1, (0, 255, 0), 2) cv2.polylines(local_image, np.int32([np.array( self._other_roi_points)]), 1, (0, 255, 0), 2) cv.ShowImage("Learn Play game RGB", cv.fromarray(local_image)) cv.SetMouseCallback("Learn Play game RGB", self._on_mouse_click, 0) cv.CreateTrackbar("Gain", "Learn Play game RGB", self._gain_slider, 100, self._on_gain_slider) cv.CreateTrackbar("Red Threshold", "Learn Play game RGB", self._inrange_colour_thresh, 500, self._on_red_slider) cv.CreateTrackbar("High red", "Learn Play game RGB", self._high_colour_slider, 40, self._on_high_colour_slider) cv.CreateTrackbar("Low red", "Learn Play game RGB", self._low_colour_slider, 40, self._on_low_colour_slider) cv.WaitKey(3)
def start_logging(): """Takes a picture using opencv and sends it to the manager every 4 seconds.""" while device_logging.is_set(): gevent.sleep(0) if not vid.isOpened(): vid.open(-1) vid.set(cv.CV_CAP_PROP_FRAME_WIDTH, 1280) vid.set(cv.CV_CAP_PROP_FRAME_HEIGHT, 720) gevent.sleep(0.05) flag, im_array = vid.read() img = cv.fromarray(im_array) gevent.sleep(0.05) cv.SaveImage("static/cam.jpeg", img) cmd_str = "/manager.php?action=storeBig&uuid=" url = manager_ip + ":" + manager_port + cmd_str + device_uuid handle = open("static/cam.jpeg", "rb") image = handle.read() print "Logging picture" r = requests.post(url, data=image) print r.status_code handle.close() cv.Zero(img) vid.release() gevent.sleep(4)
def crop_waffle(img): hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) greyscale = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) lower_yellow = np.array([0,50,50]) upper_yellow = np.array([70,255,255]) mask = cv2.inRange(hsv, np.uint8(lower_yellow), np.uint8(upper_yellow)) kernel = np.ones((9,9),np.uint8) closed_mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel) masked_img = cv2.bitwise_and(greyscale,greyscale,mask = closed_mask) [contours,hiearchy] = cv2.findContours(masked_img,cv.CV_RETR_EXTERNAL,cv.CV_CHAIN_APPROX_SIMPLE) #now find the largest contour max_area = 0 max_contour = None for c in contours: #we change datatypes from numpy arrays to cv arrays and back because contour area only takes cv arrays. c = cv.fromarray(c) if cv.ContourArea(c) > max_area: max_contour = c max_area = cv.ContourArea(c) max_contour = np.asarray(max_contour) shape = img.shape largest_blob_mask = np.zeros((shape[0],shape[1],1),np.uint8) cv2.fillPoly(largest_blob_mask, pts =[max_contour], color=(255,255,255)) print_rgb_hist(img,largest_blob_mask) return cv2.bitwise_and(img,img, mask= largest_blob_mask)
def __init__(self, image, grayness=False): """Konstruktor za objekt Image.""" self._image = image # ovo je OpenCV image dobiven iz imread self._grayness = grayness # true ako je slika siva, false inace (self._width, self._height) = cv.GetSize(cv.fromarray(self._image)) # dimenzije self._coef = 1.0 # koeficijent resizanja slike [0..1]
def capture(video): video = cv.CaptureFromFile(video) cv.SetCaptureProperty( video, cv.CV_CAP_PROP_FRAME_WIDTH, 280 ) cv.SetCaptureProperty( video, cv.CV_CAP_PROP_FRAME_HEIGHT, 160 ) width = int(cv.GetCaptureProperty(video, cv.CV_CAP_PROP_FRAME_WIDTH)) height = int(cv.GetCaptureProperty(video, cv.CV_CAP_PROP_FRAME_HEIGHT)) n = cv.GetCaptureProperty(video, cv.CV_CAP_PROP_FRAME_COUNT) curr = 0 while True: cv.SetCaptureProperty(video, cv.CV_CAP_PROP_POS_FRAMES, curr) frame1 = cv.QueryFrame(video) frame1 = cv.CloneImage(frame1) #if frame1 == None: #break frame2 = cv.QueryFrame(video) frame2 = cv.CloneImage(frame2) array1 = toarray(frame1) array2 = toarray(frame2) out = difference(array1, array2) out = grayScale(out) out = binarize(out, 10) paint = paint_motion(array1, out) detection = detect(frame1, out, width, height) im = cv.fromarray(paint) #im = cv.fromarray(out) cv.SaveImage('motion/prueba%s.png'%curr,detection) #cv.SaveImage('motion/prueba%s.png'%curr,im) curr+=1 if curr >= n - 1: break
def _filter_yellow(self): # Finds yellow colors in HSV space hsv = cv2.cvtColor(self._projected, cv2.COLOR_BGR2HSV) lower_yellow = np.array([20, 60, 60]) upper_yellow = np.array([45, 255, 255]) self._yellow = cv2.inRange(hsv, lower_yellow, upper_yellow) cv.ShowImage('Yellow', cv.fromarray(self._yellow))
def callFaceTracker(self, imageFrame): imageMat = cv.fromarray(imageFrame) image = cv.GetImage(imageMat) (isFaceDetected, detectedFaceImage, wholeImage, pt1, pt2) = self.tracker.detect_and_draw(image) if isFaceDetected: array = np.asarray(detectedFaceImage, np.uint8, 3) arrayCopy = array.copy() cv2.imshow("face image ", arrayCopy) # print "got type " + str(type(arrayCopy)) a = Image.fromarray(arrayCopy) b = ImageTk.PhotoImage(image=a) self.canvas2.create_image(0, 0, image=b, anchor=tk.NW) self.canvas2.update() print "here....." self.goToNextState(True) originalImage2 = cv.CloneImage(wholeImage) self.camshift = Camshift() self.camshift.defineRegionOfInterest(originalImage2, pt1, pt2)
def collect_costs_info(cost_info, counts, frame, mask, frec, max_rec, idx): # Now convert incoming frame to HSV color space and # copy the color of each pixel to the corresponding # location in newInfo hsvIm = cv.fromarray(frame) cv.CvtColor(hsvIm, hsvIm, cv.CV_RGB2HSV) frame_hsv = np.asarray(hsvIm) dispX = int(np.round(frec.left - max_rec.left)) dispY = int(np.round(frec.top - max_rec.top)) blndMask = np.zeros(counts.shape, dtype=int) cost_info[dispY:int(dispY + np.round(frec.height())):, dispX:int(dispX + np.round(frec.width())):, idx, :3] \ = frame_hsv[:, :, :3] blndMask[dispY:int(dispY + np.round(frec.height())):, dispX:int(dispX + np.round(frec.width())):] \ = mask[:, :] blndMask = blndMask > 0 counts[blndMask] = counts[blndMask] + 1 return cost_info
def render(self, window): with self.lock: if self.image and self.image_time + rospy.Duration(2.0) > rospy.Time.now() and self.info_time + rospy.Duration(2.0) > rospy.Time.now(): cv2mat = self.bridge.imgmsg_to_cv2(self.image, 'rgb8') cvmat = cv.fromarray(cv2mat) cv.Resize(cvmat, window) interval = min(1,(self.interval / self.max_interval)) cv.Rectangle(window, (int(0.05*window.width), int(window.height*0.9)), (int(interval*window.width*0.9+0.05*window.width), int(window.height*0.95)), (0, interval*255, (1-interval)*255), thickness=-1) cv.Rectangle(window, (int(0.05*window.width), int(window.height*0.9)), (int(window.width*0.9+0.05*window.width), int(window.height*0.95)), (0, interval*255, (1-interval)*255)) cv.PutText(window, self.ns, (int(window.width * .05), int(window.height * 0.1)), self.font1, (0,0,255)) if self.features and self.features.header.stamp + rospy.Duration(4.0) > self.image.header.stamp: w_scaling = float (window.width) / self.image.width h_scaling = float (window.height) / self.image.height if self.features.success: corner_color = (0,255,0) for cur_pt in self.features.image_points: cv.Circle(window, (int(cur_pt.x*w_scaling), int(cur_pt.y*h_scaling)), int(w_scaling*5), corner_color) else: window = add_text(cv2mat, ["Could not detect", "checkerboard"], False) else: window = add_text(cv2mat, ["Timed out waiting", "for checkerboard"], False) else: # Generate random white noise (for fun) noise = numpy.random.rand(window.height, window.width)*256 numpy.asarray(window)[:,:,0] = noise; numpy.asarray(window)[:,:,1] = noise; numpy.asarray(window)[:,:,2] = noise; cv.PutText(window, self.ns, (int(window.width * .05), int(window.height * .95)), self.font, (0,0,255))
def process(img, blocks): cv2.imshow("img", img) cv2.imshow("blocks", blocks) C, M, Y, K = util.cvtColorBGR2CMYK_(blocks) #convert to CMYK #imgNoBgnd = getUniformBackGround(img, K) #get uniform background #cv2.imshow("imgnobgnd", imgNoBgnd) invWhiteLines = getWhiteLines(C, M, Y, K) #get only white lines #cv2.imshow("White Lines", invWhiteLines) #remove white lines from image invWhite3 = cv2.merge([invWhiteLines, invWhiteLines, invWhiteLines]) #imgBlocks = cv2.bitwise_and(imgNoBgnd, invWhite3) imgBlocks = cv2.bitwise_and(img, invWhite3) #imgBlocks = blocks #find the blocks in imgBlocks imgray = cv2.cvtColor(imgBlocks,cv.CV_RGB2GRAY); cv2.imshow("grayscale for blocks", imgray) _,imbw = cv2.threshold(imgray, 0, 255, cv2.THRESH_BINARY) cv2.imshow("bw for blocks", imbw) #cv2.imshow("img Blocks", imgBlocks) ctrs = cv2.findContours(imbw, cv2.RETR_LIST , cv2.CHAIN_APPROX_SIMPLE); #imgCopy = img rectList = getRects(img, ctrs) #img #print "B:\tG:\tR:" for i in range(len(rectList)): #print rectList[i] #(x,y),(w,h),t = rectList[i][1] #print x,y,w,h,t #x,y,w,h = rectList[i][0] rec = np.asarray(cv.GetSubRect(cv.fromarray(imgBlocks),rectList[i][0])) cv2.imshow(str(i), rec)
def process_image (self, image): self.images["Src"] = deepcopy (image) self.images["MFilter"] = cv2.medianBlur (self.images["Src"], 9) self.images["HSV"] = cv2.cvtColor (self.images["MFilter"], cv2.COLOR_BGR2HSV) self.images["Color_Filter"] = cv2.inRange (self.images["HSV"], (170, 50, 50), (180, 230, 230)) self.outputs["Contours"], heirarchy = cv2.findContours (deepcopy (self.images["Color_Filter"]), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cv2.drawContours (self.images["Src"], self.outputs["Contours"], -1, (0, 0, 0), thickness=5) self.outputs["Big_Contours"] = [] for i in self.outputs["Contours"]: area = cv.ContourArea (cv.fromarray (i)) moment = cv2.moments (i) if area > 3330: self.outputs["Big_Contours"].append (i) print area, print " ", print moment return 1 return 0
def funGetResponseCurve2(camera, levelContinuous, levelHaltone, widthF, heightF, sub_rec1, sub_rec2): ''' The function does things like measuring response curve of continuous tone and halftone ramp as the webcam was a proper measuring device, yes you read it right MF. ''' global vecLevel global vecSearchLevel global sizeTilePatchHT global tabOscillographeDifferences global max_number_frame_to_wait_between_measurement # imitialize some parameters tabResultsContinousL = np.zeros((1,np.size(vecSearchLevel))) tabResultsHalftoneL = np.zeros((1,np.size(vecSearchLevel))) tabDiffContinuousHalftoneL = np.zeros((1,np.size(vecSearchLevel))) print 'data to be saved of size ',np.shape(tabResultsContinousL) counterSearchLevel = 0 for searchLevel in vecSearchLevel: # Here I create a testchart imgTestchart = imCreateTestchartContinuousAndHalftoned(searchLevel, searchLevel, sizeTilePatchHT) imgT = cv.CreateImage((widthF,heightF), cv.IPL_DEPTH_8U,1) imgT = cv.fromarray(imgTestchart) cv.ShowImage("winTestChart",imgT) #testChartName = 'imTestChart_'+repr(searchLevel)+'_'+repr(searchLevel)+'.png' testChartName = 'imTestChart_'+repr(searchLevel)+'_'+repr(searchLevel)+'.jpg' cv.SaveImage('./'+testChartName, imgT) cv.ShowImage("winTestChart",imgT) cv.WaitKey(10) # I display the stream again timer = 0 while timer < max_number_frame_to_wait_between_measurement: #for i in np.arange(0,50): #print '\ntemp frame ' + repr(timer) + '/24.' frame = cv.QueryFrame(camera) frame, dL, dLab, LabT, LabB, dY, XYZT, XYZB, dRGB = function_display_live_information(frame, widthF, heightF, sub_rec1, sub_rec2) # Here we do something to display data difference as an osilloscope tabOscillographeDifferences[:,0:-1] = tabOscillographeDifferences[:,1:] tabOscillographeDifferences[:,-1] = [dL, dLab, dY, dRGB] frame = function_display_oscilloscope(frame, widthF, heightF, tabOscillographeDifferences) cv.ShowImage("Window", frame) timer = timer +1 cv.WaitKey(5) #if timer == max_number_frame_to_wait_between_measurement: # Here I save the image taken by the webcam #funCaptureImage('frameFor_'+testChartName,frame, dirToSaveWebcamFrame) # And here we save the differences. We will use them for computing the various ratio tabResultsContinousL[0,counterSearchLevel] = LabT[0] # Here we save the Luminance of the left patch alone tabResultsHalftoneL[0,counterSearchLevel] = LabB[0] # Here we save the Luminace of the right patch alone tabDiffContinuousHalftoneL[0,counterSearchLevel] = dL # Here we save the difference counterSearchLevel = counterSearchLevel + 1 #print counterSearchLevel return frame, tabDiffContinuousHalftoneL, tabResultsContinousL, tabResultsHalftoneL
def subspace_project(self, test_features): projected_file = os.path.join(self.model_dir, 'proj.xml') # creating copy of subspace file since we over write the same file temp_subspace_file = os.path.join( self.model_dir, 'temp_subspace_file_%s.xml' % datetime.utcnow()) shutil.copy(self.subspace_file, temp_subspace_file) # create a feature file feature_file = os.path.join(self.model_dir, 'test_features.xml') la = np.asarray([[-1] * test_features.shape[0]]) cv_feats = np.transpose(np.concatenate( (la, test_features.transpose()))) mat_to_save = cv_feats.transpose().copy() cv.Save(feature_file, cv.fromarray(mat_to_save)) # call binary bin_dir = config.bin_dir() run_cmd([ os.path.join(bin_dir, 'aff_face_subspace_project'), feature_file, temp_subspace_file, projected_file ]) return temp_subspace_file, projected_file
def image (self): """ Get an image from the camera. Returns a SimpleCV Image. """ try: flag, img_array = yield threads.deferToThread(self.camera.read) except SystemError: return if flag is False: print("No image") return defer.returnValue(SimpleCV.Image( source = cv.fromarray(img_array), cv2image = True) )
def drawPlate(self, plate=True): pos1 = (self.select.startX, self.select.startY) pos2 = (self.select.endX, self.select.endY) if (pos1[0] > pos2[0]) or (pos1[1] > pos2[1]): cor1 = cv.Scalar(0, 0, 255, 0) cor2 = cv.Scalar(255, 255, 255, 0) thick = 8 else: cor1 = cv.Scalar(36, 186, 255, 0) cor2 = cv.Scalar(8, 44, 72, 0) thick = 4 numpyImg = cv2.resize(numpy.asarray(self.src[:, :]), (0, 0), fx=self.fx, fy=self.fy) cimg = cv.fromarray(numpyImg) # draw plate if setted if plate: cv.Rectangle(cimg, pos1, pos2, cor1, thickness=thick) cv.Rectangle(cimg, pos1, pos2, cor2, thickness=2) cv.ShowImage("Camera", cimg)
def gist_feature_extract(in_file, out_file, orientations, blocks): """ Extract gist descriptors for every image in in_file Args: in_file: infofile out_file: xml with gist descriptors (opencv format) orientations: a list of orientations per scale [8, 8, 8, 8] nblocks: number of blocks to partition the image (grid) """ verify_files_readable(in_file) verify_files_writable(out_file) with open(in_file, 'r') as fo: files = fo.read().splitlines() g = Gist(orientations, blocks) features = np.zeros((len(files), g.nfeatures), dtype=np.float32) for idx, f in enumerate(files): img = f.split() descriptor = g.compute_gist_descriptor(img[0]) features[idx, :] = descriptor final = cv.fromarray(features) cv2.cv.Save(out_file, final)
def getText(image0): #thicken the border in order to make tesseract feel happy offset = textOffset height, width, channel = image0.shape image1=cv2.copyMakeBorder(image0, offset, offset,offset,offset,\ cv2.BORDER_CONSTANT,value=rgbWhite) #set up tesseract API api = tesseract.TessBaseAPI() api.Init(".", "eng", tesseract.OEM_DEFAULT) api.SetPageSegMode(tesseract.PSM_AUTO) height1, width1, channel1 = image1.shape #star text extraction cvmat_image = cv.fromarray(image1) iplimage = cv.GetImage(cvmat_image) #extract texts tesseract.SetCvImage(iplimage, api) text = api.GetUTF8Text() conf = api.MeanTextConf() return text, conf
def get_hand_area(im): im_tmp = im.copy() drawing = np.zeros(im.shape) drw_mat = np.zeros(im.shape)[:,:,0] gray_img = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY) #mark = cv2.Canny(im, 1, 255) (thresh, bin_img) = cv2.threshold(gray_img, 0, 255, cv2.THRESH_BINARY|cv2.THRESH_OTSU) contours, hierarchy = cv2.findContours(bin_img, cv2.RETR_LIST, cv2.CHAIN_APPROX_TC89_L1) #contours = get_hand_contours(im) # using edges # 距離変換 im_dist = cv2.distanceTransform(bin_img, cv.CV_DIST_L2, cv.CV_DIST_MASK_PRECISE) """ cv2.imshow( "Result", im_dist ) cv2.waitKey(6) """ # 0-255に正規化 #dst = cv2.normalize(im_dist) minVal, maxVal, minLoc, maxLoc = cv2.minMaxLoc(im_dist) # maxLoc=(x,y) for c in contours: if len(c) < 20: continue # 点が少なすぎる場合ははじく cv2.drawContours(drawing,[c],0,(0,255,0),-2) # 最大点を中に含んでいれば tmp_im = np.array(drawing[:,:,1]) drw_mat = cv.fromarray(tmp_im) if maxLoc[0]>=0 and maxLoc[1]>=0 and \ maxLoc[0]<=im.shape[1] and maxLoc[1]<=im.shape[0]: if cv.GetReal2D(drw_mat, maxLoc[1], maxLoc[0]): # cvGetReal2D( 入力画像, y, x ) contour_max = c break dst = np.asarray(drw_mat) """ cv2.imshow( "Result", dst ) cv2.waitKey(5) """ area = np.sum(1*(dst>0)) return area
def detect(self, frame): hsv_img = cv2.cvtColor(frame, cv.CV_BGR2HSV) LASER_MIN = np.array([0, 0, 230], np.uint8) LASER_MAX = np.array([8, 115, 255], np.uint8) frame_threshed = cv2.inRange(hsv_img, LASER_MIN, LASER_MAX) #cv.InRangeS(hsv_img,cv.Scalar(5, 50, 50),cv.Scalar(15, 255, 255),frame_threshed) # Select a range of yellow color src = cv.fromarray(frame_threshed) #rect = cv.BoundingRect(frame_threshed, update=0) leftmost = 0 rightmost = 0 topmost = 0 bottommost = 0 temp = 0 laserx = 0 lasery = 0 for i in range(src.width): col = cv.GetCol(src, i) if cv.Sum(col)[0] != 0.0: rightmost = i if temp == 0: leftmost = i temp = 1 for i in range(src.height): row = cv.GetRow(src, i) if cv.Sum(row)[0] != 0.0: bottommost = i if temp == 1: topmost = i temp = 2 laserx = cv.Round((rightmost + leftmost) / 2) lasery = cv.Round((bottommost + topmost) / 2) #return (leftmost,rightmost,topmost,bottommost) return laserx, lasery
def pav_dejimas_ant_veido(self, frame): """Paveikslelio dejimas ant veido""" frame_cvmat = cv.fromarray(frame) #sukuriamas kadras kitu formatu webcam_width = self.webcam.get(3) #cameros resoliuzijos gavimas webcam_height = self.webcam.get(4) # running the classifiers detectedFace = cv.HaarDetectObjects(frame_cvmat, haarFace, storage) #rasta veida dengia paveiksleliu if detectedFace: for face in detectedFace: s_img = self.image s_img = cv2.resize(s_img, (face[0][2], face[0][3])) if face[0][2] > webcam_width - face[0][0] and face[0][3] > webcam_height - face[0][1]: s_img = s_img[0:webcam_width - face[0][0], 0:webcam_height - face[0][1]] print s_img.shape[0], s_img.shape[1] x_offset=face[0][0] y_offset=face[0][1] for c in range(0,3): frame[y_offset:y_offset+s_img.shape[0], x_offset:x_offset+s_img.shape[1], c] = s_img[:,:,c] * (s_img[:,:,3]/255.0) + frame[y_offset:y_offset+s_img.shape[0], x_offset:x_offset+s_img.shape[1], c] * (1.0 - s_img[:,:,3]/255.0) return frame
def populate_from_mp4_file(self,file): if(cv2 is None): raise ValueError, "MP4 is unavailable as OpenCV is not installed" vc = cv2.VideoCapture(file) self.width = int(vc.get(cv.CV_CAP_PROP_FRAME_WIDTH)) self.height = int(vc.get(cv.CV_CAP_PROP_FRAME_HEIGHT)) frame_count = int(vc.get(cv.CV_CAP_PROP_FRAME_COUNT)) #vc.set(cv.CV_CAP_PROP_CONVERT_RGB, True) #print "width:" + str(self.width) + " Height: " + str(self.height) + "frame count: " + str(frame_count) for i in range(frame_count): rval, video_frame = vc.read() if rval is not None: video_frame = cv2.cvtColor(video_frame,cv2.cv.CV_BGR2RGB) the_frame = cv.fromarray(video_frame) # surface = pygame.image.frombuffer(the_frame.tostring(), (self.movie.width, self.movie.height), 'RGB') surf = sdl2_DisplayManager.inst().make_texture_from_imagebits(bits=the_frame.tostring(), width=self.width, height=self.height, mode='RGB', composite_op = None) new_frame = Frame(self.width, self.height, from_surface=surf) self.frames.append(new_frame) vc.release()
def leerPlaca(self, binarizado): """ Args: img: imagen que contiene el texto, obtenida con la funcion cv2.imread. Returns: El string del texto en la imagen. """ cvmat_image = cv.fromarray(binarizado) imgbincv = cv.GetImage(cvmat_image) # GetImage fuente de un error, revisar # Configuración del tesseract api = tesseract.TessBaseAPI() api.Init(".", "eng", tesseract.OEM_DEFAULT) api.SetPageSegMode(tesseract.PSM_AUTO) # Enviando imagen binarizada al tesseract tesseract.SetCvImage(imgbincv, api) text = api.GetUTF8Text() text = re.sub(r'\W+', '', text) return text
def main(): cv.NamedWindow("camera", 1) #cv.NamedWindow("camera1", 1) #cv.NamedWindow("camera2", 1) capture = cv.CaptureFromCAM(0) mid = cv.QueryFrame(capture) frames = cv.QueryFrame(capture) while True: mid = cv.CloneImage(frames) img = cv.QueryFrame(capture) im_mid = cv.CreateMat(mid.height, mid.width, cv.CV_8U) cv.CvtColor(mid, im_mid, cv.CV_RGB2GRAY) im_img = cv.CreateMat(img.height, img.width, cv.CV_8U) cv.CvtColor(img, im_img, cv.CV_RGB2GRAY) im_mi = np.asarray(im_mid) im_i = np.asarray(im_img) #differ(array_mid,array_img) a=0.01 for y in range(im_i.shape[1]): for x in range(im_i.shape[0]): im_mi[x][y]= (a)*int(im_mi[x,y])+(1-a)*int(im_i[x,y]) img_diff = cv.fromarray(im_mi) frames = im_mi.operator IplImage(); #iplImage = cv2.adaptors.NumPy2Ipl(im_mi) cv.ShowImage("After differ", img_diff) c=cv.WaitKey(1) if c==27: #Break if user enters 'Esc'. break
def __init__(self, ns_list): print "Creating aggregator for ", ns_list self.lock = threading.Lock() # image w = 640 h = 480 #self.image_out = cv.CreateMat(h, w, cv.CV_8UC3) self.image_out = numpy.zeros((h, w, 3), numpy.uint8) self.pub = rospy.Publisher('aggregated_image', Image) self.bridge = CvBridge() self.image_captured = get_image(["Successfully captured checkerboard"]) self.image_optimized = get_image(["Successfully ran optimization"]) self.image_failed = get_image(["Failed to run optimization"], False) # create render windows layouts = [ (1,1), (2,2), (2,2), (2,2), (3,3), (3,3), (3,3), (3,3), (3,3) ] layout = layouts[len(ns_list)-1] sub_w = w / layout[0] sub_h = h / layout[1] self.windows = [] cvmat = cv.fromarray(self.image_out) for j in range(layout[1]): for i in range(layout[0]): self.windows.append( cv.GetSubRect(cvmat, (i*sub_w, j*sub_h, sub_w, sub_h) ) ) # create renderers self.renderer_list = [] for ns in ns_list: self.renderer_list.append(ImageRenderer(ns)) # subscribers self.capture_time = rospy.Time(0) self.calibrate_time = rospy.Time(0) self.captured_sub = rospy.Subscriber('robot_measurement', RobotMeasurement, self.captured_cb) self.optimized_sub = rospy.Subscriber('camera_calibration', CameraCalibration, self.calibrated_cb)
def train(self, features, labels): ''' features = all the features for the faces, each row is a feature labels = a list of labels representing the id for each corresponding row in the features array ''' if not self._is_trained: assert len(labels) == features.shape[0] self.model_dir = mkdtemp() # create feature file with given features # feature file needs to be such that the first dim in the label and # then each row in a feature self.feature_file = os.path.join(self.model_dir, 'features.xml') la = np.asarray([labels]) cv_feats = np.transpose(np.concatenate((la, features.transpose()))) mat_to_save = cv_feats.transpose().copy() # nFaces = mat_to_save->cols; # nDesc = mat_to_save->rows-1; cv.Save(self.feature_file, cv.fromarray(mat_to_save)) self.subspace_file = os.path.join(self.model_dir, 'learned_subspace.xml') self.face_subspace() self._is_trained = True
def vid_contour_selection(self): while True: self.frame = cv.QueryFrame(self.capture) aframe = numpy.asarray(self.frame[:,:]) g = cv.fromarray(aframe) g = numpy.asarray(g) imgray = cv2.cvtColor(g,cv2.COLOR_BGR2GRAY) raw = str(imgray.data) scanner = zbar.ImageScanner() scanner.parse_config('enable') imageZbar = zbar.Image( self.frame.width, self.frame.height,'Y800', raw) scanner.scan(imageZbar) for symbol in imageZbar: print 'decoded', symbol.type, 'symbol', '"%s"' % symbol.data subprocess.call('echo "'+ symbol.data +'"| festival --tts', shell=True) cv2.imshow("w1", aframe) c = cv.WaitKey(5) if c == 110: #pressing the 'n' key will cause the program to exit exit()
def leerPlaca(self, binarizado): """ Args: img: imagen que contiene el texto, obtenida con la funcion cv2.imread. Returns: El string del texto en la imagen. """ cvmat_image = cv.fromarray(binarizado) imgbincv = cv.GetImage(cvmat_image) # GetImage fuente de un error, revisar # Configuración del tesseract api = tesseract.TessBaseAPI() api.Init('.', 'eng', tesseract.OEM_DEFAULT) api.SetVariable("tessedit_char_whitelist", "0123456789ABCDFGHIJKLMNOPQRSTVWXYZ-UE") api.SetPageSegMode(tesseract.PSM_AUTO) # Enviando imagen binarizada al tesseract tesseract.SetCvImage(imgbincv, api) text = api.GetUTF8Text() text = re.sub(r'\W+', '', text) return text
print R T = np.load('T.npy') print "-T:" print T E = np.load('E.npy') print "-E:" print E F = np.load('F.npy') print "-F:" print F R1 = np.array((3, 3)) R2 = np.array((3, 3)) P1 = np.array((3, 4)) P2 = np.array((3, 4)) Q = np.array((4, 4)) cameraMatrix1 = cv.fromarray(cameraMatrix1) cameraMatrix2 = cv.fromarray(cameraMatrix2) distCoeffs1 = cv.fromarray(distCoeffs1) distCoeffs2 = cv.fromarray(distCoeffs2) R = cv.fromarray(R) T = cv.fromarray(T) R1 = cv.CreateMat(3, 3, cv.CV_64FC1) R2 = cv.CreateMat(3, 3, cv.CV_64FC1) P1 = cv.CreateMat(3, 4, cv.CV_64FC1) P2 = cv.CreateMat(3, 4, cv.CV_64FC1) Q = cv.CreateMat(4, 4, cv.CV_64FC1) cv.StereoRectify(cameraMatrix1, cameraMatrix2, distCoeffs1, distCoeffs2, (640, 480), R,
def findWhiteDot(self): ## The code here is based on findPupil() from Eye.py """ Detects a whiteDot within a pupil region. Uses opencv libarary methods to detect the white dot in the center of the pupil caused by the reflection of the flash. Algorithm Overview: Load the source image. GrayScale Invert it. Convert to binary image by thresholding it. Find all blobs. Remove noise by filling holes in each blob. Get blob which is big enough and has round shape. Then initializes whiteDot to the region found and sets whiteDotCenter. Returns false if any errors are encountered Args: None Return: bool - True if there were no issues. False for any error """ # Image Processing # read the im from disc using absolute path im = cv2.imread( os.path.join(os.path.dirname(__file__), 'PUPILPHOTO.jpg')) # TODO - change all the random explicit numbers in this method # to descriptively named class level variables if DEBUG: print "im is of type: " + str(type(im)) im2 = im.copy() imblur = cv2.blur(im, (3, 3)) imgray = cv2.cvtColor(imblur, cv2.COLOR_BGR2GRAY) if DEBUG: cv.ShowImage("Grayscaled", cv.fromarray(imgray)) # Grayscale Picture cv.WaitKey(0) cv.DestroyWindow("Grayscaled") ret, thresh = cv2.threshold( imgray, 127, 255, 0) # ret : type float. thresh: type :numpy.ndarray if DEBUG: cv.ShowImage("Binary", cv.fromarray(thresh)) # Binary Picture cv.WaitKey(0) cv.DestroyWindow("Binary") contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) if DEBUG: print("Number of Contours Found: " + str(len(contours))) cv2.drawContours( im, contours, -1, (0, 255, 0), 0 ) # Final argument for drawContours() : 0 = Outline, -1 = Fill-In cv.ShowImage("All Contours", cv.fromarray(im)) cv.WaitKey(0) cv.DestroyWindow("All Contours") # Finding center coordinates of photo photoCenterX = len(im[0]) / 2 photoCenterY = len(im) / 2 if DEBUG: print("Photo's Center Coordinates: (" + str(photoCenterX) + ", " + str(photoCenterY) + ")") min_area = maxint ## This is finding WhiteDot by comparing contour centroids shortestDist = maxint closestCnt = contours[0] closestX = closestY = 0 for cnt in contours: M = cv2.moments(cnt) ## Ignores all contours with M00 = 0, ## because that will cause divide by 0 error if (M['m00'] != 0.0): centroid_x = int(M['m10'] / M['m00']) centroid_y = int(M['m01'] / M['m00']) if DEBUG: print cnt print("\n") print M['m10'], M['m00'] print M['m01'], M['m00'] print("\n\n") dist = np.sqrt( np.square(centroid_x - photoCenterX) + np.square(centroid_y - photoCenterY)) if DEBUG: print("Distance to center = " + str(dist)) ## At the end of the loop, the closest contour to center of image is stored if (dist < shortestDist): closestX = centroid_x closestY = centroid_y shortestDist = dist closestCnt = cnt self.setWhiteDotCenter((closestX, closestY)) if DEBUG: #print (shortestDist) print("Closest Contour: (" + str(closestX) + ", " + str(closestY) + ")") ## This only prints the one contour that is passed, on top of the image cv2.drawContours(im, [closestCnt], 0, (255, 0, 0), -1) cv2.drawContours(im2, [closestCnt], 0, (255, 0, 0), 1) cv.ShowImage("White Dot with Contours", cv.fromarray(im)) cv.WaitKey(0) cv.DestroyWindow("White Dot with Contours") cv.ShowImage("White Dot only", cv.fromarray(im2)) cv.WaitKey(0) cv.DestroyWindow("White Dot only")
def findCrescent(self): """ Detects a crescent within a pupil region. Uses opencv libarary methods to detect a crescent. Then initializes crescent to the area of the region found. Returns false if any errors are encountered Args: None Return: bool - True if there were no issues. False for any error """ if DEBUG: print "self.pupilPhoto is of type: " + str(type(self.pupilPhoto)) # Currently self.pupilPhoto is stored as a cvmat so we need to convert to a # numpy array before working with it. #im = np.asarray(self.pupilPhoto) # read the im from disc using absolute path im = cv2.imread( os.path.join(os.path.dirname(__file__), 'PUPILPHOTO.jpg')) if DEBUG: print "im is of type: " + str(type(im)) imblur = cv2.blur(im, (3, 3)) imgray = cv2.cvtColor(imblur, cv2.COLOR_BGR2GRAY) # TODO Take away magic (ex: 127,255,0) numbers here and make pretty # Variables at the top ret, thresh = cv2.threshold(imgray, 127, 255, 0) contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) if DEBUG: print "Contours is of type: " + str(type(contours)) print "Contours is of id: " + str(hex(id(contours))) print "Countours: " + str(contours) cv.ShowImage("Thresholded", cv.fromarray(thresh)) cv.WaitKey(0) cv.DestroyWindow("Thresholded") cnt = contours[0] len(cnt) cv2.drawContours(im, contours, -1, (0, 255, 0), -1) cv.ShowImage("Coutours", cv.fromarray(im)) cv.WaitKey(0) cv.DestroyWindow("Contours") max_area = 0 for cnt in contours: area = cv2.contourArea(cnt) if area > max_area: max_area = area best_cnt = cnt #set the max_area found into the actual structure self.setCrescent(max_area) #show it, or exit on waitkey #cv2.imshow('imblur',imblur) if DEBUG: #find centroids of best_cnt M = cv2.moments(best_cnt) cx, cy = int(M['m10'] / M['m00']), int(M['m01'] / M['m00']) cv2.circle(imblur, (cx, cy), 5, 255, -1) cv2.imshow('thresh', thresh) if cv2.waitKey(33) == 27: cv.DestroyAllWindows() cnt = contours[0] len(cnt) cv2.drawContours(imblur, contours, -1, (0, 255, 0), -1) cv2.circle(imblur, (cx, cy), 5, 255, -1) cv.ShowImage("Contour Shading", cv.fromarray(imblur)) #cv.WaitKey(0) #cv.DestroyWindow("Testing") cv.WaitKey(0) cv.DestroyAllWindows()
for failure_case in failure_cases: must_fail = failure_case.test(detection, detector_state) if must_fail: failure_text = failure_case.get_failure_text() print failure_text cv2.putText(frame, failure_text, (10, 20), cv2.FONT_HERSHEY_PLAIN, 1, (0, 0, 255)) # TODO: handle fixing case break else: # Update State detector_state.update_detections(detection) if not must_fail: for blob in detection.chosen_blobs: cv.Circle(cv.fromarray(frame), (int(blob.x), int(blob.y)), int(math.ceil(blob.size)), (0, 0, 255), thickness=2, lineType=8, shift=0) # Select smallest blob # TODO: make sure we send only ONE chosen blob if len(detection.chosen_blobs) > 0: best_blob = min(detection.chosen_blobs, key=lambda x: x.real_y) msg = [{ "name": "sample!", "x": best_blob.real_y, "y": -best_blob.real_x }] publisher.send(json.dumps(msg))
def find_faces(pil_image, save=False): """ Returns an array of identified faces. Each face is represented as an 48x48 array of 8-bit integers. It is possible to save the identified faces as png images. """ # convert from PIL to OpenCV img = cv.fromarray(cv2.cvtColor(np.array(pil_image), cv2.COLOR_RGB2BGR)) #img = cv.fromarray(np.array(pil_image)) #img = np.array(pil_image) #img = cv.LoadImage("../Desktop/IMG_4423-150x150.jpg", 1) # allocate temporary images gray = cv.CreateImage((img.width, img.height), 8, 1) small_img = cv.CreateImage( (cv.Round(img.width / imageScale), cv.Round(img.height / imageScale)), 8, 1) # convert color input image to grayscale cv.CvtColor(img, gray, cv.CV_BGR2GRAY) # scale input image for faster processing cv.Resize(gray, small_img, cv.CV_INTER_LINEAR) cv.EqualizeHist(small_img, small_img) faces = cv.HaarDetectObjects(small_img, cascade, cv.CreateMemStorage(0), haarScale, minNeighbors, haarFlags, minSize) face_array = [] if faces: print "\tDetected ", len(faces), " object(s)" for ((x, y, w, h), n) in faces: #the input to cv.HaarDetectObjects was resized, scale the #bounding box of each face and convert it to two CvPoints pt1 = (int(x * imageScale), int(y * imageScale)) pt2 = (int((x + w) * imageScale), int((y + h) * imageScale)) cv.Rectangle(img, pt1, pt2, cv.RGB(255, 0, 0), 3, 8, 0) face_img = small_img[y:y + h, x:x + w] #print face_img face_small = cv.CreateImage((48, 48), 8, 1) cv.Resize(face_img, face_small, cv.CV_INTER_LINEAR) #print face_small #print x, y, w, h, n #arr = np.fromstring( face_small.tostring(), dtype='c' ).reshape((48, 48)) arr = np.fromstring(face_small.tostring(), dtype=np.uint8).reshape( (48, 48)) #print arr.shape #print arr face_array.append(arr) # plt.ion() # fig = plt.figure(figsize = (4, 3)) # ax = fig.add_subplot(111) # ax.imshow(arr, cmap='gray') #ax.imshow(face_img, cmap='gray') # fig.canvas.draw() # save the faces if save: for array in face_array: s = int(time.time()) mpimg.imsave('faces/img%4d.png' % s, array, cmap="gray") return face_array
from cv2 import cv import numpy as np from pylab import * import glob import os i = 0 for files in glob.glob('D:/pic/car/*.jpg'): filepath,filename = os.path.split(files) image = cv2.imread(filepath + '/' + filename) # image = cv2.imread('D:/pic/car2.jpg') h,w = image.shape[:2] #灰度化 gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY) grayIPimage = cv.GetImage(cv.fromarray(gray)) sobel = cv.CreateImage((w, h),cv2.IPL_DEPTH_16S, 1) #创建一张深度为16位有符号(-65536~65535)的的图像区域保持处理结果 cv.Sobel(grayIPimage,sobel,2,0,7) # 进行x方向的sobel检测 temp = cv.CreateImage(cv.GetSize(sobel),cv2.IPL_DEPTH_8U, 1) #图像格式转换回8位深度已进行下一步处理 cv.ConvertScale(sobel, temp,0.00390625, 0) cv.Threshold(temp, temp, 0, 255, cv2.THRESH_OTSU) kernal = cv.CreateStructuringElementEx(3,1, 1, 0, 0) cv.Dilate(temp, temp,kernal,2) cv.Erode(temp, temp,kernal,4) cv.Dilate(temp, temp,kernal,2) # cv.ShowImage('1', temp) kernal = cv.CreateStructuringElementEx(1,3, 0, 1, 0) cv.Erode(temp, temp,kernal,1) cv.Dilate(temp, temp,kernal,3) # cv.ShowImage('2', temp) temp = np.asarray(cv.GetMat(temp))
s, c = size_and_center(rect) subtargets = detect(roi.copy(), cascade_nested, size=(max(30, s[0] - REFIND_BUFFER), max(30, s[1] - REFIND_BUFFER))) # with the_lock: # print "subtarget detect: ", clock() - subt if len(subtargets) == 1: sx1, sy1, sx2, sy2 = subtargets[0] vis_roi = vis[max(0, y1 - BUFFER):min(width, y2 + BUFFER), max(0, x1 - BUFFER):min(height, x2 + BUFFER)] if recognizing: detection_image = roi[sy1:sy2, sx1:sx2] predictions = recognizer.predict( cv.fromarray(detection_image)) for i, (label, confidence) in enumerate(predictions): color = recog.LABEL_COLORS[label] who = recog.LABELS[label] # cv2.putText(vis_roi, # "%s (%s)" % (who, confidence), # (sx1 + 2, sy1 + 20 + 20 * i), # cv2.FONT_HERSHEY_PLAIN, 1.1, color) winning_label = recog.get_overall_prediction( predictions) #rect_color = recog.LABEL_COLORS[winning_label] karlness_update = 1 if who == "Karl" else 0 else: #rect_color = (0, 255, 255) karlness_update = 1
def match_template(self, cv_image): frame = np.array(cv_image, dtype=np.uint8) W,H = frame.shape[1], frame.shape[0] w,h = self.template.shape[1], self.template.shape[0] width = W - w + 1 height = H - h + 1 # Make sure that the template image is smaller than the source if W < w or H < h: rospy.loginfo( "Template image must be smaller than video frame." ) return False if frame.dtype != self.template.dtype: rospy.loginfo("Template and video frame must have same depth and number of channels.") return False # Create copies of the images to modify frame_copy = frame.copy() template_copy = self.template.copy() # Down pyramid the images for k in range(self.numDownPyrs): # Start with the source image W = (W + 1) / 2 H = (H + 1) / 2 frame_small = np.array([H, W], dtype=frame.dtype) frame_small = cv2.pyrDown(frame_copy) # frame_window = "PyrDown " + str(k) # cv.NamedWindow(frame_window, cv.CV_NORMAL) # cv.ShowImage(frame_window, cv.fromarray(frame_small)) # cv.ResizeWindow(frame_window, 640, 480) # Prepare for next loop, if any frame_copy = frame_small.copy() #Next, do the target w = (w + 1) / 2 h = (h + 1) / 2 template_small = np.array([h, w], dtype=self.template.dtype) template_small = cv2.pyrDown(template_copy) # template_window = "Template PyrDown " + str(k) # cv.NamedWindow(template_window, cv.CV_NORMAL) # cv.ShowImage(template_window, cv.fromarray(template_small)) # cv.ResizeWindow(template_window, 640, 480) # Prepare for next loop, if any template_copy = template_small.copy() # Perform the match on the shrunken images small_frame_width = frame_copy.shape[1] small_frame_height = frame_copy.shape[0] small_template_width = template_copy.shape[1] small_template_height = template_copy.shape[0] result_width = small_frame_width - small_template_width + 1 result_height = small_frame_height - small_template_height + 1 result_mat = cv.CreateMat(result_height, result_width, cv.CV_32FC1) result = np.array(result_mat, dtype = np.float32) cv2.matchTemplate(frame_copy, template_copy, cv.CV_TM_CCOEFF_NORMED, result) cv2.imshow("Result", result) return (0, 0, 100, 100) # # Find the best match location # (minValue, maxValue, minLoc, maxLoc) = cv2.minMaxLoc(result) # # # Transform point back to original image # target_location = Point() # target_location.x, target_location.y = maxLoc # # return (target_location.x, target_location.y, w, h) # Find the top match locations locations = self.MultipleMaxLoc(result, self.numMaxima) foundPointsList = list() confidencesList = list() W,H = frame.shape[1], frame.shape[0] w,h = self.template.shape[1], self.template.shape[0] # Search the large images at the returned locations for currMax in range(self.numMaxima): # Transform the point to its corresponding point in the larger image #locations[currMax].x *= int(pow(2.0, self.numDownPyrs)) #locations[currMax].y *= int(pow(2.0, self.numDownPyrs)) locations[currMax].x += w / 2 locations[currMax].y += h / 2 searchPoint = locations[currMax] print "Search Point", searchPoint # If we are searching for multiple targets and we have found a target or # multiple targets, we don't want to search in the same location(s) again # if self.findMultipleTargets and len(foundPointsList) != 0: # thisTargetFound = False # numPoints = len(foundPointsList) # # for currPoint in range(numPoints): # foundPoint = foundPointsList[currPoint] # if (abs(searchPoint.x - foundPoint.x) <= self.searchExpansion * 2) and (abs(searchPoint.y - foundPoint.y) <= self.searchExpansion * 2): # thisTargetFound = True # break # # # If the current target has been found, continue onto the next point # if thisTargetFound: # continue # Set the source image's ROI to slightly larger than the target image, # centred at the current point searchRoi = RegionOfInterest() searchRoi.x_offset = searchPoint.x - w / 2 - self.searchExpansion searchRoi.y_offset = searchPoint.y - h / 2 - self.searchExpansion searchRoi.width = w + self.searchExpansion * 2 searchRoi.height = h + self.searchExpansion * 2 #print (searchRoi.x_offset, searchRoi.y_offset, searchRoi.width, searchRoi.height) # Make sure ROI doesn't extend outside of image if searchRoi.x_offset < 0: searchRoi.x_offset = 0 if searchRoi.y_offset < 0: searchRoi.y_offset = 0 if (searchRoi.x_offset + searchRoi.width) > (W - 1): numPixelsOver = (searchRoi.x_offset + searchRoi.width) - (W - 1) print "NUM PIXELS OVER", numPixelsOver searchRoi.width -= numPixelsOver if (searchRoi.y_offset + searchRoi.height) > (H - 1): numPixelsOver = (searchRoi.y_offset + searchRoi.height) - (H - 1) searchRoi.height -= numPixelsOver mask = (searchRoi.x_offset, searchRoi.y_offset, searchRoi.width, searchRoi.height) frame_mat = cv.fromarray(frame) searchImage = cv.CreateMat(searchRoi.height, searchRoi.width, cv.CV_8UC3) searchImage = cv.GetSubRect(frame_mat, mask) searchArray = np.array(searchImage, dtype=np.uint8) # Perform the search on the large images result_width = searchRoi.width - w + 1 result_height = searchRoi.height - h + 1 result_mat = cv.CreateMat(result_height, result_width, cv.CV_32FC1) result = np.array(result_mat, dtype = np.float32) cv2.matchTemplate(searchArray, self.template, cv.CV_TM_CCOEFF_NORMED, result) # Find the best match location (minValue, maxValue, minLoc, maxLoc) = cv2.minMaxLoc(result) maxValue *= 100 # Transform point back to original image target_location = Point() target_location.x, target_location.y = maxLoc target_location.x += searchRoi.x_offset - w / 2 + self.searchExpansion target_location.y += searchRoi.y_offset - h / 2 + self.searchExpansion if maxValue >= self.matchPercentage: # Add the point to the list foundPointsList.append(maxLoc) confidencesList.append(maxValue) # If we are only looking for a single target, we have found it, so we # can return if not self.findMultipleTargets: break if len(foundPointsList) == 0: rospy.loginfo("Target was not found to required confidence") return (target_location.x, target_location.y, w, h)
vc = cv2.VideoCapture("/media/bat/DATA/images/2013/06/27/00176.MTS") width, height = 640, 480 writer = cv2.VideoWriter(filename="outputVideo.avi", fourcc=cv.CV_FOURCC('M', 'J', 'P', 'G'), fps=15, frameSize=(width, height)) if vc.isOpened(): # try to get the first frame rval, frame = vc.read() else: rval = False while rval: cv2.imshow("preview", frame) rval, frame = vc.read() im = cv.fromarray(frame) im = cv.Flip(im, flipMode=-1) cv.ShowImage('180_rotation', im) key = cv2.waitKey(20) key -= 0x100000 # Corrects bug in openCV... if key == 27: # Esc key to stop break elif key == 115: # s key for snapshot cv2.imwrite( datetime.datetime.utcnow().strftime("%Yy%mm%dd%Hh%Mm%Ss") + '.jpg', frame) writer.write(frame) cv2.destroyAllWindows()
def transmit_thread(): '''thread for image transmit to GCS''' state = mpstate.camera_state tx_count = 0 skip_count = 0 bsend = block_xmit.BlockSender(0, bandwidth=state.settings.bandwidth, debug=False) state.bsocket = MavSocket(mpstate.mav_master[0]) state.bsend2 = block_xmit.BlockSender(mss=96, sock=state.bsocket, dest_ip='mavlink', dest_port=0, backlog=5, debug=False) state.bsend2.set_bandwidth(state.settings.bandwidth2) while not state.unload.wait(0.02): bsend.tick(packet_count=1000, max_queue=state.settings.maxqueue1) state.bsend2.tick(packet_count=1000, max_queue=state.settings.maxqueue2) check_commands() if state.transmit_queue.empty(): continue (frame_time, regions, im_full, im_640) = state.transmit_queue.get() if state.settings.roll_stabilised: roll=0 else: roll=None pos = get_plane_position(frame_time, roll=roll) # this adds the latlon field to the regions log_joe_position(pos, frame_time, regions) # filter out any regions outside the boundary if state.boundary_polygon: regions = cuav_region.filter_boundary(regions, state.boundary_polygon, pos) regions = cuav_region.filter_regions(im_full, regions, min_score=state.settings.minscore) state.xmit_queue = bsend.sendq_size() state.xmit_queue2 = state.bsend2.sendq_size() state.efficiency = bsend.get_efficiency() state.bandwidth_used = bsend.get_bandwidth_used() state.rtt_estimate = bsend.get_rtt_estimate() jpeg = None if len(regions) > 0: lowscore = 0 highscore = 0 for r in regions: lowscore = min(lowscore, r.score) highscore = max(highscore, r.score) if state.settings.transmit: # send a region message with thumbnails to the ground station thumb = None if state.settings.send1: thumb_img = cuav_mosaic.CompositeThumbnail(cv.GetImage(cv.fromarray(im_full)), regions, thumb_size=state.settings.thumbsize) thumb = scanner.jpeg_compress(numpy.ascontiguousarray(cv.GetMat(thumb_img)), state.settings.quality) pkt = ThumbPacket(frame_time, regions, thumb, state.frame_loss, state.xmit_queue, pos) buf = cPickle.dumps(pkt, cPickle.HIGHEST_PROTOCOL) bsend.set_bandwidth(state.settings.bandwidth) bsend.set_packet_loss(state.settings.packet_loss) bsend.send(buf, dest=(state.settings.gcs_address, state.settings.gcs_view_port), priority=1) # also send thumbnails via 900MHz telemetry if state.settings.send2 and highscore >= state.settings.minscore2: if thumb is None or lowscore < state.settings.minscore2: # remove some of the regions regions = cuav_region.filter_regions(im_full, regions, min_score=state.settings.minscore2) thumb_img = cuav_mosaic.CompositeThumbnail(cv.GetImage(cv.fromarray(im_full)), regions, thumb_size=state.settings.thumbsize) thumb = scanner.jpeg_compress(numpy.ascontiguousarray(cv.GetMat(thumb_img)), state.settings.quality) pkt = ThumbPacket(frame_time, regions, thumb, state.frame_loss, state.xmit_queue, pos) buf = cPickle.dumps(pkt, cPickle.HIGHEST_PROTOCOL) state.bsend2.set_bandwidth(state.settings.bandwidth2) state.bsend2.send(buf, priority=highscore) # Base how many images we send on the send queue size send_frequency = state.xmit_queue // 3 if send_frequency == 0 or (tx_count+skip_count) % send_frequency == 0: jpeg = scanner.jpeg_compress(im_640, state.settings.quality) if jpeg is None: skip_count += 1 continue # keep filtered image size state.jpeg_size = 0.95 * state.jpeg_size + 0.05 * len(jpeg) tx_count += 1 if state.settings.gcs_address is None: continue bsend.set_packet_loss(state.settings.packet_loss) bsend.set_bandwidth(state.settings.bandwidth) pkt = ImagePacket(frame_time, jpeg, state.xmit_queue, pos) str = cPickle.dumps(pkt, cPickle.HIGHEST_PROTOCOL) bsend.send(str, dest=(state.settings.gcs_address, state.settings.gcs_view_port))
def calculate_params(self, file_path): global lookupTable print "calculating params for the cameras" if not os.path.isfile(file_path) and os.access(file_path, os.R_OK): print "Either file is missing or is not readable" exit(-1) stream = open(file_path, "r") doc = yaml.load(stream) ''' read from the file the width and the height and asign it to both cameras read the distorsion model but assign only pumb_bob because that one is requiered by sptam read the distCoeffs for both cameras and assign the lists to the cameras read k, make a list of 9 elements with the correct positions out of it and then assign t to both cameras Read the extrinsics of both cameras, inverse one of them and calculate the R and t from camera_left to camera_left_to_right make everything be a cv matrix cv.StereoRectify(K1_mat, K2_mat, distCoeffs1_mat, distCoeffs2_mat, imageSize, R, t_mat, R1, R2, P1, P2,T_disp2depth, flags=cv.CV_CALIB_ZERO_DISPARITY, alpha=-1, newImageSize=(0,0)) make P1 into a list and assign it as camera_left.P make P2 into a list and assign it as camera_right.P make R1 into a list and assign it as camera_left.R make R2 into a list and assign it as camera_right.R ''' #width and height self.camera_left.width = doc['camera0']['resolution'][0] self.camera_left.height = doc['camera0']['resolution'][1] self.camera_right.width = doc['camera1']['resolution'][0] self.camera_right.height = doc['camera1']['resolution'][1] #distortion_model self.camera_left.distortion_model = "plumb_bob" self.camera_right.distortion_model = "plumb_bob" #distortion_coefficients self.camera_left.D = doc['camera0']['distortion_coefficients'] self.camera_right.D = doc['camera1']['distortion_coefficients'] #K_left self.camera_left.K[0] = doc['camera0']['intrinsics'][0] self.camera_left.K[1] = 0.0 self.camera_left.K[2] = doc['camera0']['intrinsics'][2] self.camera_left.K[3] = 0.0 self.camera_left.K[4] = doc['camera0']['intrinsics'][1] self.camera_left.K[5] = doc['camera0']['intrinsics'][3] self.camera_left.K[6] = 0.0 self.camera_left.K[7] = 0.0 self.camera_left.K[8] = 1.0 #K_right self.camera_right.K[0] = doc['camera1']['intrinsics'][0] self.camera_right.K[1] = 0.0 self.camera_right.K[2] = doc['camera1']['intrinsics'][2] self.camera_right.K[3] = 0.0 self.camera_right.K[4] = doc['camera1']['intrinsics'][1] self.camera_right.K[5] = doc['camera1']['intrinsics'][3] self.camera_right.K[6] = 0.0 self.camera_right.K[7] = 0.0 self.camera_right.K[8] = 1.0 #R and t from camera left to right extrinsics_4x4_l = np.array(doc['camera0']['T_BS']['data']) extrinsics_left = extrinsics_4x4_l.reshape(4, 4) extrinsics_4x4_r = np.array(doc['camera1']['T_BS']['data']) extrinsics_right = extrinsics_4x4_r.reshape(4, 4) inverse = np.linalg.inv(extrinsics_left) camera_left_to_right = np.matrix(inverse) * np.matrix(extrinsics_right) camera_left_to_right = camera_left_to_right[0:3, 0:4] R = camera_left_to_right[0:3, 0:3] t = camera_left_to_right[0:3, 3] stream.close() #Prepare values for rectifying function calling R1 = cv.CreateMat(3, 3, cv.CV_64F) R2 = cv.CreateMat(3, 3, cv.CV_64F) P1 = cv.CreateMat(3, 4, cv.CV_64F) P2 = cv.CreateMat(3, 4, cv.CV_64F) K1_arr = np.asarray(self.camera_left.K) K1_arr = K1_arr.reshape(3, 3) K2_arr = np.asarray(self.camera_right.K) K2_arr = K2_arr.reshape(3, 3) K1_mat_cont = cv.fromarray( cv2.copyMakeBorder(K1_arr, 0, 0, 0, 0, cv2.BORDER_REPLICATE)) K2_mat_cont = cv.fromarray( cv2.copyMakeBorder(K2_arr, 0, 0, 0, 0, cv2.BORDER_REPLICATE)) distCoeffs1_array = np.matrix(np.array(self.camera_left.D)) distCoeffs2_array = np.matrix(np.array(self.camera_right.D)) distCoeffs1_mat_cont = cv.fromarray( cv2.copyMakeBorder(distCoeffs1_array, 0, 0, 0, 0, cv2.BORDER_REPLICATE)) distCoeffs2_mat_cont = cv.fromarray( cv2.copyMakeBorder(distCoeffs2_array, 0, 0, 0, 0, cv2.BORDER_REPLICATE)) R_mat_cont = cv.fromarray( cv2.copyMakeBorder(R, 0, 0, 0, 0, cv2.BORDER_REPLICATE)) t_mat_cont = cv.fromarray( cv2.copyMakeBorder(t, 0, 0, 0, 0, cv2.BORDER_REPLICATE)) imageSize = (self.camera_left.width, self.camera_left.height) ''' t[0,0]=-50.706459062 t[1,0]=0.15556820057 t[2,0]=0.00088938278 ''' T_disp2depth = cv.CreateMat(4, 4, cv.CV_32F) ####################### cv.StereoRectify(K1_mat_cont, K2_mat_cont, distCoeffs1_mat_cont, distCoeffs2_mat_cont, imageSize, R_mat_cont, t_mat_cont, R1, R2, P1, P2, T_disp2depth, flags=cv.CV_CALIB_ZERO_DISPARITY, alpha=-1, newImageSize=(0, 0)) lookupTable.rectifiedFocalLen = P1[0, 0] lookupTable.rectifiedCenterCol = P1[0, 2] lookupTable.rectifiedCenterRow = P1[1, 2] #lookupTable.baseline = cv.norm(t_mat); cv.InitUndistortRectifyMap(K1_mat_cont, distCoeffs1_mat_cont, R1, P1, None, None) cv.InitUndistortRectifyMap(K2_mat_cont, distCoeffs2_mat_cont, R2, P2, None, None) #Transform R1,R2 and P1 and P2 and put thhem in the cameras P1_arr = np.asarray(P1[:, :]) P1_arr = np.squeeze(np.asarray(P1_arr)).flatten() P2_arr = np.asarray(P2[:, :]) P2_arr = np.squeeze(np.asarray(P2_arr)).flatten() R1_arr = np.asarray(R1[:, :]) R1_arr = np.squeeze(np.asarray(R1_arr)).flatten() R2_arr = np.asarray(R2[:, :]) R2_arr = np.squeeze(np.asarray(R2_arr)).flatten() R_id_arr = [1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0] self.camera_left.R = R_id_arr self.camera_right.R = np.squeeze(np.asarray(R)).flatten() self.camera_left.P = P1_arr self.camera_right.P = P2_arr # Tx = - fx * B self.camera_right.P[ 3] = -self.camera_right.P[3] * self.camera_right.K[0] print "finished calculating params, we return" self.calculated = True return
# mirror cv.Flip(frame, None, 1) originalImage = frame hsvImage = cv.CreateImage(cv.GetSize(originalImage), 8, 3) cv.CvtColor(originalImage, hsvImage, cv.CV_BGR2HSV) thresholdImage = cv.CreateImage(cv.GetSize(originalImage), 8, 1) cv.InRangeS(hsvImage, cv.Scalar(20.74, 75, 75), cv.Scalar(30.74, 255, 255), thresholdImage) thresholdImageArray = np.asarray(cv.GetMat(thresholdImage)) thresholdImageArray = cv2.GaussianBlur(thresholdImageArray, (0, 0), 2) thresholdImage = cv.fromarray(thresholdImageArray) circles = cv2.HoughCircles(thresholdImageArray, cv.CV_HOUGH_GRADIENT, 2, 10, param1=40, param2=80, minRadius=4, maxRadius=1500) if circles is not None: if circles.size > 0: circles = np.uint16(np.around(circles)) for i in circles[0, :]:
def convertCV32(stacked): hist64 = cv.fromarray(stacked) hist32 = cv.CreateMat(hist64.rows, hist64.cols, cv.CV_32FC1) cv.Convert(hist64, hist32) return hist32
def run_wmd_mix(dir): import numpy as np import json k = 10 f = open(str(dir) + "/files.json") Files = json.loads(f.read()) f2 = open(str(dir) + "/file_cipin_vec_mapper.json") cipins_mapper = json.loads(f2.read()) f3 = open(str(dir) + "/file_wcd_sim_mapper.json") wcd_sims_mapper = json.loads(f3.read()) f.close() f2.close() f3.close() del (f) del (f2) del (f3) import gc gc.collect() print "Load complete" from cv2 import cv sims = [] # import time # start = time.clock() from insert_sort import InsertSortItem count = 0 for file in Files[1800:]: count += 1 cipin_gram = cipins_mapper[file] c_list = cipin_gram[0] d_list = cipin_gram[1] word_count = len(d_list) c_recover = np.array(c_list, np.float32).reshape(word_count, -1) d_recover = np.array(d_list, np.float32) signature1 = np.column_stack((np.transpose(d_recover), c_recover)) wcd_sims = wcd_sims_mapper[file] wmd_mix_sims = [] for wcd_sim_gram in wcd_sims[:k]: file_wcd = wcd_sim_gram[0] cipin_gram_wcd = cipins_mapper[file_wcd] c_list_wcd = cipin_gram_wcd[0] d_list_wcd = cipin_gram_wcd[1] word_count_wcd = len(d_list_wcd) c_recover_wcd = np.array(c_list_wcd, np.float32).reshape(word_count_wcd, -1) d_recover_wcd = np.array(d_list_wcd, np.float32) signature2 = np.column_stack( (np.transpose(d_recover_wcd), c_recover_wcd)) pp = cv.fromarray(signature1) qq = cv.fromarray(signature2) emd = cv.CalcEMD2(pp, qq, cv.CV_DIST_L2) wmd_mix_sims.append([file_wcd, emd]) wmd_mix_sims = sorted(wmd_mix_sims, key=lambda x: x[1], reverse=False) for wcd_sim_gram in wcd_sims[k:10 * k]: file_wcd = wcd_sim_gram[0] cipin_gram_wcd = cipins_mapper[file_wcd] c_list_wcd = cipin_gram_wcd[0] d_list_wcd = cipin_gram_wcd[1] word_count_wcd = len(d_list_wcd) c_recover_wcd = np.array(c_list_wcd, np.float32).reshape(word_count_wcd, -1) d_recover_wcd = np.array(d_list_wcd, np.float32) # rwmd = getRWMD((c_recover,d_recover),(c_recover_wcd,d_recover_wcd)) # if InsertTest(wmd_mix_sims, rwmd): signature2 = np.column_stack( (np.transpose(d_recover_wcd), c_recover_wcd)) pp = cv.fromarray(signature1) qq = cv.fromarray(signature2) emd = cv.CalcEMD2(pp, qq, cv.CV_DIST_L2) InsertSortItem(wmd_mix_sims, [file_wcd, emd]) sims.append([wmd_mix_sims, file]) if count % 100 == 0: print count * 1.0 / totalFilesNumber / 5 * 2, totalFilesNumber / 5 * 2 - count # end = time.clock() # print end-start f = open(str(dir) + "/wmd_mix_sim_10k.json", "w") data = json.dumps(sims, ensure_ascii=False) f.write(data.encode('utf-8'))
def __init__(self): rospy.init_node('video2ros', anonymous=False) rospy.on_shutdown(self.cleanup) """ Define the input (path to video file) as a ROS parameter so it can be defined in a launch file or on the command line """ self.input = rospy.get_param("~input", "") """ Define the image publisher with generic topic name "output" so that it can be remapped in the launch file. """ image_pub = rospy.Publisher("output", Image) # The target frames per second for the video self.fps = rospy.get_param("~fps", 25) # Do we restart the video when the end is reached? self.loop = rospy.get_param("~loop", False) # Start the video paused? self.start_paused = rospy.get_param("~start_paused", False) # Show text feedback by default? self.show_text = rospy.get_param("~show_text", True) # Resize the original video? self.resize_video = rospy.get_param("~resize_video", False) # If self.resize_video is True, set the desired width and height here self.resize_width = rospy.get_param("~resize_width", 0) self.resize_height = rospy.get_param("~resize_height", 0) # Initialize a few variables self.paused = self.start_paused self.loop_video = True self.keystroke = None self.restart = False self.last_frame = None # Initialize the text font font_face = cv2.FONT_HERSHEY_SIMPLEX font_scale = 0.5 # Define the capture object as pointing to the input file self.capture = cv2.VideoCapture(self.input) # Get the original frames per second fps = self.capture.get(cv.CV_CAP_PROP_FPS) # Get the original frame size self.frame_size = (self.capture.get(cv.CV_CAP_PROP_FRAME_HEIGHT), self.capture.get(cv.CV_CAP_PROP_FRAME_WIDTH)) # Check that we actually have a valid video source if fps == 0.0: print "Video source", self.input, "not found!" return None # Bring the fps up to the specified rate try: fps = int(fps * self.fps / fps) except: fps = self.fps # Create the display window cv.NamedWindow("Video Playback", True) # autosize the display cv.MoveWindow("Video Playback", 375, 25) # Create the CvBridge object bridge = CvBridge() # Enter the main processing loop while not rospy.is_shutdown(): # Get the next frame from the video frame = self.get_frame() # Convert the frame to ROS format try: image_pub.publish(bridge.cv_to_imgmsg(cv.fromarray(frame), "bgr8")) except CvBridgeError, e: print e # Create a copy of the frame for displaying the text display_image = frame.copy() if self.show_text: cv2.putText(display_image, "FPS: " + str(self.fps), (10, 20), font_face, font_scale, cv.RGB(255, 255, 0)) cv2.putText(display_image, "Keyboard commands:", (20, int(self.frame_size[0] * 0.6)), font_face, font_scale, cv.RGB(255, 255, 0)) cv2.putText(display_image, " ", (20, int(self.frame_size[0] * 0.65)), font_face, font_scale, cv.RGB(255, 255, 0)) cv2.putText(display_image, "space - toggle pause/play", (20, int(self.frame_size[0] * 0.72)), font_face, font_scale, cv.RGB(255, 255, 0)) cv2.putText(display_image, " r - restart video from beginning", (20, int(self.frame_size[0] * 0.79)), font_face, font_scale, cv.RGB(255, 255, 0)) cv2.putText(display_image, " t - hide/show this text", (20, int(self.frame_size[0] * 0.86)), font_face, font_scale, cv.RGB(255, 255, 0)) cv2.putText(display_image, " q - quit the program", (20, int(self.frame_size[0] * 0.93)), font_face, font_scale, cv.RGB(255, 255, 0)) # Merge the original image and the display image (text overlay) display_image = cv2.bitwise_or(frame, display_image) # Now display the image. cv2.imshow("Video Playback", display_image) """ Handle keyboard events """ self.keystroke = cv.WaitKey(1000 / fps) """ Process any keyboard commands """ if 32 <= self.keystroke and self.keystroke < 128: cc = chr(self.keystroke).lower() if cc == 'q': """ user has press the q key, so exit """ rospy.signal_shutdown("User hit q key to quit.") elif cc == ' ': """ Pause or continue the video """ self.paused = not self.paused elif cc == 'r': """ Restart the video from the beginning """ self.restart = True elif cc == 't': """ Toggle display of text help message """ self.show_text = not self.show_text