| |

Getting Coordinate and Cropping an Image with OpenCV

OpenCV is popular library for computer vision. OpenCV is an open source computer vision and machine learning software library. OpenCV was built to provide a common infrastructure for computer vision applications and to accelerate the use of machine perception in the commercial products.

OpenCV uses machine learning to detect object/faces in picture. for detecting face, the algorithm start from top left on picture unltil right down on the picture.

Why we use OpenCV for getting the cordinate?

there’se so much way to goes to Roma, same as here, there so much way to getting the cordinate. We can use countours detection or maybe R-cnn to get the cordinate. But for this situation we will use Opencv and Cascades.

First of all we need to download the xml of haar-cascades, here is the link

After that we need to import some library:

import cv2
import matplotlib.pyplot as plt
from PIL import Image
%matplotlib inline

And then import the xml files:

cascPath = "haarcascade_frontalface_default.xml"

And then create the haar-cascade:

faceCascade = cv2.CascadeClassifier(cascPath)

After that, just import the image that we want to use:

path = "/content/ktp3.png"
image = cv2.imread(path)
image_crop = Image.open(path)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

After that, detect the face in the image:

faces = faceCascade.detectMultiScale(
    gray,
    scaleFactor=1.1,
    minNeighbors=2,
    minSize=(40, 60),
    flags = cv2.CASCADE_SCALE_IMAGE
)

print("Found {0} faces!".format(len(faces)))

Here is some explanation about the parameters:

Parameters:cascade – Haar classifier cascade (OpenCV 1.x API only). It can be loaded from XML or YAML file using Load(). When the cascade is not needed anymore, release it using cvReleaseHaarClassifierCascade(&cascade).
image – Matrix of the type CV_8U containing an image where objects are detected.
objects – Vector of rectangles where each rectangle contains the detected object.
scaleFactor – Parameter specifying how much the image size is reduced at each image scale.
minNeighbors – Parameter specifying how many neighbors each candidate rectangle should have to retain it.
flags – Parameter with the same meaning for an old cascade as in the function cvHaarDetectObjects. It is not used for a new cascade.
minSize – Minimum possible object size. Objects smaller than that are ignored.
maxSize – Maximum possible object size. Objects larger than that are ignored.

And then display the image:

for (x, y, w, h) in faces:
    cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)

plt.imshow(image)

And the result would be like this:

And then for cropping the image, we just need to use the cordinate that we just got from the image detector, it would be like this:

im_crop = image_crop.crop((x, y, (x+w), (y+h)))
plt.imshow(im_crop)

The result would be like this:

Here is some source that might be help:

Similar Posts

One Comment

Comments are closed.