pythonopencvmachine-learningimage-processingk-means

How does use MASK R_CNN to extract exact region in image by OpenCV?


I have a medical project that want extract a special part (Conjuctiva Puplpebral) my just wanted region

to extract of eye image without understanding of its coordinates automatically not manually also the coordinate of this wanted region changing because I capture from many patients and I think must find the shape of it. my goal is determining anemia from non anemia by counting red pixel in conjuctiva pulpubral. I use masking method(k means) to do that, but I wish could do a way that first extract conjuctiva pulpebral directly, then use k means to mask the image and find because my result will be more accurate. When I use k mean from image segmentation, I find another and overlapping red pixel that ruin my accuracy.

k_means. I also hear about machine learning to use but but after doing machine learning to find near region in images of my patients, then I will need to extract conjuctival pulpabral. So I need to codes to extract only and only conjuctival pulpubral.

I try k_means and kernel but add another unwanted red pixel. I hear about instance segmentation and MASK RCNN. you assume I have my just wanted region as see its image above as data for CNN so how use it for my project.

import cv2
import numpy as np

# Read the image
image = cv2.imread('c:/users/stk/desktop/d.png')

# Convert the image to HSV
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)

# Define the lower and upper bounds for the red color
lower_red = np.array([0, 120, 70])
upper_red = np.array([10, 255, 255])

# Create a mask for the red color
mask1 = cv2.inRange(hsv, lower_red, upper_red)

# Define the lower and upper bounds for the red color
lower_red = np.array([170, 120, 70])
upper_red = np.array([180, 255, 255])

# Create a mask for the red color
mask2 = cv2.inRange(hsv, lower_red, upper_red)

# Combine the two masks
mask = mask1 + mask2

# Create a kernel for morphological operations
kernal = np.ones((5, 5), np.uint8)

# Perform morphological operations
mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernal)
mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernal)

# Apply the mask to the original image
result = cv2.bitwise_and(image, image, mask = mask)

# Save the result
cv2.imwrite('extracted_red_object.png', result)

# Display the result
cv2.imshow('EXTRACTED RED OBJECT', result)
cv2.waitKey(0)
cv2.destroyAllWindows()

Solution

  • I thought that i need to mask the images previously but has many errors. i know my problem by help of dears that i need to a CNN model and machine learning. thanks them. i assume that the shape in my project is a circle instead of conjuctival pulpabral. so base of my codes is like this even has many distance from it.

    import os
    from mrcnn.config import Config
    from mrcnn.model import MaskRCNNModel
    # prepare training dataset take circle instead of con_pal
    dataset_dir = 'path/to/dataset'
    circle_dir = os.path.join(dataset_dir, 'circle')
    no_circle_dir = os.path.join(dataset_dir, 'no_circle')
    
    #prepare the training enviroment
    class CircleConfig(Config):
        NAME = "circle"
        NUM_CLASSES = 2  # Background + circle
        IMAGE_MIN_DIM = 512
        IMAGE_MAX_DIM = 512
        RPN_ANCHOR_SCALES = (8, 16, 32, 64, 128)
        TRAIN_ROIS_PER_IMAGE = 32
        STEPS_PER_EPOCH = 100
        VALIDATION_STEPS = 50
        DETECTION_MIN_CONFIDENCE = 0.7
    
    #train the MASK RCNN model
    model = MaskRCNNModel(mode="training", config=CircleConfig(), model_dir="./")
    model.load_dataset(dataset_dir)
    model.train()
    
    #save the trained wieghts
    model.save_weights("mask_rcnn_circle.h5")