今、わたしは赤色と青色だけを抽出することができるプログラムを作りました。(赤色のみ黒で塗りつぶした画像、青色のみ画像を塗りつぶした画像を表示)
次に私はこの画像が赤色か青色かを判断するプログラム(printfで赤、青を表示する)を作りたいと考えています。
しかしどうのようにしたらよいか全くわからず、ご助言をお願いした次第でございます。
using namespace cv;
int main(int argc, const char *argv[])
{
/*genga*/
Mat src_image = imread("big.jpg");
namedWindow("genga");
imshow("genga", src_image);
int width = src_image.cols;
int height = src_image.rows;
/*HSV*/
Mat hsv_image;
cvtColor(src_image, hsv_image, CV_BGR2HSV);
uchar hue, sat, val;
Mat red_image = Mat(Size(width, height),CV_8UC1);
Mat blue_image = Mat(Size(width, height),CV_8UC1);
for(int y=0; y<height; y++)
for(int x=0; x<width; x++)
{
/*H,S,V*/
hue = hsv_image.at<Vec3b>(y, x)[0];
sat = hsv_image.at<Vec3b>(y, x)[1];
val = hsv_image.at<Vec3b>(y, x)[2];
/*red_detect*/
if((hue<10 || hue>170) && sat>45)
red_image.at<uchar>(y, x) = 0;
else
red_image.at<uchar>(y, x) = 255;
/*blue_detect*/
if((hue>75 && hue<90) && sat>60)
blue_image.at<uchar>(y, x) = 0;
else
blue_image.at<uchar>(y, x) = 255;
}
namedWindow("red_detect");
imshow("red_detect", red_image);
namedWindow("blue_detect");
imshow("blue_detect", blue_image);
if(red_image)
waitKey(0);
destroyAllWindows();
return 0;
}