c++ - K-means and EM algorithms -
how can implement k-means & em algorithms without calling opencv functions image segmentation?
i begin code :
#include opencv2/highgui/highgui.hpp #include iostream using namespace cv; using namespace std; int main() { mat img = imread("testimage.png", 0); mat label_img; label_img.create(img.cols, img.rows, cv_8uc1); } how can continue ?
k-means work iterating 2 steps till convergence, e-step , m-step.
initialization:
assign each pixel, @ random, 1 of k clusters. is, each entry in label_img choose number [0..k-1] @ random.
expectation (e-step)
given assignment of pixels clusters (label_img), can compute center of each cluster (simply mean of pixel values assigned cluster).
@ end of stage you'll have k vectors pointing @ centers of k clusters.
maximization (m-step)
once have k clusters, compute distance of each pixel k centers , assign (by changing respective entry in label_img) center closest pixel.
@ end of stage you'll have new assignment of each pixel clusters (new values of label_img)
you need repeat these 2 steps until label_img not change anymore or if exceed pre-defined number of iterations.
Comments
Post a Comment