2

I'm trying my hand at locating contours in opencv, and am using an image with a transparent background. After loading the image into memory and showing the image, the transparent background has been recolored into black and white rectangular shapes surrounding the focus of the picture.

image = cv.imread('C:/Users/H/Desktop/overhead.png')

cv.namedWindow('image', cv.WINDOW_NORMAL)
cv.imshow('image', image)
cv.waitKey(0)

Is the code that I'm currently using

Instead of having black pixels surrounding the image, there are several large white blocks(which are being detected as a contour).

Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
Harrison
  • 31
  • 3

1 Answers1

1

Convert White Pixels to Black in OpenCV python

I found an appropriate solution.

However now the ~circular shape in the upper right is not being detected. All 3 rectangles are found. tresh

gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
blurred = cv.GaussianBlur(gray, (5, 5), 0)
thresh = cv.threshold(blurred, 103, 255, cv.THRESH_BINARY)[1]

cnts = cv.findContours(thresh.copy(), cv.RETR_EXTERNAL,
cv.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)

# loop over the contours
for c in cnts:
# compute the center of the contour
M = cv.moments(c)
cX = int(M["m10"] / M["m00"])
cY = int(M["m01"] / M["m00"])

# draw the contour and center of the shape on the image
cv.drawContours(image, [c], -1, (0, 255, 0), 2)
cv.circle(image, (cX, cY), 7, (255, 255, 255), -1)
cv.putText(image, "center", (cX - 20, cY - 20),
cv.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2)

# show the image
cv.namedWindow('image', cv.WINDOW_NORMAL)
cv.imshow('image', image)
cv.waitKey(0)
Harrison
  • 31
  • 3