Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

I figured out the solution

#include "stdafx.h"
#include <iostream>
#include "opencv2/opencv.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"

using namespace cv;
using namespace std;

struct palette {
Vec3b Lower_range;
Vec3b Upper_range;
string name;
     };


int main(int argc, char** argv)
    {
struct palette range[8];


range[0].Lower_range = (170, 100, 0);
range[0].Upper_range = (180, 255, 255);
range[0].name = "Red";
range[1].Lower_range = (90, 50, 50);
range[1].Upper_range = (130, 255, 255);
range[1].name = "Blue";
range[2].Lower_range = (23, 41, 133);
range[2].Upper_range = (40, 150, 255);
range[2].name = "Yellow";
range[3].Lower_range = (40, 50, 50);
range[3].Upper_range = (70, 255, 255);
range[3].name = "Green";
range[4].Lower_range = (157, 50, 50);
range[4].Upper_range = (158, 255, 255);
range[4].name = "Pink";
range[5].Lower_range = (255, 255, 233);
range[5].Upper_range = (255, 255, 233);
range[5].name = "Black";
range[6].Lower_range = (10, 65, 40);
range[6].Upper_range = (20, 255, 255);
range[6].name = "Orange";
range[7].Lower_range = (28, 200, 20);
range[7].Upper_range = (30, 255, 233);
range[7].name = "Brown";
range[8].Lower_range = (0, 50, 0);
range[8].Upper_range = (10, 255, 255);
range[8].name = "Red";

vector< vector<Point> > contours;
vector<Vec4i> hierarchy;
Mat im = imread("blue_flower.jpeg");
Mat im_hsv;
cvtColor(im, im_hsv, COLOR_BGR2HSV);
Mat mask;
for (int i = 0; i < 9; i++) {

    inRange(im_hsv, Scalar(range[i].Lower_range[0], range[i].Lower_range[1], range[i].Lower_range[2]), Scalar(range[i].Upper_range[0], range[i].Upper_range[0], range[i].Upper_range[0]), mask); //Threshold the image
    findContours(mask, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
    if (contours.size() > 0) {
        cout << range[i].name << " color detected" << endl;
    }
}
imshow("Original", im); //show the original image

if (waitKey(0) == 27)
{
}


return 0;

   }