Skip to content

Commit

Permalink
Update main.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
T-Xingchen authored Jul 15, 2018
1 parent 865c771 commit a34f52f
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ using namespace std;

int main( int argc, char** argv )
{

namedWindow("Control", CV_WINDOW_AUTOSIZE); //create a window called "Control"

//红色在HSV空间中有两个分布区间
int R_LowH1 = 0; //red1 in HSV
int R_HighH1 = 10;

Expand Down Expand Up @@ -39,23 +37,27 @@ int main( int argc, char** argv )

Mat imgOriginal=imread("blue.jpg");

Mat B_imgThresholded,R_imgThresholded1,R_imgThresholded2;
Mat B_imgThresholded,R_imgThresholded1,R_imgThresholded2; //用来存储红色和蓝色图片的黑白图
unsigned int number=imgOriginal.cols*imgOriginal.rows;
unsigned int number_threshold=number*2/3;
unsigned int number_threshold=number*2/3; //像素点个数阈值
//用来存储红色区域和蓝色区域像素点的个数
unsigned int B_not_zero_num=0,R_not_zero_num=0,R_not_zero_num1=0,R_not_zero_num2=0;

Mat imgHSV;
vector<Mat> hsvSplit;
cvtColor(imgOriginal, imgHSV, COLOR_BGR2HSV); //Convert the captured frame from BGR to HSV
cvtColor(imgOriginal, imgHSV, COLOR_BGR2HSV); //将原图转换到HSV空间

//因为我们读取的是彩色图,直方图均衡化需要在HSV空间做
split(imgHSV, hsvSplit);
equalizeHist(hsvSplit[2],hsvSplit[2]);
equalizeHist(hsvSplit[2],hsvSplit[2]); //增强对比度
merge(hsvSplit,imgHSV);


//确定红蓝像素点,存储为黑白图
inRange(imgHSV, Scalar(B_LowH, B_LowS, B_LowV), Scalar(B_HighH, B_HighS, B_HighV), B_imgThresholded);
inRange(imgHSV, Scalar(R_LowH1, R_LowS1, R_LowV1), Scalar(R_HighH1, R_HighS1, R_HighV1), R_imgThresholded1);
inRange(imgHSV, Scalar(R_LowH2, R_LowS2, R_LowV2), Scalar(R_HighH2, R_HighS2, R_HighV2), R_imgThresholded2);

//对红蓝像素点计数,个数超过阈值即认为是红色或蓝色
B_not_zero_num=countNonZero(B_imgThresholded);
R_not_zero_num1=countNonZero(R_imgThresholded1);
R_not_zero_num2=countNonZero(R_imgThresholded2);
Expand Down

0 comments on commit a34f52f

Please sign in to comment.