def handle_msg(self, msg): # TODO Various asserts that images have same dimension, same board detected... (lmsg, rmsg) = msg lrgb = self.mkgray(lmsg) rrgb = self.mkgray(rmsg) epierror = -1 # Get display-images-to-be and detections of the calibration target lscrib, lcorners, ldownsampled_corners, lboard, ( x_scale, y_scale) = self.downsample_and_detect(lrgb) rscrib, rcorners, rdownsampled_corners, rboard, _ = self.downsample_and_detect( rrgb) if self.calibrated: # Show rectified images if x_scale != 1.0 or y_scale != 1.0: rgb_rect = self.l.remap(lrgb) cv.Resize(rgb_rect, lscrib) rgb_rect = self.r.remap(rrgb) cv.Resize(rgb_rect, rscrib) else: lscrib = self.l.remap(lrgb) rscrib = self.r.remap(rrgb) # Draw rectified corners if lcorners: src = self.mk_image_points([(lcorners, lboard)]) lundistorted = list( cvmat_iterator(self.l.undistort_points(src))) scrib_src = [(x / x_scale, y / y_scale) for (x, y) in lundistorted] cv.DrawChessboardCorners(lscrib, (lboard.n_cols, lboard.n_rows), scrib_src, True) if rcorners: src = self.mk_image_points([(rcorners, rboard)]) rundistorted = list( cvmat_iterator(self.r.undistort_points(src))) scrib_src = [(x / x_scale, y / y_scale) for (x, y) in rundistorted] cv.DrawChessboardCorners(rscrib, (rboard.n_cols, rboard.n_rows), scrib_src, True) # Report epipolar error if lcorners and rcorners: epierror = self.epipolar_error(lundistorted, rundistorted, lboard) else: # Draw any detected chessboards onto display (downsampled) images if lcorners: src = self.mk_image_points([(ldownsampled_corners, lboard)]) cv.DrawChessboardCorners(lscrib, (lboard.n_cols, lboard.n_rows), cvmat_iterator(src), True) if rcorners: src = self.mk_image_points([(rdownsampled_corners, rboard)]) cv.DrawChessboardCorners(rscrib, (rboard.n_cols, rboard.n_rows), cvmat_iterator(src), True) # Add sample to database only if it's sufficiently different from any previous sample if lcorners and rcorners: params = self.get_parameters(lcorners, lboard, cv.GetSize(lrgb)) if self.is_good_sample(params): self.db.append((params, lrgb, rrgb)) self.good_corners.append((lcorners, rcorners, lboard)) print "*** Added sample %d, p_x = %.3f, p_y = %.3f, p_size = %.3f, skew = %.3f" % tuple( [len(self.db)] + params) rv = StereoDrawable() rv.lscrib = lscrib rv.rscrib = rscrib rv.params = self.compute_goodenough() rv.epierror = epierror return rv
moments = cv.Moments(largest_contour, 1) positions_x.append( cv.GetSpatialMoment(moments, 1, 0) / cv.GetSpatialMoment(moments, 0, 0)) positions_y.append( cv.GetSpatialMoment(moments, 0, 1) / cv.GetSpatialMoment(moments, 0, 0)) # discard all but the last N positions positions_x, positions_y = positions_x[-SMOOTHNESS:], positions_y[ -SMOOTHNESS:] # # object_indicator will be the new image which shows where the identified # blob has been located. # object_indicator = cv.CreateImage(cv.GetSize(image), image.depth, 3) # # the average location of the identified blob # pos_x = (sum(positions_x) / len(positions_x)) pos_y = (sum(positions_y) / len(positions_y)) object_position = (int(pos_x), int(pos_y)) cv.Circle(object_indicator, object_position, 12, (0, 0, 255), 4) if FOLLOW and blobContour: # draw a line to the desiredPosition desiredPosition = cv.GetSize(image) desiredPosition = tuple(map(operator.mul, desiredPosition, (0.5, 0.5))) desiredPosition = tuple(map(int, desiredPosition))
# but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. from __future__ import division import logging import cv import numpy as np import os, pickle logging.basicConfig(level=logging.DEBUG) PICS_DIR = "256_ObjectCategories" CATS = {'moto': '145.motorbikes-101', 'plane': '251.airplanes-101'} for name, dir in CATS.items(): dir = os.path.join(PICS_DIR, dir) surfs = {} for file in os.listdir(dir): file = os.path.join(dir, file) logging.debug('%s in progress ...' % file) image = cv.LoadImage(file) image_gray = cv.CreateImage(cv.GetSize(image), image.depth, 1) cv.CvtColor(image, image_gray, cv.CV_BGR2GRAY) k, v = cv.ExtractSURF(image_gray, None, cv.CreateMemStorage(), (1, 300, 3, 1)) surfs[file] = np.array(v) pickle.dump(surfs, open(name, 'wb'))
dots.append((-x, off)) olt.dots = dots time.sleep(1) camera = cv.CreateCameraCapture(0) cv.SetCaptureProperty(camera, cv.CV_CAP_PROP_FRAME_WIDTH, WIDTH) cv.SetCaptureProperty(camera, cv.CV_CAP_PROP_FRAME_HEIGHT, HEIGHT) cv.SetCaptureProperty(camera, cv.CV_CAP_PROP_BRIGHTNESS, 0) image = cv.QueryFrame(camera) image = cv.QueryFrame(camera) image = cv.QueryFrame(camera) image = cv.QueryFrame(camera) grey = cv.CreateImage(cv.GetSize(image), cv.IPL_DEPTH_8U, 1) grey2 = cv.CreateImage(cv.GetSize(image), cv.IPL_DEPTH_8U, 1) olt.start() print "Thread running" time.sleep(1) try: for i in range(10): image = cv.QueryFrame(camera) refpoints = getpoints(image) refpoints.sort(key=lambda x: x[0]) print len(refpoints), n_end - n_start + 1 frameno = 1
#!/usr/bin/python import cv tolerancia = 30 imagen=cv.LoadImage('5.png') cv.ShowImage('Prueba',imagen) rojo = cv.CreateImage(cv.GetSize(imagen),cv.IPL_DEPTH_8U,1) verde = cv.CreateImage(cv.GetSize(imagen),cv.IPL_DEPTH_8U,1) azul = cv.CreateImage(cv.GetSize(imagen),cv.IPL_DEPTH_8U,1) amarillo = cv.CreateImage(cv.GetSize(imagen),cv.IPL_DEPTH_8U,1) pixel_rojo = [36,28,237] pixel_verde = [76,177,34] pixel_azul = [232,162,0] pixel_amarillo = [164,73,163] cv.InRangeS(imagen,(pixel_rojo[0]-tolerancia,pixel_rojo[1]-tolerancia,pixel_rojo[2]-tolerancia),(pixel_rojo[0]+tolerancia,pixel_rojo[1]+tolerancia,pixel_rojo[2]+tolerancia),rojo) cv.InRangeS(imagen,(pixel_verde[0]-tolerancia,pixel_verde[1]-tolerancia,pixel_verde[2]-tolerancia),(pixel_verde[0]+tolerancia,pixel_verde[1]+tolerancia,pixel_verde[2]+tolerancia),verde) cv.InRangeS(imagen,(pixel_azul[0]-tolerancia,pixel_azul[1]-tolerancia,pixel_azul[2]-tolerancia),(pixel_azul[0]+tolerancia,pixel_azul[1]+tolerancia,pixel_azul[2]+tolerancia),azul) cv.InRangeS(imagen,(pixel_amarillo[0]-tolerancia,pixel_amarillo[1]-tolerancia,pixel_amarillo[2]-tolerancia),(pixel_amarillo[0]+tolerancia,pixel_amarillo[1]+tolerancia,pixel_amarillo[2]+tolerancia),amarillo) cv.ShowImage('Color Rojo' ,rojo) cv.ShowImage('Color Verde' ,verde) cv.ShowImage('Color Azul' ,azul) cv.ShowImage('Color Amarillo' ,amarillo)
from __future__ import print_function from psychopy import visual, event, core import Image, time, pylab, cv, numpy mywin = visual.Window(allowGUI=False, monitor='testMonitor', units='norm',colorSpace='rgb',color=[-1,-1,-1], fullscr=True) mywin.setMouseVisible(False) capture = cv.CaptureFromCAM(0) img = cv.QueryFrame(capture) pi = Image.fromstring("RGB", cv.GetSize(img), img.tostring(), "raw", "BGR", 0, 1) print(pi.size) myStim = visual.GratingStim(win=mywin, tex=pi, pos=[0,0.5], size = [0.6,0.6], opacity = 1.0, units = 'norm') myStim.setAutoDraw(True) while True: img = cv.QueryFrame(capture) pi = Image.fromstring("RGB", cv.GetSize(img), img.tostring(), "raw", "BGR", 0, 1) myStim.setTex(pi) mywin.flip() theKey = event.getKeys() if len(theKey) != 0: break
capture = cv.CaptureFromCAM(0) cv.SetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_WIDTH, 320) cv.SetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_HEIGHT, 240) if not capture: print "Error opening capture device" sys.exit(1) while 1: frame = cv.QueryFrame(capture) if frame is None: break cv.Flip(frame, None, 1) face_center = detect(frame) if face_center: offset = face_center[0] - (cv.GetSize(frame)[0]/2) if abs(offset) > 5: serialpos = serialpos + (float(offset)/50) if offset > 0: LAST_DIRECTION = SCAN_SPEED else: LAST_DIRECTION = -SCAN_SPEED else: LAST_DIRECTION = None else: if LAST_DIRECTION: serialpos += LAST_DIRECTION if serialpos > 200: serialpos = 200 LAST_DIRECTION = -LAST_DIRECTION if serialpos < 54:
def CVtoPIL_4Channel(CV_img): """converts CV image to PIL image""" cv_img = cv.CreateMatHeader(cv.GetSize(img)[1], cv.GetSize(img)[0], cv.CV_8UC1) #cv.SetData(cv_img, pil_img.tostring()) pil_img = Image.fromstring("L", cv.GetSize(img), img.tostring()) return pil_img
# coding: utf-8 import cv, commands, sys, os FOLDER_PATH = "/Users/satokazuki/Desktop/zikken5/G1_kirinuki/" OUTPUT_PATH = "/Users/satokazuki/Desktop/zikken5/G1_database/" img_list = os.listdir(FOLDER_PATH) print img_list count = 0 for img_name in img_list: angle = (0, 90, 180, 270) item = img_name.split(".") if item[1] == "png": im_in = cv.LoadImage(FOLDER_PATH + img_name) im_ro = cv.CreateImage(cv.GetSize(im_in), cv.IPL_DEPTH_8U, 3) rotate_mat = cv.CreateMat(2, 3, cv.CV_32FC1) count += 1 for i in range(0, 4): cv.GetRotationMatrix2D((im_in.height / 2, im_in.width / 2), angle[i], 1, rotate_mat) cv.WarpAffine(im_in, im_ro, rotate_mat) cv.SaveImage(OUTPUT_PATH + item[0] + "_" + str(i) + ".png", im_ro) count += 1 print count
#!/usr/bin/python import cv #Import functions from OpenCV cv.NamedWindow('a_window', cv.CV_WINDOW_AUTOSIZE) storage=cv.CreateMemStorage() image=cv.LoadImage('amundi.jpg', cv.CV_LOAD_IMAGE_COLOR) #Load the image grey = cv.CreateImage(cv.GetSize(image), 8, 1) cv.CvtColor(image, grey, cv.CV_BGR2GRAY) #cv.EqualizeHist(grey, grey) cv.Laplace(grey, grey, 3) #clone = cv.CloneImage(image) #contours = cv.FindContours(grey, storage, cv.CV_RETR_LIST, cv.CV_CHAIN_APPROX_SIMPLE, (0,0)) #cv.DrawContours(image, contours, -1, (255,0,0), 5) cv.ShowImage('a_window', grey) #Show the image cv.WaitKey(10000)
''' import cv from raspicam import RaspiCam # initialize the class cam = RaspiCam() #cam.width,cam.height = 2592/2,1944/2 # half size image cam.width,cam.height = 2592/6,1944/6 # capture image from Raspi camera image = cam.piCapture() #capture = cv.CaptureFromFile(file) #image = cv.QueryFrame(capture) # font options w,h = cv.GetSize(image) #font_h = 2 font_h = .75 #font_w = 2 font_w = .75 #thickness = 2 thickness = 1 line_type = 8 # 8 (or omitted) 8-connected line # 4 4-connected line # CV_AA antialiased line # create font font = cv.InitFont(cv.CV_FONT_HERSHEY_PLAIN,font_h,font_w,0,thickness,line_type) color = cv.CV_RGB(255,255,255) cv.PutText(image,"This image was taken using the\n Raspberry Pi Camera Module", (50,h-50),font,color) cv.SaveImage("original.jpg",image) print "Saving image with overlayed text as 'original.jpg'"
font=pygame.font.Font(None,50) font2=pygame.font.Font(None,30) # Window Title pygame.display.set_caption("OpenCV + SURF") screen = pygame.display.get_surface() # Image capture, we throw away the first few images and # thereby let the auto-gain do its thing. for i in xrange(10): img = cv.QueryFrame(camera) image_size = cv.GetSize(img) img_rgb = cv.CreateImage(image_size, cv.IPL_DEPTH_8U, 3) grayscale = cv.CreateImage(image_size,cv.IPL_DEPTH_8U, 1) exit = False while not exit: startTime = time.time() for event in pygame.event.get(): if event.type == pygame.QUIT: exit = True # CAPTURE IMAGE img = cv.QueryFrame(camera) cv.CvtColor(img, img_rgb, cv.CV_BGR2RGB)
def process_frame(self, frame): (w, h) = cv.GetSize(frame) #generate hue selection frames #create locations for the a pair of test frames frametest = cv.CreateImage(cv.GetSize(frame), 8, 3) binarytest = cv.CreateImage(cv.GetSize(frame), 8, 1) #use the red channel for the binary frame (just for debugging purposes) cv.Copy(frame, frametest) cv.SetImageCOI(frametest, 3) cv.Copy(frametest, binarytest) cv.SetImageCOI(frametest, 0) #reset COI #svr.debug("R?",binarytest) # Resize image to 320x240 #copy = cv.CreateImage(cv.GetSize(frame), 8, 3) #cv.Copy(frame, copy) #cv.SetImageROI(frame, (0, 0, 320, 240)) #cv.Resize(copy, frame, cv.CV_INTER_NN) found_gate = False #create a new frame just for comparison purposes unchanged_frame = cv.CreateImage(cv.GetSize(frame), 8, 3) cv.Copy(frame, unchanged_frame) #apply a course noise filter cv.Smooth(frame, frame, cv.CV_MEDIAN, 7, 7) # Set binary image to have saturation channel hsv = cv.CreateImage(cv.GetSize(frame), 8, 3) binary = cv.CreateImage(cv.GetSize(frame), 8, 1) cv.CvtColor(frame, hsv, cv.CV_BGR2HSV) cv.SetImageCOI(hsv, 1) cv.Copy(hsv, binary) cv.SetImageCOI(hsv, 0) #reset COI #shift hue of image such that orange->red are at top of spectrum binary = libvision.misc.cv_to_cv2(binary) binary = libvision.misc.shift_hueCV2(binary, self.target_shift) binary = libvision.misc.cv2_to_cv(binary) #correct for wraparound on red spectrum #cv.InRange(binary,a_array,b_array,binarytest) #generate mask #cv.Add(binary,cv.fromarray(ones*180),binary,mask=binarytest) #use mask to selectively add values #svr.debug("R2?",binary) svr.debug("R2?", binary) #run adaptive threshold for edge detection and more noise filtering cv.AdaptiveThreshold( binary, binary, 255, cv.CV_ADAPTIVE_THRESH_MEAN_C, cv.CV_THRESH_BINARY_INV, self.adaptive_thresh_blocksize, self.adaptive_thresh, ) # Morphology kernel = cv.CreateStructuringElementEx(5, 5, 3, 3, cv.CV_SHAPE_ELLIPSE) cv.Erode(binary, binary, kernel, 1) cv.Dilate(binary, binary, kernel, 1) if self.debug: color_filtered = cv.CloneImage(binary) # Get Edges cv.Canny(binary, binary, 30, 40) # Hough Transform line_storage = cv.CreateMemStorage() raw_lines = cv.HoughLines2(binary, line_storage, cv.CV_HOUGH_STANDARD, rho=1, theta=math.pi / 180, threshold=self.hough_threshold, param1=0, param2=0) # Get vertical lines vertical_lines = [] for line in raw_lines: if line[1] < self.vertical_threshold or \ line[1] > math.pi-self.vertical_threshold: #absolute value does better grouping currently vertical_lines.append((abs(line[0]), line[1])) #print message to user for performance purposes logging.debug("{} possibilities reduced to {} lines".format( len(raw_lines), len(vertical_lines))) # Group vertical lines vertical_line_groups = [ ] # A list of line groups which are each a line list i = 0 for line in vertical_lines: group_found = False for line_group in vertical_line_groups: i += 1 if line_group_accept_test(line_group, line, self.max_range): line_group.append(line) group_found = True if not group_found: vertical_line_groups.append([line]) #quick debugging statement logging.debug("{} internal iterations for {} groups".format( i, len(vertical_line_groups))) # Average line groups into lines vertical_lines = [] for line_group in vertical_line_groups: rhos = map(lambda line: line[0], line_group) angles = map(lambda line: line[1], line_group) line = (sum(rhos) / len(rhos), circular_average(angles, math.pi)) vertical_lines.append(line) #################################################### #vvvv Horizontal line code isn't used for anything # Get horizontal lines horizontal_lines = [] for line in raw_lines: dist_from_horizontal = (math.pi / 2 + line[1]) % math.pi if dist_from_horizontal < self.horizontal_threshold or \ dist_from_horizontal > math.pi-self.horizontal_threshold: horizontal_lines.append((abs(line[0]), line[1])) # Group horizontal lines horizontal_line_groups = [ ] # A list of line groups which are each a line list for line in horizontal_lines: group_found = False for line_group in horizontal_line_groups: if line_group_accept_test(line_group, line, self.max_range): line_group.append(line) group_found = True if not group_found: horizontal_line_groups.append([line]) if len(horizontal_line_groups) is 1: self.seen_crossbar = True if self.debug: rhos = map(lambda line: line[0], horizontal_line_groups[0]) angles = map(lambda line: line[1], horizontal_line_groups[0]) line = (sum(rhos) / len(rhos), circular_average(angles, math.pi)) horizontal_lines = [line] else: self.seen_crossbar = False horizontal_lines = [] #^^^ Horizontal line code isn't used for anything ################################################### self.left_pole = None self.right_pole = None #print vertical_lines self.returning = 0 self.found = False if len(vertical_lines) is 2: roi = cv.GetImageROI(frame) width = roi[2] height = roi[3] self.left_pole = round( min(vertical_lines[0][0], vertical_lines[1][0]), 2) - width / 2 self.right_pole = round( max(vertical_lines[0][0], vertical_lines[1][0]), 2) - width / 2 self.returning = (self.left_pole + self.right_pole) / 2 logging.info("Returning {} as gate center delta.".format( self.returning)) #initialize first iteration with 2 known poles if self.last_seen < 0: self.last_center = None self.last_seen = 0 #increment a counter if result is good. if self.last_center is None: self.last_center = self.returning self.seen_count = 1 elif math.fabs(self.last_center - self.returning) < self.center_trans_thresh: self.seen_count += 1 self.last_seen += 2 else: self.last_seen -= 1 #if not conviced, forget left/right pole. Else proclaim success. if self.seen_count < self.seen_count_thresh: self.left_pole = None self.right_pole = None else: print "FOUND CENTER AND RETURNED IT" self.found = True else: self.returning = 0 if self.last_seen < 0: self.last_center = None self.last_seen = 0 self.last_seen -= 1 self.left_pole = None self.right_pole = None #TODO: If one pole is seen, is it left or right pole? if self.debug: cv.CvtColor(color_filtered, frame, cv.CV_GRAY2RGB) libvision.misc.draw_lines(frame, vertical_lines) libvision.misc.draw_lines(frame, horizontal_lines) if self.found: cv.Circle(frame, (int(frame.width / 2 + self.returning), int(frame.height / 2)), 15, (0, 255, 0), 2, 8, 0) font = cv.InitFont(cv.CV_FONT_HERSHEY_SIMPLEX, 1, 1, 1, 3) cv.PutText(frame, "Gate Sent to Mission Control", (100, 400), font, (255, 255, 0)) #print frame.width #cv.ShowImage("Gate", cv.CloneImage(frame)) svr.debug("Gate", cv.CloneImage(frame)) svr.debug("Unchanged", cv.CloneImage(unchanged_frame)) #populate self.output with infos self.output.seen_crossbar = self.seen_crossbar self.output.left_pole = self.left_pole self.output.right_pole = self.right_pole self.return_output()
def downsample_and_detect(self, rgb): """ Downsample the input image to approximately VGA resolution and detect the calibration target corners in the full-size image. Combines these apparently orthogonal duties as an optimization. Checkerboard detection is too expensive on large images, so it's better to do detection on the smaller display image and scale the corners back up to the correct size. Returns (scrib, corners, downsampled_corners, board, (x_scale, y_scale)). """ # Scale the input image down to ~VGA size (width, height) = cv.GetSize(rgb) scale = math.sqrt((width * height) / (640. * 480.)) if scale > 1.0: scrib = cv.CreateMat(int(height / scale), int(width / scale), cv.GetElemType(rgb)) cv.Resize(rgb, scrib) else: scrib = cv.CloneMat(rgb) # Due to rounding, actual horizontal/vertical scaling may differ slightly x_scale = float(width) / scrib.cols y_scale = float(height) / scrib.rows if self.pattern == Patterns.Chessboard: # Detect checkerboard (ok, downsampled_corners, board) = self.get_corners(scrib, refine=True) # Scale corners back to full size image corners = None if ok: if scale > 1.0: # Refine up-scaled corners in the original full-res image # TODO Does this really make a difference in practice? corners_unrefined = [(c[0] * x_scale, c[1] * y_scale) for c in downsampled_corners] # TODO It's silly that this conversion is needed, this function should just work # on the one-channel mono image mono = cv.CreateMat(rgb.rows, rgb.cols, cv.CV_8UC1) cv.CvtColor(rgb, mono, cv.CV_BGR2GRAY) radius = int(math.ceil(scale)) corners = cv.FindCornerSubPix( mono, corners_unrefined, (radius, radius), (-1, -1), (cv.CV_TERMCRIT_EPS + cv.CV_TERMCRIT_ITER, 30, 0.1)) else: corners = downsampled_corners else: # Circle grid detection is fast even on large images (ok, corners, board) = self.get_corners(rgb) # Scale corners to downsampled image for display downsampled_corners = None if ok: print corners if scale > 1.0: downsampled_corners = [(c[0] / x_scale, c[1] / y_scale) for c in corners] else: downsampled_corners = corners return (scrib, corners, downsampled_corners, board, (x_scale, y_scale))
def cropImage(img, croparea): inbetween = cv.CreateImage(cv.GetSize(img), img.depth, img.nChannels) cv.Copy(img, inbetween) cv.SetImageROI(inbetween, croparea) return inbetween
def show( self ): """ Process and show the current frame """ source = cv.LoadImage( self.files[self.index] ) width, height = cv.GetSize(source) center = (width/2) + self.offset; cv.Line( source, (center,0), (center,height), (0,255,0), 1) if self.roi: x,y,a,b = self.roi; width, height = ((a - x), (b - y)) mask = cv.CreateImage( (width, height), cv.IPL_DEPTH_8U, 1) cv.SetImageROI( source, (x, y, width, height)) cv.Split( source, None, None, mask, None ); gray = cv.CloneImage( mask ); cv.InRangeS( mask, self.thresholdMin, self.thresholdMax, mask ); cv.And( mask, gray, gray ); line = []; points = []; for i in range(0,height-1): point = (0, 0, 0) row = cv.GetRow( gray, i) minVal,minLoc,maxLoc,maxVal = cv.MinMaxLoc(row); y = i; x = maxVal[0] if x > 0: line.append((x,y)); s = x / sin(radians(self.camAngle)) x = s * cos(self.angles[self.index]) z = height - y y = s * sin(self.angles[self.index]) point = (round(x,2),round(y,2),z); points.append(point) cv.PolyLine( source, [line], False, (255,0,0), 2, 8) cv.ResetImageROI( source ) x,y,a,b = self.roi; cv.Rectangle( source, (int(x), int(y)), (int(a), int(b)), (255.0, 255, 255, 0) ); if self.roi: x,y,a,b = self.roi; width, height = ((a - x), (b - y)) mask = cv.CreateImage( (width, height), cv.IPL_DEPTH_8U, 1) cv.SetImageROI( source, (x-width, y, width, height)) # moves roi to the left cv.Split( source, None, None, mask, None ); gray = cv.CloneImage( mask ); cv.InRangeS( mask, self.thresholdMin, self.thresholdMax, mask ); cv.And( mask, gray, gray ); line = []; #points = []; for i in range(0,height-1): point = (0, 0, 0) row = cv.GetRow( gray, i) minVal,minLoc,maxLoc,maxVal = cv.MinMaxLoc(row); y = i; x = maxVal[0] if x > 0: line.append((x,y)); x = width - x; # left to the x-axis s = x / sin(radians(self.camAngle)) x = s * cos(self.angles[self.index]) z = height - y# 500 higher then the other. y = s * sin(self.angles[self.index]) #x' = x*cos q - y*sin q #y' = x*sin q + y*cos q #z' = z a = radians(300) nx = ( cos(a) * x ) - ( sin(a) * y ) ny = ( sin(a) * x ) + ( cos(a) * y ) point = (nx,ny,z); #if point[0] != 0: # points[i] = point; points.append(point) cv.PolyLine( source, [line], False, (255,0,0), 2, 8) cv.ResetImageROI( source ) x,y,a,b = self.roi; cv.Rectangle( source, (int(x), int(y)), (int(a), int(b)), (255.0, 255, 255, 0) ); #if self.roi: # x,y,a,b = self.roi; # # width, height = ((a - x), (b - y)) # # roi = [x-width, y, x, b]; # # x,y,a,b = roi; # # # mask = cv.CreateImage( (width, height), cv.IPL_DEPTH_8U, 1) # # cv.SetImageROI( source, (x, y, width, height)) # cv.Split( source, None, None, mask, None ); # # gray = cv.CloneImage( mask ); # # cv.InRangeS( mask, self.thresholdMin, self.thresholdMax, mask ); # cv.And( mask, gray, gray ); # cv.ShowImage('tmp', gray) # # line = []; # #points = []; # # for i in range(0,height-1): # point = (0, 0, 0) # row = cv.GetRow( gray, i) # # minVal,minLoc,maxLoc,maxVal = cv.MinMaxLoc(row); # # y = i; # x = maxVal[0] # # if x > 0: # line.append((x,y)); # # x = x - width # # s = x / sin(-330) # x = s * cos(self.angles[self.index]) # z = y/2.0 # y = s * sin(self.angles[self.index]) # # point = (x,y,z); # # points.append(point) # # # cv.PolyLine( source, [line], False, (255,0,0), 2, 8) # cv.ResetImageROI( source ) # x,y,a,b = self.roi; # # cv.Rectangle( source, (roi[0], roi[1]), (roi[2], roi[3]), (255.0, 255, 255, 0) ); # if self.mode == 'mask': cv.ShowImage( 'preview', mask ) return if self.mode == 'record' and self.roi: font = cv.InitFont(cv.CV_FONT_HERSHEY_SIMPLEX,0.5,0.5,1) cv.PutText( source, "recording %d" % self.index, (20,20), font, (0,0,255)) self.points.extend(points); #self.colors.extend(colors); cv.ShowImage( 'preview', source )
def findBlobs(img, min_color, max_color, window, renderedwindow, location, area): blobs = cvblob.Blobs() #initializes the blobs class size = cv.GetSize(img) #gets size of image hsv = cv.CreateImage(size, cv.IPL_DEPTH_8U, 3) #New HSV image for alter thresh = cv.CreateImage(size, cv.IPL_DEPTH_8U, 1) #New Gray Image for later labelImg = cv.CreateImage(size, cvblob.IPL_DEPTH_LABEL, 1) #New Blob image for later cv.CvtColor(img, hsv, cv.CV_BGR2HSV) #converts image to hsv image cv.InRangeS(hsv, min_color, max_color, thresh) #thresholds it #Corrections to remove false positives cv.Smooth(thresh, thresh, cv.CV_BLUR) cv.Dilate(thresh, thresh) cv.Erode(thresh, thresh) result = cvblob.Label(thresh, labelImg, blobs) #extracts blobs from a greyscale image numblobs = len( blobs.keys()) #number of blobs based off of length of blobs dictionary #if there are blobs found if (numblobs > 0): avgsize = int(result / numblobs) print str(numblobs) + " blobs found covering " + str(result) + "px" #Removes blobs that are smaller than a certain size based off of average size remv = [] for x in blobs: if (blobs[x].area < avgsize / 3): remv.append(x) for x in remv: del blobs[x] numblobs = len( blobs.keys()) #gets the number of blobs again after removing some print str(numblobs) + " blobs remaining" filtered = cv.CreateImage(cv.GetSize(img), cv.IPL_DEPTH_8U, 1) cvblob.FilterLabels( labelImg, filtered, blobs ) #Creates a binary image with the blobs formed (imgIn, imgOut, blobs) #Centroid, area, and circle for all blobs for blob in blobs: location.append(cvblob.Centroid(blobs[blob])) area.append(blobs[blob].area) cv.Circle(img, (int(cvblob.Centroid( blobs[blob])[0]), int(cvblob.Centroid(blobs[blob])[1])), int(math.sqrt(int(blobs[blob].area) / 3.14)) + 25, cv.Scalar(0, 0, 0)) imgOut = cv.CreateImage(cv.GetSize(img), cv.IPL_DEPTH_8U, 3) cv.Zero(imgOut) cvblob.RenderBlobs( labelImg, blobs, img, imgOut, cvblob.CV_BLOB_RENDER_COLOR | cvblob.CV_BLOB_RENDER_CENTROID | cvblob.CV_BLOB_RENDER_BOUNDING_BOX | cvblob.CV_BLOB_RENDER_ANGLE, 1.0) #Marks up the blobs image to put bounding boxes, etc on it cv.ShowImage("Window", img) #shows the orininalimage cv.ShowImage("Rendered", imgOut) #shows the blobs image return blobs #returns the list of blobs else: print " ...Zero blobs found. \nRedifine color range for better results" #if no blobs were found print an error message cv.ShowImage("Window", img) #show the original image
def img2ary(self, img): a = numpy.fromstring(img.tostring(), numpy.float32) a.shape = cv.GetSize(img) return a
def getThresholdImage(imgHSV): imgThresh = cv.CreateImage(cv.GetSize(imgHSV), IPL_DEPTH_8U, 1) cv.InRangeS(imgHSV, cv.Scalar(lowerH, lowerS, lowerV), cv.Scalar(upperH, upperS, UpperV), imgThresh) return imgThresh
def frame2surface(self, frame): frame_rgb = cv.CreateMat(frame.height, frame.width, cv.CV_8UC3) cv.CvtColor(frame, frame_rgb, cv.CV_BGR2RGB) return pygame.image.frombuffer(frame_rgb.tostring(), cv.GetSize(frame_rgb), "RGB")
def process_frame(self, frame): found_path = False cv.Smooth(frame, frame, cv.CV_MEDIAN, 7, 7) # use RGB color finder binary = libvision.cmodules.target_color_rgb.find_target_color_rgb(frame, 250, 125, 0, 1500, 500, .3) if self.debug: color_filtered = cv.CloneImage(binary) # Get Edges cv.Canny(binary, binary, 30, 40) # Hough Transform line_storage = cv.CreateMemStorage() lines = cv.HoughLines2(binary, line_storage, cv.CV_HOUGH_STANDARD, rho=1, theta=math.pi / 180, threshold=self.hough_threshold, param1=0, param2=0 ) lines = lines[:self.lines_to_consider] # Limit number of lines # If there are at least 2 lines and they are close to parallel... # There's a path! if len(lines) >= 2: # Find: min, max, average theta_max = lines[0][1] theta_min = lines[0][1] total_theta = 0 for rho, theta in lines: total_theta += theta if theta_max < theta: theta_max = theta if theta_min > theta: theta_min = theta theta_range = theta_max - theta_min # Near vertical angles will wrap around from pi to 0. If the range # crosses this vertical line, the range will be way too large. To # correct for this, we always take the smallest angle between the # min and max. if theta_range > math.pi / 2: theta_range = math.pi - theta_range if theta_range < self.theta_threshold: found_path = True angles = map(lambda line: line[1], lines) self.theta = circular_average(angles, math.pi) if found_path: self.seen_in_a_row += 1 else: self.seen_in_a_row = 0 # stores whether or not we are confident about the path's presence object_present = False if self.seen_in_a_row >= self.seen_in_a_row_threshold: object_present = True self.image_coordinate_center = self.find_centroid(binary) # Move the origin to the center of the image self.center = ( self.image_coordinate_center[0] - frame.width / 2, self.image_coordinate_center[1] * -1 + frame.height / 2 ) if self.debug: # Show color filtered color_filtered_rgb = cv.CreateImage(cv.GetSize(frame), 8, 3) cv.CvtColor(color_filtered, color_filtered_rgb, cv.CV_GRAY2RGB) cv.SubS(color_filtered_rgb, (255, 0, 0), color_filtered_rgb) cv.Sub(frame, color_filtered_rgb, frame) # Show edges binary_rgb = cv.CreateImage(cv.GetSize(frame), 8, 3) cv.CvtColor(binary, binary_rgb, cv.CV_GRAY2RGB) cv.Add(frame, binary_rgb, frame) # Add white to edge pixels cv.SubS(binary_rgb, (0, 0, 255), binary_rgb) cv.Sub(frame, binary_rgb, frame) # Remove all but Red # Show lines if self.seen_in_a_row >= self.seen_in_a_row_threshold: rounded_center = ( int(round(self.image_coordinate_center[0])), int(round(self.image_coordinate_center[1])), ) cv.Circle(frame, rounded_center, 5, (0, 255, 0)) libvision.misc.draw_lines(frame, [(frame.width / 2, self.theta)]) else: libvision.misc.draw_lines(frame, lines) #cv.ShowImage("Path", frame) svr.debug("Path", frame) # populate self.output with infos self.output.found = object_present self.output.theta = self.theta if self.center: # scale center coordinates of path based on frame size self.output.x = self.center[0] / (frame.width / 2) self.output.y = self.center[1] / (frame.height / 2) libvision.misc.draw_linesC(frame, [(frame.width / 2, self.output.theta)],[255,0,255]) print "Output Returned!!! ", self.output.theta else: self.output.x = None self.output.y = None print "No output..." if self.output.found and self.center: print self.output self.return_output()
def show(self): """ Process and show the current frame """ source = cv.LoadImage(self.files[self.index]) width, height = cv.GetSize(source) center = (width / 2) + self.offset cv.Line(source, (center, 0), (center, height), (0, 255, 0), 1) mask = cv.CreateImage((width, height), cv.IPL_DEPTH_8U, 1) if self.roi: red = cv.CreateImage((width, height), cv.IPL_DEPTH_8U, 1) cv.Zero(mask) cv.Split(source, None, None, red, None) cv.InRangeS(red, self.thresholdMin, self.thresholdMax, red) cv.Copy(red, mask) x, y, x1, y1 = self.roi width = x1 - x height = y1 - y cv.Rectangle(source, (int(x), int(y)), (int(x1), int(y1)), (255.0, 255, 255, 0)) cv.SetImageROI(source, (x, y, width, height)) cv.SetImageROI(red, (x, y, width, height)) tmp = cv.CreateImage(cv.GetSize(red), cv.IPL_DEPTH_8U, 1) cv.Zero(tmp) cv.Copy(red, tmp) line = [] points = [] #cv.ShowImage('red',tmp); step = 1 for i in range(0, height - 1): row = cv.GetRow(tmp, i) minVal, minLoc, maxLoc, maxVal = cv.MinMaxLoc(row) y = i x = maxVal[0] point = (0, 0, 0) if x > 0: line.append((x, y)) s = x / sin(self.camAngle) x = s * cos(self.angles[self.index]) z = y / 2.0 y = s * sin(self.angles[self.index]) point = (x, y, z) points.append(point) cv.PolyLine(source, [line], False, (255, 0, 0), 2, 8) cv.ResetImageROI(red) cv.ResetImageROI(source) if self.mode == 'mask': cv.ShowImage('preview', mask) return if self.mode == 'record' and self.roi: font = cv.InitFont(cv.CV_FONT_HERSHEY_SIMPLEX, 0.5, 0.5, 1) cv.PutText(source, "recording %d" % self.index, (20, 20), font, (0, 0, 255)) self.points.extend(points) #self.colors.extend(colors); cv.ShowImage('preview', source)
def __init__(self, cv_image): self.cv_image = cv_image cv_size = cv.GetSize(cv_image) self.width = cv_size[0] self.height = cv_size[1]
def spectrum(): array = numpy.zeros((480, 640, 3), dtype='uint8') for y in range(480): for x in range(640): h = int((255 * x) / 640) s = 255 v = int((255 * y) / 480) array[y, x] = (h, s, v) hsv = cv.GetImage(cv.fromarray(array)) return hsv if len(args) == 0: hsv = spectrum() rgb = cv.CreateImage(cv.GetSize(hsv), 8, 3) cv.CvtColor(hsv, rgb, cv.CV_HSV2RGB) else: img = cuav_util.LoadImage(args[0]) (w, h) = cv.GetSize(img) img2 = cv.CreateImage((w / 2, h / 2), 8, 3) cv.Resize(img, img2) hsv = cv.CreateImage((w / 2, h / 2), 8, 3) cv.CvtColor(img2, hsv, cv.CV_RGB2HSV) rgb = cv.CreateImage(cv.GetSize(hsv), 8, 3) cv.CvtColor(hsv, rgb, cv.CV_HSV2RGB) cv.ShowImage('HSV', rgb) cv.SetMouseCallback('HSV', mouse_event, (rgb, hsv)) cuav_util.cv_wait_quit()
def callback(data): global frame global frame_size global gray_frame global canny_result global forrun global old_target bridge = CvBridge() try: cv_image = bridge.imgmsg_to_cv(data, "bgr8") except CvBridgeError, e: print e frame = cv_image frame_size = cv.GetSize(frame) gray_frame = cv.CreateImage(frame_size, cv.IPL_DEPTH_8U, 1) canny_result = cv.CreateImage(frame_size, cv.IPL_DEPTH_8U, 1) cv.CreateTrackbar("canny_init", 'camera', get_canny_init(), 1000, on_init_change) cv.CreateTrackbar("canny_link", 'camera', get_canny_link(), 1000, on_link_change) forrun = frame_size[1] / 2 old_target = [frame_size[0] / 2, forrun] los() def main(args): global canny_init global canny_link global frame_on
img_cal_f_p = [-6.2062e-07, 2.1236e-03, 2.3781] for im in range(0, 5): print "" print img_src[im] cv_img_input = cv.LoadImage(img_src[im], cv.CV_LOAD_IMAGE_GRAYSCALE) cv.SetImageROI(cv_img_input, ((img_center[im][0] - img_outsideR[im]), (img_center[im][1] - img_outsideR[im]), (img_outsideR[im] * 2), (img_outsideR[im] * 2))) cv.SaveImage("out_roi_" + img_src[im], cv_img_input) cv_img_width = cv.GetSize(cv_img_input)[0] cv_img_height = cv.GetSize(cv_img_input)[1] i_0 = cv_img_width / 2 j_0 = cv_img_height / 2 # magic schrooms #d_c = (((cv_img_width / 2)**2 + 0*(cv_img_height / 2)**2)**0.5) * (img_cal_f[im]) # inteligent schrooms magic = img_cal_f_p[0] * ( img_outsideR[im]** 2) + img_cal_f_p[1] * img_outsideR[im] + img_cal_f_p[2] d_c = (((cv_img_width / 2)**2 + 0 * (cv_img_height / 2)**2)**0.5) * magic
def pixelNo2xy(pixelNo,img): (width,height) = cv.GetSize(img) y = int(pixelNo / (width-1)) x = pixelNo - y*(width-1) return (x,y)
import cv capture = cv.CaptureFromCAM(-1) cv.SetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_WIDTH, 960) cv.SetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_HEIGHT, 720) im = cv.QueryFrame(capture) cv.SaveImage("/home/pi/dish/capture/camera-test.jpg", im) edges = cv.CreateImage(cv.GetSize(im), cv.IPL_DEPTH_8U, 1) cv.CvtColor(im, edges, cv.CV_BGR2GRAY) thresh = 100 cv.Canny(edges, edges, thresh, thresh / 2, 3) cv.Smooth(edges, edges, cv.CV_GAUSSIAN, 3, 3) storage = cv.CreateMat(640, 1, cv.CV_32FC3) cv.HoughCircles(edges, storage, cv.CV_HOUGH_GRADIENT, 2, edges.width / 10, thresh, 350, 0, 0) # f = open("/home/pi/dish/sink-empty.txt", "w") # for i in range(storage.rows): # val = storage[i, 0] # radius = int(val[2]) # center = (int(val[0]), int(val[1])) # f.write(str(center[0]) + "," + str(center[1]) + "," + str(radius) + "\n") # cv.Circle(im, center, radius, (0, 255, 0), thickness=2) # cv.SaveImage("/home/pi/dish/capture/sink-empty.jpg", im) dirty = False f = open("/home/pi/dish/sink-empty.txt", "r") drains = [] for line in f: val = line.split(",")
def start_vision(): """ Creates a video feed and shows the colored blobs detected """ frame = 0 cv.NamedWindow("video", cv.CV_WINDOW_AUTOSIZE) #NOTE: BREAKS HERE cam = s.Camera(0, {"width": 720, "height": 576}) # im = cam.getImage() # print type(im) print "There" #Calibrate cam #TO DO:Create new calibration data chessboard_dim = (9, 6) cal = Calibrate(chessboard_dim) cal.calibrate_camera_from_mem("utils/calibration_data/") img = cam.getImage().getBitmap() #number of frames to construct bg numFrames = 30 #Backg image, undistort it bg = cv.LoadImage("00000006.jpg") undist_im = cal.undist_fisheye(bg) cropped = cal.crop_frame(undist_im) bgFixed = cal.undist_perspective(cropped, corners_from_mem=False) frame_dim = cv.GetSize(bgFixed) ba = BackgroundAveraging(bgFixed) while (True): #This is frame count(?) frame += 1 img = cam.getImage().getBitmap() #Build BG model if (frame < numFrames): undist_im = cal.undist_fisheye(img) cropped = cal.crop_frame(undist_im) baseimg = cal.undist_perspective(cropped, corners_from_mem=False) ba.accumulate_background(baseimg) elif (frame == numFrames): ba.create_models_from_stats() else: #ba.background_diff(img, mask1) #more arguments added, cal is a camera calibration object # ba is background model #If ba = None, pre-saved image is used as bg # (im, centers) = detect_centers(img, cal, ba, bgFixed) # OUTPUT THE LINE INTO THE FORMAT THE STRATEGY TEAM WANT #--- only after bg is built----- yellow, blue, ball = centers framebuffer = "%d;%f,%f,%f;%f,%f,%f;%f,%f" % ( frame, yellow[0], yellow[1], yellow[2], blue[0], blue[1], blue[2], ball[0], ball[1]) cv.ShowImage("video", im) # cv.WaitKey(0) if (cv.WaitKey(10) != -1): break # Exit the server thread when the main thread terminates cv.DestroyWindow("video")
import cv ob = cv.LoadImage("../ref/Level1/level1_sample3.png", cv.CV_LOAD_IMAGE_GRAYSCALE) size = cv.GetSize(ob) obb = cv.CreateImage(size, 8, 1) cv.Threshold(ob, obb, 128, 255, cv.CV_THRESH_BINARY) obd = cv.CreateImage(size, 8, 1) obe = cv.CreateImage(size, 8, 1) cv.Dilate(obb, obd, None, 4) cv.Erode(obd, obe, None, 4) cv.NamedWindow("src") cv.ShowImage("src", ob) cv.NamedWindow("Closing") cv.ShowImage("Closing", obe) cv.WaitKey(0) cv.DestroyWindow("Closing")