void QuickDemo::inrange_Demo(Mat& image) {
int height = image.rows;
int width = image.cols;
const char* colors[8] = { "raw_image","red_mask","orange_mask","yellow_mask","green_mask","cyan_mask","blue_mask","purple_mask" };
//Mat result = Mat::zeros(Size(height * 4 + 5*3, width * 2 + 5), CV_8UC3);
Mat result(height * 4 + 5 * 3, width * 2 + 5, CV_8UC3, Scalar(127, 127, 127));
Mat hsv, red_mask, red0_mask, red1_mask, green_mask, blue_mask;
Mat orange_mask, yellow_mask, cyan_mask, purple_mask;
cvtColor(image, hsv, COLOR_BGR2HSV);
//红
inRange(hsv, Scalar( 0, 43, 46), Scalar( 10, 255, 255), red0_mask);
inRange(hsv, Scalar(156, 43, 46), Scalar(180, 255, 255), red1_mask);
bitwise_or(red0_mask, red1_mask, red_mask);
//橙、黄
inRange(hsv, Scalar(11, 43, 46), Scalar(25, 255, 255), orange_mask);
inRange(hsv, Scalar(26, 43, 46), Scalar(34, 255, 255), yellow_mask);
//绿、青、蓝、紫
inRange(hsv, Scalar( 35, 43, 46), Scalar( 77, 255, 255), green_mask);
inRange(hsv, Scalar( 78, 43, 46), Scalar( 99, 255, 255), cyan_mask);
inRange(hsv, Scalar(100, 43, 46), Scalar(124, 255, 255), blue_mask);
inRange(hsv, Scalar(125, 43, 46), Scalar(155, 255, 255), purple_mask);
cvtColor(red_mask, red_mask, COLOR_GRAY2BGR);
cvtColor(orange_mask, orange_mask, COLOR_GRAY2BGR);
cvtColor(yellow_mask, yellow_mask, COLOR_GRAY2BGR);
cvtColor(green_mask, green_mask, COLOR_GRAY2BGR);
cvtColor(cyan_mask, cyan_mask, COLOR_GRAY2BGR);
cvtColor(blue_mask, blue_mask, COLOR_GRAY2BGR);
cvtColor(purple_mask, purple_mask, COLOR_GRAY2BGR);
image.copyTo( result(Rect(0, 0, width, height)));
red_mask.copyTo( result(Rect(width + 5, 0, width, height)));
orange_mask.copyTo(result(Rect(0, height + 5, width, height)));
yellow_mask.copyTo(result(Rect(width + 5, height + 5, width, height)));
green_mask.copyTo( result(Rect(0, 2 * height + 5*2, width, height)));
cyan_mask.copyTo( result(Rect(width + 5, 2 * height + 5*2, width, height)));
blue_mask.copyTo( result(Rect(0, 3 * height + 5*3, width, height)));
purple_mask.copyTo(result(Rect(width + 5, 3 * height + 5*3, width, height)));
int half_x = (int)width / 3;
int half_y = (int)height * 0.95;
for (int i = 0; i < 4; i++)
{
int y = i * height + 5 * i;
putText(result, colors[2*i], Point(half_x, y + half_y), cv::FONT_HERSHEY_COMPLEX, 1, Scalar(100, 255, 255), 1, 8);
putText(result, colors[2*i+1], Point(width + 5 + half_x, y + half_y), cv::FONT_HERSHEY_COMPLEX, 1, Scalar(100, 255, 255), 1, 8);
}
imwrite("result.png", result);
waitKey(0);
}