Exemple #1
0
def finding_lungs_non_DL_approach_and_save(image, file_name):
    # print(row.columns.values)
    # file_name = row[0]
    # print("line is", file_name, image.shape)
    # when reading from txt there is something in the end so we need to eliminate that
    # image = cv2.imread(os.path.join("Z:\\", "Data", "ChestXRay", "images", file_name), 0)

    img_height = image.shape[0]
    img_width = image.shape[1]
    # Get both lungs image. It uses HOG as main method,
    # but if HOG found nothing it uses HAAR or LBP.
    found_lungs = lf.get_lungs(image)

    # this can be written in a more concise way but we just keep it a bit redundant for easy reading
    if found_lungs is not None and found_lungs.shape[
            0] > img_height / 2 and found_lungs.shape[1] > img_width / 2:
        # print(found_lungs.shape)
        found_lungs_resized = cv2.resize(found_lungs, im_shape)
        # cv2.imshow(file_name, found_lungs)
        # code = cv2.waitKey(0)
        cv2.imwrite(os.path.join(out_folder_matched_img, file_name),
                    found_lungs_resized)
        return True
    else:
        cv2.imwrite(os.path.join(out_folder_mismatched_image, file_name),
                    cv2.resize(image, im_shape))
        return False
Exemple #2
0
def lung_finder(data):
    ''' The purpose of below is to find 
    just the lungs from the chest 
    scans. '''
    comb = []
    for i in data:
        found_lungs = lf.get_lungs(i)
        
        if found_lungs is not None:
            comb.append(found_lungs)
    
    return comb
    def extract_lungs(self, image_group):
        """  use lung-finder to extract lung region
        """
        new_group = []
        for img in image_group:
            found_lungs = lf.get_lungs(img)
            if found_lungs is not None and 0 not in found_lungs.shape:
                new_group.append(found_lungs)
            else:
                new_group.append(img)

        return new_group
Exemple #4
0
def scan(argv):
    path_to_folder, use_labels = parse_argv(argv)
    walks = list(os.walk(path_to_folder))
    i = 0

    while i < len(walks):
        path, directories, files = walks[i]
        files = [file for file in files if not file[0] == "."]
        j = 0

        while j < len(files):
            file = files[j]

            _, extension = os.path.splitext(file)

            if extension == ".dcm" and dicom_support:
                parsed = dicomparser.DicomParser(path + os.sep + file)
                image = np.array(parsed.GetImage(), dtype=np.uint8)

                if parsed.GetImageData(
                )["photometricinterpretation"] == "MONOCHROME1":
                    image = 255 - image

                image = cv2.equalizeHist(image)
                image = cv2.medianBlur(image, 3)
            elif extension in [
                    ".bmp", ".pbm", ".pgm", ".ppm", ".sr", ".ras", ".jpeg",
                    ".jpg", ".jpe", ".png", ".tiff", ".tif"
            ]:
                image = cv2.imread(path + os.sep + file, 0)
                image = cv2.equalizeHist(image)
                image = cv2.medianBlur(image, 3)
            else:
                j += 1
                continue

            scaled_image = proportional_resize(image, 512)
            right_lung_hog_rectangle = lf.find_right_lung_hog(scaled_image)
            left_lung_hog_rectangle = lf.find_left_lung_hog(scaled_image)
            right_lung_lbp_rectangle = lf.find_right_lung_lbp(scaled_image)
            left_lung_lbp_rectangle = lf.find_left_lung_lbp(scaled_image)
            right_lung_haar_rectangle = lf.find_right_lung_haar(scaled_image)
            left_lung_haar_rectangle = lf.find_left_lung_haar(scaled_image)
            color_image = cv2.cvtColor(scaled_image, cv2.COLOR_GRAY2BGR)

            if right_lung_hog_rectangle is not None:
                x, y, width, height = right_lung_hog_rectangle
                cv2.rectangle(color_image, (x, y), (x + width, y + height),
                              (59, 254, 211), 2)
                if use_labels:
                    cv2.putText(color_image, "HOG Right", (x + 5, y + 15),
                                cv2.FONT_HERSHEY_SIMPLEX, 0.5, (59, 254, 211),
                                1)

            if left_lung_hog_rectangle is not None:
                x, y, width, height = left_lung_hog_rectangle
                cv2.rectangle(color_image, (x, y), (x + width, y + height),
                              (59, 254, 211), 2)
                if use_labels:
                    cv2.putText(color_image, "HOG Left", (x + 5, y + 15),
                                cv2.FONT_HERSHEY_SIMPLEX, 0.5, (59, 254, 211),
                                1)

            if right_lung_lbp_rectangle is not None:
                x, y, width, height = right_lung_lbp_rectangle
                cv2.rectangle(color_image, (x, y), (x + width, y + height),
                              (130, 199, 0), 2)
                if use_labels:
                    cv2.putText(color_image, "LBP Right", (x + 5, y + 15),
                                cv2.FONT_HERSHEY_SIMPLEX, 0.5, (130, 199, 0),
                                1)

            if left_lung_lbp_rectangle is not None:
                x, y, width, height = left_lung_lbp_rectangle
                cv2.rectangle(color_image, (x, y), (x + width, y + height),
                              (130, 199, 0), 2)
                if use_labels:
                    cv2.putText(color_image, "LBP Left", (x + 5, y + 15),
                                cv2.FONT_HERSHEY_SIMPLEX, 0.5, (130, 199, 0),
                                1)

            if right_lung_haar_rectangle is not None:
                x, y, width, height = right_lung_haar_rectangle
                cv2.rectangle(color_image, (x, y), (x + width, y + height),
                              (245, 199, 75), 2)
                if use_labels:
                    cv2.putText(color_image, "HAAR Right", (x + 5, y + 15),
                                cv2.FONT_HERSHEY_SIMPLEX, 0.5, (245, 199, 75),
                                1)

            if left_lung_haar_rectangle is not None:
                x, y, width, height = left_lung_haar_rectangle
                cv2.rectangle(color_image, (x, y), (x + width, y + height),
                              (245, 199, 75), 2)
                if use_labels:
                    cv2.putText(color_image, "HAAR Left", (x + 5, y + 15),
                                cv2.FONT_HERSHEY_SIMPLEX, 0.5, (245, 199, 75),
                                1)

            cv2.imshow("lungs-finder", color_image)
            found_lungs = lf.get_lungs(scaled_image)

            if found_lungs is not None:
                cv2.imshow("Found lungs", found_lungs)

            code = cv2.waitKey(0)

            while code not in [2, 3, 27, 32]:
                code = cv2.waitKey(0)

            if code == 27:
                exit(0)
            elif code in [3, 32]:
                j += 1
            else:
                if j > 0:
                    j -= 1
                else:
                    if i > 0:
                        i -= 2
        i += 1