Contours and Challenges

I thought of using contours to identify a person in a picture but the challenge with Dr Mick’s picture is the background noises using the code below:

#Opening the images
I = cv2.imread("DrMick.jpg")
Original = I.copy()
# Accessing pixel's b(blue) g(green) r(red) values:
b,g,r = I[1,1]
# Setting the lower and upper range values
lowerR = (b-25, g-25, r-25)
upperR = (b+50, g+50, r+50)
# Getting the values within this range
B = cv2.inRange(I, lowerR, upperR)
# subtract them from 255 so we would not go out of range
B=255-B
# Finding Contour
B, contours,_ = cv2.findContours(B, mode = cv2.RETR_EXTERNAL, method = cv2.CHAIN_APPROX_NONE)
# Draw contour
I = cv2.drawContours(I, contours, contourIdx = -1, color = (0,0,255), thickness = 5)
# Show the Original Image
cv2.imshow("Original", Original)
# Show the Result Image
cv2.imshow("Result", I)
cv2.waitKey(0)

The code will work in the following steps:

  1. Open Dr.Micks image, make a copy of it.
  2. Accessing pixel’s b(blue) g(green) r(red) values.
  3. Setting the lower and upper range values.
  4. Getting the values within this range, subtract them from 255 so we would not go out of range.
  5. Find the contour, draw a contour around the person.
  6. Show the original and the result.

The Original image and the Result image side by side looked like this:

DrMickResults

The problem with this method is that it is not effective with images that have background noise in them, but if we passed an image with a white background the result is far more satisfying:

Man2Results

Therefore, if we said that our design requires that the image passed into it to have a solid white background, that will solve the background noise problem.

Leave a comment