Beispiel #1
0
    def semantic2whole(self, mask_path=None, seg_path=None):
        s_m, s_n = self.result.shape
        self.result = hole_fill(
            remove_small_objects(self.result.astype(np.bool), 200))
        rows = np.where(self.result)[0]
        columns = np.where(self.result)[1]
        wsi = OpenSlide(os.path.join(self.img_WSI_dir, self.name))
        big = np.zeros(
            (s_m * self.stride / self.scale_ + 256 / self.scale_,
             s_n * self.stride / self.scale_ + 256 / self.scale_, 3))
        # get the original area
        for idx, i in enumerate(rows):
            print idx, '/', len(rows)
            img = np.array(
                wsi.read_region(
                    (columns[idx] * self.stride * 2**self.using_level,
                     i * self.stride * 2**self.using_level), self.using_level,
                    (self.stride, self.stride)))[..., 0:-1]
            resize_img = cv2.resize(
                img, (self.stride / self.scale_, self.stride / self.scale_))
            big[i * self.stride / self.scale_:(i + 1) * self.stride /
                self.scale_,
                columns[idx] * self.stride / self.scale_:(columns[idx] + 1) *
                self.stride / self.scale_, :] = resize_img

        # get rid of other area
        contours = measure.find_contours(self.result, 0.5)
        for i in xrange(len(contours)):
            for j in range(len(contours[i])):
                y = int(contours[i][j, 0] * self.stride * 2**self.using_level)
                x = int(contours[i][j, 1] * self.stride * 2**self.using_level)
                img = io.imread(
                    os.path.join(seg_path,
                                 str(x) + '_' + str(y) + '_seg.png'))
                resize_img = cv2.resize(img,
                                        (256 / self.scale_, 256 / self.scale_))
                y_ = y / (self.scale_ * 2**self.using_level)
                x_ = x / (self.scale_ * 2**self.using_level)
                big[y_:y_ + 256 / self.scale_,
                    x_:x_ + 256 / self.scale_, :][resize_img == 0] = 0

        io.imsave(os.path.join(mask_path, 'tif1.png'),
                  big.astype(np.float) / 255)
        print 'semantic2whole done'
Beispiel #2
0
def process(mask, min_size=100):
    mask = label(
        hole_fill(remove_small_objects(mask.astype(np.bool), min_size)))
    mask = regionprops(mask)
    mask_coord = [i.centroid for i in mask]
    return mask_coord
Beispiel #3
0
def semantic2xml(label, doc, Annotations):
    stride = 36  # set stride 36
    using_level = 0  # 0: max level; 1
    scale_ = 4
    # mrophology operation
    small_like = np.zeros_like(thumbnail_mask)
    small_like[np.where(thumbnail_mask == label)] = 1
    shape = (3, 3)
    kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, shape)
    image_open = cv2.morphologyEx(np.array(small_like), cv2.MORPH_OPEN, kernel)
    image_close = cv2.morphologyEx(image_open, cv2.MORPH_CLOSE, kernel)
    image_open = cv2.morphologyEx(image_close, cv2.MORPH_OPEN, kernel)
    result = cv2.morphologyEx(image_open, cv2.MORPH_CLOSE, kernel)
    result = cv2.morphologyEx(
        result, cv2.MORPH_DILATE,
        cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5)))
    s_m, s_n = result.shape
    result = remove_small_objects(result.astype(np.bool), 200)
    result = hole_fill(result)
    #
    rows = np.where(result)[0]
    columns = np.where(result)[1]
    wsi = OpenSlide(os.path.join(img_WSI_dir, 'test.svs'))
    big = np.zeros((s_m * stride / scale_ + 256 / scale_,
                    s_n * stride / scale_ + 256 / scale_))
    # get the original area
    for idx, i in enumerate(rows):
        print idx, '/', len(rows)
        img = np.array(
            wsi.read_region((columns[idx] * stride * 2**using_level,
                             i * stride * 2**using_level), using_level,
                            (stride, stride)))[..., 0:-1]
        resize_img = cv2.resize(img, (stride / scale_, stride / scale_))
        big[i * stride / scale_:(i + 1) * stride / scale_,
            columns[idx] * stride / scale_:(columns[idx] + 1) * stride /
            scale_] = np.ones(resize_img.shape[0:-1])

    # get rid of other area
    segpath = '/media/zzr/Data/skin_xml/semantic_result/test.svs'
    contours = measure.find_contours(result, 0.5)
    for i in xrange(len(contours)):
        for j in range(len(contours[i])):
            y = int(contours[i][j, 0] * stride * 2**using_level)
            x = int(contours[i][j, 1] * stride * 2**using_level)
            img = io.imread(
                os.path.join(segpath,
                             str(x) + '_' + str(y) + '_seg.png'))
            resize_img = cv2.resize(img, (256 / scale_, 256 / scale_))
            y_ = y / (scale_ * 2**using_level)
            x_ = x / (scale_ * 2**using_level)
            big[y_:y_ + 256 / scale_,
                x_:x_ + 256 / scale_][resize_img == 0] = 0

    # get xml
    big = remove_small_objects(big.astype(np.bool), 1000)
    big = hole_fill(big)
    contours = measure.find_contours(big, 0.5)
    for i in xrange(len(contours)):
        Annotation = doc.createElement("Annotation")
        Annotation.setAttribute("Name", 'Semantic_' + str(i))
        Annotation.setAttribute("Type", "Polygon")
        Annotation.setAttribute("PartOfGroup", "None")
        Annotation.setAttribute("Color", "#aa5500")
        Annotations.appendChild(Annotation)
        Coordinates = doc.createElement("Coordinates")
        Annotation.appendChild(Coordinates)
        for j in range(len(contours[i])):
            y = int(contours[i][j, 0] * scale_) * 2**using_level
            x = int(contours[i][j, 1] * scale_) * 2**using_level
            Coordinate1 = doc.createElement("Coordinate")
            Coordinate1.setAttribute("Order", str(j))
            Coordinate1.setAttribute("Y", str(y))
            Coordinate1.setAttribute("X", str(x))
            Coordinates.appendChild(Coordinate1)

    return doc
Beispiel #4
0
    def semantic2xml(self, resultFile=None, seg_path=None):
        """

        :param resultFile:
        :param seg_path: the result of segnet
        :return:
        """
        s_m, s_n = self.result.shape
        self.result = hole_fill(
            remove_small_objects(self.result.astype(np.bool), 200))
        rows = np.where(self.result)[0]
        columns = np.where(self.result)[1]
        big = np.zeros((s_m * self.stride / self.scale_ + 256 / self.scale_,
                        s_n * self.stride / self.scale_ + 256 / self.scale_))
        # get the original area
        for idx, i in enumerate(rows):
            # print idx, '/', len(rows)
            big[i * self.stride / self.scale_:(i + 1) * self.stride /
                self.scale_,
                columns[idx] * self.stride / self.scale_:(columns[idx] + 1) *
                self.stride / self.scale_] = np.ones(
                    [self.stride / self.scale_, self.stride / self.scale_])

        # get rid of other area
        contours = measure.find_contours(self.result, 0.5)
        for i in xrange(len(contours)):
            for j in range(len(contours[i])):
                y = int(contours[i][j, 0] * self.stride * 2**self.using_level)
                x = int(contours[i][j, 1] * self.stride * 2**self.using_level)
                img = io.imread(
                    os.path.join(seg_path,
                                 str(x) + '_' + str(y) + '_seg.png'))
                resize_img = cv2.resize(img,
                                        (256 / self.scale_, 256 / self.scale_))
                y_ = y / (self.scale_ * 2**self.using_level)
                x_ = x / (self.scale_ * 2**self.using_level)
                big[y_:y_ + 256 / self.scale_,
                    x_:x_ + 256 / self.scale_][resize_img == 0] = 0

        # get xml
        big = hole_fill(remove_small_objects(big.astype(np.bool), 1000))
        # io.imsave('/media/zzr/Data/skin_xml/mask_result/tif/tif_3.png', big.astype(np.float))   # binary map
        contours = measure.find_contours(big, 0.5)
        # np.save('contours.npy', contours)
        if resultFile:
            doc = minidom.Document()
            ASAP_Annotation = doc.createElement("ASAP_Annotations")
            doc.appendChild(ASAP_Annotation)
            Annotations = doc.createElement("Annotations")
            ASAP_Annotation.appendChild(Annotations)
            for i in xrange(len(contours)):
                Annotation = doc.createElement("Annotation")
                Annotation.setAttribute("Name", "_" + str(i))
                Annotation.setAttribute("Type", "Polygon")
                Annotation.setAttribute("PartOfGroup", "None")
                Annotation.setAttribute("Color", "#F4FA58")
                Annotations.appendChild(Annotation)
                Coordinates = doc.createElement("Coordinates")
                Annotation.appendChild(Coordinates)
                for j in range(len(contours[i])):
                    y = int(
                        contours[i][j, 0] * self.scale_) * 2**self.using_level
                    x = int(
                        contours[i][j, 1] * self.scale_) * 2**self.using_level
                    Coordinate1 = doc.createElement("Coordinate")
                    Coordinate1.setAttribute("Order", str(j))
                    Coordinate1.setAttribute("Y", str(y))
                    Coordinate1.setAttribute("X", str(x))
                    Coordinates.appendChild(Coordinate1)

            f = file(resultFile, "w")
            doc.writexml(f)
            f.close()

        print 'semantic2xml done'
        return big