Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How can you use K-Means clustering to posterize an image using c++?

Hi all,

I'm trying to posterize an image, i.e. reduce the number of colours in an image, but I'm not having much luck.

I've found the following Python code from OpenCV's documentation, which uses K-Means:

import numpy as np
import cv2

img = cv2.imread('home.jpg')
Z = img.reshape((-1,3))

# convert to np.float32
Z = np.float32(Z)

# define criteria, number of clusters(K) and apply kmeans()
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
K = 8
ret,label,center=cv2.kmeans(Z,K,None,criteria,10,cv2.KMEANS_RANDOM_CENTERS)

# Now convert back into uint8, and make original image
center = np.uint8(center)
res = center[label.flatten()]
res2 = res.reshape((img.shape))

cv2.imshow('res2',res2)
cv2.waitKey(0)
cv2.destroyAllWindows()

My problem is that I only know C/C++. Would someone please help me out by converting this to the C++ equivalent?

Thanks in advance.

click to hide/show revision 2
retagged

updated 2014-02-06 02:42:20 -0600

berak gravatar image

How can you use K-Means clustering to posterize an image using c++?

Hi all,

I'm trying to posterize an image, i.e. reduce the number of colours in an image, but I'm not having much luck.

I've found the following Python code from OpenCV's documentation, which uses K-Means:

import numpy as np
import cv2

img = cv2.imread('home.jpg')
Z = img.reshape((-1,3))

# convert to np.float32
Z = np.float32(Z)

# define criteria, number of clusters(K) and apply kmeans()
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
K = 8
ret,label,center=cv2.kmeans(Z,K,None,criteria,10,cv2.KMEANS_RANDOM_CENTERS)

# Now convert back into uint8, and make original image
center = np.uint8(center)
res = center[label.flatten()]
res2 = res.reshape((img.shape))

cv2.imshow('res2',res2)
cv2.waitKey(0)
cv2.destroyAllWindows()

My problem is that I only know C/C++. Would someone please help me out by converting this to the C++ equivalent?

Thanks in advance.

How can you use K-Means clustering to posterize an image using c++?

Hi all,

I'm trying to posterize an image, i.e. reduce the number of colours in an image, but I'm not having much luck.

I've found the following Python code from OpenCV's documentation, which uses K-Means:

import numpy as np
import cv2

img = cv2.imread('home.jpg')
Z = img.reshape((-1,3))

# convert to np.float32
Z = np.float32(Z)

# define criteria, number of clusters(K) and apply kmeans()
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
K = 8
ret,label,center=cv2.kmeans(Z,K,None,criteria,10,cv2.KMEANS_RANDOM_CENTERS)

# Now convert back into uint8, and make original image
center = np.uint8(center)
res = center[label.flatten()]
res2 = res.reshape((img.shape))

cv2.imshow('res2',res2)
cv2.waitKey(0)
cv2.destroyAllWindows()

My problem is that I only know C/C++. Would someone please help me out by converting this to the C++ equivalent?

Thanks in advance.