我需要指导才能实现代码。目的是计算自习室中的学生人数。我的想法是:
1) 空荡荡的教室学习拍照 2)在一天中的某个时间与学生坐在预定位置拍照,因为椅子不能移动。 3) 定义座位类学习对应的图片中的关键点。 4)两张照片的差异。 5)如果这些职位现在被占用(差异已经给出了可见的结果),那么计算与学生人数相对应的差异数。
有没有人知道如何在代码中实现它?
Mat differenceFrame(Mat prev_frame, Mat curr_frame);
int main(void) {
cv::Mat frame, frame1, framedifference;
int key = 0;
frame = imread("2.jpg", CV_LOAD_IMAGE_COLOR); // Read the file
frame1 = imread("1.jpg", CV_LOAD_IMAGE_COLOR); // Read the file
while (key != 27){
differenceFrame(frame, frame1);
cv::absdiff(frame, frame1, framedifference);
key = 0;
cv::imshow("stream", framedifference);
key = cv::waitKey(10);
}
ContPeople(framedifference) ?????
}
现在: 我试过这个解决方案。我不知道它是否是最有效的。 blob 可以帮助我吗? 我做图像差分的时候,有些反光点我标记出来好像变了一样,我觉得是光线太强的问题,你能提炼差分避免这些问题吗?
cv::Mat imgFrame1Copy = F_RoomFull.clone(); cv::Mat imgFrame2Copy = F_RoomEmpty.clone();
cv::Mat imgDifference;
cv::Mat imgThresh;
cv::cvtColor(imgFrame1Copy, imgFrame1Copy, CV_BGR2GRAY);
cv::cvtColor(imgFrame2Copy, imgFrame2Copy, CV_BGR2GRAY);
cv::GaussianBlur(imgFrame1Copy, imgFrame1Copy, cv::Size(5, 5), 0);
cv::GaussianBlur(imgFrame2Copy, imgFrame2Copy, cv::Size(5, 5), 0);
cv::absdiff(imgFrame1Copy, imgFrame2Copy, imgDifference);
cv::threshold(imgDifference, imgThresh, 180, 255, CV_THRESH_BINARY);
cv::imshow("imgThresh", imgThresh);
cv::Mat structuringElement3x3 = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(3, 3));
cv::Mat structuringElement5x5 = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(5, 5));
cv::Mat structuringElement7x7 = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(7, 7));
cv::Mat structuringElement9x9 = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(9, 9));
cv::dilate(imgThresh, imgThresh, structuringElement5x5);
cv::dilate(imgThresh, imgThresh, structuringElement5x5);
cv::erode(imgThresh, imgThresh, structuringElement5x5);
cv::Mat imgThreshCopy = imgThresh.clone();
cv::findContours(imgThreshCopy, contours, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_SIMPLE);
cv::Mat imgContours(imgThresh.size(), CV_8UC3, SCALAR_BLACK);
cv::drawContours(imgContours, contours, -1, SCALAR_WHITE, -1);
cv::imshow("imgContours", imgContours);
printf("%d", contours.size());
最佳答案
当你减去那两个图像(矩阵)时,只有学生的位置会有非零值。其他值应为零或非常接近于零。
设置一个阈值将所有其他值设置为零(我的意思是如果在上一张图像中没有学生的位置有任何非零小值)。
然后做轮廓检测。 请参阅此处的代码: http://docs.opencv.org/2.4/doc/tutorials/imgproc/shapedescriptors/find_contours/find_contours.html
轮廓数= no。学生人数
如果两个轮廓重叠,使用轮廓面积来计算它们。期望最大轮廓将不重叠
关于c++ - OpenCV 计算房间里的人数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37211812/