def test_invalid_ext(self): # Test writing a valid image with improper file extension image = read(self._test_file('test_read_image.jpg')) with self._temp_dir() as temp_dir: path = os.path.join(temp_dir, 'foo.xxx') with self.assertRaises(cv2.error): write(path, image)
def test_roi(self): path = self._test_file(TEST_CASES['test_roi']) img = read(path) roi = img.set_roi(img.width / 2, img.height / 2) offset = roi.width, roi.height contours = TestElement().find_contours(roi, WHITE, offset=offset) img = img.draw.contours(contours, color=GREEN) write(self.get_path(path), img)
def test_cyrillic_path(self): # Test cyrillic filename image = read(self._test_file('test_read_image.jpg')) with self._temp_dir() as temp_dir: path = os.path.join(temp_dir, 'изображения', 'изображение.png') write(path, image) self.assertTrue(os.path.exists(path)) new_image = read(path) numpy.testing.assert_array_equal(new_image, image)
def test_valid_path(self): # Test writing a valid image image = read(self._test_file('test_read_image.jpg')) with self._temp_dir() as temp_dir: path = os.path.join(temp_dir, 'test_write_image.png') write(path, image) self.assertTrue(os.path.exists(path)) new_image = read(path) numpy.testing.assert_array_equal(new_image, image)
def test_rect_ranged(self): _ = [0, 25, 75] k_range = [(i / 100, j / 100) for i in _ for j in _] test_case_path = TEST_CASES['rect_ranged'] for k in k_range: img = read(test_case_path) contours = TestElement().find_contours(img, GRAY, k) img = img.draw.contours(contours, color=(0, 255, 0)) path = self.get_path(test_case_path) dir_name = dirname(path) base_name = basename(path) file_name, extension = splitext(base_name) file_name = f'{file_name}_{str(int(k[0]*100)).zfill(2)}_{str(int(k[1]*100)).zfill(2)}{extension}' write(join(dir_name, file_name), img)
def screenShot(): width = glutGet(GLUT_WINDOW_WIDTH) height = glutGet(GLUT_WINDOW_HEIGHT) # rgba data size size = width * height * 4 pixelBuffer = glGenBuffers(1) glBindBuffer(GL_PIXEL_PACK_BUFFER, pixelBuffer) glBufferData(GL_PIXEL_PACK_BUFFER, size, None, GL_STREAM_READ) glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, c_void_p(0)) data = glMapBuffer(GL_PIXEL_PACK_BUFFER, GL_READ_ONLY) image.write(width, height, data, size) glUnmapBuffer(GL_PIXEL_PACK_BUFFER) glBindBuffer(GL_PIXEL_PACK_BUFFER, 0) glDeleteBuffers(1, [pixelBuffer])
def prepare_test_data(): w, h = 200, 200 pt1 = int(w * 1 / 4), int(h * 1 / 4) pt2 = int(w * 3 / 4), int(h * 3 / 4) # Rectangle on black img = Image.create(size=(w, h)) img = img.draw.rectangle(pt1, pt2, GRAY, filled=True) write(TEST_CASES['rect_black'], img) # Rectangle on white img = Image.create(size=(w, h), background=WHITE) img = img.draw.rectangle(pt1, pt2, GRAY, filled=True) write(TEST_CASES['rect_white'], img) # Ranged rectangles img = Image.create(size=(w, h)) rect1 = (int(w * 1 / 4), int(h * 1 / 4)), (int(w * 2 / 4), int(w * 2 / 4)) rect2 = (int(w * 2 / 4), int(h * 1 / 4)), (int(w * 3 / 4), int(w * 2 / 4)) rect3 = (int(w * 1 / 4), int(h * 2 / 4)), (int(w * 2 / 4), int(w * 3 / 4)) rect4 = (int(w * 2 / 4), int(h * 2 / 4)), (int(w * 3 / 4), int(w * 3 / 4)) rect5 = (int(w * 3 / 8), int(h * 3 / 8)), (int(w * 5 / 8), int(w * 5 / 8)) img = img.draw.rectangle(*rect1, GRAY_75, filled=True) img = img.draw.rectangle(*rect2, GRAY_50, filled=True) img = img.draw.rectangle(*rect3, GRAY_125, filled=True) img = img.draw.rectangle(*rect4, GRAY_175, filled=True) img = img.draw.rectangle(*rect5, GRAY, filled=True) write(TEST_CASES['rect_ranged'], img) # Rectangle with noise img = Image.create(size=(w, h)) img = img.draw.rectangle(pt1, pt2, GRAY, filled=True) noise = [] for i in range(50): x, y = randint(0, w), randint(0, h) noise.append((x, y)) for center in noise: img = img.draw.circle(center, 3, GRAY, filled=True) write(TEST_CASES['rect_noised'], img)
def write(self, img): '''Writes the given image to the output writer(s).''' if self._write_images: im.write(img, self.out_impath % self._reader.frame_number) if self._write_videos: self._video_writer.write(img)
b'\xff\xff\xff\xff', b'\x00\x80\x00\xff', b'\x29\x29\xa3\xff', b'\x00\x00\x00\xff', b'\x50\x50\x50\xff' ] } patterns = { # a pattern associates a pattern function with a palette 'checker': [checker, 'blackwhite'], 'block': [random_block, 'bgbwg'], 'randcheck': [random_checker, 'wgbbg'] } def fetch_pattern(pattern, pixtype, size): """Fetches a named pattern, sizes it, and down-channels the palette to the pixel type.""" fxpx = fixpix(pixtype) palette = [fxpx(pixel) for pixel in palettes[patterns[pattern][1]]] pixfun = patterns[pattern][0](size, palette) return (pixfun, pixtype) if __name__ == "__main__": from image import render, write from units import mm pixtype = 'rgb' for name in patterns.keys(): scene = fetch_pattern(name, pixtype, 15 * mm) img = render(scene) filename = "patterns_{0}.png".format(name) print("writing {0}".format(filename)) write(filename, img)
def render_scene(args, scene): """Renders the scene to an image using defined page and resolution.""" page = pages.pages[args.page] resolution = pages.resolutions[args.resolution] img = image.render(scene, page, resolution) return img def view_scene(scene, xform): """Applies a transformation to the scene.""" pixfun, pixtype = scene[0], scene[1] return lambda x, y: pixfun(*xform(x, y)), pixtype def timestr(s): hours, remainder = divmod(s, 3600) minutes, seconds = divmod(remainder, 60) return "{:02d}:{:02d}:{:02d}".format(int(hours), int(minutes), int(round(seconds))) if __name__ == "__main__": import time, sys parser = cmd_parser() args = parser.parse_args() t1 = time.time() scene = generate_scene(args) view = view_scene(scene, lambda x, y: (x,y)) # identity transformation as example img = render_scene(args, view) image.write(args.outfile, img) t2 = time.time() if args.verbose: sys.stderr.write("{0} {1}x{2} {3}\n".format(args.outfile.name, img.cols, img.rows, timestr(t2-t1)))
def test_rect_white(self): test_case_path = TEST_CASES['rect_white'] img = read(test_case_path) contours = TestElement().find_contours(img, GRAY) img = img.draw.contours(contours, color=(0, 255, 0)) write(self.get_path(test_case_path), img)
rectified2, [int(ratio * x) for x in rectified2.shape]) # Load mask1 and mask2, set image1 to the image with the largest corresponding mask mask1 = numpy.load("mask1.npy") mask2 = numpy.load("mask2.npy") reverse = (numpy.sum(mask1) < numpy.sum(mask2)) image1 = rectified1 image2 = rectified2 if reverse: image1 = rectified2 image2 = rectified1 # Run GCS shape = image1.shape gcs = GrowingCorrespondenceSeeds(shape, disparity, window) if reverse: (disparity2, disparity1, similarity2, similarity1) = gcs.match(image1, image2) else: (disparity1, disparity2, similarity1, similarity2) = gcs.match(image1, image2) image.write("gcs1.png", disparity1) image.write("gcs2.png", disparity2)
#!/usr/bin/env python3 import sys import image as im img = im.read(sys.stdin.buffer) im.write(sys.stdout.buffer.raw, img)
height = numpy.min([L.shape[0], R.shape[0]]) width = numpy.min([L.shape[1], R.shape[1]]) L = L[:height, :width] R = R[:height, :width] # TODO: Scale rectify_mask? # Scale by height if (MAX_WIDTH < width): aspect = float(MAX_WIDTH) / width L = resample(L, [int(aspect * x) for x in L.shape]) R = resample(R, [int(aspect * x) for x in R.shape]) print("Left shape: " + str(L.shape)) print("Right shape: " + str(R.shape)) (L_disparity_map, R_disparity_map, L_cost_map, R_cost_map) = gcs_matching(L, R) pickle.dump(L_disparity_map, open(L_disparity_map_path, "wb")) pickle.dump(R_disparity_map, open(R_disparity_map_path, "wb")) image.show(L_disparity_map, title="Left Dispartiy Map") image.show(R_disparity_map, title="Right Dispartiy Map") image.write("disparity_1.png", L_disparity_map) image.write("disparity_2.png", R_disparity_map)