[PYTHON] Resize, mosaic, face detection with OpenCV, sometimes Zojirushi

Introduction

It's getting more and more.

Resize cv2.resize (src, dsize, fx, fy, interpolation)

There are two ways to specify image resizing in OpenCV. Magnification and size specifications.

-* src * is the original image. -* dsize * specifies the output size in ** tuples in the order of (width, height). ** Required even when the magnification is specified. In that case, specify (0, 0) or None. Of course an integer.

-* fx * and * fy * are magnifications in the width and height directions. It may be a decimal number. -* Interpolation * indicates how to complement the image. There are several, but you should remember the following two. Since cv2.INTER_LINEAR is the default value, cv2.INTER_NEAREST alone is sufficient. --cv2.INTER_LINEAR Bilinear interpolation (default) --cv2.INTER_NEAREST Nearest-neighbor interpolation

The difference is difficult to see in the background and jpeg images of people, but it is easy to see in pixel art.

The original image cv2.INTER_LINEAR cv2.INTER_NEAREST
I am here

dote.png
INTER_LINEAR.png INTER_NEAREST.png

When Trimming, remember to write img [r: r + h, c: c + w] in the order of rows and columns because it is a matrix. However, resizing has nothing to do with the matrix, so the order is (w, h). This is confusing. It is also confusing that the size must be described even when the magnification is specified, and it is necessary to write None.

Basic program

resize.py


import cv2

filename = "hoge.jpg "
img = cv2.imread(filename)

fx, fy = 3.5, 2  #Magnification can be decimal

#How to specify with dsize
h, w = img.shape[:2]
h_resize, w_resize = round(h*fy), round(w*fx)  #Size is an integer
img_resize = cv2.resize(img, dsize=(w_resize, h_resize) ,interpolation=cv2.INTER_LINEAR)

# fx,How to specify with fy
#img_resize = cv2.resize(img, dsize=(0,0), fx=fx, fy=fy ,interpolation=cv2.INTER_LINEAR)

#This will result in an error
#img_resize = cv2.resize(img, fx=fx, fy=fy ,interpolation=cv2.INTER_LINEAR)

cv2.imshow("originai image",img)
cv2.imshow("resized image",img_resize)
cv2.waitKey(0)
cv2.destroyAllWindows()

mosaic

Mosaic does not have a special method of applying a mosaic. All you have to do is shrink it and resize it to its original size. Of course with cv2.INTER_NEAREST.

Application example 1 Focus with hints

If you gradually make the mosaic denser, you can make a quiz like a focus with hints. In the source below, the part that creates an animated GIF is omitted, so please try tapping the key appropriately.

hinto_de_pinto.py


import cv2

filename = "hoge.jpg "
img_origin = cv2.imread(filename)
imgH, imgW = img_origin.shape[:2]

i = 2
isComplete = False
while not isComplete:
    ratio = 1/2**(8-i*0.6)  #I am doing a delicate calculation, but please change it freely
    print (ratio)
    if ratio > 0.7:
        img_mosaic = img_origin
        isComplete = True
    else :
        img_mosaic = cv2.resize(img_origin, dsize=None ,fx=ratio, fy=ratio)
        img_mosaic = cv2.resize(img_mosaic, dsize=(imgW, imgH),
                                interpolation=cv2.INTER_NEAREST)
    cv2.imshow("mosaic", img_mosaic)
    cv2.waitKey(0)
    i += 1
    
cv2.destroyAllWindows()

The original image is Skiman Nurse as usual. hinto_de_pinto.gif

Face detection

Face detection is possible with the OpenCV cascade classifier. There are many classifiers on the net, and you can also make your own.

Here is a reference for the mechanism. [Explanation for beginners] OpenCV face detection mechanism and practice (detect MultiScale)  https://qiita.com/FukuharaYohei/items/ec6dce7cc5ea21a51a82

Basic program

face_detect.py


import cv2

filename = "hoge.jpg "
img_origin = cv2.imread(filename)
img_gray= cv2.cvtColor(img_origin, cv2.COLOR_BGR2GRAY) #Grayscale for speed

# https://github.com/opencv/opencv/tree/master/data/Get cascade files from haarcascades
cascade_path = "./models/haarcascade_frontalface_alt2.xml"
cascade = cv2.CascadeClassifier(cascade_path)
faces = cascade.detectMultiScale(img_gray)

print (faces)

So far, various parameters have been described, but here only the output results will be described. First of all, the original image that I did not introduce earlier.

nurse.jpg

Running the program on this image gives the following results:

result


[[374  78  88  88]
 [137  90 127 127]]

Official reference only says "Rectangle list", but write it more concretely. And ** a list of "a list of detected rectangles stored in the order x, y, w, h" **. Don't forget that the bracket is doubled even if there is only one detection result because it is a list of lists.

Application example 2 Mosaic linked with face detection

Speaking of face detection, we often see programs that enclose the detected area in a square, but if you understand the output, it is easy to make a mosaic. The Laughing Man will soon be able to do it for the images captured by the camera instead of the images. I've never seen Ghost in the Shell.

rectangle_and_mosaic.py


import cv2

filename = "hoge.jpg "
img_origin = cv2.imread(filename)
img_gray = cv2.imread(filename,0)  #There is also a way to read in grayscale instead of converting with cvtColor

# img_origin.copy()Not img_Let's see what happens with the origin itself
img_rect = img_origin.copy()
img_mosaic = img_origin.copy()

cascade_path = "./models/haarcascade_frontalface_alt2.xml"
cascade = cv2.CascadeClassifier(cascade_path)
faces = cascade.detectMultiScale(img_gray)

if len(faces) > 0:
    for face in faces:
        x, y, w, h = face

        #Enclose the detected face area with a square
        img_rect = cv2.rectangle(img_rect, (x, y), (x+w, y+h), color=(255, 255, 255), thickness=2)

        #Mosaic the detected face area
        roi = img_mosaic[y:y+h, x:x+w]
        roi = cv2.resize(roi, (w//10, h//10))
        roi = cv2.resize(roi, (w, h), interpolation=cv2.INTER_NEAREST)
        img_mosaic[y:y+h, x:x+w] = roi               

cv2.imshow("face originai", img_origin)
cv2.imshow("face rectangle", img_rect)
cv2.imshow("face mosaic", img_mosaic)
cv2.waitKey(0)
cv2.destroyAllWindows()

face detection_rect.jpg face detection_mosaic.jpg

Application example 3 Mirror that looks thin

I came up with a thin-looking mirror that uses face detection and scaling. I had a delusion that it would be very popular with high school girls all over the country if I released a smartphone app that allows me to lose weight and gain weight by pinching in and out, but unfortunately I didn't have the technology to make a smartphone app. Or rather, it had a function similar to SNOW. I don't think SNOW's beauty function is as simple as this.

Above: Original image (Pakutaso) Medium: After processing Bottom: Animated GIF made in good condition

original.jpg slim80.png slim.gif

Well, the source of this ... the margins here are too narrow to write it.

At the end

OpenCV is interesting, but I have to study deep learning, which is the main story, as soon as possible.

Recommended Posts

Resize, mosaic, face detection with OpenCV, sometimes Zojirushi
Face detection with Python + OpenCV
Anime face detection with OpenCV
Face detection with Python + OpenCV (rotation invariant)
Hello World and face detection with OpenCV 4.3 + Python
Face recognition with Python's OpenCV
Try edge detection with OpenCV
Face detection with Python + dlib
Real-time edge detection with OpenCV
Face recognition / cutting with OpenCV
Face detection with Haar Cascades
Improve detection accuracy quickly by specifying parameters with openCV face detection
Face detection with YOLO Face (Windows10, Python3.6)
Face detection with Lambda (Python) + Rekognition
[Python] Using OpenCV with Python (Edge Detection)
[OpenCV] Personal identification with face photo
Try face recognition with python + OpenCV
I tried face detection with MTCNN
OpenCV feature detection with Google Colaboratory
Cat detection with OpenCV (model distribution)
I tried face recognition with OpenCV
Hello World and face detection with opencv-python 4.2
[Python] Face detection by OpenCV (Haar Cascade)
Face detection from multiple image files with openCV, cut out and save
[python, openCV] base64 Face recognition with images
Performance comparison of face detector with Python + OpenCV
[Ubuntu] [Python] Face detection comparison between dlib and OpenCV
Face detection from images taken with Raspberry Pi camera