Mosaic images in various shapes (Python, OpenCV)

Mosaic anywhere in the image (Python, OpenCV)

Introduction

Hands-on article to look back and fix the knowledge gained by developing Serverless Web application Mosaic / 87b57dfdbcf218de91e2) This is a separate article from the set, but it is about the implementation of mosaicking the image.

At first, only the face could be detected, but as I wrote in this article, I made it possible to detect the wording as well. Is the detection area rectangular when only the face is processed by mosaic processing? It was parallel to the image, but in the case of wording, the detection area is slanted.

I implemented a mosaic on a diagonal area that is not a rectangle (I wonder if polygons are also good), so I will write an article as a memorandum before I forget it.

Thing you want to do

This is the original image. photo-1484608856193-968d2be4080e.jpeg I want to mosaic the face and the letters "LIFEGUARD" in this image. faces-photo-1484608856193-968d2be4080e.jpeg The detection result looks like this. The face is parallel to the image, but the letters are slanted. photo-1484608856193-968d2be4080e-200203132846-458d11b515.jpeg I want to make a mosaic image like this.

How did you do

  1. Create an image with a mosaic over the entire image. fullmosaic.jpeg
  2. Create a mask image of the part where you want to put the mosaic. mask.jpeg
  3. A mosaic image is combined with the original image by mask processing. photo-1484608856193-968d2be4080e-200203132846-458d11b515.jpeg
    You can create a mosaic image like this.

code

    try:
        height = image.shape[0]
        width = image.shape[1]

        # 1.Create an image with a mosaic over the entire image.
        ratio=0.1
        imageSmall = cv2.resize(image, None, fx=ratio, fy=ratio, interpolation=cv2.INTER_NEAREST)
        imageMosaic = cv2.resize(imageSmall, image.shape[:2][::-1], interpolation=cv2.INTER_NEAREST)

        # 2.Create a mask image of the part where you want to put a mosaic.
        imageMask = np.tile(np.uint8(0), (height, width, 1))
        for points in pointsList:
            pointAry = convertToArray(points)
            contours = np.array(
                [
                    [pointAry[0].x, pointAry[0].y],
                    [pointAry[1].x, pointAry[1].y],
                    [pointAry[2].x, pointAry[2].y],
                    [pointAry[3].x, pointAry[3].y],
                ]
            )
            cv2.fillConvexPoly(imageMask, contours, color=(255, 255, 255))

        # 3.A mosaic image is combined with the original image by mask processing.
        # for y in range(height) :
        #     for x in range(width) :
        #         color = imageMask[y][x]
        #         if color != 0 :
        #             image[y][x] = imageMosaic[y][x]
        #↑ 10-second code ↓ 80-millisecond code(800x600)
        image = np.where(imageMask != 0, imageMosaic, image)

    except Exception as e:
        logger.exception(e)

Loose recruitment

~~ Yuru recruitment 1 ~~ ~~ 3. A mosaic image is combined with the original image by mask processing. ~~ ~~ I'm processing each pixel in a loop, but it's slow. I'm wondering if OpenCV can be implemented more quickly and efficiently. Someone. ~~ I got some advice from @yousuke_papa and solved it. The processing time for 800x600 images has been reduced from 10 seconds to 80 milliseconds! Long loops in Python, no, absolutely! !!

Loose recruitment 2 "Is it a rectangle? Parallel to the image" or "diagonal area" The official name around here. Someone!

-Mr. Chuui Comment! Thank you! "Is it a rectangle? Parallel to the image"-> "Rectangle parallel to the image" "Slanted area"-> "Polygon area" / "Polygon" The fillConvexPoly function explains "draw a convex polygon". In the academic field of algorithms (non-digital), it seems that polygonal regions are roughly divided into Convex (convex) and non-Convex (concave) ...: innocent: ...

Recommended Posts

Mosaic images in various shapes (Python, OpenCV)
Base64 encoding images in Python 3
Make Opencv available in Python
Number recognition in images with Python
How to collect images in Python
Put python, numpy, opencv3 in ubuntu14
Extract text from images in Python
Load gif images with Python + OpenCV
Use OpenCV with Python 3 in Window
Working with DICOM images in Python
Object extraction in images by pattern matching using OpenCV with Python
Capturing images with Pupil, python and OpenCV
Summary of various for statements in Python
How to check opencv version in python
[python, openCV] base64 Face recognition with images
Download images from URL list in Python
[Python] Read images with OpenCV (for beginners)
Get rid of DICOM images in Python
Read text in images with python OCR
How to draw OpenCV images in Pygame
Quadtree in Python --2
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Meta-analysis in Python
Python2.7 + CentOS7 + OpenCV3
Convert PDFs to images in bulk with Python
Unittest in python
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
N-Gram in Python
Programming in python
# 3 [python3] Various operators
Plink in Python
Constant in python
Use various rabbimq features with pika in python
Getting rid of DICOM images in Python Part 2
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
StepAIC in Python
OpenCV Samples (Python)
Load images from URLs using Pillow in Python 3
[Note] openCV + python
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
Constant in python
nCr in Python.
format in python
Scons in Python3
[WP REST API v2] Upload images in Python
Puyo Puyo in python