Polyrogue
Polyrogue

Reputation: 15

How to detect the presence of a simple but translucent shape?

I'm quite new to OpenCV and CV as a whole. I'm trying to create a state detector that allows me to extract information from the screen in a certain game. This feature pre-processing is almost complete, but up until now I have used colour as the main way of extracting useful information. I wish to capture as to whether this red shield shape is present in the screen: (https://cdn.discordapp.com/attachments/537774605365149716/537786159103737869/unknown.png)

The shield is always shaped like this, but is translucent and so the colour test cannot be applied.

I have not yet tried a solution, as after much searching and learning, for this one problem I'm not sure where to start.

I have an image processedImage, and would like to extract a simple bool describing whether this shape is present or not.

Any help would be greatly appreciated.

Thanks.

Upvotes: 1

Views: 441

Answers (2)

Shawn Mathew
Shawn Mathew

Reputation: 2337

Since you know that the shield is always red, convert the image into HSV and use the hue channel to threshold/hysteresis the image and get the red region

img = cv2.imread(img_path)

hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

m = cv2.split(hsv)
plt.imshow(m[1])

Plotting the Saturation channel here just for visualization purposes. Once you get this, perhaps you can use some sort template matching algorithm to get the location.

enter image description here

Upvotes: 0

Reedinationer
Reedinationer

Reputation: 5774

I think the best solution would be to detect the edges of the shield image. You can do this through some image processing techniques at:

https://docs.opencv.org/3.0-beta/doc/py_tutorials/py_imgproc/py_table_of_contents_imgproc/py_table_of_contents_imgproc.html#py-table-of-content-imgproc

Perhaps something like:

edges = cv2.Canny(img,100,200) #img is your image

Would work for you, but if using the color image doesn't work you can try converting to greyscale first with something like:

grey = cv2.cvtColor(screen, cv2.COLOR_RGB2GRAY) #screen is your image

And then do the edge detection algorithm. Obviously once you find the shape you would have to define it (likely through tweaking the Canny parameters, or doing some other technique like HoughLines), but to do this it would be helpful first to think about things like

  1. Is the shield always in the same area of the screen?
  2. Is it always the same size?
  3. Are there any other defining characteristics you can think of?

Then craft your algorithm around these assumptions! I think you would want to define a function and have it return True or return False and then you can call this to "get a boolean of if the image is present" Because you're not asking an explicit question I think this answer will be sufficient, but feel free to drop a comment if it needs elaboration! Otherwise, if this answered your question, then to accept it you can click the check mark next to the post :)

Upvotes: 0

Related Questions