به نام خدا : تو این مطلب یه پروژه ساده با OpenCV رو میبینیم، تو این پروژه یه سری اشکال عبوری ( 4 ضلعی یا 3 ضلعی ) رو شناسایی و شمارش میکنیم، کل کار این پروژه همینه؛ اصل این پروژه از اینجا ( detect and track objects ) گرفته شده که خب به زبون Python نوشته شده، تنها کاری که من کردم این بود که تبدیلش کردم به C++ و تو این مطلب برای شما قرار دادم!
کد پروژه :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
#include "opencv2/imgcodecs.hpp" #include "opencv2/highgui.hpp" #include "opencv2/imgproc.hpp" #include <iostream> // we make class to our objects class obj { public: cv::Rect rect = cv::Rect(0, 0, 0, 0); // the upper left coordinates of the contour bool followed = false; // if this contour is already in video ( in previous frames ) std::string shape = ""; // to detect shape int ide = 0; // id of contour bool passed = false; // to know if the object already passed the line void remove() { rect = cv::Rect(0, 0, 0, 0); } }; int main() { int OffsetRefLines = 20; std::vector<obj> objects; //--- int allObject_count = 0; int rectangle_count = 0; int triangle_count = 0; //--- cv::Mat frame, gray, thresh; cv::VideoCapture vc("C:/Users/Mahdi/Desktop/Input.mp4"); /*cv::VideoWriter vw("C:/Users/Mahdi/Desktop/Output.mp4", vc.get(cv::CAP_PROP_FOURCC), vc.get(cv::CAP_PROP_FPS), cv::Size(vc.get(cv::CAP_PROP_FRAME_WIDTH), vc.get(cv::CAP_PROP_FRAME_HEIGHT)), true);*/ // read frames loop while(true) { if(!vc.read(frame)) break; cv::Mat kernel = cv::Mat::ones(cv::Size(11,11), CV_8U); cv::morphologyEx(frame, frame, cv::MORPH_CLOSE, kernel); cv::morphologyEx(frame, frame, cv::MORPH_OPEN, kernel); cv::cvtColor(frame, gray, cv::COLOR_BGR2GRAY); cv::GaussianBlur(gray, gray, cv::Size(7,7), 0); cv::threshold(gray, thresh, 127, 255, cv::THRESH_BINARY); std::vector<std::vector<cv::Point>> contours; std::vector<cv::Vec4i> hierarchy; cv::findContours(thresh, contours, hierarchy, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_SIMPLE); for(int i=0; i<contours.size(); i++) { // Filter small contours or noise cv::Rect bRect = cv::boundingRect(contours[i]); // top left int x = bRect.x, y = bRect.y, w = bRect.width, h = bRect.height; if(w < 105 && h < 105) continue; // Approximates a polygonal curve(s) with the specified precision. [ Reducing contour vertices ] // detect the corners of contour to know the shape double epsilon = 0.1 * cv::arcLength(contours[i], true); std::vector<cv::Point> approx; cv::approxPolyDP(contours[i], approx, epsilon, true); cv::polylines(frame, approx, true, cv::Scalar(0,255,0), 2); // detect the shape std::string shape = ""; if(approx.size() == 3) shape = "triangle"; else if (approx.size() == 4) shape = "square"; // center of contour cv::Point center(x+0.5*w, y+0.5*h); cv::circle(frame, center, 5, cv::Scalar(255,0,0), 5); // to know if this contour is already exist and not new bool existed = false; // loop over all object for(int j=0; j<objects.size(); j++) { // if center pixel is in bounding rect of object if( center.x > objects[j].rect.x && center.x < objects[j].rect.x + objects[j].rect.width && center.y > objects[j].rect.y && center.y < objects[j].rect.y + objects[j].rect.height ) { // update contour with new coordinates objects[j].rect = bRect; objects[j].followed = true; // this object is exist in video objects[j].shape = shape; existed = true; // contour is not new break; } } // add new object if(existed == false) { obj new_cont; new_cont.rect = bRect; new_cont.followed = true; new_cont.shape = shape; new_cont.ide = (int)objects.size() + 1; new_cont.passed = false; objects.push_back(new_cont); } } for(int i=0; i<objects.size(); i++) { // if object is no loonger exist in video we remove it if(objects[i].followed == false) objects[i].remove(); // print object if(objects[i].followed == true) { cv::putText(frame, std::to_string(objects[i].ide) + " " + objects[i].shape, cv::Size(objects[i].rect.x, objects[i].rect.y), cv::FONT_HERSHEY_SIMPLEX, 1, cv::Scalar(255,255,255), 4); } bool CheckExitLineCrossing = false; if (cv::abs(objects[i].rect.y - int(frame.rows/2)) <= OffsetRefLines) CheckExitLineCrossing = true; // if y inside the line and the objct didnt pass the line before if(CheckExitLineCrossing && objects[i].passed == false) { allObject_count++; if(objects[i].shape == "triangle") triangle_count++; else if(objects[i].shape == "square") rectangle_count++; objects[i].passed = true; } // object is set to false to next frame objects[i].followed = false; } // OffsetRefLines cv::line(frame, cv::Point(0, int(frame.rows/2) - OffsetRefLines), cv::Point(frame.cols-1, int(frame.rows/2) - OffsetRefLines), cv::Scalar(0,0,255), 2); cv::line(frame, cv::Point(0, int(frame.rows/2)), cv::Point(frame.cols-1, int(frame.rows/2)), cv::Scalar(0,0,255), 3); cv::line(frame, cv::Point(0, int(frame.rows/2) + OffsetRefLines), cv::Point(frame.cols-1, int(frame.rows/2) + OffsetRefLines), cv::Scalar(0,0,255), 2); cv::putText(frame, "total: " + std::to_string(allObject_count), cv::Size(10, 20), cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar::all(255), 2); cv::putText(frame, "rectangle: " + std::to_string(rectangle_count), cv::Size(100, 20), cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar::all(255), 2); cv::putText(frame, "triangle: " + std::to_string(triangle_count), cv::Size(250, 20), cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar::all(255), 2); // show frame cv::imshow("frame", frame); // save frame //vw.write(frame); if( cv::waitKey(30) == 'q' ) break; } std::cout << "objects passed the line = " << std::to_string(allObject_count) << std::endl; std::cout << "recatangles the line = " << std::to_string(rectangle_count) << std::endl; std::cout << "triangles the line = " << std::to_string(triangle_count) << std::endl; vc.release(); //vw.release(); cv::destroyAllWindows(); return 0; } |
توابع و کلاس های مورد استفاده در این پروژه : توابع و کلاس های مورد استفاده در این پروژه که قبلا آموزششون رو در سایت قرار دادم، در ادامه ارجا میدم به مطالب مرتبط :
- کلاس VideoCapture : آموزش ماژول Video I/O در OpenCV
- توابع morphologyEx و GaussianBlur : آموزش ماژول Image Filtering در OpenCV
- تابع threshold : آموزش ماژول Miscellaneous Image Transformations در OpenCV
- توابع findContours و arcLength و approxPolyDP : آموزش ماژول Structural Analysis and Shape Descriptors در OpenCV
- توابع polylines و circle و putText و line : آموزش Drawing Functions در OpenCV
- توابع imshow و waitKey و destroyAllWindows : آموزش ماژول High-level GUI در OpenCV
فیلم های ورودی و خروجی پروژه :
تا مطلب بعدی اگه زنده بودیم و قسمت شد، یا علی.