Numpy 배열 (이미지)을 타일로 분할하여 반복하는 방법은 무엇입니까? [복제]

Jan 04 2021

반복 할 수있는 9 (3 x 3) 타일로 분할하려는 Numpy Array 유형 이미지가 있습니다. 어떻게 할 수 있습니까?

다음은 지금까지 numpy.ndarray를 생성하는 코드이지만 분할하지 못했습니다.

분할 할 numpy 이미지 배열은 th1입니다.

import cv2
import numpy as np

# Only for the threshold display
from matplotlib import pyplot as plt

# The Image to be used
image = 'six.png'

# Finding the average greyscale value
image_bgr = cv2.imread(image, cv2.IMREAD_COLOR)

# Calculate the mean of each channel
channels = cv2.mean(image_bgr)
# Type Float
thresh = channels[0]/2
#print (thresh)

# Displaying the threshold value
img = cv2.imread(image,0)
img = cv2.medianBlur(img,5)

# If below then black else white 
ret,th1 = cv2.threshold(img,thresh,255,cv2.THRESH_BINARY)


titles = ['Original Image', 'Global Thresholding']
images = [img, th1, ret]

for i in range(2):
    plt.subplot(2,2,i+1),plt.imshow(images[i],'gray')
    plt.title(titles[i])
    plt.xticks([]),plt.yticks([])

# Shows single image on its' own
plt.imshow(images[1], 'gray')
plt.xticks([]),plt.yticks([])

다음은 이미지 (six.png)입니다.

답변

2 GrgurDamiani Jan 04 2021 at 18:57

내가 할 일은 전체 이미지의 "타일"을 저장할 목록 또는 2 차원 배열을 만드는 것입니다. 타일은 아래 코드를 사용하여 복사됩니다.

height, width, dim = img.shape
image_tile = img[0:height/3, 0:width/3]

좌표 내부에있는 전체 이미지 배열의 일부를 포함 하는 새로운 ndarray image_tile 을 생성합니다 . 이 경우 왼쪽 상단 타일입니다.

for 루프를 사용한 전체 예제는 다음과 같습니다.

image_tile = []
for i in range(0,3):
    for j in range(0,3):
        image_tile.append(img[i * height/3:(i+1) * height/3, j * width/3:(j+1) * width/3])

도움이되고 행운을 빕니다!

1 Aiyush Jan 04 2021 at 19:31

다른 곳에서 적용된 솔루션을 찾았으며 훌륭하게 작동합니다!

img = th1
numrows, numcols = 3, 3
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
        individual =  (img[y0:y1, x0:x1])
        plt.imshow(individual, 'gray')
        plt.xticks([]),plt.yticks([])
        plt.show()