Ask Your Question

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 -0500

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 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), (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), (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.newDepthArray = cv2.bitwise_and(self.oldDepthArray, self.oldDepthArray, mask = boundary_circle)
self.newImage = self.bridge.cv2_to_imgmsg(self.newDepthArray, encoding="passthrough")

image description

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

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

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.

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower


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

Seen: 43 times

Last updated: Sep 27