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.
This is the original image. I want to mosaic the face and the letters "LIFEGUARD" in this image. The detection result looks like this. The face is parallel to the image, but the letters are slanted. I want to make a mosaic image like this.
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)
~~ 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