Ask Your Question
1

How to detect different random colors from the picture?

asked 2016-04-13 08:23:09 -0600

Akki gravatar image

Hello,

I am doing phase detection program. I would like to know if there is any possibility to say that this picture frame has 3 various color or this picture frame has one color. colors should be changed picture by picture. So I am looking for some logic to say how many different color are there in the picture.

Please guide me. Thank you very much in advance.

edit retag flag offensive close merge delete

Comments

Have you try histograms?

Mathieu Barnachon gravatar imageMathieu Barnachon ( 2016-04-13 09:58:23 -0600 )edit
2

Maybe you could build a histogram and count number of non-zero elements (using cv::countNonZero(mat) )?

strann gravatar imagestrann ( 2016-04-13 10:02:49 -0600 )edit

You can try color Image quantization in google or try this or this

LBerger gravatar imageLBerger ( 2016-04-13 10:18:45 -0600 )edit
2

have a look in this thread. However, have in mind that partition() is a bit slow in big images. In case that you need something faster I have a kind of optimization. However, I haven't implemented the lookup table for recognizing the color intensity, but getting the number of different colors in your image it works pretty well.

theodore gravatar imagetheodore ( 2016-04-13 13:30:25 -0600 )edit

Thank you very much for reply. I have different 4 colors to detect not more than that. and Image is also not so big. its like random color made draw in paint some time its two color some time 3 that is it. I have idea about histogram but I dont know how to implement it. I will also take a look in other suggestions. Thank you very much once again and it will be great help is someone can provide sample program.

Akki gravatar imageAkki ( 2016-04-13 16:24:18 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
2

answered 2016-04-14 04:12:23 -0600

pklab gravatar image

To count number of colors you could find peaks in the histogram of HUE channel.

Because you have few colors, @strann has right solution: you could can simply check the histogram where the number of pixel count is non zero.

int CountColors(const Mat &img)
{
  Mat hsv,hue,hist;
  vector<Mat> hsv_planes;
  uchar rangeMin = 0;
  uchar rangeMax = 255;
  if (img.type() == CV_8UC1) hue = img;
  else if (img.type() == CV_8UC3)
  {
    cvtColor(img, hsv, CV_BGR2HSV);
    rangeMax = 360/2; //hue range is 0..180
    split(hsv, hsv_planes);
    hue= hsv_planes[0];
  }

    // Set the range for histogram
    float range[] = { 0, rangeMax};
    int histSize = rangeMax+1;
    const float* histRange = { range };
    bool uniform = true;
    bool accumulate = false;
    cv::calcHist(&hue, 1, 0, cv::Mat(), hist, 1, &histSize, &histRange, uniform, accumulate);
    int numOfColors = countNonZero(hist);
    return numOfColors ;
}
edit flag offensive delete link more

Comments

Thank you very much for your help. could you please explain, where to enter the image path.

Thank you very much once again.

Akki gravatar imageAkki ( 2016-04-14 10:17:50 -0600 )edit

I can't understand your comment... did you see that CountColors is a function that receive an image and returns the number of color ?

pklab gravatar imagepklab ( 2016-04-14 12:09:55 -0600 )edit

Hello, When I am trying to run the program I am getting error: fatal error LNK1561: entry point must be defined. Can you please guide me how can I overcome this. Thank you very much once again.

Akki gravatar imageAkki ( 2016-04-15 07:02:12 -0600 )edit

@Akki I think you should start from here http://www.cplusplus.com/doc/tutorial/ check "Structure of a program" for the answer

pklab gravatar imagepklab ( 2016-04-15 10:48:27 -0600 )edit

Thank you very much for suggestions. I am not so good in programming. I went through the link which you mentioned, but still I didnt understand where I am doing wrong. Sorry for such silly question.

Akki gravatar imageAkki ( 2016-04-15 12:33:04 -0600 )edit
2

you need of main` function !!!!!!!!!!!!!

int main(int argc, char *argv[])
{
  Mat img = imread("your image file name");
  int numOfColors = CountColors(img);
  ...
}

I hope It was only a small oversight otherwise I really suggest to restart all from a basic computer programming course

pklab gravatar imagepklab ( 2016-04-15 14:14:20 -0600 )edit

Thank you very much for your suggestion. when I tried to run program, I am Getting error like 'CountColors': identifier not found 'numOfColors' : redefinition; multiple initialization

please help me how can I solve this problem.

Thank you very much once again.

Akki gravatar imageAkki ( 2016-04-15 16:57:41 -0600 )edit
2

@Akki I would not to be rude but, believe me, I'm really helping you by suggesting a computer programming course... a bit of knowledge is required even for copy&paste

pklab gravatar imagepklab ( 2016-04-16 02:43:44 -0600 )edit

Thank you very much.. I will make clear my basics first. Thank you once again.

Akki gravatar imageAkki ( 2016-04-16 03:05:39 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-04-13 08:23:09 -0600

Seen: 370 times

Last updated: Apr 14 '16