def watershed(thresh):
    from _8connected import get_8connected_v2
    h, w = thresh.shape
    val = 1
    thresh = padding2D_zero(thresh, val)
    pareas = 0
    ite = 100
    kernel = [
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],
    ]
    while ite:
        # points = []
        count = 0
        temp = thresh.copy()
        for i in range(val, h + val):
            for j in range(val, w + val):
                if thresh[i, j] == 0 and np.any(thresh[i - val:i + val + 1,
                                                       j - val:j + val + 1]):
                    temp[i - val:i + val + 1, j - val:j + val + 1] *= kernel
                    count += 1
                # else:
                #     points.append((i,j))
        if ite % 2 == 0:
            i = get_8connected_v2(thresh)
            areas = len(set(i.reshape(i.shape[0] * i.shape[1]).tolist())) - 1
        # print(pareas, areas)
        if count == 0 or pareas > areas:
            # print(count, pareas, areas)
            break
        if ite % 2 == 0:
            pthresh = thresh.copy()
        thresh = temp.copy()
        pareas = areas
        # cv2.imshow('watershed %d' % (ite), thresh)
        # cv2.waitKey(0)
        ite -= 1
    cv2.destroyAllWindows()
    a = remove_padding2D_zero(pthresh, val)
    return a
Exemple #2
0
def segment_image4(img_file):
    org = cv2.imread(img_file, cv2.IMREAD_COLOR)
    h, w = org.shape[:2]

    img = org.copy()

    # removing noise by using Non-local Means Denoising algorithm
    img = cv2.fastNlMeansDenoisingColored(img, None, 10, 10, 7, 21)
    # cv2.imshow('cleaned',img)

    # Taking the red component out of RBG image as it is less effected by shadow of grain or impurity
    gray = img[:, :, 2]
    # cv2.imshow('gray',gray)

    # calculating threshold value by using otsu thresholding
    T = otsu_threshold(gray=gray)

    # incresing contrast about the threshold
    gray = np.array([[
        max(pixel - 25, 0) if pixel < T else min(pixel + 25, 255)
        for pixel in row
    ] for row in gray],
                    dtype=np.uint8)
    # cv2.imshow('contrast',gray)

    # generating a threshold image
    thresh = np.array([[0 if pixel < T else 255 for pixel in row]
                       for row in gray],
                      dtype=np.uint8)
    # cv2.imshow('Threshold',thresh)

    ########################## 1st level of segmentation ########################################
    # print " Level 1 segmentation"

    # generating a mask using 8-connected component method on threshold image
    mask = get_8connected_v2(thresh, mcount=5)
    # display_mask("Initial mask",mask)

    # Calcutaing the grain segment using mask image
    s = cal_segment_area(mask)

    # cv2.waitKey()
    # removing the backgraound of grain
    # timg = np.array([[[0,0,0] if mask[i,j] == 0 else org[i,j] for j in range(w)] for i in range(h)], dtype=np.uint8)

    # removing very small particals (smaller the 2^3 the average size)
    print(s)
    low_Tarea, up_Tarea = areaThreshold_by_havg(s, 3)
    slist = list(s)
    for i in slist:
        area = (s[i][0] - s[i][1]) * (s[i][2] - s[i][3])
        if area < low_Tarea or area > up_Tarea:
            s.pop(i)

    # print " Level 1 segmentation Finished"
    ####################### 1st level of segmentation Finished ##################################

    ####################### 2nd level of segmentation ###########################################
    # print "\t Level 2 segmentation"
    # print s
    new_s = {}
    s_range = [i for i in s]
    max_index = max(s_range)
    for sindex in s_range:
        if s[sindex][0] - s[sindex][1] and s[sindex][2] - s[sindex][3]:
            iimg = np.array([[
                img[i, j] if mask[i, j] == sindex else [0, 0, 0]
                for j in range(s[sindex][2], s[sindex][3])
            ] for i in range(s[sindex][0], s[sindex][1])],
                            dtype=np.uint8)
            if len(iimg) == 0:
                continue
            mask1 = L2_segmentation_2(iimg,
                                      T=T,
                                      index=max_index + 5 + len(new_s))
            if mask1 is None:
                continue
            ######################################## segmenting adding ########################
            m = s.pop(sindex)
            mask[m[0]:m[1], m[2]:m[3]] = [[
                0 if pixel == sindex else pixel for pixel in row
            ] for row in mask[m[0]:m[1], m[2]:m[3]]]
            mask[m[0]:m[1], m[2]:m[3]] += mask1

            s1 = cal_segment_area(mask1)
            # print s1
            for k in s1:
                area = (s1[k][0] - s1[k][1]) * (s1[k][2] - s1[k][3])
                if area > low_Tarea and area < up_Tarea:
                    new_s[k] = [
                        m[0] + s1[k][0], m[0] + s1[k][1], m[2] + s1[k][2],
                        m[2] + s1[k][3]
                    ]
            max_index = max([max_index] + list(new_s))
            ###################################################################################

    # print "2nd Level of segmentation Finished"
    #####################2nd level of segmentation Finished ###################################
    s.update(new_s)

    # marking the segments
    segments = {}
    torg = org.copy()
    for i in s:
        imgRectangled = cv2.rectangle(torg, (s[i][2], s[i][0]),
                                      (s[i][3], s[i][1]), (0, 0, 255), 1)
        segments[i] = np.array([[
            org[x, y] if mask[x, y] == i else [0, 0, 0]
            for y in range(s[i][2], s[i][3])
        ] for x in range(s[i][0], s[i][1])],
                               dtype=np.uint8)
        # # cv2.imshow("segment %d" % (count), segments[count])

    # # cv2.imshow('Marked image',imgRectangled)
    # cv2.waitKey(0)
    # cv2.destroyAllWindows()
    return segments, s, imgRectangled, mask
Exemple #3
0
def L2_segmentation_2(iimg, T, index):
    h, w, _ = iimg.shape
    # cv2.imshow('image', iimg)
    t0 = time.time()
    gray = iimg[:, :, 2]
    # cv2.imshow('gray', gray)

    thresh = np.array([[0 if pixel < T else 255 for pixel in row]
                       for row in gray],
                      dtype=np.uint8)

    sober = sober_operation(gray)
    # cv2.imshow('sober', sober)
    # print "\tsober operation", time.time() - t0
    # t0 = time.time()

    sober = cv2.fastNlMeansDenoising(sober,
                                     None,
                                     h=2,
                                     templateWindowSize=3,
                                     searchWindowSize=5)
    # cv2.imshow('sober cleaned', sober)
    # print "\tnoise operation", time.time() - t0
    # t0 = time.time()

    T = otsu_threshold(sober)
    # print "\tsober threshold", time.time() - t0
    # t0 = time.time()

    sthresh = np.array([[0 if pixel < T else 255 for pixel in row]
                        for row in sober],
                       dtype=np.uint8)
    # cv2.imshow('sober Threshold', sthresh)
    # print "\tcalc threshold", time.time() - t0
    # t0 = time.time()

    diluted = cv2.dilate(sthresh,
                         kernel=np.ones((5, 5), np.uint8),
                         iterations=1)
    # cv2.imshow('dilutated2 ', diluted)
    # print "\tdilation operation", time.time() - t0
    # t0 = time.time()

    thresh2 = np.where((thresh == 0) * (diluted == 255), 0, thresh - diluted)
    # cv2.imshow('Thresh - dilute ', thresh2)

    mask = get_8connected_v2(thresh=thresh2, mcount=index)
    # display_mask("Diluted mask", mask)
    # print "\tmask foamation", time.time() - t0
    # t0 = time.time()

    # Calcutaing the grain segment using mask image
    s = cal_segment_area(mask)
    # print "\tcalc area seg", time.time() - t0
    # t0 = time.time()

    rmcount = 0
    if len(s) < 2:
        # print
        return None
    low_Tarea, up_Tarea = areaThreshold_by_top(s, 3)
    slist = list(s)
    for i in slist:
        area = (s[i][0] - s[i][1]) * (s[i][2] - s[i][3])
        if area < low_Tarea:
            s.pop(i)
            rmcount += 1
    if len(s) < 2:
        # print
        return None
    # print "\tselecting area", time.time() - t0
    # t0 = time.time()

    # removing unwanted masks
    mask = np.array([[0 if pixel not in s else pixel for pixel in row]
                     for row in mask])
    # print "\tremoving unwanted mask opeation", time.time() - t0
    # t0 = time.time()

    # Adding boundry mask
    boundry = get_boundry_img_matrix(thresh, 1)
    # print "\tgetting boundry", time.time() - t0
    # t0 = time.time()

    mask = np.where(boundry == 1, 1, mask)
    # print "\tadding boundry to mask opeation", time.time() - t0
    # t0 = time.time()

    # display_mask('boundried mask', mask)

    # using mask fill the mask values in boundry
    mask = flood_filling(mask)
    # print "\tflood filling opeation", time.time() - t0
    # t0 = time.time()
    # display_mask('flood fill', mask)

    # replace boundry by respective mask value
    mask = boundry_fill(mask)
    # print "\tfilling opeation", time.time() - t0
    # t0 = time.time()
    # cv2.waitKey()

    masks = []
    for ii in s:
        img = get_mask_value_area(gray, mask, ii)
        # b1 = get_boundry_img_matrix(img)
        # b2 = get_boundry_img_matrix(get_mask_value_area(boundry, mask, i),bval=255)
        # img = b1-b2
        points = get_boundry_as_points(img)
        img = get_boundry_img_matrix(img, bval=255)
        # cv2.imshow("img %d" % (ii), img)
        coff = elliptic_fourier_descriptors(points, order=5)
        if coff is None:
            print("Ellipsis not work")
            return None
        x, y = np.int0(
            efd(coff, contour_1=points, locus=np.mean(points, axis=0)))
        boundry = make_border(zip(x, y), img.shape, bval=255)
        # cv2.imshow("border %d"%(ii), boundry)
        mask1 = mask_by_border(boundry, ii)
        # display_mask("mask %d" % (ii), mask1)
        masks.append(mask1)
    # print "\telliptical fitting operation", time.time() - t0,'\n'

    # cv2.waitKey()
    # cv2.destroyAllWindows()
    return masks, rmcount
Exemple #4
0
def L2_segmentation_2(iimg, T, index):
    h, w, _ = iimg.shape
    # cv2.imshow('image', iimg)

    gray = iimg[:, :, 2]
    # cv2.imshow('gray', gray)

    thresh = np.array([[0 if pixel < T else 255 for pixel in row]
                       for row in gray],
                      dtype=np.uint8)

    sober = sober_operation(gray)
    # cv2.imshow('sober', sober)

    sober = cv2.fastNlMeansDenoising(sober,
                                     None,
                                     h=2,
                                     templateWindowSize=3,
                                     searchWindowSize=5)
    # cv2.imshow('sober cleaned', sober)

    T = otsu_threshold(sober)

    sthresh = np.array([[0 if pixel < T else 255 for pixel in row]
                        for row in sober],
                       dtype=np.uint8)
    # cv2.imshow('sober Threshold', sthresh)

    diluted = cv2.dilate(sthresh,
                         kernel=np.ones((5, 5), np.uint8),
                         iterations=1)
    # cv2.imshow('dilutated2 ', diluted)

    thresh2 = np.where((thresh == 0) * (diluted == 255), 0, thresh - diluted)
    # cv2.imshow('Thresh - dilute ', thresh2)

    mask = get_8connected_v2(thresh=thresh2, mcount=index)
    # display_mask("Diluted mask", mask)

    # Calcutaing the grain segment using mask image
    s = cal_segment_area(mask)

    if len(s) < 2:
        return None
    low_Tarea, up_Tarea = areaThreshold_by_avg(s, 2)
    slist = list(s)
    for i in slist:
        area = (s[i][0] - s[i][1]) * (s[i][2] - s[i][3])
        if area < low_Tarea or area > up_Tarea:
            s.pop(i)
    if len(s) < 2:
        return None

    # # # cv2.imshow("watershed", water_shed)

    # removing unwanted masks
    mask = np.array([[0 if pixel not in s else pixel for pixel in row]
                     for row in mask])

    # Adding boundry mask
    boundry = get_boundry_img_matrix(thresh, 1)
    mask = np.where(boundry == 1, 1, mask)

    # display_mask('boundried mask', mask)

    # using mask fill the mask values in boundry
    mask = flood_filling(mask)
    # display_mask('flood fill', mask)

    # replace boundry by respective mask value
    mask = boundry_fill(mask)

    # display_mask("Final Mask",mask)
    # cv2.waitKey()

    # cv2.destroyAllWindows()
    return mask
def segment_image4(img_file, dlog=0):
    t0 = time.time()
    org = cv2.imread(img_file, cv2.IMREAD_COLOR)
    h, w = org.shape[:2]

    img = org.copy()
    # print("Reading ",time.time()-t0)
    t0 = time.time()

    # removing noise by using Non-local Means Denoising algorithm
    img = cv2.fastNlMeansDenoisingColored(img, None, 10, 10, 7, 21)
    # cv2.imshow('cleaned',img)
    # print("noise removing ",time.time()-t0)
    t0 = time.time()

    # Taking the red component out of RBG image as it is less effected by shadow of grain or impurity
    gray = np.array([[pixel[2] for pixel in row] for row in img])
    # cv2.imshow('gray',gray)

    # calculating threshold value by using otsu thresholding
    T = otsu_threshold(gray=gray)
    # print("threshold calc ",time.time()-t0)
    t0 = time.time()

    # incresing contrast about the threshold
    # gray = np.array([[max(pixel - 25, 0) if pixel < T else min(pixel + 25, 255) for pixel in row] for row in gray], dtype=np.uint8)
    # cv2.imshow('contrast',gray)
    # print("Increasing contrast ",time.time()-t0)
    # t0 = time.time()

    # generating a threshold image
    thresh = np.array([[0 if pixel < T else 255 for pixel in row]
                       for row in gray],
                      dtype=np.uint8)
    # cv2.imshow('Threshold',thresh)
    # print("Generating Threshold ",time.time()-t0)
    t0 = time.time()

    ########################## 1st level of segmentation ########################################
    # print(" Level 1 segmentation")

    # generating a mask using 8-connected component method on threshold image
    mask = get_8connected_v2(thresh, mcount=5)
    # display_mask("Initial mask",mask)
    # print("Mask Generation ",time.time()-t0)
    t0 = time.time()

    # Calcutaing the grain segment using mask image
    s = cal_segment_area(mask)
    # print("Calculating segment ends",time.time()-t0)
    t0 = time.time()

    # cv2.waitKey()
    # removing the backgraound of grain
    # timg = np.array([[[0,0,0] if mask[i,j] == 0 else org[i,j] for j in range(w)] for i in range(h)], dtype=np.uint8)

    # removing very small particals (smaller the 2^3 the average size)
    low_Tarea, up_Tarea = areaThreshold_by_havg(s, 3)
    slist = list(s)
    s1count = total = 0
    total += len(slist)
    for i in slist:
        area = (s[i][0] - s[i][1]) * (s[i][2] - s[i][3])
        if area < low_Tarea:  # or area > up_Tarea:
            rm = s.pop(i)
            s1count += 1
            # if dlog == 1: rm_detail.write(str(rm)+'\n')
            # cv2.imwrite('/media/zero/41FF48D81730BD9B/kisannetwork/removed/'+img_file.split('/')[-1].split(['.'])[0]+'_l1_'+str(s1count), get_img_value_inRange(org, mask, i, s[i]))
    print("Level 1 segmentation Finished:")
    print("\tRejected segment: %d" % (s1count))

    if dlog == 1:
        rm_detail.write(
            "\n\t%d Number of segment rejected out of %d in L1 segmentation\n"
            % (s1count, total))
    # print(" Level 1 segmentation Finished",time.time()-t0)
    t0 = time.time()
    ####################### 1st level of segmentation Finished ##################################

    ####################### 2nd level of segmentation ###########################################
    # print("\t Level 2 segmentation")
    # print(s)
    new_s = {}

    s_range = [i for i in s]

    max_index = max(s_range)

    segments = {}
    s2count = extra = 0
    # print("Level 2 seg. start", time.time() - t0)
    t0 = time.time()
    for sindex in s_range:
        s1 = {}
        org1 = get_img_value_inRange(org, mask, sindex, s[sindex])
        iimg = get_img_value_inRange(img, mask, sindex, s[sindex])
        # cv2.imshow('image',iimg)
        if len(iimg) == 0:
            continue

        if isMoregrain(iimg, T):
            a = L2_segmentation_2(iimg, T=T, index=max_index + 5 + len(new_s))
        else:
            segments[sindex] = org1
            continue
        if not a:
            segments[sindex] = org1
            extra += 1
            continue
        masks, trm = a
        s2count += trm
        total += len(masks) + trm - 1
        for msk in masks:
            a = cal_segment_area(msk)
            s1.update(a)
            for ii in a:
                # display_mask("mask %d"%(ii), msk)
                segments[ii] = get_img_value_inRange(org1, msk, ii, s1[ii])
        # cv2.waitKey()
        ######################################## segmenting adding ########################
        m = s.pop(sindex)
        mask = remove_mask(mask, sindex, m)
        mask1 = np.sum(masks, axis=0)
        # mask[m[0]:m[1], m[2]:m[3]] = [[0 if pixel == sindex else pixel for pixel in row] for row in mask[m[0]:m[1], m[2]:m[3]]]
        mask[m[0]:m[1], m[2]:m[3]] += mask1

        for k in s1:
            area = (s1[k][0] - s1[k][1]) * (s1[k][2] - s1[k][3])
            if area > low_Tarea and area < up_Tarea:
                new_s[k] = [
                    m[0] + s1[k][0], m[0] + s1[k][1], m[2] + s1[k][2],
                    m[2] + s1[k][3]
                ]
        max_index = max([max_index] + list(new_s))
        ###################################################################################

    print("\nLevel 2 segmentation Finished:")
    print("\tRejected segment: %d" % (s2count))
    # t0 = time.time()
    #####################2nd level of segmentation Finished ###################################
    print("\n\nTotal number of segments %d" % (total))
    print("Number of rejected segments %d\n\n" % (s1count + s2count))
    # print
    if dlog == 1:
        rm_detail.write(
            "\tIn level 2 segmentation %d rejected\n\tTotal number of segments %d\n\tNumber of rejected segments %d\n"
            % (s2count, total, s1count + s2count))

    s.update(new_s)

    # marking the segments
    torg = org.copy()
    for i in s:
        imgRectangled = cv2.rectangle(torg, (s[i][2], s[i][0]),
                                      (s[i][3], s[i][1]), (0, 0, 255), 1)
        # segments[i] = get_mask_value_inRange(org, mask, i, s[i])
        # segments[i] = np.array([[org[x,y] if mask[x,y] == i else [0,0,0] for y in range(s[i][2],s[i][3])] for x in range(s[i][0],s[i][1])], dtype=np.uint8)
        # cv2.imshow("segment %d" % (count), segments[count])

    # cv2.imshow('Marked image',imgRectangled)
    # cv2.waitKey(0)
    # cv2.destroyAllWindows()
    return segments, s, imgRectangled, mask