Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Python OpenCV在循环中进行图像平铺_Python_Image_Loops_Opencv_Tiling - Fatal编程技术网

使用Python OpenCV在循环中进行图像平铺

使用Python OpenCV在循环中进行图像平铺,python,image,loops,opencv,tiling,Python,Image,Loops,Opencv,Tiling,Python noob需要一些帮助!有人能告诉我如何使用循环重写代码吗?尝试了一些不同的语法,但似乎不起作用 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.p

Python noob需要一些帮助!有人能告诉我如何使用循环重写代码吗?尝试了一些不同的语法,但似乎不起作用

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)

如您所见,图像将被切割成9个大小相等的片段,如何使用循环编写?

这不会产生与代码相同的结果,但会给您一些想法:

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])

我需要图像平铺,其中最后一部分或边缘平铺必须是完整的平铺图像

以下是我使用的代码:

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)

这是使用部分flowfree his代码进行大规模图像重建。通过在脚本所在的同一区域使用切片图像文件夹,可以重建图像。我希望这有帮助

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])
向我们展示您尝试过的“语法”。