Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8159025
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T17:47:25+00:00 2026-06-06T17:47:25+00:00

I am trying to implement kmeans clustering on images in openCV (using C). Instead

  • 0

I am trying to implement kmeans clustering on images in openCV (using C). Instead of the clustered image resulting in some color layers, the output is pretty weird. I have also tried to debug the code, but couldn’t understand where it may have gone wrong.

Here is the input and output images.

enter image description here

The right one is the output and the left one is the input image.

Here is the code:

image = cvLoadImage( "pic65.png", CV_LOAD_IMAGE_UNCHANGED);
sample = cvCreateMat( image->height*image->width, 5, CV_32FC1 );
clusters = cvCreateMat( image->height*image->width, 1, CV_32SC1 );

data = (uchar *)image->imageData;
for(i=0;i<image->height;i++)
{
                            for(j=0;j<image->width;j++)
                            {
                            cvSetReal2D( sample, k, 0, i);
                            cvSetReal2D( sample, k, 1, j);
                            b = data[i*image->widthStep + j*image->nChannels +0];
                            g = data[i*image->widthStep + j*image->nChannels +1];
                            r = data[i*image->widthStep + j*image->nChannels +2];
                            cvSetReal2D( sample, k, 2, b);
                            cvSetReal2D( sample, k, 3, g);
                            cvSetReal2D( sample, k, 4, r);
                            k++;
                            }
}
count = get_clusters();

cvKMeans2(sample,count,clusters,cvTermCriteria(CV_TERMCRIT_EPS+CV_TERMCRIT_ITER,100,0 ));

for (x = 0; x < image->height; x++)
{
for (y = 0; y < image->width; y++)
{
    index = x * image->width + y;
    cluster_index = cvGetReal2D(clusters,index,0);
    data[x*image->widthStep + y*image->nChannels +0] = cl[cluster_index][0];
    data[x*image->widthStep + y*image->nChannels +1] = cl[cluster_index][2];
    data[x*image->widthStep + y*image->nChannels +2] = cl[cluster_index][2];
}
}

The get_clusters method returns the number of color clusters in the input image depending on a threshold value which is 30 in this case. If its required I can also provide you with the code of get_clusters, but I think its correct.

Can someone please point out where it has gone wrong. Any kind of help is appreciated.
Thanks in advance.

EDIT:My desired output is given below:

enter image description here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T17:47:26+00:00Added an answer on June 6, 2026 at 5:47 pm

    Judging on your “desired output” image, your usage of kmeans is wrong. The pixel coordinates should play no role in the clustering. You should only hand the color triplets to kmeans.

    sample = cvCreateMat( image->height*image->width, 3, CV_32FC1 );
    

    …

                      for(j=0;j<image->width;j++)
                          {
                            b = data[i*image->widthStep + j*image->nChannels +0];
                            g = data[i*image->widthStep + j*image->nChannels +1];
                            r = data[i*image->widthStep + j*image->nChannels +2];
                            cvSetReal2D( sample, k, 0, b);
                            cvSetReal2D( sample, k, 1, g);
                            cvSetReal2D( sample, k, 2, r);
                            k++;
                          }
    

    Then you also have an indexing bug further down the road when setting data.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok well I'm trying implement something similar to the 'undo' function in many image
Trying to implement some nested loops that are spitting out good old nested html
I am trying implement Unblock me Puzzle. i want to change image position from
Trying to implement search with Sunspot Gem wich is using Solr.Fulltext search works fine
I'm trying implement a way to recursively template using jsRender. The issue is, my
I'm trying implement a bracket in my program (using C#/.NET MVC) and I am
Trying to implement AVAudioplayer and get some metering data of the played music, but
trying to implement a multiplayer. Using the sample from Game Center - Sending and
Im trying to implement a UnitofWork pattern using this Scott Allen tutorial My current
I'm trying to implement a view tracking web service on my website. I'm using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.