# 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.

edit retag close merge delete

You can use inrange and all pixels belonged to range will be set to 255 in mask.

( 2018-07-06 07:40:09 -0500 )edit

using inrange I can only find in an image but I want to use that pixel

( 2018-07-06 07:44:02 -0500 )edit

Is it C++ ? Lower_magnitude <= colors_magnitude <= Upper_magnitude

( 2018-07-06 07:59:15 -0500 )edit

Yes in c++ only

( 2018-07-06 08:06:06 -0500 )edit

Lower_magnitude <= colors_magnitude result is 0 or 1 then 0 <= Upper_magnitude or 1 <= Upper_magnitude Isn't it?

Lower_magnitude <= colors_magnitude && colors_magnitude<= Upper_magnitude

( 2018-07-06 08:27:32 -0500 )edit

Oh yes, I didn't notice that thanks but then also I can't use this method now i am trying to compare every individual channel

( 2018-07-06 08:36:06 -0500 )edit