def depthmatrix(leftimage, rightimage, precision=4, mask=0): """Returns a 3-channel 32bit floating-point distance matrix. Channels 1,2,3 = x,y,z coordinates of that point. Precision is the number of times to downsample mask. Downsample is the number of loops to go through with successively smaller match areas. If mask is set, only pixels in the mask are set.""" info = cv.cvGetSize(leftimage) width = info.width height = info.height precision_pixels = (2**precision) downsampled_size = cv.cvSize(width/precision_pixels, height/precision_pixels) print "Precision of", downsampled_size.width, downsampled_size.height, "px" if mask: downsampled_mask = cv.cvCreateImage(downsampled_size, 8, 1) cv.cvResize(mask, downsampled_mask) matx = cv.cvCreateImage(downsampled_size, 8, 1) maty = cv.cvCreateImage(downsampled_size, 8, 1) matz = cv.cvCreateImage(downsampled_size, 8, 1) for i in xrange(width/precision_pixels): for j in xrange(height/precision_pixels): if mask: if (not cv.cvGetReal2D(downsampled_mask, j, i)): continue x = i*precision y = j*precision depth = depthmatch(x+precision_pixels/2, y+precision_pixels/2, leftimage, rightimage, roi=precision_pixels, buf=precision_pixels*2) #print i, j # fill in result matrix if mask wasn't 0 at this point (X,Y,Z) cv.cvSetReal2D(matx, j, i, int(depth[0][0])) cv.cvSetReal2D(maty, j, i, int(depth[0][1])) cv.cvSetReal2D(matz, j, i, int(depth[0][2])) return matz
def same2ndValue(frame, x, y): size = cv.cvGetSize(frame) if(x >= 0 and x < size.width and y >= 0 and y < size.height): if(cv.cvGetReal2D(frame, y, x) == 0): return 0 else: return 1 #only return 1 if this pixel is also white else: return 0
def averageWhitePoints(frame): xtotal = 0.0 ytotal = 0.0 count = 0 size = cv.cvGetSize(frame) for x in range(size.width): for y in range(size.height): if (cv.cvGetReal2D(frame, y, x) > 200): xtotal = xtotal + x ytotal = ytotal + y count += 1 if count == 0: return 0, 0 return int(xtotal / count), int(ytotal / count)
def averageWhitePoints(frame): xtotal = 0.0 ytotal = 0.0 count = 0; size = cv.cvGetSize(frame) for x in range(size.width): for y in range(size.height): if(cv.cvGetReal2D(frame, y, x) > 200): xtotal = xtotal + x ytotal = ytotal + y count += 1 if count == 0: return -1, -1 return int(xtotal/count), int(ytotal/count)
def removeErrantPoints(frame): size = cv.cvGetSize(frame) for x in range(size.width): for y in range(size.height): if(cv.cvGetReal2D(frame, y, x) > 0): count = 0 count += same2ndValue(frame, x-1, y) count += same2ndValue(frame, x+1, y) count += same2ndValue(frame, x, y-1) count += same2ndValue(frame, x, y+1) count += same2ndValue(frame, x-1, y-1) count += same2ndValue(frame, x-1, y+1) count += same2ndValue(frame, x+1, y-1) count += same2ndValue(frame, x+1, y+1) if count == 0: cv.cvSet2D(frame, y, x, cv.cvScalar(0, 0, 0, 0))
def averageWhitePoints(frame): xtotal = 0.0 ytotal = 0.0 count = 0.0; for x in range(cam_width): for y in range(cam_height): if(cv.cvGetReal2D(frame, y, x) > 200): xtotal = xtotal + x ytotal = ytotal + y count += 1 #if(xtotal < 0): # xtotal = 0 #if(xtotal >= cam_width): # xtotal = cam_width-1 #if(ytotal < 0): # ytotal = 0 #if(ytotal >= cam_height): # ytotal = cam_height-1 if(count > 0): return int(xtotal/count), int(ytotal/count) return 0, 0
IMGW = 640 IMGH = 400 highgui.cvSetCaptureProperty(cap, highgui.CV_CAP_PROP_FRAME_WIDTH, IMGW) highgui.cvSetCaptureProperty(cap, highgui.CV_CAP_PROP_FRAME_HEIGHT, IMGH) tmp = highgui.cvQueryFrame(cap) # resize mask size = cv.cvGetSize(tmp) mask_r = cv.cvCreateImage(size, 8, 3) cv.cvResize(mask, mask_r) mask_bw = cv.cvCreateImage(size, 8, 1) cv.cvCvtColor(mask_r, mask_bw, cv.CV_RGB2GRAY) total_pixels = size.width * size.height sample_pixels = 0.0 for x in xrange(size.width): for y in xrange(size.height): if cv.cvGetReal2D(mask_bw, y, x) > 0: sample_pixels = sample_pixels + 1 print "Sample region: %f%%" % (100 * sample_pixels / total_pixels) del (tmp) h_bins = 20 h_limit = 180 s_bins = 32 v_bins = 32 v_limit = 255 # create histogram with 30 bins h_hue = cv.cvCreateHist([h_bins], cv.CV_HIST_ARRAY, [[0, h_limit]], 1) h_sat = cv.cvCreateHist([s_bins], cv.CV_HIST_ARRAY, [[0, 255]], 1) h_val = cv.cvCreateHist([v_bins], cv.CV_HIST_ARRAY, [[0, v_limit]], 1)