Ask Your Question
0

How to color the pixels in an image depending on whether they are inside/outside a zone/circle?

asked 2020-09-26 11:55:55 -0600

Vuro H gravatar image

I am converting an input ROS image to OpenCV, drawing a circle on the image, and removing points outside the circle. I want to be able to color the pixels in the image depending on whether they fall outside or inside the circle (like a zone). I'd like the pixels inside the circle to be red, and the pixels ouside the circle to be green. This image clearly shows what I mean: I am currently getting the images on the left, but would like to get the images on the right.

I am attaching my code below. I have played around with the cv2.circle and cv2.rectangle functions, but have had trouble getting these to work. Thank you for your help!

depth_image = self.bridge.imgmsg_to_cv2(self.myImage, desired_encoding="passthrough") 
depth_image_noNaN = np.nan_to_num(depth_image,0)
self.oldDepthArray = np.array(depth_image_noNaN)
cv2.circle(self.oldDepthArray, (self.myImage.width/2, self.myImage.height/2), 130, (255,0,0), 4)

self.oldDepthArray.resize((self.myImage.height, self.myImage.width))
boundary_circle = np.zeros((self.myImage.height, self.myImage.width), np.uint8)
cv2.circle(boundary_circle, (self.myImage.width/2, self.myImage.height/2), zone_radius, (170,0,0), -1)
self.circleImage = self.bridge.cv2_to_imgmsg(self.oldDepthArray, encoding="passthrough")
self.pubCircleImage.publish(self.circleImage)

self.newDepthArray = cv2.bitwise_and(self.oldDepthArray, self.oldDepthArray, mask = boundary_circle)
self.newImage = self.bridge.cv2_to_imgmsg(self.newDepthArray, encoding="passthrough")
self.pubNewImage.publish(self.newImage)

image description

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2020-09-27 09:11:36 -0600

Use contour finding and bounding rect to get the rectangle of the collection of white pixels. Loop through all pixels in this rectangle. If a pixel is white, compute its distance from the circle center. Change the color of the pixel depending on whether it is less than or greater than he circle radius.

https://docs.opencv.org/3.4/d4/d73/tu...

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2020-09-26 11:55:55 -0600

Seen: 711 times

Last updated: Sep 27 '20