I have an image with 22500x55000x3 shape and I want to apply kmean algorithm on this image. Unfortunatelly it takes so much time even if I run the algorithm in a server. What can be done for this situation ?
Here the code I used.
import os
os.environ["OPENCV_IO_MAX_IMAGE_PIXELS"] = pow(2,40).__str__()
import cv2
import numpy
import json
import matplotlib.pyplot as plt
image = cv2.imread("images/2021/true_color_08.jpg")
mask = cv2.imread("images/2021/09/all_08.jpg")
image = cv2.bitwise_and(image,mask, dst = image)
pixel_values = image.reshape((-1, 3))
# convert to float
pixel_values = numpy.float32(pixel_values)
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 100, 0.2)
k = 4
_, labels, (centers) = cv2.kmeans(pixel_values, k, None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS)
# convert back to 8 bit values
centers = numpy.uint8(centers)
centers[0] = [255,255,255]
centers[1] = [255,0,0]
centers[2] = [0,255,0]
centers[3] = [0,0,255]
# flatten the labels array
labels = labels.flatten()
segmented_image = centers[labels]
# reshape back to the original image dimension
segmented_image = segmented_image.reshape(image.shape)
# show the image
plt.imshow(segmented_image)
plt.show()