Exemple #1
0
def blob_detector2(image, stepSize, windowSize):
    blobs = []

    print("loading model...")
    model = load_model(
        '/Users/2020shatgiskessell/Desktop/New_Mole_Detector/Mole_Detector_1_3/my_mole_model_2.h5'
    )

    for y in range(0, image.shape[0], stepSize):
        for x in range(0, image.shape[1], stepSize):
            # identify moles on  current window
            #blobs.append(identify_moles(image[y:y + windowSize[1], x:x + windowSize[0]]))
            roi = image[y:y + windowSize[1], x:x + windowSize[0]]
            try:
                roi = cv2.resize(roi, (8, 8))
            except Exception:
                continue
            roi = np.expand_dims(roi, axis=2)
            roi = np.expand_dims(roi, axis=0)
            pred = model.predict(roi)
            pred = pred.round()
            if int(pred[0][0]) == 1:
                blob = Blob()
                blob.x = x
                blob.y = y
                #blob.matrix = component_matrix
                blob.roi = roi
                blobs.append(blob)
    return blobs
Exemple #2
0
def compute_stats(num_labels, labels, stats, centroids, img, x_i, y_i, i, og):
    blobs = []
    #start = timeit.default_timer()
    #get component centroid coordinates
    x, y = centroids[i]
    #compute statistics
    # cv2.CC_STAT_LEFT The leftmost (x) coordinate which is the inclusive start of the bounding box in the horizontal direction.
    # cv2.CC_STAT_TOP The topmost (y) coordinate which is the inclusive start of the bounding box in the vertical direction.
    # cv2.CC_STAT_WIDTH The horizontal size of the bounding box
    # cv2.CC_STAT_HEIGHT The vertical size of the bounding box
    # cv2.CC_STAT_AREA The total area (in pixels) of the connected component

    width = stats[i, cv2.CC_STAT_WIDTH]
    height = stats[i, cv2.CC_STAT_HEIGHT]
    if height > 20:
        return None, None, None, None, None, None
    radius = (height + width) / 2
    area = stats[i, cv2.CC_STAT_AREA]

    #these are the top left x, y coordinates = ONLY TO BE USED FOR GETTING ROI
    x_l = stats[i, cv2.CC_STAT_LEFT]
    y_l = stats[i, cv2.CC_STAT_TOP]

    #compute line
    #slope, y_int= compute_lobf(x,y,x_l,y_l)

    #stop = timeit.default_timer()
    #print('Time to calculate properties: ', stop - start)

    #remove everything except for component i to create isolated component matrix
    #get connected component roi
    roi = og[y_l - 1:y_l + height + 1, x_l - 1:x_l + width + 1]
    #stop = timeit.default_timer()
    #print('Time to create cm and roi: ', stop - start)

    #----------------MEASURES-------------------------------------------------------------------
    #radius = (height + width)/2

    #compute more statistics related to roundness
    radius = np.sqrt((area / np.pi))
    formfactor = compute_formfactor(radius, area)
    bounding_box_area_ratio = area / (height * width)
    if height > width:
        roundness = compute_roundness(height, radius, area)
        aspect_ratio = height / width
    else:
        roundness = compute_roundness(width, radius, area)
        aspect_ratio = width / height

    #print('Time to calculate heuristic properties: ', stop - start)
    # if x >300 and y < 150:
    #     print ("(" + str(x) + ","+str(y)+") -> area ratio: " + str(area/(height*width)))
    #print ("(" + str(x) + ","+str(y)+") -> " + "radius: " + str(radius) + ", formfactor: " + str(formfactor) + ", roundness: " + str(roundness) + ", aspect ratio: " + str(aspect_ratio))
    #print ("(" + str(x) + ","+str(y)+")")
    #print ("\n")

    #calculates line of best fit and error
    try:
        cord1, cord2, error = compute_lobf(roi, x_l * y_l)
        x1, y1 = cord1
        x2, y2 = cord2
    except TypeError:
        print("cant calculate line of best fit")
        error = 0

    cv2.imwrite(
        os.path.join(
            "/Users/2020shatgiskessell/Desktop/New_Mole_Detector/ANN_Images_Fiverr",
            "roi19" + str(i) + ".png"), roi)

    #COMMENT OUT WHEN COLLECTING ANN DATA!!!!!
    #if the error is below 16, (the line of best fit closely matches connect component) return none
    if error < 16:
        return None, None, None, None, None, None

    # print ("(" + str(x1) + ","+str(y1)+")")
    # print ("(" + str(x2) + ","+str(y2)+")")
    # print ("\n")

    #next step: calculate residuals and do some sort of analysis

    #try  and bounding_box_area_ratio >= 0.5
    if roundness > 0.2 and 0.9 < aspect_ratio < 3 and 1.1 >= formfactor > 0.9:
        blob = Blob()
        blob.radius = radius
        blob.x = x + x_i
        blob.y = y + y_i
        #blob.matrix = component_matrix
        blob.roi = roi
        blobs.append(blob)

    return blobs
Exemple #3
0
def blob_detector(img, x_i, y_i):
    og = img.copy()
    img = cv2.Canny(img, 100, 200)

    blobs = []
    #get connected components of fg
    #print ("calculating connected components...")
    output = cv2.connectedComponentsWithStats(img, 4, cv2.CV_32S)
    # Get the results
    # The first cell is the number of labels
    #print ("calculating statistics...")
    num_labels = output[0]
    # The second cell is the label matrix
    labels = output[1]
    # The third cell is the stat matrix
    stats = output[2]
    # The fourth cell is the centroid matrix
    centroids = output[3]
    #print (centroids)
    #print(np.where(labels == 2))
    roundnesses = []
    aspect_ratios = []
    formfactors = []
    errors = []
    rois = []
    #imshow_components(labels, og)
    #print ("calculating connected components properties...")
    # with concurrent.futures.ThreadPoolExecutor(5) as executor:
    #     future_moles = {executor.submit(compute_stats, num_labels,labels, stats, centroids, img, x_i, y_i, i, og):i for i in range(num_labels)}
    #     for future in concurrent.futures.as_completed(future_moles):
    #         found_blobs, roundnesses1, aspect_ratios1, formfactors1, errors1, roi1 = future.result()
    #         if found_blobs != None:
    #             blobs.extend(found_blobs)
    #             roundnesses.append(roundnesses1)
    #             aspect_ratios.append(aspect_ratios1)
    #             formfactors.append(formfactors1)
    #             errors.append(errors1)
    #             rois.append(roi1)
    print("loading model...")
    model = load_model(
        '/Users/2020shatgiskessell/Desktop/New_Mole_Detector/Mole_Detector_1_3/my_mole_model_2.h5'
    )
    for i in range(num_labels):
        #start = timeit.default_timer()
        #get component centroid coordinates
        x, y = centroids[i]
        #compute statistics
        # cv2.CC_STAT_LEFT The leftmost (x) coordinate which is the inclusive start of the bounding box in the horizontal direction.
        # cv2.CC_STAT_TOP The topmost (y) coordinate which is the inclusive start of the bounding box in the vertical direction.
        # cv2.CC_STAT_WIDTH The horizontal size of the bounding box
        # cv2.CC_STAT_HEIGHT The vertical size of the bounding box
        # cv2.CC_STAT_AREA The total area (in pixels) of the connected component

        width = stats[i, cv2.CC_STAT_WIDTH]
        height = stats[i, cv2.CC_STAT_HEIGHT]
        if height > 20:
            continue
        radius = (height + width) / 2
        area = stats[i, cv2.CC_STAT_AREA]

        #these are the top left x, y coordinates = ONLY TO BE USED FOR GETTING ROI
        x_l = stats[i, cv2.CC_STAT_LEFT]
        y_l = stats[i, cv2.CC_STAT_TOP]

        #compute line
        #slope, y_int= compute_lobf(x,y,x_l,y_l)

        #stop = timeit.default_timer()
        #print('Time to calculate properties: ', stop - start)

        #remove everything except for component i to create isolated component matrix
        #get connected component roi
        roi = og[y_l - 1:y_l + height + 1, x_l - 1:x_l + width + 1]
        try:
            roi = cv2.resize(roi, (8, 8))
        except Exception:
            continue
        roi = np.expand_dims(roi, axis=2)
        roi = np.expand_dims(roi, axis=0)
        pred = model.predict(roi)
        pred = pred.round()
        if int(pred[0][0]) == 1:
            print("found blob")
            blob = Blob()
            blob.radius = radius
            blob.x = x + x_i
            blob.y = y + y_i
            #blob.matrix = component_matrix
            blob.roi = roi
            blobs.append(blob)

        # #----------------MEASURES-------------------------------------------------------------------
        #
        # #compute more statistics related to roundness
        # radius = np.sqrt((area/np.pi))
        # formfactor = compute_formfactor (radius, area)
        # bounding_box_area_ratio = area/(height*width)
        # if height > width:
        #     roundness = compute_roundness (height, radius, area)
        #     aspect_ratio = height/width
        # else:
        #     roundness = compute_roundness (width, radius, area)
        #     aspect_ratio = width/height
        #
        # #calculates line of best fit and error
        # try:
        #     cord1, cord2, error = compute_lobf(roi, x_l*y_l)
        #     x1,y1 = cord1
        #     x2,y2 = cord2
        # except TypeError:
        #     print ("cant calculate line of best fit")
        #     error = 0
        # #COMMENT OUT WHEN COLLECTING ANN DATA!!!!!
        # if error < 16:
        #     continue
    return blobs