def setUp(self): frame = cvLoadImage(image_fname) frame_size = cvGetSize(frame) r = cvCreateImage(frame_size, 8, 1) g = cvCreateImage(frame_size, 8, 1) b = cvCreateImage(frame_size, 8, 1) cvSplit(frame, r, g, b, None) self.rgb = (r, g, b) assert frame is not None hist_size = [64, 64, 64] ranges = [[0, 255], [0, 255], [0, 255]] self.hist = cvCreateHist(hist_size, CV_HIST_ARRAY, ranges, 1)
def setUp(self): frame = cvLoadImage(image_fname) frame_size = cvGetSize(frame) r = cvCreateImage(frame_size, 8, 1) g = cvCreateImage(frame_size, 8, 1) b = cvCreateImage(frame_size, 8, 1) cvSplit(frame, r, g, b, None) self.rgb = (r, g, b) assert (frame is not None) hist_size = [64, 64, 64] ranges = [[0, 255], [0, 255], [0, 255]] self.hist = cvCreateHist(hist_size, CV_HIST_ARRAY, ranges, 1)
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 __init__(self): self.active = False self.contours_image = cv.cvCreateImage((640, 480), 8, 3) self.dwidth = 240 self.dheight = 180 self.loops = 0 self.preview_image = cv.CreateImage((self.dwidth, self.dheight), cv.IPL_DEPTH_8U, 3) n = self.dwidth * self.dheight * 3 raw = (ctypes.c_ubyte * n)() for x in range(n): raw[x] = 64 ptr = ctypes.pointer(raw) pix = gtk.gdk_pixbuf_new_from_data( ptr, gtk.GDK_COLORSPACE_RGB, False, # ALPHA 8, # bits per sample self.dwidth, self.dheight, self.dwidth * 3, # row-stride ) self.preview_image_gtk = gtk.gtk_image_new_from_pixbuf(pix)
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 run(self): if self.capture: webcam_frame = cv.QueryFrame( self.capture ) else: print "Capture failed!" return if self.inverted_video.get_active(): cv.ConvertImage(webcam_frame, webcam_frame, cv._CVTIMG_FLIP) cv.ConvertImage(webcam_frame, self.display_frame, cv._CVTIMG_SWAP_RB) if False: # PROCESS WEBCAM FRAME HERE... inputImage = cv.cvCreateImage(cv.cvGetSize(webcam_frame), cv.IPL_DEPTH_8U, 1) cv.cvCvtColor(webcam_frame, inputImage, cv.CV_RGB2GRAY); cv.cvThreshold(inputImage, inputImage, 128, 255, cv.CV_THRESH_BINARY) mysize = cv.cvGetSize(webcam_frame) height = mysize.height width = mysize.width # Find horizontal first-moment: if False: mysum = 0 for i in range(height): mysum += sum(inputImage[i,:]) print "Sum:", mysum cv.cvMerge( inputImage, inputImage, inputImage, None, self.display_frame ) incoming_pixbuf = gtk.gdk.pixbuf_new_from_data( self.display_frame.imageData, gtk.gdk.COLORSPACE_RGB, False, 8, self.display_frame.width, self.display_frame.height, self.display_frame.widthStep) incoming_pixbuf.copy_area(0, 0, self.display_frame.width, self.display_frame.height, self.webcam_pixbuf, 0, 0) self.video_image.queue_draw() return self.video_enabled_button.get_active()
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 Ipl2NumPy(input): """Converts an OpenCV 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 """ import ipdb ipdb.set_trace() 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 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 """ import ipdb ipdb.set_trace() 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 test05_Ipl2NumPy(self): """Test the adaptors.Ipl2NumPy function.""" a = adaptors.Ipl2NumPy(self.ipl_image) a_1d = numpy.reshape(a, (a.size, )) # For 3-channel IPL images the order of channels will be BGR # but NumPy array order of channels will be RGB so a conversion # is needed before we can compare both images if self.ipl_image.nChannels == 3: rgb = cv.cvCreateImage(cv.cvSize(self.ipl_image.width, self.ipl_image.height), self.ipl_image.depth, 3) cv.cvCvtColor(self.ipl_image, rgb, cv.CV_BGR2RGB) self.assert_(a_1d.tostring() == rgb.imageData, 'The returned image has not been properly constructed.') else: self.assert_(a_1d.tostring() == self.ipl_image.imageData, 'The returned image has not been properly constructed.')
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_64F x 1 channel """ if not isinstance(input, cv.CvMat): raise TypeError, 'must be called with a cv.CvMat!' # assert that the channels are interleaved if input.dataOrder != 0: raise ValueError, 'dataOrder must be 0 (interleaved)!' # 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, (1, 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 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 == 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 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 initialize_video(self): webcam_frame = cv.QueryFrame( self.capture ) if not webcam_frame: print "Frame acquisition failed." return False self.webcam_pixbuf = gtk.gdk.pixbuf_new_from_data( webcam_frame.imageData, gtk.gdk.COLORSPACE_RGB, False, 8, webcam_frame.width, webcam_frame.height, webcam_frame.widthStep) self.video_image.set_from_pixbuf(self.webcam_pixbuf) self.display_frame = cv.cvCreateImage( cv.cvSize(webcam_frame.width, webcam_frame.height), cv.IPL_DEPTH_8U, 3) return True
def __init__(self): self.active = False self.contours_image = cv.cvCreateImage((640, 480), 8, 3) self.dwidth = 320 self.dheight = 240 self.preview_image = cv.CreateImage((self.dwidth, self.dheight), cv.IPL_DEPTH_8U, 3) n = self.dwidth * self.dheight * 3 raw = (ctypes.c_ubyte * n)() for x in range(n): raw[x] = 64 ptr = ctypes.pointer(raw) pix = gtk.gdk_pixbuf_new_from_data( ptr, gtk.GDK_COLORSPACE_RGB, False, # ALPHA 8, # bits per sample self.dwidth, self.dheight, self.dwidth * 3, # row-stride ) self.preview_image_gtk = gtk.gtk_image_new_from_pixbuf(pix)
def __init__(self): self.active = False self.contours_image = cv.cvCreateImage((640, 480), 8, 3) # self.storage_poly = cv.CreateMemStorage(0) gui.NamedWindow("contours", 1)
def __init__(self): self.active = False self.contours_image = cv.cvCreateImage((640, 480), 8, 3) #self.storage_poly = cv.CreateMemStorage(0) gui.NamedWindow('contours', 1)
def separation(self, debug=False): rel_slice_point = self.rel_original_point # NOQA rel_remained_point = self.rel_original_point slice_line = self.slice_line src = self.src original_size = self.original_size # vector<PixPoint>* pixels = &slice_line.pixels; pixels = slice_line.pixels # // x軸方向に分割した場合 if slice_line.is_horizontal: slice_size: CvSize = CvSize() remained_size: CvSize = CvSize() if slice_line.theta == 90: slice_size = CvSize(src.width, slice_line.position) remained_size = CvSize(src.width, src.height - slice_line.position) else: if pixels.at(pixels.size() - 1).y < slice_line.position: slice_size = CvSize( pixels.at(pixels.size() - 1).x + 1, slice_line.position + 1) else: slice_size = CvSize( pixels.at(pixels.size() - 1).x + 1, pixels.at(pixels.size() - 1).y + 1) if pixels.at(pixels.size() - 1).y < slice_line.position: remained_size = CvSize( src.width + 1, src.height - pixels.at(pixels.size() - 1).y + 1) else: remained_size = CvSize( src.width + 1, src.height - slice_line.position + 1) slice_src = cvCreateImage(slice_size, src.depth, src.nChannels) cvSet(slice_src, cvScalarAll(255), 0) remained_src = cvCreateImage(remained_size, src.depth, src.nChannels) cvSet(remained_src, cvScalarAll(255), 0) rel_remained_point.y += slice_line.position h: int = 0 w: int = 0 c: int = 0 if (src.width == pixels.size()) and (slice_line.theta == 90): for w in range(src.width): for h in range(src.height): if h < slice_line.position: for c in range(src.nChannels): slice_src.imageData[slice_src.widthStep * h + w * slice_src.nChannels + c] = src.imageData[ src.widthStep * h + w * src.nChannels + c] else: for c in range(src.nChannels): remained_src.imageData[ remained_src.widthStep * (h - slice_line.position) + w * remained_src.nChannels + c] = src.imageData[src.widthStep * h + w * src.nChannels + c] elif (src.width == pixels.size()) and (slice_line.theta > 90): for w in range(src.width): for h in range(src.height): if h < pixels.at(w).y: for c in range(src.nChannels): slice_src.imageData[slice_src.widthStep * h + w * slice_src.nChannels + c] = src.imageData[ src.widthStep * h + w * src.nChannels + c] else: for c in range(src.nChannels): remained_src.imageData[ remained_src.widthStep * (h - pixels.at(pixels.size() - 1).y) + w * slice_src.nChannels + c] = src.imageData[src.widthStep * h + w * src.nChannels + c] elif (src.width == pixels.size()) and (slice_line.theta <= 90): for w in range(src.width): for h in range(src.height): if h < pixels.at(w).y: for c in range(src.nChannels): slice_src.imageData[slice_src.widthStep * h + w * slice_src.nChannels + c] = src.imageData[ src.widthStep * h + w * src.nChannels + c] else: for c in range(src.nChannels): remained_src.imageData[ remained_src.widthStep * (h - slice_line.position) + w * slice_src.nChannels + c] = src.imageData[src.widthStep * h + w * src.nChannels + c] elif (src.width > pixels.size()) and (slice_line.theta > 90): for w in range(pixels.size()): for h in range(src.height): if h < pixels.at(w).y: for c in range(src.nChannels): slice_src.imageData[slice_src.widthStep * h + w * slice_src.nChannels + c] = src.imageData[ src.widthStep * h + w * src.nChannels + c] else: for c in range(src.nChannels): remained_src.imageData[ remained_src.widthStep * h + w * remained_src.nChannels + c] = src.imageData[src.widthStep * h + w * src.nChannels + c] for w in range(int(pixels.size()), src.width): for h in range(src.height): for c in range(src.nChannels): remained_src.imageData[remained_src.widthStep * h + w * remained_src.nChannels + c] = src.imageData[ src.widthStep * h + w * src.nChannels + c] else: for w in range(pixels.size()): for h in range(src.height): if h < pixels.at(w).y: for c in range(src.nChannels): slice_src.imageData[slice_src.widthStep * h + w * slice_src.nChannels + c] = src.imageData[ src.widthStep * h + w * src.nChannels + c] else: for c in range(src.nChannels): remained_src.imageData[ remained_src.widthStep * (h - slice_line.position) + w * remained_src.nChannels + c] = src.imageData[src.widthStep * h + w * src.nChannels + c] for w in range(int(pixels.size(), src.width)): for h in range(src.height): for c in range(src.nChannels): slice_src.imageData[slice_src.widthStep * h + w * slice_src.nChannels + c] = src.imageData[ src.widthStep * h + w * src.nChannels + c] # // y軸方向に分割した場合 else: slice_size: CvSize = CvSize() remained_size: CvSize = CvSize() if slice_line.theta == 90: slice_size = CvSize(slice_line.position, src.height) remained_size = CvSize(src.width - slice_line.position, src.height) else: if pixels.at(pixels.size() - 1).x < slice_line.position: slice_size = CvSize(slice_line.position + 1, pixels.at(pixels.size() - 1).y + 1) else: slice_size = CvSize( pixels.at(pixels.size() - 1).x + 1, src.height) # pixels.at(pixels.size() - 1).y+1) if pixels.at(pixels.size() - 1).x < slice_line.position: remained_size = CvSize( src.width - pixels.at(pixels.size() - 1).x + 1, src.height) else: remained_size = CvSize(src.width - slice_line.position, pixels.at(pixels.size() - 1).y + 1) slice_src = cvCreateImage(slice_size, src.depth, src.nChannels) cvSet(slice_src, cvScalarAll(255), 0) remained_src = cvCreateImage(remained_size, src.depth, src.nChannels) cvSet(remained_src, cvScalarAll(255), 0) rel_remained_point.x += slice_line.position h: int = 0 w: int = 0 c: int = 0 if (src.height == pixels.size()) and (slice_line.theta == 90): for h in range(src.height): for w in range(src.width): if w < slice_line.position: for c in range(src.nChannels): slice_src.imageData[slice_src.widthStep * h + w * slice_src.nChannels + c] = src.imageData[ src.widthStep * h + w * src.nChannels + c] else: for c in range(src.nChannels): remained_src.imageData[ remained_src.widthStep * h + (w - slice_line.position) * remained_src.nChannels + c] = src.imageData[src.widthStep * h + w * src.nChannels + c] elif (src.height == pixels.size()) and (slice_line.theta > 90): for h in range(src.height): for w in range(src.width): if w < pixels.at(h).x: for c in range(src.nChannels): slice_src.imageData[slice_src.widthStep * h + w * slice_src.nChannels + c] = src.imageData[ src.widthStep * h + w * src.nChannels + c] else: for c in range(src.nChannels): remained_src.imageData[ remained_src.widthStep * h + (w - pixels.at(pixels.size() - 1).x) * slice_src.nChannels + c] = src.imageData[src.widthStep * h + w * src.nChannels + c] elif (src.height == pixels.size()) and (slice_line.theta < 90): for h in range(src.height): for w in range(src.width): if w < pixels.at(h).x: for c in range(src.nChannels): slice_src.imageData[slice_src.widthStep * h + w * slice_src.nChannels + c] = src.imageData[ src.widthStep * h + w * src.nChannels + c] else: for c in range(src.nChannels): remained_src.imageData[ remained_src.widthStep * h + (w - slice_line.position) * slice_src.nChannels + c] = src.imageData[src.widthStep * h + w * src.nChannels + c] elif (src.height > pixels.size()) and (slice_line.theta > 90): for h in range(pixels.size()): for w in range(src.width): if w < pixels.at(h).x: for c in range(src.nChannels): slice_src.imageData[slice_src.widthStep * h + w * slice_src.nChannels + c] = src.imageData[ src.widthStep * h + w * src.nChannels + c] else: for c in range(src.nChannels): remained_src.imageData[ remained_src.widthStep * h + w * remained_src.nChannels + c] = src.imageData[src.widthStep * h + w * src.nChannels + c] for h in range(pixels.size()): for w in range(src.width): for c in range(src.nChannels): remained_src.imageData[remained_src.widthStep * h + w * remained_src.nChannels + c] = src.imageData[ src.widthStep * h + w * src.nChannels + c] else: for h in range(pixels.size()): for w in range(src.width): if w < pixels.at(h).x: for c in range(src.nChannels): slice_src.imageData[slice_src.widthStep * h + w * slice_src.nChannels + c] = src.imageData[ src.widthStep * h + w * src.nChannels + c] else: for c in range(src.nChannels): remained_src.imageData[ remained_src.widthStep * h + (w - slice_line.position) * remained_src.nChannels + c] = src.imageData[src.widthStep * h + w * src.nChannels + c] for h in range(int(pixels.size()), src.height): for w in range(src.width): for c in range(src.nChannels): remained_src.imageData[remained_src.widthStep * h + w * remained_src.nChannels + c] = src.imageData[ src.widthStep * h + w * src.nChannels + c] if debug: cvDestroyWindow("[ separation ] slice_src") cvShowImage("[ separation ] slice_src", slice_src) cv.waitKey(0) cvDestroyWindow("[ separation ] remained_src") cvShowImage("[ separation ] remained_src", remained_src) cv.waitKey(0) # // 保存しないで良い余白等はfalseを返す if self.is_blank(slice_src): return Response.DROP_SLICE_SRC if self.is_blank(remained_src): return Response.DROP_REMAINED_SRC threshold: float = 0.02 if slice_src.width * slice_src.height < original_size * threshold: return Response.DROP_SLICE_SRC if remained_src.width * remained_src.height < original_size * threshold: return Response.DROP_REMAINED_SRC return Response.OK