绘制直方图
- 作用:统计灰度图各像素出现次数
- 函数:CvDrawHist
Mat CvDrawHist(Mat * pMat, char * szWndName = "HistFig", BOOL bShowHist = TRUE)
{
Mat matHist;
const int idxChannel[1] = { 0 };
const int bins[1] = { 256 };
float iRange[2] = { 0, 255 };
const float * range[1] = { iRange };
calcHist(pMat, 1, idxChannel, Mat(), matHist, 1, bins, range);
int hist_w = 512;
int hist_h = 400;
int widthHist = 4;
Mat bkMat = Mat::zeros(hist_h, hist_w, CV_8UC3);
Mat matHistNor;
normalize(matHist, matHistNor, 1, 0, NORM_INF, -1, Mat());
for (int i = 1; i <= matHistNor.rows; i++)
{
int iHight = cvRound(hist_h * matHistNor.at<float>(i - 1));
cv::rectangle(bkMat,
Point(widthHist * (i - 1), hist_h - 1),
Point(widthHist * i - 1, hist_h - iHight - 1),
Scalar(255, 255, 255),
-1);
}
if (bShowHist)
{
imshow(szWndName, bkMat);
}
return matHist;
}
火焰判断函数
摘自:coldplayplay
Mat CheckColor(Mat &inImg, int red_ThresHold = 50, int sat_ThresHold = 5)
{
Mat fireImg;
fireImg.create(inImg.size(), CV_8UC1);
Mat multiRGB[3];
int a = inImg.channels();
split(inImg, multiRGB);
for (int i = 0; i < inImg.rows; i++)
{
for (int j = 0; j < inImg.cols; j++)
{
float B, G, R;
B = multiRGB[0].at<uchar>(i, j);
G = multiRGB[1].at<uchar>(i, j);
R = multiRGB[2].at<uchar>(i, j);
float maxValue = max(max(B, G), R);
float minValue = min(min(B, G), R);
double S = (1 - 3.0*minValue / (R + G + B));
if (R > red_ThresHold &&R >= G && G >= B && S > ((255 - R) * sat_ThresHold / red_ThresHold))
{
fireImg.at<uchar>(i, j) = 255;
}
else
{
fireImg.at<uchar>(i, j) = 0;
}
}
}
medianBlur(fireImg, fireImg, 5);
dilate(fireImg, fireImg, Mat(5, 5, CV_8UC1));
return fireImg;
}
火焰识别基本流程
Mat frame_color;
Mat frame_gray;
Mat frame_binary_org;
Mat frame_binary_fix1;
frame_color = imread("../TestPic/test1.PNG");
if (frame_color.empty())
{
MessageBox(NULL,_T("读取图片失败"), NULL, MB_OK);
return 0;
}
cvtColor(frame_color, frame_gray, COLOR_BGR2GRAY);
threshold(frame_gray, frame_binary_org, 240, 255, THRESH_BINARY | THRESH_OTSU);
{
Mat tempMat;
Mat kernal_OpenClose = getStructuringElement(MORPH_ELLIPSE, Size(5, 5));
morphologyEx(frame_binary_org, tempMat, CV_MOP_OPEN, kernal_OpenClose, Point(-1, -1), 3);
morphologyEx(tempMat, frame_binary_fix1, CV_MOP_CLOSE, kernal_OpenClose, Point(-1, -1), 3);
}
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
findContours(frame_binary_fix1, contours, hierarchy, RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
Mat dstImage = Mat::zeros(frame_binary_fix1.size(), CV_8UC3);
for (int i = 0; i < (int)contours.size(); i++)
{
Scalar color = Scalar(rand() % 255, rand() % 255, rand() % 255);
drawContours(frame_color, contours, i, color, 8);
}