KI-67 IHC staining, counting colored cells in an microscopic image

75 views Asked by At

I am trying to write a python code to count the total of cancerous and normal cells in microscopic images. The tissue is stained with KI-67 IHC staining, which causes the cancerous cells to be brown and the normal cells to be blue. I can't get the code to be specific enough to count every brown cell, and my code is not able to count the blue cells due to brightness problems. Help is much appreciated!

import cv2
import numpy as np
import math
from skimage import io

# Load the image and create a copy for further processing
image = cv2.imread("images/NET1.jpg")
original = image.copy()

# Convert the image to the HSV color space for color analysis
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)

# Define the HSV range to target brown colors
hsv_lower = np.array([0, 0, 0])
hsv_upper = np.array([20, 255, 255])

# Create a binary mask based on the specified HSV range
mask = cv2.inRange(hsv, hsv_lower, hsv_upper)

# Create a kernel for morphological operations
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3))

# Perform morphological opening and closing operations to refine the mask
opening = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel, iterations=1)
close = cv2.morphologyEx(opening, cv2.MORPH_CLOSE, kernel, iterations=2)

# Display the processed mask
io.imshow(close)

# Find contours in the processed binary image
cnts = cv2.findContours(close, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]

# Define parameters for cell counting
minimum_area = 10
average_cell_area = 100
connected_cell_area = 100
cells = 0

# Loop through the contours and count cells based on their area
for c in cnts:
    area = cv2.contourArea(c)
    if area > minimum_area:
        cv2.drawContours(original, [c], -1, (36, 255, 12), 2)
        if area > connected_cell_area:
            cells += math.ceil(area / average_cell_area)
        else:
            cells += 1

# Print the total number of detected cells
print('Cells: {}'.format(cells))

# Display the processed mask and the original image
cv2.imshow('close', close)
cv2.imshow('original', original)

# Wait for user input to exit
cv2.waitKey()

These are the used pictures:

NEC tissue

NET1 tissue

The code processes an image to detect and count cells that match a specified brown color. It loads the image, converts it to the HSV color space, and creates a mask to isolate the brown-colored regions. Morphological operations are applied to refine the mask. Detected cells are counted based on their area, and the results are displayed with the original image. The program waits for user input to exit.

0

There are 0 answers