def shared_roi(*images): '''Return the part of the region of interest that all arguments share. Returns None if they don't share one rect all together.''' roi = Rectangle.from_cv_rect(cv.GetImageROI(images[0])) for image in images[1:]: roi = Rectangle.from_cv_rect(cv.GetImageROI(image)).union(roi) return roi.as_cv_rect() if roi.is_real else None
def displayDepth(name, dep, imshow=cv.ShowImage): width = cv.GetImageROI(dep)[2] height = cv.GetImageROI(dep)[3] disp = cv.CloneImage(dep) cv.ConvertScale(disp, disp, 10); imshow(name, disp) del disp
def cvCmpS(src, scalar, dst, code): width1 = cv.GetImageROI(src)[2] height1 = cv.GetImageROI(src)[3] width2 = cv.GetImageROI(dst)[2] height2 = cv.GetImageROI(dst)[3] if not (width1 == width2 and height1 == height2): print 'cvCmpS src, dst size error' print (width1, height1), (width2, height2) cv.CmpS(src, scalar, dst, code)
def cvCopy(src, dst, mask=None): width1 = cv.GetImageROI(src)[2] height1 = cv.GetImageROI(src)[3] width2 = cv.GetImageROI(dst)[2] height2 = cv.GetImageROI(dst)[3] if not (src.depth == dst.depth and width1 == width2 and height1 == height2): print 'cvCopy argument error' print (width1, height1, src.depth), (width2, height2, dst.depth) raise RuntimeError cv.Copy(src, dst, mask) return
def calibrate(self, img, mask_range = (50, 100)): # mask: ignore pixels that are really close or far away, like the # Nao (close, low values) and kinect noise (far far away, high values) _, _, width, height = cv.GetImageROI(img) mask = cv.CreateImage((width, height), cv.IPL_DEPTH_8U, 1) cv.InRangeS(img, mask_range[0], mask_range[1], mask) # using the mask, create a new image containing only the pixels of # interest. self.base_img = cv.CreateImage((width, height), cv.IPL_DEPTH_8U, 1) cv.SetImageROI(self.base_img, cv.GetImageROI(img)) # get the minimum value, and subtract that from all pixels so only # the difference in depth in the image remains. minimum, _, _, _ = cv.MinMaxLoc(img, mask) cv.SubS(img, minimum, self.base_img)
def render_with_histogram(img): '''Just a utility to draw a grayscale histogram next to an image.''' _, _, width, height = cv.GetImageROI(img) canvas = cv.CreateImage((width + 200, max(height, 255)), cv.IPL_DEPTH_8U, 1) cv.Rectangle(canvas, (width, 0), (width + 200, height), (0), cv.CV_FILLED) cv.SetImageROI(canvas, (0, 0, width, height)) cv.Copy(img, canvas) cv.SetImageROI(canvas, (width, 0, 200, canvas.height)) hist = cv.CreateHist([255], cv.CV_HIST_ARRAY, [(0,255)], 1) cv.CalcHist([img], hist) values = [cv.QueryHistValue_1D(hist, n) for n in range(255)] max_value = max(values) for n, value in enumerate(values): cv.Rectangle(canvas, (0, n), (int((value / max_value) * 200), n + 1), (255), cv.CV_FILLED) cv.SetImageROI(canvas, (0, 0, canvas.width, canvas.height)) return canvas
def filter(self, input_img): original_input_roi = cv.GetImageROI(input_img) filter_roi = shared_roi(input_img, self.base_img) if not filter_roi: raise Exception("input image and training image do not share a region of interest") cv.SetImageROI(input_img, filter_roi) # and that is it! Just subtract the difference we calibrated on from # the normal image, and tada! input_roi = cv.GetImageROI(input_img) cv.SetImageROI(self.base_img, input_roi) cv.Sub(input_img, self.base_img, input_img) cv.SetImageROI(input_img, original_input_roi)
def cvConvertScale(src, dst, scale=None, offs=None): width1 = cv.GetImageROI(src)[2] height1 = cv.GetImageROI(src)[3] width2 = cv.GetImageROI(dst)[2] height2 = cv.GetImageROI(dst)[3] if not (width1 == width2 and height1 == height2 and src.nChannels == dst.nChannels): print 'cvConvertScale argument error' print (width1, height1, src.nChannels), (width2, height2, dst.nChannels) raise RuntimeError if scale and offs: cv.ConvertScale(src, dst, scale, offs) elif scale: cv.ConvertScale(src, dst, scale) elif offs: cv.ConvertScale(src, dst, offset = offs) else: cv.ConvertScale(src, dst)
def clone_color_image(img): '''Create a new color image based on a grayscale image''' roi = cv.GetImageROI(img) color = cv.CreateImage((img.width, img.height), img.depth, 4) cv.SetImageROI(color, roi) empty = cv.CreateImage((img.width, img.height), img.depth, 1) cv.SetImageROI(empty, roi) cv.Merge(img, img, img, empty, color) return color
def cvAnd(A, B, dst): width1 = cv.GetImageROI(A)[2] height1 = cv.GetImageROI(A)[3] width2 = cv.GetImageROI(B)[2] height2 = cv.GetImageROI(B)[3] width3 = cv.GetImageROI(dst)[2] height3 = cv.GetImageROI(dst)[3] if not (width1 == width3 and height1 == height3 and A.depth == dst.depth): print 'cvAnd argument error: size, type of src1, dst' print (width1, height1, A.depth), (width3, height3, dst.depth) raise RuntimeError cv.And(A,B,dst)