avatarAmit Chauhan

Summary

The webpage provides a tutorial on using the OpenCV library in Python for object segmentation and counting in blood images through a series of image processing steps.

Abstract

The article delves into the application of OpenCV, an open-source computer vision library, for the purpose of segmenting and counting objects within a blood image. It outlines a five-step process that includes reading the image, applying de-noising and blur filters for image clarity, converting the image into different color spaces, thresholding for binary image creation, and finally, finding and drawing contours around objects to count them. The tutorial emphasizes the importance of image pre-processing techniques such as de-noising, color space conversion, and morphological operations to enhance object detection and counting. It also provides code snippets and visual examples at each step to illustrate the process, and concludes by asserting that the article offers a foundational understanding of image pre-processing for object detection tasks.

Opinions

  • The author believes that the OpenCV library is beneficial due to its ability to work with NumPy arrays and integrate with other libraries.
  • The article suggests that choosing the right color space and channel is crucial for effective binary image creation, implying that the H-channel in HSV color space is particularly useful for the task at hand.
  • The use of morphological operations like erosion is presented as an effective method for noise reduction in binary images.
  • The author expresses confidence in the thresholding technique used, specifically the Otsu's method, for segmenting objects in the image.
  • The article implies that the process of drawing contours and labeling objects is a reliable way to enumerate distinct objects in an image.
  • The author encourages readers to reach out on LinkedIn and Twitter for further engagement, indicating a willingness to discuss and share more knowledge on the subject.

OpenCV: Count of Objects in Blood Image with Python

Image processing concept with OpenCV library

The photo by Author

In this article, we will discuss object segmentation with the help of the OpenCV library and pre-processing techniques in image processing. We will try to mark the contours with the number to get the total number of objects.

What is OpenCV?

It is an open-source library to process images and videos for various applications in real life, like segmentation, object detection, and many more. The main benefit of the OpenCV library is working on NumPy arrays that can be work with a different library.

The process to count the objects in an image is done with the below process:

Step1: Importing the libraries and reading the input image with the help of these libraries.

Importing the necessary libraries

import numpy as np
import imutils
import cv2

The “numpy” library is an open-source that contains data in multi-dimensional and matrix array form and processing these array data. The “imutils” is used to manipulate the image with basic functions like rotation, resizing, etc. The “cv2” is a computer vision library.

Reading the input image of blood object.

image = cv2.imread('blood.jpg') #reads the image
A photo by Author

Step 2: Using De-noising and Blur filters to get a more clear image for processing.

We used a de-noising technique to remove the noise from the raw image for further processing. The de-noise is an image pre-processing process in computer vision.

dst = cv2.fastNlMeansDenoisingColored(image, None, 10, 10, 7, 15)
#the meaning of parameters given
p1 = 10: size of pixels to compute weights of the image
p2 = 10: to compute the weighted average
p3 = 7: filter strength for luminescence
p4 = 15: filter strength for color component

When we read images in OpenCV it reads in BGR format and we need to change in RGB format for further processes.

rgb_image = cv2.cvtColor(dst, cv2.COLOR_BGR2RGB)

we can also save the image with the imwrite function as shown below:

cv2.imwrite(‘RGB_image.jpg’,rgb_image)
A photo by Author

Now, we will use the median blur function to remove the salt-and-pepper noise. T working of this median blur to take the median of the filter window and replace the center pixel value with the median value pixel.

new_image = cv2.medianBlur(rgb_image,5)
cv2.imwrite('median_blur.jpg',new_image)
The median blur image. A photo by Author

Step 3: Converting and Splitting of image into different color spaces

We have to convert the image into a particular color space to know the different channel of that color space. Here, we are taking a HSV color space and splitting in H,S and V channel.

hsv_image = cv2.cvtColor(new_image, cv2.COLOR_RGB2HSV)
h, s, v = cv2.split(hsv_image)
cv2.imwrite(‘H.jpg’,h)

The view of the Different channels is shown below:

Three-channel images of HSV image. A photo by Author

Step 4: Threshold the H — Channel image for binary image

This step is an important step to choose the perfect image to get to be a binary image for object detection. In the above images, we see that H — Channel image shows a pretty good sign to be a binary image because the pixel can be differentiating into two classes.

ret,th1=cv2.threshold(h,180,255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)
cv2.imwrite('Binary_image.jpg',th1)
Binary image. A photo by Author

Now, we want the red object in the RGB image, so we try to use the morphological operation to reduce the noise.

kernel = np.ones((5,5), dtype = "uint8")/9
bilateral = cv2.bilateralFilter(th1, 9 , 75, 75)
erosion = cv2.erode(bilateral, kernel, iterations = 6)
cv2.imwrite('mask_erosion.jpg', erosion)

Here, we use the erode function to reduce the small white noise pixels from the binary image.

Noise-free binary image. A photo by Author

If you want to use the different method to remove the noise then check this article below:

Step 5: To find the number of objects in the image and draw contour

In this step we try to draw the boundary or contour on the object and finding the total number of objects in the image.

# find contours in the thresholded image
cnts = cv2.findContours(th1.copy(), cv2.RETR_EXTERNAL,
 cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
print("[INFO] {} unique contours found".format(len(cnts)))
#output:
[INFO] 6 unique contours found
# loop over the contours
for (i, c) in enumerate(cnts):
 # draw the contour
 ((x, y), _) = cv2.minEnclosingCircle(c)
 cv2.putText(image, "#{}".format(i + 1), (int(x) - 10, int(y)),
  cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 255), 2)
 cv2.drawContours(image, [c], -1, (0, 255, 0), 2)
# show the output image
cv2.imshow("Image", image)
cv2.waitKey(0)
cv2.imwrite('Contour_Image.jpg',image)
Detected object image. A photo by Author

Conclusion

This article will give you a basic overview of the image pre-processing techniques.

I hope you like the article. Reach me on my LinkedIn and twitter.

Recommended Articles

1. NLP — Zero to Hero with Python 2. NumPy: Linear Algebra on Images 3. Exception Handling Concepts in Python 4. Principal Component Analysis in Dimensionality Reduction with Python 5. Fully Explained K-means Clustering with Python 6. Fully Explained Linear Regression with Python 7. Fully Explained Logistic Regression with Python 8. Differences Between concat(), merge() and join() with Python 9. Data Wrangling With Python — Part 1 10. Confusion Matrix in Machine Learning

Python
Programming
Computer Vision
Artificial Intelligence
Deep Learning
Recommended from ReadMedium