示例#1
0
def py_median_3x3(image, iterations=10, num_threads=1):
    ''' repeatedly filter with a 3x3 median '''
    tmpA = image.copy()
    tmpB = np.empty_like(tmpA)
    if iterations == 0:
        return tmpA
    n = tmpA.shape[0]
    # iterations x num_threads matrix
    logging.debug('num_threads = %d', num_threads)
    logging.debug('iterations = %d', iterations)
    events = [[threading.Event()] * num_threads] * iterations
    # iterate once first
    filtering.median_3x3(tmpA, tmpB, 0, 1)
    tmpA, tmpB = tmpB, tmpA
    for k in range(len(events[0])):
        e = events[0][k]
        e.set()
        logging.debug('thread %d->itr %d: finish swap; set event', k, 0)
    for i in range(num_threads):
        t = threading.Thread(target=worker_from_second_itr, args=(i, n, num_threads, iterations, tmpA, tmpB, events))
        logging.debug('start thread  %d', i)
        t.start()
    # for i in range(iterations):
    #     filtering.median_3x3(tmpA, tmpB, 0, 1)
    #     # swap direction of filtering
    #     tmpA, tmpB = tmpB, tmpA

    main_thread = threading.currentThread()
    for t in threading.enumerate():
        if t is main_thread:
            continue
        logging.debug('joining %s', t.getName())
        t.join()
    return tmpA
示例#2
0
def py_median_3x3(image, iterations=10, num_threads=1):
    """ repeatedly filter with a 3x3 median """
    tmpA = image.copy()
    tmpB = np.empty_like(tmpA)

    for i in range(iterations):
        filtering.median_3x3(tmpA, tmpB, 0, 1)
        # swap direction of filtering
        tmpA, tmpB = tmpB, tmpA

    return tmpA
示例#3
0
def worker_from_second_itr(i, n, num_threads, iterations, tmpA, tmpB, events):
    for itr in range(1, iterations):
        # wait for neighboring events to finish
        prec_i = i - 1 if i > 0 else ( num_threads - 1 )
        succ_i = i + 1 if i < ( num_threads - 1 ) else 0
        # @x: wait for neighbors
        logging.debug('thread %d->itr %d: waiting for neighbors', i, itr)
        events[itr - 1][prec_i].wait()
        events[itr - 1][i].wait()
        events[itr - 1][succ_i].wait()
        # @x: filter
        logging.debug('thread %d->itr %d: get event signal; filters', i, itr)
        filtering.median_3x3(tmpA, tmpB, i, num_threads)
        # @x: swap i th
        logging.debug('thread %d->itr %d: finish filtering; swap', i, itr)
        for k in range(0, n, num_threads):
            tmpA[k], tmpB[k] = tmpB[k], tmpA[k]
        # @x: signal waiting threads
        logging.debug('thread %d->itr %d: finish swap; set event', i, itr)
        events[itr][i].set()
示例#4
0
 def run(self):
     filtering.median_3x3(self.tmpA, self.tmpB, self.offset, self.step)