Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How can we check if a particular pixel is in this pixel range or not?

I have a BGR value of a pixel in Vec3b format and I want to check if this BGR value is in some predefined BGR range.Now I know we cannot compare two vectors simply like >= or <= so I tried using their magnitude by norm but I am getting false results. Can anyone please suggest something?

My code is

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

     };

struct palette range[8];

Mat get_dominant_palette(vector<Vec3b> colors) {

range[0].Lower_range = (75, 5, 0);
range[0].Upper_range = (252, 142, 32);
range[0].name = "Royal Blue";
range[1].Lower_range = (163, 140, 90);
range[1].Upper_range = (245, 225, 181);
range[1].name = "Sky Blue";

const int tile_size = 64;
Mat ret = Mat(tile_size, tile_size*colors.size(), CV_8UC3, cv::Scalar(0));

for (int i = 0; i<colors.size(); i++) {
    Rect rect(i*tile_size, 0, tile_size, tile_size);
    myfile << "[" << (int)colors[i][0] << " ," << (int)colors[i][1] << " ," << (int)colors[i][2] << "]" << endl;
    rectangle(ret, rect, Scalar(colors[i][0], colors[i][1], colors[i][2]), CV_FILLED);
}

for (int i = 0; i < colors.size(); i++) {
    double colors_magnitude = cv::norm(colors[i], CV_L2);
    for (int j = 0; j < 2; j++) {
        double Lower_magnitude = cv::norm(range[j].Lower_range, CV_L2);
        double Upper_magnitude = cv::norm(range[j].Upper_range, CV_L2);
        if (Lower_magnitude <= colors_magnitude <= Upper_magnitude) {

            color << range[j].name << "---" << "[" << (int)colors[i][0] << " ," << (int)colors[i][1] << " ," << (int)colors[i][2] << "]" << endl;
        }
    }
}
return ret;

}

So here my pixel values are in the colors vector and I am checking if the pixel is skyblue or royal blue but I am getting all the pixels as the both colors.