当前位置: 动力学知识库 > 问答 > 编程问答 >

opencv - Calculation of percentage of filled-in color in particular ROI?

问题描述:

here is an example. I have a health bar. If player has 100% HP, he has his health bar all the way red. If he gets attacked, part of the health bar is red and the rest is dark (as he lost some HP). I made a ROI on the place where his health bar is. How would I calculate a percentage of his life using OpenCV?

网友答案:

Here is the final solution I got. Thanks to Miki

Mat testBar = src(Rec_UserBarHP);
Mat testBarHSV;

cvtColor(testBar, testBarHSV, COLOR_BGR2HSV);

Mat mask1;
inRange(testBarHSV, Scalar(0, 70, 50), Scalar(10, 255, 255), mask1);
Mat mask = mask1;

int TotalPixels = testBarHSV.rows * testBarHSV.cols;
float ZeroPixels = TotalPixels - countNonZero(testBarHSV);

ostringstream str;
str << "Number of red pixels:" << ZeroPixels;
putText(src, str.str(),
    Point(5, 5), 
    FONT_HERSHEY_COMPLEX_SMALL, 
    1.0, 
    Scalar(255, 255, 255), 
    1, 
    CV_AA);
分享给朋友:
您可能感兴趣的文章:
随机阅读: