Home > Blockchain >  Enlarge Image in specific pattern using opencv
Enlarge Image in specific pattern using opencv

Time:10-20

I'm trying to resize an image in a specific pattern using opencv but facing some issues.

so here's the code I wrote:

x1 = cv2.resize(x, (1500, 1600), interpolation = cv2.INTER_AREA)

Following is my input image:

Input Image

and this is what I get:

Output Image

While I need something like this: Target Image

So what's the best way to achieve this?

Thanks.

CodePudding user response:

You can do that in Python/OpenCV two ways -- 1) simple tiling and 2) seamless tiling.

The concept is to reduce the image by some factor, then tile it out by the same factor. If seamless tiling, one flips the image horizontally and concatenates with the original. Then flips that vertically and concatenates vertically with the previous concatenated image.

Input:

enter image description here

import cv2
import numpy as np

# read image
img1 = cv2.imread('pattern.jpg')

# -----------------------------------------------------------------------------
# SIMPLE TILING

# reduce size by 1/20
xrepeats = 20
yrepeats = 20
xfact = 1/xrepeats
yfact = 1/yrepeats
reduced1 = cv2.resize(img1, (0,0), fx=xfact, fy=yfact, interpolation=cv2.INTER_AREA)

# tile (repeat) pattern 20 times in each dimension
result1 = cv2.repeat(reduced1, yrepeats, xrepeats)
# -----------------------------------------------------------------------------


# -----------------------------------------------------------------------------
# SEAMLESS TILING

# flip horizontally
img2 = cv2.flip(img1, 1)

# concat left-right
#img3 = np.hstack((img1, img2))
img3 = cv2.hconcat([img1, img2])

# flip vertically
img4 = cv2.flip(img3, 0)

# concat top-bottom
#img = np.vstack((img3, img4))
img5 = cv2.vconcat([img3, img4])

# reduce size by 1/20
xrepeats = 10
yrepeats = 10
xfact = 1/(2*xrepeats)
yfact = 1/(2*yrepeats)
reduced2 = cv2.resize(img5, (0,0), fx=xfact, fy=yfact, interpolation=cv2.INTER_AREA)

# tile (repeat) pattern 10 times in each dimension
result2 = cv2.repeat(reduced2, yrepeats, xrepeats)
# -----------------------------------------------------------------------------

# save results
cv2.imwrite("pattern_tiled1.jpg", result1)
cv2.imwrite("pattern_tiled2.jpg", result2)

# show the results
cv2.imshow("result", result1)
cv2.imshow("result2", result2)
cv2.waitKey(0)

Simple tiled result:

enter image description here

Seamless tiled result:

enter image description here

  • Related