To find objects in images

0

In the modern era of data science, different kinds of data types need to be dealt with. For example, an image as data can give us a lot of information. This information can lead us to many visionary ideas. There are different techniques to process image data and one of the most important techniques is to know the content of an image. To extract a smaller object from a larger or multi-object support image using an object image is called pattern matching.

Pattern Match

Pattern matching is a technique to extract or highlight the area or object of an image using a smaller image of that area. The basic goal of pattern matching is to find a smaller image or pattern within a larger image.

As we can understand from the above statement, for pattern matching, we need two images, one is a source image containing the template image below, and the other is a template image which must be found from the source image.

Basically, in pattern matching, we discover the location in the source image of the pattern image. Here we can understand that it is necessary to have the source image size larger than the template image.

In this article, we are going to see how we can do this using python. To perform pattern matching using python, we have an open source library called OpenCV that we mainly use to analyze and augment image and video data. To better understand the subject, let’s take an overview of openCV.

OpenCV

Open cv is a library for performing computer vision using python. This is a python binding. Instead of python, we can use it in different programming languages ​​like C++ and java. We can perform many tasks using OpenCV such as image processing, image blending and image compositing. And also it can be integrated with many libraries like NumPy and pandas or scipy. These libraries are highly optimized libraries for numerical operations and data augmentation operations. There can be many applications of OpenCV like

Later in the article we will talk about pattern matching, and this operation falls under image processing. Let’s take an overview of image processing so that we can also involve it in our next projects.

Image processing

As the name suggests, image processing is the field of operation where we perform operations to extract information, edit or enhance images. More formally, we can say that image processing is the process of analyzing and editing scanned images to improve the quality of the image or analysis.

Mathematically, the image can be considered as the two-dimensional (in black and white image) or three-dimensional (in color image) painting. In image processing, we apply some methods to these arrays to get the changes on the image data.

In image processing, the major term we work with is the image pixel, which is nothing but the value of x and y (in black and white image) or x, y and z (in color image) in the image table. The magnitude of x, y, and z defines the pixel size and color of images.

Basically, image processing consists of three processes.

  • Image Import
  • Image analysis and manipulation
  • Output (result of analysis and manipulation)

Let’s take a look at the steps of google colab.

Importing libraries:

grab:

import cv2

from google.colab.patches import cv2_imshow

Reading the picture.

grab:

pic = cv2.imread('/content/drive/MyDrive/Yugesh/Template Matching (find object in an image)/photo-1509909756405-be0199881695.jpeg')

Image display.

Grab:

 cv2_imshow(pic)
  cv2.waitKey(0) 

To go out:

This is how we can read and display our images in Google Colab.

Printing of the table format of the image.

Grab:

print(pic)

To go out:

Here we can see the array format of a colored image which is a three-dimensional array. We can convert this image to a grayscale image.

Grab:

pic = cv2.cvtColor(pic, cv2.COLOR_RGB2GRAY)

Displaying the image in grayscale.

Grab:

  cv2_imshow(pic)
  cv2.waitKey(0) 

To go out:

Let’s check the table format for the converted image.

Grab:

print(pic)

To go out:

Here in the outputs above we can see how the changes happened mathematically when manipulating any image.

We can save the final image or output.

Grab:

cv2.imwrite("graypic.jpg", pic)

To go out:

This is how we can perform basic image processing operations.

But the article is mainly focused on pattern matching and object detection, so in the next steps we will see how we can detect any pattern image from the source image.

We will use the above image as the source image for pattern matching, and we will match or detect the football in the image using Opencv in python.

This is the image of football that we are going to use for matching purposes.

Implementing pattern matching code

Importing libraries.

Grab:

 import numpy as np
 import cv2 

Read pictures

Grab:

 src = cv2.imread("/content/drive/MyDrive/Yugesh/Template Matching (find object in an image)/souce.jpg")
 Temp = cv2.imread("/content/drive/MyDrive/Yugesh/Template Matching (find object in an image)/tamplet.png") 

Most operational image processing methods operate on grayscale images; that’s why we need to convert the images to grayscale images.

Conversion of images to grayscale.

Grab:

 src = cv2.cvtColor(src, cv2.COLOR_RGB2GRAY)
 temp = cv2.cvtColor(Temp, cv2.COLOR_RGB2GRAY) 

Pull height and width of src image and temp image into height, width, H and W objects.

Grab:

 height, width =src.shape
 height, width 

To go out:

Grab:

 H, W = temp.shape
 H, W 

To go out:

Here we can see the shape of our images.

There are methods provided by cv2 to perform pattern matching. And in this article, we are going to run and test them all so that we can easily choose one of them for our future projects. So here I am defining the list of all methods.

Grab:

 methods = [cv2.TM_CCOEFF, cv2.TM_CCOEFF_NORMED, cv2.TM_CCORR,
             cv2.TM_CCORR_NORMED, cv2.TM_SQDIFF, cv2.TM_SQDIFF_NORMED] 

In the next step, we’ll use the list of methods in a loop to get the results of all the methods.

Grab:

 for method in methods:
   src2 = src.copy()
   result = cv2.matchTemplate(src2, temp, method)
   min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)
   print(min_loc, max_loc)
   if method in [cv2.TM_SQDIFF,cv2.TM_CCORR]:
     lacation = min_loc
   else:
     location = max_loc
   bottom_right = (location[0] + W, location[1] + H)
   cv2.rectangle(src2, location,bottom_right, 255, 5)
   cv2_imshow(src2)
   cv2.waitKey(0)
   cv2.destroyAllWindows() 

To go out:

(267, 403) (672, 83)
(544, 817) (672, 83)
(1054, 77) (207, 163)
(88, 232) (672, 83)
(672, 83) (270, 405)
(672, 83) (0, 0)

We can see that all 5 out of 6 methods match this picture perfectly and one cv2.TM_SQDIFF_NORMED method did not work well. We can see the X and Y locations where the modeled image matches the source image in the upper part of the image. And also, the match venue is covered by the rectangle in the image.

There can be different methods for performing text matching; this is one of them, and yes, as we can see, it works great, and it’s simple to do. We don’t need to know so much about internal spaces and image processing terminology.

The references


Source link

Share.

Comments are closed.