我需要指导才能实现代码。目的是计算自修室中的学生人数。我的想法是:
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) ?????
}
现在:我尝试了此解决方案。我不知道这是否是最有效的。这个斑点可以帮助我吗?当我改变图像时,我在某些反射点上将它们标记为好像已被更改,我认为这是光线过多的问题,您可以优化差异以避免这些问题吗?
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
轮廓数量=否。学生的
如果两个轮廓重叠,请使用轮廓区域对它们进行计数。期望最大轮廓不会重叠
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句