Search for objects in the image. Part 1

Computer vision is an amazing field that allows computers to see and understand the world through image and video processing. One of the most popular tools for working with computer vision is the OpenCV library. In this article, we will look at how to use OpenCV to recognize objects in an image.

Let’s say we want to find cards from the Fool game online. This is the image we will process.

Step 1: Installing and configuring OpenCV

The first step is to install and configure OpenCV. You can install OpenCV using pip by running the following command:

pip install opencv-python

Step 2: Loading and preprocessing the image

Before we can start recognizing the cards, we need to upload an image of the table with the cards. We use the function cv2.imreadto load the image into a variable image:

image = cv2.imread('table_image.jpg')

We can then convert the image to grayscale and apply a blur to remove the noise:

gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (5, 5), 0)

Step 3: Thresholding

To detect map edges in an image, we use thresholding. Thresholding converts the image to a binary image where each pixel is considered either black or white. We can use the function cv2.threshold for this:

thresholded = cv2.threshold(blurred, 100, 255, cv2.THRESH_BINARY)[1]

Step 4: Finding Outlines

Now we can use the function cv2.findContoursto find contours in an image:

contours, _ = cv2.findContours(thresholded, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

Function cv2.findContours returns a list of contours in the image.

Step 5: Outlining the Found Cards on the Screen

To circle the found cards on the screen, we use the function cv2.drawContours. For example, to outline outlines in green, we can use the following code:

cv2.draw

Contours(image, contours, -1, (0, 255, 0), 2)

This code will outline all found paths in green with a thickness of 2 pixels.

Step 6: Displaying the Outlined Card Image

Finally, we can display the outlined cards image on the screen with the function cv2.imshow:

cv2.imshow('Detected Cards', image)
cv2.waitKey(0)
cv2.destroyAllWindows()

This code will open a window with an image on which the found cards will be circled. Waiting for a key press cv2.waitKey(0) allows the user to preview the image before closing it.

Here is the complete code

import cv2

# Загрузка изображения
image = cv2.imread('test_card.jpg')

# Предобработка изображения
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
thresholded = cv2.threshold(blurred, 100, 255, cv2.THRESH_BINARY)[1]

# Поиск контуров на изображении
contours, _ = cv2.findContours(thresholded, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

# Отображение контуров на изображении
cv2.drawContours(image, contours, -1, (0, 255, 0), 2)

# Отображение изображения с контурами
cv2.imshow('Detected Cards', image)
cv2.waitKey(0)
cv2.destroyAllWindows()

Here is our image

As we can see, the program successfully coped with the search for maps in the foreground and side plans, but in the center everything did not work very well.

Conclusion

In this article, we looked at how to use the OpenCV library to recognize cards on a table and stroke them on the screen. We’ve covered the steps from loading and pre-processing an image to finding the outlines and tracing the found maps. OpenCV provides powerful tools for image processing and computer vision, and you can use these techniques to create various projects related to image recognition and image processing. However, in general, everything was not recognized very well, it’s okay, in the following articles we will fix this.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *