def numpy2pink(array): """ Makes an image from a numpy array """ ## Not elegant if (np.size(array.shape) == 2): Image = { 'uint8_t': pink.char_image([array.shape[1], array.shape[0]]), 'uint16_t': pink.short_image([array.shape[1], array.shape[0]]), 'int32_t': pink.int_image([array.shape[1], array.shape[0]]), 'float': pink.float_image([array.shape[1], array.shape[0]]), 'double': pink.double_image([array.shape[1], array.shape[0]]), } elif (np.size(array.shape) == 3): Image = { 'uint8_t': pink.char_image([array.shape[2], array.shape[1], array.shape[0]]), 'uint16_t': pink.short_image([array.shape[2], array.shape[1], array.shape[0]]), 'int32_t': pink.int_image([array.shape[2], array.shape[1], array.shape[0]]), 'float': pink.float_image([array.shape[2], array.shape[1], array.shape[0]]), 'double': pink.double_image([array.shape[2], array.shape[1], array.shape[0]]), } else: sys.stder.write("Number of dimensions not supported\n") return None myimg = Image.get(np2pinkdtype[array.dtype]) # copy data myimg.set_pixels(array.ctypes.data, array.nbytes) return myimg
def array_2_image(arr): S = list(arr.shape) N = arr.size img = pink.char_image(S) arr.resize(N) for i in range(N): img[i] = int(arr[i]) return img
def disk_structuring_element(radius): d = radius + radius + 1 res = pink.char_image([d, d]) res.fill(0) res.center = [radius, radius] res[[radius, radius]] = 255 res = pink.dilatball(res, radius) return (res)
def extract_cells(image, threshold=24): # creating the structuring element elem = pink.char_image([3, 3]) elem.fill(1) elem.center = [1, 1] grad = pink.gradmorph(muscle, elem) seuil = pink.seuil(grad, threshold) frame = pink.frame(pink.char_image(image.size), 255) dilated = pink.geodilat(frame, seuil, 8) skeleton = pink.skeleton(dilated, 0, 8) inv = pink.inverse(skeleton) eroded = pink.erosball(inv, 5) inv = pink.inverse(eroded) skeleton2 = pink.skeleton(inv, image, 4) return skeleton2
def correct_bias(image, xc, yc, alpha): result = pink.char_image([0, 0]) result.copy(image) rs = result.size[0] cs = result.size[1] for y in range(cs): for x in range(rs): R = pow((xc - x) * (xc - x) + (yc - y) * (yc - y), 0.5) T = (1.0 * result[[x, y]]) - (alpha * R) if T > 255: T = 255 if T < 0: T = 0 result[[x, y]] = int(round(T)) return (result)
def find_bias(image, xc, yc): rs = image.size[0] cs = image.size[1] circle = pink.char_image(image.size) rad = 20 # below, circles are too small Y = [] while True: if debug: print rad if (xc + rad >= rs) or (xc - rad < 0): break if (yc + rad >= cs) or (yc - rad < 0): break circle = pink.drawball(circle, rad, xc, yc, 0) circle = pink.border(circle, 8) av = pink.average(image, circle) Y = Y + [av] rad = rad + 1 X = range(len(Y)) res = pink.identifyline(X, Y) # output : coefficient a of the equation of the line y = ax+b # matching the data return(res[0])
#ex 4 ## filtering and inverting circuit = pink.readimage("../images/circuit.pgm") inv = pink.inverse(circuit) inv = pink.seuil(inv, 180, 0, 255) inv = filter_noise(inv) viewer2 = Imview(inv) #inv.writeimage("circ.pgm") # Filtering out the strips to get the round ends round = pink.openball(inv, 5, 0) # plus one round of geodesic dilation to get the shape back roundok = pink.geodilat(round, inv, 8, 1) # note the 1 here viewer2.show(roundok, "roundok") # get the strips by difference strips = inv - roundok viewer2.show(strips, "strips") ## dilation structuring_element = pink.char_image([11, 1]) structuring_element.fill(255) structuring_element.writeimage("se.pgm") structuring_element.center = [5, 0] dilated = pink.dilation(inv, structuring_element) #dilated.writeimage("dil.pgm") viewer2.show(dilated, "dilated") # LuM end of file
def fill_the_hole(image, tbmin=1, tbmax=1): frame = pink.frame(pink.char_image(image.size), 255) dist = pink.dist(image, 0) return pink.toposhrink(inv(frame), inv(dist), 26, tbmin, tbmax, 1, 1, image)
from pink import cpp as pink # ex 4.1 numbers = pink.readimage("../images/numbers.pgm") ball = pink.genball(5) openn = pink.opening(numbers, ball) normalized = pink.seuil(numbers - openn, 10, 0, 255) normalized.writeimage("normalized.pgm") # ex 5.1-1 cell = pink.readimage("../images/cell.pgm") ball = pink.genball(1) dcell = pink.dilation(cell, ball) ecell = pink.erosion(cell, ball) pink.normalize(dcell - ecell).writeimage("grad.pgm") # ex 5.1-2 ## HELP ME!!! #ex 6.1-1 bloodcells = pink.readimage("../images/bloodcells.pgm") marker = pink.char_image(bloodcells.size) marker[[50, 50]] = 255 marker.writeimage("marker.pgm") ws = pink.watershed(bloodcells, marker, 8) ws.writeimage("ws.pgm") # LuM end of file
def fill_the_holes(image): frame = pink.frame(pink.char_image(image.size), 255) inv = pink.inverse(image) dilated = pink.geodilat(frame, inv, 8) result = pink.inverse(dilated) return result
def remove_objects(image): frame = pink.frame(pink.char_image(image.size), 255) border_objects = pink.geodilat(frame, image, 8) result = image - border_objects return result
def crop(image, x, y, w, h): res = pink.char_image([w, h]) res = pink.insert_image(res, image, [-x, -y]) return (res)
RS = 4 # numbers of tiles in a row CS = 4 # numbers of tiles in a column rs = 100 # row size for a tile cs = 100 # column size for a tile nb_grains = 0 grain_size = 0 for J in range(CS): for I in range(RS): filename = 'tile%02d.pgm' % (J * CS + I) tile = pink.readimage(filename) # separate objects into interior and border objects frame = pink.frame(pink.char_image(tile.size), 255) border_objects = pink.geodilat(frame, tile, 8) interior_objects = tile - border_objects # measure interior objects grain_size = grain_size + pink.area(interior_objects) lab = pink.labelfgd(interior_objects, 8) nb_grains = nb_grains + pink.minmax(lab)[1] if DEBUG: print("I,J,s,n: " + str(I) + " " + str(J) + " " + str(grain_size) + " " + str(nb_grains)) # deal with border objects if (I < RS - 1) and (J < CS - 1): filename_r = 'tile%02d.pgm' % (J * CS + I + 1) tile_r = pink.readimage(filename_r) filename_d = 'tile%02d.pgm' % ((J + 1) * CS + I) tile_d = pink.readimage(filename_d)
if debug: view3d(carotide, dilated) diff = carotide - dilated seuil = pink.seuil(diff, 94) if debug: view3d(carotide, seuil) component = pink.selectcomp(seuil, 26, 58, 46, 0) if debug: view3d(carotide, component) carotide_seg = component ### the skeleton operator axonepair = pink.readimage("../images/axonepair.pgm") #imview(axonepair) ## HUGUES WRITE ME! blank = pink.char_image(axonepair.size) skeleton = pink.skeleton(axonepair, blank, 8) #imview(skeleton) pink.surimp(axonepair, skeleton, "axoskel.ppm") ### distances dist = pink.distc(axonepair, 4) dist.writeimage("imagea.pgm") ### randrgb will be replaced with imview colourmap (rand rgb) dist = pink.distc(axonepair, 8) dist.writeimage("imageb.pgm") dist = pink.distc(axonepair, 0) dist.writeimage("imagec.pgm") ### skeletons guided by a priority image
image.fill(0) rs = image.size[0] cs = image.size[1] for j in range(margin, cs-margin): for i in range(margin, rs-margin): if random.random() <= p: image[[i,j]] = 255 rad = 4 # radius of particles RS = 4 # numbers of tiles in a row CS = 4 # numbers of tiles in a column rs = 100 # row size for a tile cs = 100 # column size for a tile pr = 0.0004 # proportion of particle centers img = pink.char_image([RS*rs,CS*cs]) randimage2(img, pr, rad+1) img = pink.dilatball(img, rad) img.writeimage("wholeimage.pgm") imview(img) grain_size = pink.area(img) lab = pink.labelfgd(img, 8) nb_grains = pink.minmax(lab)[1] print("grain_size = " + str(grain_size)) print("nb_grains = " + str(nb_grains)) img = pink.readimage("wholeimage.pgm") tile = pink.char_image([rs,cs]) for J in range(CS):