def normalise(self, i_ipl_image): #Do the affine transform if self.__rot_mat == None: warped_image = i_ipl_image else: warped_image = cv.cvCreateImage( cv.cvSize(i_ipl_image.width, i_ipl_image.height), 8, 1) cv.cvWarpAffine(i_ipl_image, warped_image, self.__rot_mat) #Crop if self.__roi == None: self.__cropped_image = warped_image else: self.crop(warped_image) #Scale if self.__resize_scale == 1: scaled_image = self.__cropped_image else: w = int(round(self.__cropped_image.width * self.__resize_scale)) h = int(round(self.__cropped_image.height * self.__resize_scale)) scaled_image = cv.cvCreateImage(cv.cvSize(w, h), 8, 1) cv.cvResize(self.__cropped_image, scaled_image, cv.CV_INTER_LINEAR) #Histogram equalisation if self.__equalise_hist: cv.cvEqualizeHist(scaled_image, scaled_image) #Blur if self.__filter_size == 0: smoothed_image = scaled_image else: smoothed_image = cv.cvCreateImage( cv.cvSize(scaled_image.width, scaled_image.height), 8, 1) cv.cvSmooth(scaled_image, smoothed_image, cv.CV_GAUSSIAN, self.__filter_size) return smoothed_image
def normalise(self, i_ipl_image): #Do the affine transform if self.__rot_mat == None: warped_image = i_ipl_image else: warped_image = cv.cvCreateImage(cv.cvSize(i_ipl_image.width,i_ipl_image.height), 8, 1) cv.cvWarpAffine(i_ipl_image , warped_image, self.__rot_mat ); #Crop if self.__roi == None: self.__cropped_image = warped_image else: self.crop(warped_image) #Scale if self.__resize_scale == 1: scaled_image = self.__cropped_image else: w = int(round( self.__cropped_image.width * self.__resize_scale)) h = int(round( self.__cropped_image.height * self.__resize_scale)) scaled_image = cv.cvCreateImage(cv.cvSize(w, h), 8, 1) cv.cvResize( self.__cropped_image, scaled_image ,cv.CV_INTER_LINEAR) #Histogram equalisation if self.__equalise_hist: cv.cvEqualizeHist(scaled_image,scaled_image) #Blur if self.__filter_size == 0: smoothed_image = scaled_image else: smoothed_image = cv.cvCreateImage(cv.cvSize(scaled_image.width, scaled_image.height), 8, 1) cv.cvSmooth(scaled_image, smoothed_image, cv.CV_GAUSSIAN, self.__filter_size) return smoothed_image
def normalize(img): """Utility function to normalize the image and return the new normalized image using opencv.""" norm = cvCreateImage(cvSize(img.width, img.height), IPL_DEPTH_32F, 1) cvCopy(img, norm) cvNormalize(norm, norm, 1, 0, CV_MINMAX) norm_u = cvCreateImage(cvSize(img.width, img.height), IPL_DEPTH_8U, 1) cvConvertScale(norm, norm_u, 255) return norm_u
def __init__(self, device_num = 0): self.camera = vd.VidereCap(device_num) #self.camera.process_frames() self.camera.set_mode(vd.NONE) vd.set_gain(self.camera.capture, 96) vd.set_exposure(self.camera.capture, 255) self.camera.process_frames() self.low_exposure_left = cv.cvCreateImage(cv.cvGetSize(self.camera.left_debayered), 8, 3) self.low_exposure_right = cv.cvCreateImage(cv.cvGetSize(self.camera.right_debayered), 8, 3)
def eigen_texture(cv_image, blocksize=8, filtersize=3): gray_image = cv.cvCreateImage(cv.cvSize(cv_image.width, cv_image.height), cv.IPL_DEPTH_8U, 1) eig_tex = cv.cvCreateImage(cv.cvSize(cv_image.width * 6, cv_image.height), cv.IPL_DEPTH_32F, 1) cv.cvCvtColor(cv_image, gray_image, cv.CV_BGR2GRAY) cv.cvCornerEigenValsAndVecs(gray_image, eig_tex, blocksize, filtersize) eig_tex_np = ut.cv2np(eig_tex) eig_tex_np = np.reshape(eig_tex_np, [cv_image.height, cv_image.width, 6]) return eig_tex_np[:, :, 0:2]
def eigen_texture(cv_image, blocksize=8, filtersize=3): gray_image = cv.cvCreateImage(cv.cvSize(cv_image.width, cv_image.height), cv.IPL_DEPTH_8U, 1) eig_tex = cv.cvCreateImage(cv.cvSize(cv_image.width*6, cv_image.height), cv.IPL_DEPTH_32F, 1) cv.cvCvtColor(cv_image, gray_image, cv.CV_BGR2GRAY) cv.cvCornerEigenValsAndVecs(gray_image, eig_tex, blocksize, filtersize) eig_tex_np = ut.cv2np(eig_tex) eig_tex_np = np.reshape(eig_tex_np, [cv_image.height, cv_image.width, 6]) return eig_tex_np[:,:,0:2]
def __init__(self, device_num=0): self.camera = vd.VidereCap(device_num) #self.camera.process_frames() self.camera.set_mode(vd.NONE) vd.set_gain(self.camera.capture, 96) vd.set_exposure(self.camera.capture, 255) self.camera.process_frames() self.low_exposure_left = cv.cvCreateImage( cv.cvGetSize(self.camera.left_debayered), 8, 3) self.low_exposure_right = cv.cvCreateImage( cv.cvGetSize(self.camera.right_debayered), 8, 3)
def drawImage(image,h,w,psize): """ Draw the image as a continuous line on a surface h by w "pixels" where each continuous line representation of a pixel in image is represented on the output suface using psize by psize "pixels". @param image an opencv image with at least 3 channels @param h integer representing the hight of the output surface @param w integer representing the width of the output surface @param psize ammount that each pixel in the input image is scaled up """ h = (h/psize)-2 w = (w/psize)-2 size = opencv.cvSize(w,h) scaled = opencv.cvCreateImage(size,8,3) red = opencv.cvCreateImage(size,8,1) blue = opencv.cvCreateImage(size,8,1) green = opencv.cvCreateImage(size,8,1) opencv.cvSplit(scaled,blue,green,red,None) opencv.cvEqualizeHist(red,red) opencv.cvEqualizeHist(green,green) opencv.cvEqualizeHist(blue,blue) opencv.cvMerge(red,green,blue,None,scaled) opencv.cvResize(image,scaled,opencv.CV_INTER_LINEAR) opencv.cvNot(scaled,scaled) # Draw each pixel in the image xr = range(scaled.width) whitespace = 0 for y in range(scaled.height): for x in xr: s = opencv.cvGet2D(scaled,y,x) s = [s[j] for j in range(3)] if (sum(s)/710.0 < 1.0/psize): whitespace = whitespace+psize else: if whitespace is not 0: line(whitespace,6,(xr[0]>0)) whitespace = 0 drawPixel([j/255.0 for j in s],psize,(xr[0]>0)) if whitespace is not 0: line(whitespace,6,(xr[0]>0)) whitespace = 0 line(psize,2) xr.reverse() displayImage(output) events = pygame.event.get() for event in events: if event.type == QUIT: exit()
def mask(img, img_mask): dim = img.width, img.height depth = img.depth channels = img.nChannels r_chan = cv.cvCreateImage(cv.cvSize(*dim), depth, 1) g_chan = cv.cvCreateImage(cv.cvSize(*dim), depth, 1) b_chan = cv.cvCreateImage(cv.cvSize(*dim), depth, 1) combined = cv.cvCreateImage(cv.cvSize(*dim), depth, 3) cv.cvSplit(img, r_chan, g_chan, b_chan, None) cv.cvAnd(r_chan, img_mask, r_chan) cv.cvAnd(g_chan, img_mask, g_chan) cv.cvAnd(b_chan, img_mask, b_chan) cv.cvMerge(r_chan, g_chan, b_chan, None, combined) return combined
def detect(image): # Find out how large the file is, as the underlying C-based code # needs to allocate memory in the following steps image_size = opencv.cvGetSize(image) # create grayscale version - this is also the point where the allegation about # facial recognition being racist might be most true. A caucasian face would have more # definition on a webcam image than an African face when greyscaled. # I would suggest that adding in a routine to overlay edge-detection enhancements may # help, but you would also need to do this to the training images as well. grayscale = opencv.cvCreateImage(image_size, 8, 1) opencv.cvCvtColor(image, grayscale, opencv.CV_BGR2GRAY) # create storage (It is C-based so you need to do this sort of thing) storage = opencv.cvCreateMemStorage(0) opencv.cvClearMemStorage(storage) # equalize histogram opencv.cvEqualizeHist(grayscale, grayscale) # detect objects - Haar cascade step # In this case, the code uses a frontal_face cascade - trained to spot faces that look directly # at the camera. In reality, I found that no bearded or hairy person must have been in the training # set of images, as the detection routine turned out to be beardist as well as a little racist! cascade = opencv.cvLoadHaarClassifierCascade('haarcascade_frontalface_alt.xml', opencv.cvSize(1,1)) faces = opencv.cvHaarDetectObjects(grayscale, cascade, storage, 1.2, 2, opencv.CV_HAAR_DO_CANNY_PRUNING, opencv.cvSize(50, 50)) if faces: for face in faces: # Hmm should I do a min-size check? # Draw a Chartreuse rectangle around the face - Chartruese rocks opencv.cvRectangle(image, opencv.cvPoint( int(face.x), int(face.y)), opencv.cvPoint(int(face.x + face.width), int(face.y + face.height)), opencv.CV_RGB(127, 255, 0), 2) # RGB #7FFF00 width=2
def PIL2Ipl(input): """Converts a PIL image to the OpenCV/IPL CvMat data format. Supported input image formats are: RGB L F """ if not (isinstance(input, PIL.Image.Image)): raise TypeError, 'Must be called with PIL.Image.Image!' # mode dictionary: # (pil_mode : (ipl_depth, ipl_channels) mode_list = { "RGB": (cv.IPL_DEPTH_8U, 3), "L": (cv.IPL_DEPTH_8U, 1), "F": (cv.IPL_DEPTH_32F, 1) } if not mode_list.has_key(input.mode): raise ValueError, 'unknown or unsupported input mode' result = cv.cvCreateImage( cv.cvSize(input.size[0], input.size[1]), # size mode_list[input.mode][0], # depth mode_list[input.mode][1] # channels ) # set imageData result.imageData = input.tostring() return result
def detect(self, image): # image size is needed by underlying opencv lib to allocate memory image_size = opencv.cvGetSize(image) # the algorithm works with grayscale images grayscale = opencv.cvCreateImage(image_size, 8, 1) opencv.cvCvtColor(image, grayscale, opencv.CV_BGR2GRAY) # more underlying c lib memory allocation storage = opencv.cvCreateMemStorage(0) opencv.cvClearMemStorage(storage) # equalize histogram opencv.cvEqualizeHist(grayscale, grayscale) # detect faces using haar cascade, the used file is trained to # detect frontal faces cascade = opencv.cvLoadHaarClassifierCascade( 'haarcascade_frontalface_alt.xml', opencv.cvSize(1, 1)) faces = opencv.cvHaarDetectObjects(grayscale, cascade, storage, 1.2, 2, opencv.CV_HAAR_DO_CANNY_PRUNING, opencv.cvSize(100, 100)) # draw rectangles around faces for face in faces: opencv.cvRectangle( image, opencv.cvPoint(int(face.x), int(face.y)), opencv.cvPoint(int(face.x + face.width), int(face.y + face.height)), opencv.CV_RGB(127, 255, 0), 2) # return faces casted to list here, otherwise some obscure bug # in opencv will make it segfault if the casting happens later return image, list(faces)
def detect(self, image): # image size is needed by underlying opencv lib to allocate memory image_size = opencv.cvGetSize(image) # the algorithm works with grayscale images grayscale = opencv.cvCreateImage(image_size, 8, 1) opencv.cvCvtColor(image, grayscale, opencv.CV_BGR2GRAY) # more underlying c lib memory allocation storage = opencv.cvCreateMemStorage(0) opencv.cvClearMemStorage(storage) # equalize histogram opencv.cvEqualizeHist(grayscale, grayscale) # detect faces using haar cascade, the used file is trained to # detect frontal faces cascade = opencv.cvLoadHaarClassifierCascade( 'haarcascade_frontalface_alt.xml', opencv.cvSize(1, 1)) faces = opencv.cvHaarDetectObjects( grayscale, cascade, storage, 1.2, 2, opencv.CV_HAAR_DO_CANNY_PRUNING, opencv.cvSize(100, 100)) # draw rectangles around faces for face in faces: opencv.cvRectangle( image, opencv.cvPoint( int(face.x), int(face.y)), opencv.cvPoint(int(face.x + face.width), int(face.y + face.height)), opencv.CV_RGB(127, 255, 0), 2) # return faces casted to list here, otherwise some obscure bug # in opencv will make it segfault if the casting happens later return image, list(faces)
def Ipl2Pil(i_image): o_flipped_image = cv.cvCreateImage( cv.cvSize(i_image.width, i_image.height), i_image.depth, i_image.nChannels) cv.cvFlip(i_image, o_flipped_image) o_pil_image = cv.adaptors.Ipl2Pil(o_flipped_image) return o_pil_image
def findcontours(iplimage, threshold=100): srcimage = opencv.cvCloneImage(iplimage) # create the storage area and bw image grayscale = opencv.cvCreateImage(opencv.cvGetSize(srcimage), 8, 1) opencv.cvCvtColor(srcimage, grayscale, opencv.CV_BGR2GRAY) # threshold opencv.cvThreshold(grayscale, grayscale, threshold, 255, opencv.CV_THRESH_BINARY) storage = opencv.cvCreateMemStorage(0) opencv.cvClearMemStorage(storage) # find the contours nb_contours, contours = opencv.cvFindContours(grayscale, storage) # comment this out if you do not want approximation contours = opencv.cvApproxPoly(contours, opencv.sizeof_CvContour, storage, opencv.CV_POLY_APPROX_DP, 3, 1) # next line is for ctypes-opencv # contours = opencv.cvApproxPoly (contours, opencv.sizeof(opencv.CvContour), storage, opencv.CV_POLY_APPROX_DP, 3, 1) conts = [] for cont in contours.hrange(): points = [] for pt in cont: points.append((pt.x, pt.y)) conts.append(points) opencv.cvReleaseMemStorage(storage) opencv.cvReleaseImage(srcimage) opencv.cvReleaseImage(grayscale) return (nb_contours, conts)
def findcontours(iplimage, threshold=100): srcimage = opencv.cvCloneImage(iplimage) # create the storage area and bw image grayscale = opencv.cvCreateImage(opencv.cvGetSize(srcimage), 8, 1) opencv.cvCvtColor(srcimage, grayscale, opencv.CV_BGR2GRAY) #threshold opencv.cvThreshold(grayscale, grayscale, threshold, 255, opencv.CV_THRESH_BINARY) storage = opencv.cvCreateMemStorage(0) opencv.cvClearMemStorage(storage) # find the contours nb_contours, contours = opencv.cvFindContours (grayscale, storage) # comment this out if you do not want approximation contours = opencv.cvApproxPoly (contours, opencv.sizeof_CvContour, storage, opencv.CV_POLY_APPROX_DP, 3, 1) # next line is for ctypes-opencv #contours = opencv.cvApproxPoly (contours, opencv.sizeof(opencv.CvContour), storage, opencv.CV_POLY_APPROX_DP, 3, 1) conts = [] for cont in contours.hrange(): points=[] for pt in cont: points.append((pt.x,pt.y)) conts.append(points) opencv.cvReleaseMemStorage(storage) opencv.cvReleaseImage(srcimage) opencv.cvReleaseImage(grayscale) return (nb_contours, conts)
def get_frame(self, face_rec = False): image = highgui.cvQueryFrame(self.device) face_matches = False if face_rec: grayscale = cv.cvCreateImage(cv.cvSize(640, 480), 8, 1) cv.cvCvtColor(image, grayscale, cv.CV_BGR2GRAY) storage = cv.cvCreateMemStorage(0) cv.cvClearMemStorage(storage) cv.cvEqualizeHist(grayscale, grayscale) for cascade in self.haarfiles: matches = cv.cvHaarDetectObjects(grayscale, cascade, storage, 1.2, 2, cv.CV_HAAR_DO_CANNY_PRUNING, cv.cvSize(100,100)) if matches: face_matches = True for i in matches: cv.cvRectangle(image, cv.cvPoint( int(i.x), int(i.y)), cv.cvPoint(int(i.x+i.width), int(i.y+i.height)), cv.CV_RGB(0,0,255), 2, 5, 0) image = cv.cvGetMat(image) return (image, face_matches)
def PIL2Ipl(input): """Converts a PIL image to the OpenCV/IPL CvMat data format. Supported input image formats are: RGB L F """ if not (isinstance(input, PIL.Image.Image)): raise TypeError, 'Must be called with PIL.Image.Image!' # mode dictionary: # (pil_mode : (ipl_depth, ipl_channels) mode_list = { "RGB" : (cv.IPL_DEPTH_8U, 3), "L" : (cv.IPL_DEPTH_8U, 1), "F" : (cv.IPL_DEPTH_32F, 1) } if not mode_list.has_key(input.mode): raise ValueError, 'unknown or unsupported input mode' result = cv.cvCreateImage( cv.cvSize(input.size[0], input.size[1]), # size mode_list[input.mode][0], # depth mode_list[input.mode][1] # channels ) # set imageData result.imageData = input.tostring() return result
def transformImage(self,im): ''' Transform an image. ''' matrix = pv.NumpyToOpenCV(self.matrix) src = im.asOpenCV() dst = cv.cvCreateImage( cv.cvSize(self.size[0],self.size[1]), cv.IPL_DEPTH_8U, src.nChannels ); cv.cvWarpPerspective( src, dst, matrix) return pv.Image(dst)
def __init__(self, frame): self.blob_image = opencv.cvCloneImage(frame.iplimage) self.blob_gray = opencv.cvCreateImage(opencv.cvGetSize(self.blob_image), 8, 1) self.blob_mask = opencv.cvCreateImage(opencv.cvGetSize(self.blob_image), 8, 1) opencv.cvSet(self.blob_mask, 1) opencv.cvCvtColor(self.blob_image, self.blob_gray, opencv.CV_BGR2GRAY) #opencv.cvEqualizeHist(self.blob_gray, self.blob_gray) #opencv.cvThreshold(self.blob_gray, self.blob_gray, frame.thresh, 255, opencv.CV_THRESH_BINARY) #opencv.cvThreshold(self.blob_gray, self.blob_gray, frame.thresh, 255, opencv.CV_THRESH_TOZERO) opencv.cvThreshold(self.blob_gray, self.blob_gray, frame.bthresh, 255, frame.bthreshmode) #opencv.cvAdaptiveThreshold(self.blob_gray, self.blob_gray, 255, opencv.CV_ADAPTIVE_THRESH_MEAN_C, opencv.CV_THRESH_BINARY_INV) self._frame_blobs = CBlobResult(self.blob_gray, self.blob_mask, 100, True) self._frame_blobs.filter_blobs(10, 10000) self.count = self._frame_blobs.GetNumBlobs() self.items = [] for i in range(self.count): self.items.append(self._frame_blobs.GetBlob(i))
def __init__(self, frame): self.blob_image = opencv.cvCloneImage(frame.iplimage) self.blob_gray = opencv.cvCreateImage(opencv.cvGetSize(self.blob_image), 8, 1) self.blob_mask = opencv.cvCreateImage(opencv.cvGetSize(self.blob_image), 8, 1) opencv.cvSet(self.blob_mask, 1) opencv.cvCvtColor(self.blob_image, self.blob_gray, opencv.CV_BGR2GRAY) # opencv.cvEqualizeHist(self.blob_gray, self.blob_gray) # opencv.cvThreshold(self.blob_gray, self.blob_gray, frame.thresh, 255, opencv.CV_THRESH_BINARY) # opencv.cvThreshold(self.blob_gray, self.blob_gray, frame.thresh, 255, opencv.CV_THRESH_TOZERO) opencv.cvThreshold(self.blob_gray, self.blob_gray, frame.bthresh, 255, frame.bthreshmode) # opencv.cvAdaptiveThreshold(self.blob_gray, self.blob_gray, 255, opencv.CV_ADAPTIVE_THRESH_MEAN_C, opencv.CV_THRESH_BINARY_INV) self._frame_blobs = CBlobResult(self.blob_gray, self.blob_mask, 100, True) self._frame_blobs.filter_blobs(10, 10000) self.count = self._frame_blobs.GetNumBlobs() self.items = [] for i in range(self.count): self.items.append(self._frame_blobs.GetBlob(i))
def IplGrayToRGB(i_image): """Convert RGB Ipl image to gray scale, returns copy of image if the format is already right.""" o_image = cv.cvCreateImage(cv.cvSize(i_image.width, i_image.height), 8, 3) if (i_image.depth == 8) and (i_image.nChannels == 3): cv.cvCopy(i_image, o_image) else: #cv.cvCvtColor(i_image, o_image , cv.CV_BGR2GRAY ) cv.highgui.cvConvertImage(i_image, o_image) return o_image
def IplGrayToRGB( i_image ): """Convert RGB Ipl image to gray scale, returns copy of image if the format is already right.""" o_image = cv.cvCreateImage( cv.cvSize( i_image.width, i_image.height ), 8 ,3) if (i_image.depth == 8) and (i_image.nChannels == 3) : cv.cvCopy( i_image, o_image ) else: #cv.cvCvtColor(i_image, o_image , cv.CV_BGR2GRAY ) cv.highgui.cvConvertImage( i_image, o_image ) return o_image
def ipl2cairo(iplimage): srcimage = opencv.cvCloneImage(iplimage) width = srcimage.width height = srcimage.height image = opencv.cvCreateImage(opencv.cvGetSize(srcimage), 8, 4) opencv.cvCvtColor(srcimage, image, opencv.CV_BGR2BGRA) buffer = numpy.fromstring(image.imageData, dtype=numpy.uint32).astype(numpy.uint32) buffer.shape = (image.width, image.height) opencv.cvReleaseImage(srcimage) opencv.cvReleaseImage(image) return cairo.ImageSurface.create_for_data(buffer, cairo.FORMAT_RGB24, width, height, width * 4)
def matchTemplate(image, template): """Utility function to match the template against the image and return a heightmap. The match is made using V_TM_SQDIFF_NORMED, so smaller values in the heightmap indicate closer matches (look for local minima).""" match_hmap = cvCreateImage( cvSize(image.width-template.width+1, image.height-template.height+1), IPL_DEPTH_32F, 1 ) cvMatchTemplate(image, template, match_hmap, CV_TM_SQDIFF_NORMED) return match_hmap
def ipl2cairo(iplimage): srcimage = opencv.cvCloneImage(iplimage) width = srcimage.width height = srcimage.height image = opencv.cvCreateImage(opencv.cvGetSize(srcimage),8, 4) opencv.cvCvtColor(srcimage,image,opencv.CV_BGR2BGRA) buffer = numpy.fromstring(image.imageData, dtype=numpy.uint32).astype(numpy.uint32) buffer.shape = (image.width, image.height) opencv.cvReleaseImage(srcimage) opencv.cvReleaseImage(image) return cairo.ImageSurface.create_for_data(buffer, cairo.FORMAT_RGB24, width, height, width*4)
def main(): pub = rospy.TopicPub('image', Image) rospy.ready('test_send') image = cv.cvCreateImage(cv.cvSize(640,480), 8, 1) cv.cvSet(image, 133) while not rospy.is_shutdown(): pub.publish(Image(None, 'test', image.width, image.height, 'none', 'mono8', image.imageData)) time.sleep(.033) rospy.spin()
def run(): filename = 'C:\\Documents and Settings\\rmccormack\\Desktop\\work_projects\\openCV\\test\\test1.jpg' im = hg.cvLoadImage(filename) if not im: print "Error opening %s" % filename sys.exit(-1) im2 = opencv.cvCreateImage(opencv.cvGetSize(im), 8, 4) opencv.cvCvtColor(im, im2, opencv.CV_BGR2BGRA) buffer = numpy.fromstring(im2.imageData, dtype=numpy.uint32).astype(numpy.float32) buffer.shape = (im2.width, im2.height) return buffer
def __init__(self, src="", time=None): self.src = src self.time = time if self.time: hg.cvSetCaptureProperty(self.src, hg.CV_CAP_PROP_POS_FRAMES, self.time) self.iplimage = hg.cvQueryFrame(self.src) self.width = self.iplimage.width self.height = self.iplimage.height self.image = opencv.cvCreateImage(opencv.cvGetSize(self.iplimage),8, 4) opencv.cvCvtColor(self.iplimage,self.image,opencv.CV_BGR2BGRA) self.buffer = numpy.fromstring(self.image.imageData, dtype=numpy.uint32).astype(numpy.uint32) self.buffer.shape = (self.image.width, self.image.height) self.time = hg.cvGetCaptureProperty(self.src, hg.CV_CAP_PROP_POS_MSEC)
def NumPy2Ipl(input): """Converts a numpy array to the OpenCV/IPL CvMat data format. Supported input array layouts: 2 dimensions of numpy.uint8 3 dimensions of numpy.uint8 2 dimensions of numpy.float32 2 dimensions of numpy.float64 """ if not isinstance(input, numpy.ndarray): raise TypeError, 'Must be called with numpy.ndarray!' # Check the number of dimensions of the input array ndim = input.ndim if not ndim in (2, 3): raise ValueError, 'Only 2D-arrays and 3D-arrays are supported!' # Get the number of channels if ndim == 2: channels = 1 else: channels = input.shape[2] # Get the image depth if input.dtype == numpy.uint8: depth = cv.IPL_DEPTH_8U elif input.dtype == numpy.float32: depth = cv.IPL_DEPTH_32F elif input.dtype == numpy.float64: depth = cv.IPL_DEPTH_64F # supported modes list: [(channels, dtype), ...] modes_list = [(1, numpy.uint8), (3, numpy.uint8), (1, numpy.float32), (1, numpy.float64)] # Check if the input array layout is supported if not (channels, input.dtype) in modes_list: raise ValueError, 'Unknown or unsupported input mode' result = cv.cvCreateImage( cv.cvSize(input.shape[1], input.shape[0]), # size depth, # depth channels # channels ) # set imageData result.imageData = input.tostring() return result
def main(): pub = rospy.TopicPub('image', Image) rospy.ready('test_send') image = cv.cvCreateImage(cv.cvSize(640, 480), 8, 1) cv.cvSet(image, 133) while not rospy.is_shutdown(): pub.publish( Image(None, 'test', image.width, image.height, 'none', 'mono8', image.imageData)) time.sleep(.033) rospy.spin()
def __init__(self, src="", time=None): self.src = src self.time = time if self.time: hg.cvSetCaptureProperty(self.src, hg.CV_CAP_PROP_POS_FRAMES, self.time) self.iplimage = hg.cvQueryFrame(self.src) self.width = self.iplimage.width self.height = self.iplimage.height self.image = opencv.cvCreateImage(opencv.cvGetSize(self.iplimage), 8, 4) opencv.cvCvtColor(self.iplimage, self.image, opencv.CV_BGR2BGRA) self.buffer = numpy.fromstring(self.image.imageData, dtype=numpy.uint32).astype(numpy.uint32) self.buffer.shape = (self.image.width, self.image.height) self.time = hg.cvGetCaptureProperty(self.src, hg.CV_CAP_PROP_POS_MSEC)
def detectObject(self, classifier): self.grayscale = opencv.cvCreateImage(opencv.cvGetSize(self.iplimage), 8, 1) opencv.cvCvtColor(self.iplimage, self.grayscale, opencv.CV_BGR2GRAY) self.storage = opencv.cvCreateMemStorage(0) opencv.cvClearMemStorage(self.storage) opencv.cvEqualizeHist(self.grayscale, self.grayscale) try: self.cascade = opencv.cvLoadHaarClassifierCascade(os.path.join(os.path.dirname(__file__), classifier+".xml"),opencv.cvSize(1,1)) except: raise AttributeError, "could not load classifier file" self.objects = opencv.cvHaarDetectObjects(self.grayscale, self.cascade, self.storage, 1.2, 2, opencv.CV_HAAR_DO_CANNY_PRUNING, opencv.cvSize(50,50)) return self.objects
def detectObject(self, classifier): self.grayscale = opencv.cvCreateImage(opencv.cvGetSize(self.iplimage), 8, 1) opencv.cvCvtColor(self.iplimage, self.grayscale, opencv.CV_BGR2GRAY) self.storage = opencv.cvCreateMemStorage(0) opencv.cvClearMemStorage(self.storage) opencv.cvEqualizeHist(self.grayscale, self.grayscale) try: self.cascade = opencv.cvLoadHaarClassifierCascade(os.path.join(os.path.dirname(__file__), classifier+".xml"),opencv.cvSize(1, 1)) except: raise AttributeError("could not load classifier file") self.objects = opencv.cvHaarDetectObjects(self.grayscale, self.cascade, self.storage, 1.2, 2, opencv.CV_HAAR_DO_CANNY_PRUNING, opencv.cvSize(50, 50)) return self.objects
def tile_images(img_width, img_height, num_width, num_height, images, channels=3): w = img_width * num_width h = img_height * num_height image = cv.cvCreateImage(cv.cvSize(int(w), int(h)), 8, channels) cv.cvSet(image, cv.cvScalar(255,255,255)) while len(images) > 0: try: for y in range(int(num_height)): for x in range(int(num_width)): small_tile = images.pop() img_x = x * img_width img_y = y * img_height cropped = cv.cvGetSubRect(image, cv.cvRect(img_x, img_y, img_width,img_height)) cv.cvCopy(small_tile, cropped) except exceptions.IndexError, e: break
def Ipl2NumPy(input): """Converts an OpenCV/IPL image to a numpy array. Supported input image formats are IPL_DEPTH_8U x 1 channel IPL_DEPTH_8U x 3 channels IPL_DEPTH_32F x 1 channel IPL_DEPTH_32F x 2 channels IPL_DEPTH_32S x 1 channel IPL_DEPTH_64F x 1 channel IPL_DEPTH_64F x 2 channels """ if not isinstance(input, cv.CvMat): raise TypeError, 'must be called with a cv.CvMat!' # data type dictionary: # (channels, depth) : numpy dtype ipl2dtype = { (1, cv.IPL_DEPTH_8U): numpy.uint8, (3, cv.IPL_DEPTH_8U): numpy.uint8, (1, cv.IPL_DEPTH_32F): numpy.float32, (2, cv.IPL_DEPTH_32F): numpy.float32, (1, cv.IPL_DEPTH_32S): numpy.int32, (1, cv.IPL_DEPTH_64F): numpy.float64, (2, cv.IPL_DEPTH_64F): numpy.float64 } key = (input.nChannels, input.depth) if not ipl2dtype.has_key(key): raise ValueError, 'unknown or unsupported input mode' # Get the numpy array and reshape it correctly # ATTENTION: flipped dimensions width/height on 2007-11-15 if input.nChannels == 1: array_1d = numpy.fromstring(input.imageData, dtype=ipl2dtype[key]) return numpy.reshape(array_1d, (input.height, input.width)) elif input.nChannels == 2: array_1d = numpy.fromstring(input.imageData, dtype=ipl2dtype[key]) return numpy.reshape(array_1d, (input.height, input.width, 2)) elif input.nChannels == 3: # Change the order of channels from BGR to RGB rgb = cv.cvCreateImage(cv.cvSize(input.width, input.height), input.depth, 3) cv.cvCvtColor(input, rgb, cv.CV_BGR2RGB) array_1d = numpy.fromstring(rgb.imageData, dtype=ipl2dtype[key]) return numpy.reshape(array_1d, (input.height, input.width, 3))
def Ipl2NumPy(input): """Converts an OpenCV/IPL image to a numpy array. Supported input image formats are IPL_DEPTH_8U x 1 channel IPL_DEPTH_8U x 3 channels IPL_DEPTH_32F x 1 channel IPL_DEPTH_32F x 2 channels IPL_DEPTH_32S x 1 channel IPL_DEPTH_64F x 1 channel IPL_DEPTH_64F x 2 channels """ if not isinstance(input, cv.CvMat): raise TypeError, 'must be called with a cv.CvMat!' # data type dictionary: # (channels, depth) : numpy dtype ipl2dtype = { (1, cv.IPL_DEPTH_8U) : numpy.uint8, (3, cv.IPL_DEPTH_8U) : numpy.uint8, (1, cv.IPL_DEPTH_32F) : numpy.float32, (2, cv.IPL_DEPTH_32F) : numpy.float32, (1, cv.IPL_DEPTH_32S) : numpy.int32, (1, cv.IPL_DEPTH_64F) : numpy.float64, (2, cv.IPL_DEPTH_64F) : numpy.float64 } key = (input.nChannels, input.depth) if not ipl2dtype.has_key(key): raise ValueError, 'unknown or unsupported input mode' # Get the numpy array and reshape it correctly # ATTENTION: flipped dimensions width/height on 2007-11-15 if input.nChannels == 1: array_1d = numpy.fromstring(input.imageData, dtype=ipl2dtype[key]) return numpy.reshape(array_1d, (input.height, input.width)) elif input.nChannels == 2: array_1d = numpy.fromstring(input.imageData, dtype=ipl2dtype[key]) return numpy.reshape(array_1d, (input.height, input.width, 2)) elif input.nChannels == 3: # Change the order of channels from BGR to RGB rgb = cv.cvCreateImage(cv.cvSize(input.width, input.height), input.depth, 3) cv.cvCvtColor(input, rgb, cv.CV_BGR2RGB) array_1d = numpy.fromstring(rgb.imageData, dtype=ipl2dtype[key]) return numpy.reshape(array_1d, (input.height, input.width, 3))
def get_image(): im = highgui.cvQueryFrame(camera) # Add the line below if you need it (Ubuntu 8.04+) im = opencv.cvGetMat(im) # Resize image if needed resized_im = opencv.cvCreateImage(opencv.cvSize(XSCALE, YSCALE), 8, 3) opencv.cvResize(im, resized_im, opencv.CV_INTER_CUBIC) #convert Ipl image to PIL image pil_img = opencv.adaptors.Ipl2PIL(resized_im) enhancer = ImageEnhance.Brightness(pil_img) pil_img = enhancer.enhance(BRIGHTNESS) pil_img = ImageOps.mirror(pil_img) return pil_img
def detectHaar(iplimage, classifier): srcimage = opencv.cvCloneImage(iplimage) grayscale = opencv.cvCreateImage(opencv.cvGetSize(srcimage), 8, 1) opencv.cvCvtColor(srcimage, grayscale, opencv.CV_BGR2GRAY) storage = opencv.cvCreateMemStorage(0) opencv.cvClearMemStorage(storage) opencv.cvEqualizeHist(grayscale, grayscale) try: cascade = opencv.cvLoadHaarClassifierCascade(os.path.join(os.path.dirname(__file__), classifier + ".xml"), opencv.cvSize(1, 1)) except: raise AttributeError("could not load classifier file") objs = opencv.cvHaarDetectObjects(grayscale, cascade, storage, 1.2, 2, opencv.CV_HAAR_DO_CANNY_PRUNING, opencv.cvSize(50, 50)) objects = [] for obj in objs: objects.append(Haarobj(obj)) opencv.cvReleaseImage(srcimage) opencv.cvReleaseImage(grayscale) opencv.cvReleaseMemStorage(storage) return objects
def detectHaar(iplimage, classifier): srcimage = opencv.cvCloneImage(iplimage) grayscale = opencv.cvCreateImage(opencv.cvGetSize(srcimage), 8, 1) opencv.cvCvtColor(srcimage, grayscale, opencv.CV_BGR2GRAY) storage = opencv.cvCreateMemStorage(0) opencv.cvClearMemStorage(storage) opencv.cvEqualizeHist(grayscale, grayscale) try: cascade = opencv.cvLoadHaarClassifierCascade(os.path.join(os.path.dirname(__file__), classifier+".xml"),opencv.cvSize(1,1)) except: raise AttributeError, "could not load classifier file" objs = opencv.cvHaarDetectObjects(grayscale, cascade, storage, 1.2, 2, opencv.CV_HAAR_DO_CANNY_PRUNING, opencv.cvSize(50,50)) objects = [] for obj in objs: objects.append(Haarobj(obj)) opencv.cvReleaseImage(srcimage) opencv.cvReleaseImage(grayscale) opencv.cvReleaseMemStorage(storage) return objects
def get_image(): im = highgui.cvQueryFrame(camera) # Add the line below if you need it (Ubuntu 8.04+) im = opencv.cvGetMat(im) # Resize image if needed resized_im=opencv.cvCreateImage(opencv.cvSize(XSCALE,YSCALE),8,3) opencv.cvResize( im, resized_im, opencv.CV_INTER_CUBIC); #convert Ipl image to PIL image pil_img = opencv.adaptors.Ipl2PIL(resized_im) enhancer=ImageEnhance.Brightness(pil_img) pil_img=enhancer.enhance(BRIGHTNESS) pil_img=ImageOps.mirror(pil_img) return pil_img
def main(argv=None): # declair globals global ser, output, curX, curY, last_dir # Open serial connection ser = serial.Serial(DEFAULT_SERIAL_PORT, 9600, timeout=1) last_dir = [None, None] # Print starting time stamp print time.strftime ('Start Time: %H:%M:%S') # Create the output image curX = 0 curY = 0 output = opencv.cvCreateImage(opencv.cvSize(DEFAULT_OUTPUT_WIDTH,DEFAULT_OUTPUT_HEIGHT), opencv.IPL_DEPTH_8U, 3) opencv.cvZero(output) opencv.cvNot(output,output) global window, screen # Initialize pygame if not already initialized if not pygame.display.get_init(): pygame.init() window = pygame.display.set_mode((DEFAULT_OUTPUT_WIDTH,DEFAULT_OUTPUT_HEIGHT)) pygame.display.set_caption("Etch-A-Sketch") screen = pygame.display.get_surface() # Draw the image calibrationRoutine(60) # Show the image displayImage(output) # Print end time stamp print time.strftime ('End Time: %H:%M:%S') # loop while(1): events = pygame.event.get() for event in events: if event.type == QUIT or (event.type == KEYDOWN): exit()
def tile_images(img_width, img_height, num_width, num_height, images, channels=3): w = img_width * num_width h = img_height * num_height image = cv.cvCreateImage(cv.cvSize(int(w), int(h)), 8, channels) cv.cvSet(image, cv.cvScalar(255, 255, 255)) while len(images) > 0: try: for y in range(int(num_height)): for x in range(int(num_width)): small_tile = images.pop() img_x = x * img_width img_y = y * img_height cropped = cv.cvGetSubRect( image, cv.cvRect(img_x, img_y, img_width, img_height)) cv.cvCopy(small_tile, cropped) except exceptions.IndexError, e: break
def detect(self, pil_image, cascade_name, recogn_w = 50, recogn_h = 50): # Get cascade: cascade = self.get_cascade(cascade_name) image = opencv.PIL2Ipl(pil_image) image_size = opencv.cvGetSize(image) grayscale = image if pil_image.mode == "RGB": # create grayscale version grayscale = opencv.cvCreateImage(image_size, 8, 1) # Change to RGB2Gray - I dont think itll affect the conversion opencv.cvCvtColor(image, grayscale, opencv.CV_BGR2GRAY) # create storage storage = opencv.cvCreateMemStorage(0) opencv.cvClearMemStorage(storage) # equalize histogram opencv.cvEqualizeHist(grayscale, grayscale) # detect objects return opencv.cvHaarDetectObjects(grayscale, cascade, storage, 1.2, 2, opencv.CV_HAAR_DO_CANNY_PRUNING, opencv.cvSize(recogn_w, recogn_h))
def logPolar(im,center=None,radius=None,M=None,size=(64,128)): ''' Produce a log polar transform of the image. See OpenCV for details. The scale space is calculated based on radius or M. If both are given M takes priority. ''' #M=1.0 w,h = im.size if radius == None: radius = 0.5*min(w,h) if M == None: #rho=M*log(sqrt(x2+y2)) #size[0] = M*log(r) M = size[0]/np.log(radius) if center == None: center = pv.Point(0.5*w,0.5*h) src = im.asOpenCV() dst = cv.cvCreateImage( cv.cvSize(size[0],size[1]), 8, 3 ); cv.cvLogPolar( src, dst, center.asOpenCV(), M, cv.CV_INTER_LINEAR+cv.CV_WARP_FILL_OUTLIERS ) return pv.Image(dst)
def detect(self, pil_image, cascade_name, recogn_w=50, recogn_h=50): # Get cascade: cascade = self.get_cascade(cascade_name) image = opencv.PIL2Ipl(pil_image) image_size = opencv.cvGetSize(image) grayscale = image if pil_image.mode == "RGB": # create grayscale version grayscale = opencv.cvCreateImage(image_size, 8, 1) # Change to RGB2Gray - I dont think itll affect the conversion opencv.cvCvtColor(image, grayscale, opencv.CV_BGR2GRAY) # create storage storage = opencv.cvCreateMemStorage(0) opencv.cvClearMemStorage(storage) # equalize histogram opencv.cvEqualizeHist(grayscale, grayscale) # detect objects return opencv.cvHaarDetectObjects(grayscale, cascade, storage, 1.2, 2, opencv.CV_HAAR_DO_CANNY_PRUNING, opencv.cvSize(recogn_w, recogn_h))
def detect_and_draw(image): image_size = hg.cvGetSize(image) grayscale = cv.cvCreateImage(image_size, 8, 1) cv.cvCvtColor(image, grayscale, CV_RGB2GRAY) return grayscale
def run(exposure, video=None, display=False, debug=False): if display: hg.cvNamedWindow("video", 1) hg.cvMoveWindow("video", 0, 0) if debug: hg.cvNamedWindow('right', 1) hg.cvMoveWindow("right", 800, 0) hg.cvNamedWindow("thresholded", 1) hg.cvNamedWindow('motion', 1) hg.cvNamedWindow('intensity', 1) hg.cvMoveWindow("thresholded", 800, 0) hg.cvMoveWindow("intensity", 0, 600) hg.cvMoveWindow("motion", 800, 600) if video is None: #video = cam.VidereStereo(0, gain=96, exposure=exposure) video = cam.StereoFile('measuring_tape_red_left.avi','measuring_tape_red_right.avi') frames = video.next() detector = LaserPointerDetector(frames[0], LaserPointerDetector.SUN_EXPOSURE, use_color=False, use_learning=True) detector_right = LaserPointerDetector(frames[1], LaserPointerDetector.SUN_EXPOSURE, use_color=False, use_learning=True, classifier=detector.classifier) stereo_cam = cam.KNOWN_CAMERAS['videre_stereo2'] for i in xrange(10): frames = video.next() detector.detect(frames[0]) detector_right.detect(frames[1]) lt = cv.cvCreateImage(cv.cvSize(640,480), 8, 3) rt = cv.cvCreateImage(cv.cvSize(640,480), 8, 3) for l, r in video: start_time = time.time() #l = stereo_cam.camera_left.undistort_img(l) #r = stereo_cam.camera_right.undistort_img(r) cv.cvCopy(l, lt) cv.cvCopy(r, rt) l = lt r = rt undistort_time = time.time() _, _, right_cam_detection, stats = detector_right.detect(r) if debug: draw_blobs(r, stats) draw_detection(r, right_cam_detection) hg.cvShowImage('right', r) image, combined, left_cam_detection, stats = detector.detect(l) detect_time = time.time() if debug: motion, intensity = detector.get_motion_intensity_images() show_processed(l, [combined, motion, intensity], left_cam_detection, stats, detector) elif display: #draw_blobs(l, stats) draw_detection(l, left_cam_detection) hg.cvShowImage('video', l) hg.cvWaitKey(10) if right_cam_detection != None and left_cam_detection != None: x = np.matrix(left_cam_detection['centroid']).T xp = np.matrix(right_cam_detection['centroid']).T result = stereo_cam.triangulate_3d(x, xp) print '3D point located at', result['point'].T, print 'distance %.2f error %.3f' % (np.linalg.norm(result['point']), result['error']) triangulation_time = time.time() diff = time.time() - start_time print 'Main: Running at %.2f fps, took %.4f s' % (1.0 / diff, diff)
def CropImage(i_ipl_image, i_ipl_roi): src_region = cv.cvGetSubRect(i_ipl_image, i_ipl_roi) cropped_image = cv.cvCreateImage( cv.cvSize(i_ipl_roi.width, i_ipl_roi.height), 8, 1) cv.cvCopy(src_region, cropped_image) return cropped_image
def prepare(self, features_k_nearest_neighbors, nonzero_indices = None, all_save_load = False, regenerate_neightborhood_indices = False): #print np.shape(self.processor.pts3d_bound), 'shape pts3d_bound' imgTmp = cv.cvCloneImage(self.processor.img) self.imNP = ut.cv2np(imgTmp,format='BGR') ###self.processor.map2d = np.asarray(self.processor.camPts_bound) #copied from laser to image mapping if features_k_nearest_neighbors == None or features_k_nearest_neighbors == False: #use range self.kdtree2d = kdtree.KDTree(self.processor.pts3d_bound.T) #print len(nonzero_indices) #print np.shape(np.asarray((self.processor.pts3d_bound.T)[nonzero_indices])) if nonzero_indices != None: print ut.getTime(), 'query ball tree for ', len(nonzero_indices), 'points' kdtree_query = kdtree.KDTree((self.processor.pts3d_bound.T)[nonzero_indices]) else: print ut.getTime(), 'query ball tree' kdtree_query = kdtree.KDTree(self.processor.pts3d_bound.T) filename = self.processor.config.path+'/data/'+self.processor.scan_dataset.id+'_sphere_neighborhood_indices_'+str(self.processor.feature_radius)+'.pkl' if all_save_load == True and os.path.exists(filename) and regenerate_neightborhood_indices == False: #if its already there, load it: print ut.getTime(), 'loading',filename self.kdtree_queried_indices = ut.load_pickle(filename) else: self.kdtree_queried_indices = kdtree_query.query_ball_tree(self.kdtree2d, self.processor.feature_radius, 2.0, 0.2) #approximate print ut.getTime(), 'queried kdtree: ',len(self.kdtree_queried_indices),'points, radius:',self.processor.feature_radius if all_save_load == True: ut.save_pickle(self.kdtree_queried_indices, filename) #make dict out of list for faster operations? (doesn't seem to change speed significantly): #self.kdtree_queried_indices = dict(zip(xrange(len(self.kdtree_queried_indices)), self.kdtree_queried_indices)) else: #experiemental: use_20_nearest_neighbors == True #TODO: exclude invalid values in get_featurevector (uncomment code there) self.kdtree2d = kdtree.KDTree(self.processor.pts3d_bound.T) self.kdtree_queried_indices = [] print ut.getTime(), 'kdtree single queries for kNN start, k=', features_k_nearest_neighbors count = 0 for point in ((self.processor.pts3d_bound.T)[nonzero_indices]): count = count + 1 result = self.kdtree2d.query(point, features_k_nearest_neighbors,0.2,2,self.processor.feature_radius) #existing = result[0][0] != np.Inf #print existing #print result[1] self.kdtree_queried_indices += [result[1]] #[existing] if count % 4096 == 0: print ut.getTime(),count print ut.getTime(), 'kdtree singe queries end' #convert to numpy array -> faster access self.kdtree_queried_indices = np.asarray(self.kdtree_queried_indices) #print self.kdtree_queried_indices #takes long to compute: #avg_len = 0 #minlen = 999999 #maxlen = 0 #for x in self.kdtree_queried_indices: # avg_len += len(x) # minlen = min(minlen, len(x)) # maxlen = max(maxlen, len(x)) #avg_len = avg_len / len(self.kdtree_queried_indices) #print ut.getTime(), "range neighbors: avg_len", avg_len, 'minlen', minlen, 'maxlen', maxlen #create HSV numpy images: # compute the hsv version of the image image_size = cv.cvGetSize(self.processor.img) img_h = cv.cvCreateImage (image_size, 8, 1) img_s = cv.cvCreateImage (image_size, 8, 1) img_v = cv.cvCreateImage (image_size, 8, 1) img_hsv = cv.cvCreateImage (image_size, 8, 3) cv.cvCvtColor (self.processor.img, img_hsv, cv.CV_BGR2HSV) cv.cvSplit (img_hsv, img_h, img_s, img_v, None) self.imNP_h = ut.cv2np(img_h) self.imNP_s = ut.cv2np(img_s) self.imNP_v = ut.cv2np(img_v) textures = texture_features.eigen_texture(self.processor.img) self.imNP_tex1 = textures[:,:,0] self.imNP_tex2 = textures[:,:,1] self.debug_before_first_featurevector = True self.generate_voi_histogram(self.processor.point_of_interest,self.processor.voi_width)
def undistort_img(self, image): if self.temp is None: self.temp = cv.cvCreateImage(cv.cvGetSize(image), 8, 3) #cv.cvUndistort2(image, self.temp, self.intrinsic_mat_cv, self.lens_distortion_radial_cv) cv.cvRemap(image, self.temp, self.mapx, self.mapy) return self.temp
def IplResize(i_image, i_width, i_height): """Convert RGB to Gray scale and resize to input width and height""" small_image = cv.cvCreateImage(cv.cvSize(i_width, i_height), i_image.depth, i_image.nChannels) cv.cvResize(i_image, small_image) return small_image
def scale_image(img, scale): new_img = cv.cvCreateImage( cv.cvSize(img.width * scale, img.height * scale), img.depth, img.nChannels) cv.cvResize(img, new_img, cv.CV_INTER_NN) return new_img
def prepare(self, features_k_nearest_neighbors, nonzero_indices=None, all_save_load=False, regenerate_neightborhood_indices=False): #print np.shape(self.processor.pts3d_bound), 'shape pts3d_bound' imgTmp = cv.cvCloneImage(self.processor.img) self.imNP = ut.cv2np(imgTmp, format='BGR') ###self.processor.map2d = np.asarray(self.processor.camPts_bound) #copied from laser to image mapping if features_k_nearest_neighbors == None or features_k_nearest_neighbors == False: #use range self.kdtree2d = kdtree.KDTree(self.processor.pts3d_bound.T) #print len(nonzero_indices) #print np.shape(np.asarray((self.processor.pts3d_bound.T)[nonzero_indices])) if nonzero_indices != None: print ut.getTime(), 'query ball tree for ', len( nonzero_indices), 'points' kdtree_query = kdtree.KDTree( (self.processor.pts3d_bound.T)[nonzero_indices]) else: print ut.getTime(), 'query ball tree' kdtree_query = kdtree.KDTree(self.processor.pts3d_bound.T) filename = self.processor.config.path + '/data/' + self.processor.scan_dataset.id + '_sphere_neighborhood_indices_' + str( self.processor.feature_radius) + '.pkl' if all_save_load == True and os.path.exists( filename) and regenerate_neightborhood_indices == False: #if its already there, load it: print ut.getTime(), 'loading', filename self.kdtree_queried_indices = ut.load_pickle(filename) else: self.kdtree_queried_indices = kdtree_query.query_ball_tree( self.kdtree2d, self.processor.feature_radius, 2.0, 0.2) #approximate print ut.getTime(), 'queried kdtree: ', len( self.kdtree_queried_indices ), 'points, radius:', self.processor.feature_radius if all_save_load == True: ut.save_pickle(self.kdtree_queried_indices, filename) #make dict out of list for faster operations? (doesn't seem to change speed significantly): #self.kdtree_queried_indices = dict(zip(xrange(len(self.kdtree_queried_indices)), self.kdtree_queried_indices)) else: #experiemental: use_20_nearest_neighbors == True #TODO: exclude invalid values in get_featurevector (uncomment code there) self.kdtree2d = kdtree.KDTree(self.processor.pts3d_bound.T) self.kdtree_queried_indices = [] print ut.getTime( ), 'kdtree single queries for kNN start, k=', features_k_nearest_neighbors count = 0 for point in ((self.processor.pts3d_bound.T)[nonzero_indices]): count = count + 1 result = self.kdtree2d.query(point, features_k_nearest_neighbors, 0.2, 2, self.processor.feature_radius) #existing = result[0][0] != np.Inf #print existing #print result[1] self.kdtree_queried_indices += [result[1]] #[existing] if count % 4096 == 0: print ut.getTime(), count print ut.getTime(), 'kdtree singe queries end' #convert to numpy array -> faster access self.kdtree_queried_indices = np.asarray( self.kdtree_queried_indices) #print self.kdtree_queried_indices #takes long to compute: #avg_len = 0 #minlen = 999999 #maxlen = 0 #for x in self.kdtree_queried_indices: # avg_len += len(x) # minlen = min(minlen, len(x)) # maxlen = max(maxlen, len(x)) #avg_len = avg_len / len(self.kdtree_queried_indices) #print ut.getTime(), "range neighbors: avg_len", avg_len, 'minlen', minlen, 'maxlen', maxlen #create HSV numpy images: # compute the hsv version of the image image_size = cv.cvGetSize(self.processor.img) img_h = cv.cvCreateImage(image_size, 8, 1) img_s = cv.cvCreateImage(image_size, 8, 1) img_v = cv.cvCreateImage(image_size, 8, 1) img_hsv = cv.cvCreateImage(image_size, 8, 3) cv.cvCvtColor(self.processor.img, img_hsv, cv.CV_BGR2HSV) cv.cvSplit(img_hsv, img_h, img_s, img_v, None) self.imNP_h = ut.cv2np(img_h) self.imNP_s = ut.cv2np(img_s) self.imNP_v = ut.cv2np(img_v) textures = texture_features.eigen_texture(self.processor.img) self.imNP_tex1 = textures[:, :, 0] self.imNP_tex2 = textures[:, :, 1] self.debug_before_first_featurevector = True self.generate_voi_histogram(self.processor.point_of_interest, self.processor.voi_width)
def split(image): img1 = cv.cvCreateImage(cv.cvSize(image.width, image.height), 8, 1) img2 = cv.cvCreateImage(cv.cvSize(image.width, image.height), 8, 1) img3 = cv.cvCreateImage(cv.cvSize(image.width, image.height), 8, 1) cv.cvSplit(image, img1, img2, img3, None) return (img1, img2, img3)
def compute(self, i_data, i_ipl=False): frame = 0 self.__frame = -1 max_dist = 0 if i_ipl: nframes = len(i_data) else: nframes = i_data.shape[2] list_of_roi = [] list_of_frames = [] list_of_sizes = [] for frame in range(0, nframes): if i_ipl: ipl_image = i_data[frame] else: ipl_image = cv.NumPy2Ipl(i_data[:,:,frame]) if self.__scale < 1.0: w = numpy.int(numpy.round( float( ipl_image.width ) * self.__scale )) h = numpy.int(numpy.round( float( ipl_image.height ) * self.__scale )) small_image = cv.cvCreateImage( cv.cvSize( w, h ) , ipl_image.depth, ipl_image.nChannels ) cv.cvResize( ipl_image , small_image ) vj_box = viola_jones_opencv(small_image) else: vj_box = viola_jones_opencv(ipl_image) if vj_box is not None: (min_row, min_col, max_row, max_col) = vj_box w = max_col - min_col h = max_row - min_row dist = w*w + h*h list_of_roi.append(vj_box) list_of_frames.append(frame) list_of_sizes.append(dist) self.__n_rows = ipl_image.height self.__n_cols = ipl_image.width #Choose a percentile of the sorted list nboxes = len(list_of_sizes) if nboxes == 0: #print "Viola-Jones failed on all images" return list_of_sizes = numpy.array(list_of_sizes) idx = numpy.argsort(list_of_sizes) percentile = 0.8 arg_idx = numpy.int(numpy.round(percentile * nboxes)) if arg_idx >= nboxes: arg_idx = nboxes - 1 if arg_idx < 0: arg_idx = 0 #print "n boxes: ", nboxes, " chosen arg: ", arg_idx self.__frame = frame = list_of_frames[idx[arg_idx]] best_roi = list_of_roi[idx[arg_idx]] (min_row, min_col, max_row, max_col) = best_roi if self.__scale < 1.0: #print "best roi width = ", max_col - min_col, " best roi height = ", max_row-min_row max_col = numpy.int(numpy.round( float(max_col) / self.__scale )) max_row = numpy.int(numpy.round( float(max_row) / self.__scale )) min_col = numpy.int(numpy.round( float(min_col) / self.__scale )) min_row = numpy.int(numpy.round( float(min_row) / self.__scale )) vj_box = (min_row, min_col, max_row, max_col) self.setRoi(vj_box) return self.getRoi()
def scale(image, s): scaled = cv.cvCreateImage( cv.cvSize(int(image.width * s), int(image.height * s)), image.depth, image.nChannels) cv.cvResize(image, scaled, cv.CV_INTER_AREA) return scaled