Justin Mcgill
Justin Mcgill

Reputation: 11

image tiling in loops using Python OpenCV

Python noob needs some help guys! Can someone show me how to rewrite my code using loops? Tried some different syntaxes but did not seem to work!

img = cv2.imread("C://Users//user//Desktop//research//images//Underwater_Caustics//set1//set1_color_0001.png")

    tile11=img[1:640, 1:360]
    cv2.imwrite('tile11_underwater_caustic_set1_0001.png', tile11)

    tile12=img[641:1280, 1:360]
    cv2.imwrite('tile12_underwater_caustic_set1_0001.png', tile12)

    tile13=img[1281:1920, 1:360]
    cv2.imwrite('tile13_underwater_caustic_set1_0001.png', tile13)

    tile21=img[1:640, 361:720]
    cv2.imwrite('tile21_underwater_caustic_set1_0001.png', tile21)

    tile22=img[641:1280, 361:720]
    cv2.imwrite('tile22_underwater_caustic_set1_0001.png', tile22)

    tile23=img[1281:1920, 361:720]
    cv2.imwrite('tile23_underwater_caustic_set1_0001.png', tile23)

    tile31=img[1:640, 721:1080]
    cv2.imwrite('tile31_underwater_caustic_set1_0001.png', tile31)

    tile32=img[641:1280, 721:1080]
    cv2.imwrite('tile32_underwater_caustic_set1_0001.png', tile32)

    tile33=img[1281:1920, 721:1080]
    cv2.imwrite('tile33_underwater_caustic_set1_0001.png', tile33)

As you can see, the image will be cut into 9 equal-size pieces, how to write it using loops?

Upvotes: 1

Views: 6363

Answers (3)

Daniel
Daniel

Reputation: 61

This is for massive image reconstruction using part of flowfree his code. By using a folder of sliced images in the same area the script is, you can rebuild the image. I hope this helps.

import cv2
import glob
import os

dir = "." 

pathname = os.path.join(dir, "*" + ".png")
images = [cv2.imread(img) for img in glob.glob(pathname)]

img = images[0]

numrows, numcols = 1,1
height = int(img.shape[0] / numrows)
width = int(img.shape[1] / numcols)


for row in range(numrows):
    for col in range(numcols):
        y0 = row * height
        y1 = y0 + height
        x0 = col * width
        x1 = x0 + width
        cv2.imwrite('merged_img_%d%d.jpg' % (row, col), img[y0:y1, x0:x1])

Upvotes: 0

telepinu
telepinu

Reputation: 91

I needed image tiling where last parts or edge tiles are required to be full tile images.

Here is the code I use:

import cv2
import math
import os

Path = "FullImage.tif";
filename, file_extension = os.path.splitext(Path)
image = cv2.imread(Path, 0)

tileSizeX = 256;
tileSizeY = 256;
numTilesX = math.ceil(image.shape[1]/tileSizeX)
numTilesY = math.ceil(image.shape[0]/tileSizeY)

makeLastPartFull = True; # in case you need even siez

for nTileX in range(numTilesX):
    for nTileY in range(numTilesY):
        startX = nTileX*tileSizeX
        endX = startX + tileSizeX
        startY = nTileY*tileSizeY
        endY = startY + tileSizeY;

        if(endY > image.shape[0]):
            endY = image.shape[0]

        if(endX > image.shape[1]):
            endX = image.shape[1]

        if( makeLastPartFull == True and (nTileX == numTilesX-1 or nTileY == numTilesY-1) ):
            startX = endX - tileSizeX
            startY = endY - tileSizeY

        currentTile = image[startY:endY, startX:endX]
        cv2.imwrite(filename + '_%d_%d' % (nTileY, nTileX)  + file_extension, currentTile)

Upvotes: 2

flowfree
flowfree

Reputation: 16462

This won't produce the same result like your code, but will give you some ideas:

img = cv2.imread('sample.jpg')
numrows, numcols = 4, 4
height = int(img.shape[0] / numrows)
width = int(img.shape[1] / numcols)
for row in range(numrows):
    for col in range(numcols):
        y0 = row * height
        y1 = y0 + height
        x0 = col * width
        x1 = x0 + width
        cv2.imwrite('tile_%d%d.jpg' % (row, col), img[y0:y1, x0:x1])

Upvotes: 3

Related Questions