#5
by aaaa » 6年前
毎度のことながら度々申し訳ございません
何か関数を使わなければいけないのかと思いましたが、自分なりにプログラムを作ってみたのでこれが考え方があってるのか教えていただけたら嬉しいです。
コード:
/*
* hsv change
*/
#include "opencv2/calib3d/calib3d.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/core/core.hpp"
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <iostream>
#include <fstream>
#include <sstream>
using namespace cv;
int main(int argc, const char *argv[])
{
/*genga*/
Mat src_image = imread("red.jpg");
namedWindow("genga");
imshow("genga", src_image);
int width = src_image.cols;
int height = src_image.rows;
/*
int count =0;
count +=CountNonZero(src_image);
printf("%d\n",count);
*/
/*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);
int flag_red = 0;
int flag_blue = 0;
/*ピクセル*/
int pixel_red = 0;
int pixel_blue = 0;
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;
flag_red = 1;
pixel_red ++;
}
else
red_image.at<uchar>(y, x) = 255;
/*blue_detect*/
if((hue>75 && hue<90) && sat>60){
blue_image.at<uchar>(y, x) = 0;
flag_blue = 1;
pixel_blue ++;
}
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(flag_red=1){
printf("red\n");
}
*/
if(flag_blue=1){
printf("blue\n");
}
printf("%d\n",pixel_red);
printf("%d\n",pixel_blue);
waitKey(0);
destroyAllWindows();
return 0;
}
赤、青それぞれのif の中でカウントをすればいいのではないかと思いこうしてみました。
一応横280、縦200の赤色の画像を読み込んだら、赤56000、青0と出ました。
よろしくお願いします。
毎度のことながら度々申し訳ございません
何か関数を使わなければいけないのかと思いましたが、自分なりにプログラムを作ってみたのでこれが考え方があってるのか教えていただけたら嬉しいです。
[code]
/*
* hsv change
*/
#include "opencv2/calib3d/calib3d.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/core/core.hpp"
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <iostream>
#include <fstream>
#include <sstream>
using namespace cv;
int main(int argc, const char *argv[])
{
/*genga*/
Mat src_image = imread("red.jpg");
namedWindow("genga");
imshow("genga", src_image);
int width = src_image.cols;
int height = src_image.rows;
/*
int count =0;
count +=CountNonZero(src_image);
printf("%d\n",count);
*/
/*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);
int flag_red = 0;
int flag_blue = 0;
/*ピクセル*/
int pixel_red = 0;
int pixel_blue = 0;
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;
flag_red = 1;
pixel_red ++;
}
else
red_image.at<uchar>(y, x) = 255;
/*blue_detect*/
if((hue>75 && hue<90) && sat>60){
blue_image.at<uchar>(y, x) = 0;
flag_blue = 1;
pixel_blue ++;
}
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(flag_red=1){
printf("red\n");
}
*/
if(flag_blue=1){
printf("blue\n");
}
printf("%d\n",pixel_red);
printf("%d\n",pixel_blue);
waitKey(0);
destroyAllWindows();
return 0;
}
[/code]
赤、青それぞれのif の中でカウントをすればいいのではないかと思いこうしてみました。
一応横280、縦200の赤色の画像を読み込んだら、赤56000、青0と出ました。
よろしくお願いします。