You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

568 lines
19 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "blobDetector.h"
  2. //http://www.keithlantz.net/2011/04/detecting-blobs-with-cvblobslib-and-tracking-blob-events-across-frames/
  3. //http://opencv.willowgarage.com/wiki/cvBlobsLib
  4. // filter parameters
  5. static const int FKERNELSIZE = 7; //7// for gaussian blur
  6. static const float FSTDDEV = 1.5;
  7. static const int FBLOCKSIZE = 51; //47// for adaptive threshold
  8. static const int FC = -5; //-5
  9. blobDetector::blobDetector() :basicVideoAnalyzer(){
  10. m_nMinArea = TMINAREA;
  11. m_nMaxRadius = TMAXRADIUS;
  12. type = VA_BLOB_DETECT;
  13. m_vBlobs = NULL;
  14. m_oFilter = new filterImage( FKERNELSIZE, FSTDDEV, FBLOCKSIZE, FC );
  15. m_pIplThr = NULL;
  16. m_pIplOrig = NULL;
  17. m_pIplWork = NULL;
  18. m_bPIP = true;
  19. m_bUsePolygones = false;
  20. m_bShowPolygones = false;
  21. m_bShowRectangles = false;
  22. m_bShowCentres = false;
  23. m_nPrepMethod = 1;
  24. m_bReduceSize = false;
  25. memset(&m_aSegments, 0, V_SEGMENT_SIZE*H_SEGMENT_SIZE);
  26. m_nCursorX = m_nCursorY = m_nStepX = m_nStepY = 0;
  27. m_bCursorIsSet = false;
  28. m_pPTZ = NULL;
  29. m_nCamControllCurrent = 0;
  30. };
  31. blobDetector::blobDetector(float min_area, float max_radius) :basicVideoAnalyzer(){
  32. m_nMinArea = min_area;
  33. m_nMaxRadius = max_radius;
  34. type = VA_BLOB_DETECT;
  35. m_vBlobs = NULL;
  36. m_oFilter = new filterImage( FKERNELSIZE, FSTDDEV, FBLOCKSIZE, FC );
  37. m_pIplThr = NULL;
  38. m_pIplOrig = NULL;
  39. m_pIplWork = NULL;
  40. m_bPIP = true;
  41. m_bUsePolygones = false;
  42. m_bShowPolygones = false;
  43. m_bShowRectangles = false;
  44. m_bShowCentres = false;
  45. m_nPrepMethod = 1;
  46. m_bReduceSize = false;
  47. memset(&m_aSegments, 0, V_SEGMENT_SIZE*H_SEGMENT_SIZE);
  48. m_nCursorX = m_nCursorY = m_nStepX = m_nStepY = 0;
  49. m_bCursorIsSet = false;
  50. m_pPTZ = NULL;
  51. m_nCamControllCurrent = 0;
  52. };
  53. blobDetector::~blobDetector(){
  54. if( m_vBlobs != NULL ) delete( m_vBlobs );
  55. if( m_pIplThr != NULL ) delete( m_pIplThr );
  56. if( m_pIplOrig != NULL ) delete( m_pIplOrig );
  57. if( m_pIplWork != NULL ) delete( m_pIplWork );
  58. delete( m_oFilter );
  59. };
  60. bool blobDetector::attachPTZ(basicCameraPTZ *cam){
  61. if (cam == NULL) return false;
  62. m_pPTZ = cam;
  63. return true;
  64. };
  65. inline bool intersectBox(Rect A, Rect B){
  66. bool intersectX = false;
  67. bool intersectY = false;
  68. if (A.x > B.x){
  69. intersectX = B.x + B.width >= A.x;
  70. } else {
  71. intersectX = A.x + A.width >= B.x;
  72. };
  73. if (!intersectX) return false;
  74. if (A.y > B.y){
  75. intersectY = B.y + B.height >= A.y;
  76. } else {
  77. intersectY = A.y + A.height >= B.y;
  78. };
  79. if (!intersectY) return false;
  80. return true;
  81. };
  82. inline void increaseBy(Rect &A, Rect &B){
  83. int tAX, tAY, tBX, tBY;
  84. tAX = A.x + A.width;
  85. tAY = A.y + A.height;
  86. tBX = B.x + B.width;
  87. tBY = B.y + B.height;
  88. A.x = (A.x > B.x) ? B.x : A.x;
  89. A.y = (A.y > B.y) ? B.y : A.y;
  90. A.width = (tAX > tBX) ? tAX - A.x : tBX - A.x;
  91. A.height = (tAY > tBY) ? tAY - A.y : tBY - A.y;
  92. };
  93. bool blobDetector::m_fImageAnalyzer(Mat &Picture){
  94. CBlobResult blob_result;
  95. float x, y;
  96. CvRect tRect;
  97. cBlob temp;
  98. if( Picture.empty() ) return false;
  99. // Prepare Frame to deal with
  100. if ( m_nPrepMethod ){
  101. if (m_bReduceSize){
  102. Mat tMat;
  103. resize(Picture, tMat, Size(Picture.cols >> 1, Picture.rows >> 1), 0.0, 0.0, INTER_AREA);
  104. m_oFilter->filter(tMat);
  105. }else {
  106. m_oFilter->filter(Picture);
  107. };
  108. // convert our OpenCV matrix object to one of type IplImage
  109. if (m_pIplWork != NULL) {
  110. delete(m_pIplWork);
  111. };
  112. m_pIplWork = new IplImage(m_oFilter->thresholdFrame());
  113. } else {
  114. Mat tMat;
  115. if (m_bReduceSize){
  116. resize(Picture, tMat, Size(Picture.cols >> 1, Picture.rows >> 1), 0.0, 0.0, INTER_AREA);
  117. cvtColor(tMat, tMat, CV_RGB2GRAY);
  118. }else{
  119. cvtColor(Picture, tMat, CV_RGB2GRAY);
  120. };
  121. Mat tDelta;
  122. //GaussianBlur(tMat, tMat, Size(7, 7), 0);
  123. //medianBlur(tMat, tMat, 7);
  124. //buildPyramid(tMat, tDelta, 255, BORDER_DEFAULT);
  125. //blur(tMat, tDelta, Size(11,11) );
  126. //blur(InputArray src, OutputArray dst, Size ksize, Point anchor = Point(-1, -1), int borderType = BORDER_DEFAULT)
  127. //adaptiveBilateralFilter(InputArray src, OutputArray dst, Size ksize, double sigmaSpace, double maxSigmaColor = 20.0, Point anchor = Point(-1, -1), int borderType = BORDER_DEFAULT)
  128. //bilateralFilter(InputArray src, OutputArray dst, int d, double sigmaColor, double sigmaSpace, int borderType = BORDER_DEFAULT)
  129. //boxFilter(InputArray src, OutputArray dst, int ddepth, Size ksize, Point anchor = Point(-1, -1), bool normalize = true, int borderType = BORDER_DEFAULT)
  130. //buildPyramid(InputArray src, OutputArrayOfArrays dst, int maxlevel, int borderType=BORDER_DEFAULT )
  131. //GaussianBlur(InputArray src, OutputArray dst, Size ksize, double sigmaX, double sigmaY=0, int borderType=BORDER_DEFAULT )
  132. //medianBlur(InputArray src, OutputArray dst, int ksize)
  133. //Laplacian(InputArray src, OutputArray dst, int ddepth, int ksize=1, double scale=1, double delta=0, int borderType=BORDER_DEFAULT )
  134. equalizeHist(tMat, tMat);
  135. blur(tMat, tMat, Size(11, 11));
  136. adaptiveThreshold(tMat, tMat, 255, cv::ADAPTIVE_THRESH_MEAN_C, cv::THRESH_BINARY, 47, -7 );
  137. /// Detect edges using canny
  138. //Canny(tMat, tMat, 100, 200, 3);
  139. if (m_oMat.empty()){
  140. tMat.copyTo(m_oMat);
  141. return false;
  142. };
  143. tMat.copyTo(m_oMat);
  144. //absdiff(m_oMat, tMat, tThresh);// tDelta);
  145. tMat.copyTo(tThresh);
  146. //threshold(tDelta, tThresh, 50, 255, THRESH_BINARY);
  147. // convert our OpenCV matrix object to one of type IplImage
  148. if (m_pIplWork != NULL) {
  149. delete(m_pIplWork);
  150. };
  151. m_pIplWork = new IplImage(tThresh);
  152. };
  153. if(m_bCursorIsSet == false){
  154. m_nCursorX = Picture.cols / 2;
  155. m_nCursorY = Picture.rows / 2;
  156. m_bCursorIsSet = true;
  157. };
  158. /*
  159. // Set up the detector with default parameters.
  160. SimpleBlobDetector detector;
  161. // Detect blobs.
  162. std::vector<KeyPoint> keypoints;
  163. detector.detect(Picture, keypoints);
  164. // Draw detected blobs as red circles.
  165. // DrawMatchesFlags::DRAW_RICH_KEYPOINTS flag ensures the size of the circle corresponds to the size of blob
  166. //Mat im_with_keypoints;
  167. if (keypoints.size()> 0) drawKeypoints(tThresh, keypoints, tThresh, Scalar(0, 0, 255), DrawMatchesFlags::DRAW_RICH_KEYPOINTS);
  168. */
  169. // cvblobslib blob extraction
  170. blob_result = CBlobResult( m_pIplWork, NULL, 0 );//, false );
  171. // filter blobs with area less than min_area units
  172. blob_result.Filter( blob_result, B_EXCLUDE, CBlobGetArea(), B_LESS, m_nMinArea );
  173. // clear the blobs from two frames ago
  174. blobs_previous.clear();
  175. // before we populate the blobs vector with the current frame, we need to store the live blobs in blobs_previous
  176. for (int i = 0; i < blobs.size(); i++)
  177. //if( blobs[i].event != BLOB_STEADY )
  178. blobs_previous.push_back(blobs[i]);
  179. // populate the blobs vector with the current frame
  180. blobs.clear();
  181. for (int i = 1; i < blob_result.GetNumBlobs(); i++){
  182. temp.ConvexHull.clear();
  183. current_blob = blob_result.GetBlob(i);
  184. if (m_bUsePolygones){
  185. CBlobContour *tContour;
  186. tContour = current_blob.GetExternalContour();
  187. if (tContour != NULL){
  188. //if (!tContour->IsEmpty()){
  189. if (!tContour->IsEmpty()){
  190. for (int dex = 0; dex < tContour->GetContourPoints()->total; dex++){
  191. point2D p2D;
  192. CvPoint* p = (CvPoint*)cvGetSeqElem(tContour->GetContourPoints(), dex);
  193. p2D.x = p->x;
  194. p2D.y = p->y;
  195. temp.ConvexHull.push_back(p2D);
  196. };
  197. };
  198. };
  199. };
  200. x = XCenter(current_blob);
  201. y = YCenter(current_blob);
  202. tRect = current_blob.GetBoundingBox();
  203. if (m_bReduceSize){
  204. temp.location.x = temp.origin.x = x * 2.f;
  205. temp.location.y = temp.origin.y = y * 2.f;
  206. tRect.height = tRect.height << 1;
  207. tRect.width = tRect.width << 1;
  208. tRect.x = tRect.x << 1;
  209. tRect.y = tRect.y << 1;
  210. }else{
  211. temp.location.x = temp.origin.x = x;
  212. temp.location.y = temp.origin.y = y;
  213. tRect.height = tRect.height;
  214. tRect.width = tRect.width;
  215. tRect.x = tRect.x;
  216. tRect.y = tRect.y;
  217. };
  218. //temp.ConvexHull = current_blob.GetConvexHull();
  219. temp.box = tRect;
  220. temp.area = current_blob.Area();
  221. temp.event = BLOB_NEW;
  222. temp.id = current_blob.GetID();
  223. temp.color = Scalar(128, 128, 128);
  224. temp.tracked = false;
  225. temp.weight = 0;
  226. blobs.push_back(temp);
  227. };
  228. // initialize previous blobs to untracked
  229. for (int i = 0; i < blobs_previous.size(); i++) blobs_previous[i].tracked = false;
  230. bool history = false;
  231. memset(&m_aSegments, 0, V_SEGMENT_SIZE*H_SEGMENT_SIZE);
  232. // main tracking loop -- O(n^2) -- simply looks for a blob in the previous frame within a specified radius
  233. float dx, dy; int rrr;
  234. m_fSegmentStepX = ((float)Picture.cols) / ((float)H_SEGMENT_SIZE);
  235. m_fSegmentStepY = ((float)Picture.rows) / ((float)V_SEGMENT_SIZE);
  236. for (int i = 0; i < blobs.size(); i++) {
  237. for (int j = 0; j < blobs_previous.size(); j++) {
  238. // Skip already connected
  239. if (blobs_previous[j].tracked == true) continue;
  240. dx = blobs[i].location.x - blobs_previous[j].location.x;
  241. dy = blobs[i].location.y - blobs_previous[j].location.y;
  242. rrr = (int)sqrt(pow(dx, 2.0f) + pow(dy, 2.0f));
  243. if ( rrr < m_nMaxRadius ){
  244. blobs[i].event = BLOB_STEADY;
  245. blobs[i].color = Scalar(0, 255, 0 );
  246. blobs[i].id = blobs_previous[j].id;
  247. blobs[i].weight = blobs_previous[j].weight+1;
  248. blobs[i].origin.x = history ? blobs_previous[j].origin.x : blobs_previous[j].location.x;
  249. blobs[i].origin.y = history ? blobs_previous[j].origin.y : blobs_previous[j].location.y;
  250. blobs_previous[j].tracked = true;
  251. // Done with that point
  252. break;
  253. };
  254. };
  255. // If did not found old point
  256. if (blobs[i].event == BLOB_NEW){
  257. blobs[i].origin.x = blobs[i].location.x;
  258. blobs[i].origin.y = blobs[i].location.y;
  259. } else {
  260. dx = blobs[i].origin.x - blobs[i].location.x;
  261. dy = blobs[i].origin.y - blobs[i].location.y;
  262. rrr = (int)sqrt(pow(dx, 2.0f) + pow(dy, 2.0f));
  263. if( rrr > 10 ){
  264. blobs[i].event = BLOB_MOVE;
  265. blobs[i].color = Scalar(0, 0, 255);
  266. // blobs[i].dir = atanf(dx / dy);
  267. };// else blobs[i].dir = 0.f;
  268. };
  269. //if (blobs[i].event == BLOB_MOVE ) m_aSegments[(U8)(blobs[i].location.y / m_fSegmentStepY)][(U8)(blobs[i].location.x / m_fSegmentStepX)]++;
  270. // Rect Intersect bool intersects = ((A & B).area() > 0);
  271. };
  272. for (int i = 0; i < blobs.size() / 2; i++) {
  273. if (blobs[i].event == BLOB_MOVE){
  274. for (int j = i + 1; j < blobs.size(); j++){
  275. if (blobs[j].event == BLOB_MOVE){
  276. if (intersectBox(blobs[i].box, blobs[j].box)){
  277. increaseBy(blobs[i].box, blobs[j].box);
  278. blobs[j].event = BLOB_NEW;
  279. };
  280. };
  281. };
  282. };
  283. };
  284. // add any blobs from the previous frame that weren't tracked as having been removed
  285. //for (int i = 0; i < blobs_previous.size(); i++) {
  286. // if (!blobs_previous[i].tracked) {
  287. // blobs_previous[i].event = BLOB_NEW;
  288. // blobs.push_back(blobs_previous[i]);
  289. // };
  290. //};
  291. /*
  292. if( m_pIplOrig != NULL ) {
  293. if( m_pIplThr== NULL ){
  294. m_pIplThr = cvCreateImage( cvGetSize( m_pIplOrig ), IPL_DEPTH_8U, 1 );
  295. };
  296. if( m_pIplWork == NULL ){
  297. m_pIplWork = new IplImage( tMat );
  298. } else {
  299. delete( m_pIplWork );
  300. m_pIplWork = new IplImage( tMat );
  301. };
  302. } else {
  303. m_pIplOrig = new IplImage( tMat );
  304. return false;
  305. };
  306. // threshold input image
  307. cvThreshold( m_pIplOrig, m_pIplThr, m_nParam1, 255, CV_THRESH_BINARY );
  308. // get blobs and filter them using its area
  309. // find blobs in image
  310. //if( m_vBlobs != NULL ) delete( m_vBlobs );
  311. m_vBlobs = new CBlobResult( m_pIplThr, NULL, 255 );
  312. if( m_vBlobs!= NULL ){
  313. m_vBlobs->Filter( *m_vBlobs, B_EXCLUDE, CBlobGetArea(), B_LESS, m_nParam2 );
  314. return true;
  315. }; */
  316. return true;
  317. };
  318. vector<cBlob>& blobDetector::getBlobs() {
  319. return blobs;
  320. };
  321. bool blobDetector::lookupMaxActiveSegment(U8 &x, U8 &y){
  322. int MaxVal = 0;
  323. x = y = 0;
  324. for (U8 i = 0; i < V_SEGMENT_SIZE; i++)
  325. for (U8 j = 0; j < H_SEGMENT_SIZE; j++){
  326. if (m_aSegments[i][j] > MaxVal){
  327. x = j;
  328. y = i;
  329. MaxVal = m_aSegments[i][j];
  330. };
  331. };
  332. if (MaxVal == 0)return false;
  333. return true;
  334. };
  335. void blobDetector::markup(Mat &Picture){
  336. char Buffer[200];
  337. sprintf( Buffer, "Blobs: %d", blobs.size() );
  338. rectangle( Picture, Point( Picture.cols-220, 5 ), Point( Picture.cols-120, 15 ), Scalar( 0, 255, 255 ), CV_FILLED );
  339. putText( Picture, Buffer, Point( Picture.cols-215, 13 ), FONT_HERSHEY_SIMPLEX, 0.3, Scalar( 255, 0, 0 ), 1 );
  340. if (m_bShowPolygones || m_bShowRectangles || m_bShowCentres){
  341. for (int i = 0; i < blobs.size(); i++) {
  342. if (blobs[i].event == BLOB_MOVE){
  343. //rectangle(Picture, blobs[i].box, blobs[i].color, 2);// CV_FILLED);
  344. //sprintf(Buffer, "%d:%d [%.2f]", blobs[i].id, blobs[i].weight, blobs[i].area );
  345. //putText(Picture, Buffer, Point(blobs[i].box.x, blobs[i].box.y), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(255, 0, 0), 1);
  346. //circle(Picture, Point(blobs[i].location.x, blobs[i].location.y), 5, blobs[i].color, 2, 8);
  347. //if (m_bShowRectangles )
  348. rectangle(Picture, blobs[i].box, blobs[i].color, 2);
  349. if (m_bShowPolygones){
  350. for (int j = 0; j < blobs[i].ConvexHull.size() - 1; j++) {
  351. Point p1, p2;
  352. //CV_READ_SEQ_ELEM(pt[0], reader);
  353. //CV_READ_SEQ_ELEM(pt[1], reader);
  354. p1.x = blobs[i].ConvexHull.at(j).x;
  355. p1.y = blobs[i].ConvexHull.at(j).y;
  356. p2.x = blobs[i].ConvexHull.at(j + 1).x;
  357. p2.y = blobs[i].ConvexHull.at(j + 1).y;
  358. //cvLine(&Picture, p1, p2, CV_RGB(255, 0, 0));
  359. line(Picture, p1, p2, Scalar(0, 255, 255));
  360. };
  361. };
  362. if (m_bShowCentres) circle(Picture, Point(blobs[i].origin.x, blobs[i].origin.y), 1, blobs[i].color, 1, 8);
  363. //circle(Picture, Point(blobs[i].location.x, blobs[i].location.y), 15, Scalar(255, 255, 255), 2, 8);
  364. } else {
  365. if (m_bShowCentres) circle(Picture, Point(blobs[i].origin.x, blobs[i].origin.y), 1, blobs[i].color, 1, 8);
  366. if (m_bShowPolygones){
  367. for (int j = 0; j < blobs[i].ConvexHull.size() - 1; j++) {
  368. Point p1, p2;
  369. p1.x = blobs[i].ConvexHull.at(j).x;
  370. p1.y = blobs[i].ConvexHull.at(j).y;
  371. p2.x = blobs[i].ConvexHull.at(j + 1).x;
  372. p2.y = blobs[i].ConvexHull.at(j + 1).y;
  373. line(Picture, p1, p2, Scalar(0, 0, 255));
  374. };
  375. };
  376. };
  377. };
  378. };
  379. /*
  380. U8 X, Y;
  381. int x, y, w, h;
  382. int mCamControllNew;
  383. if (blobs.size() > 0){
  384. if (lookupMaxActiveSegment(X, Y)){
  385. mCamControllNew = 0;
  386. x = (int)X*m_fSegmentStepX;
  387. y = (int)Y*m_fSegmentStepY;
  388. w = (int)m_fSegmentStepX;
  389. h = (int)m_fSegmentStepY;
  390. //sprintf(Buffer, "%d", m_aSegments[Y][X]);
  391. //putText(Picture, Buffer,Point( x, y ), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(255, 0, 255), 1);
  392. rectangle(Picture, Rect(x, y, w, h), Scalar(255, 128, 0), 4);
  393. if (X == 0){
  394. m_nStepX = -(m_aSegments[Y][X] - blobs.size() / 9);
  395. mCamControllNew &= ~3;
  396. mCamControllNew |= 2;
  397. } else if (X == 1){
  398. m_nStepX = 0;
  399. m_nCursorX = Picture.cols / 2;
  400. mCamControllNew &= ~3;
  401. } else if (X == 2){
  402. m_nStepX = m_aSegments[Y][X] - blobs.size() / 9;
  403. mCamControllNew &= ~3;
  404. mCamControllNew |= 1;
  405. };
  406. if (Y == 0){
  407. m_nStepY = -(m_aSegments[Y][X] - blobs.size() / 9);
  408. mCamControllNew &= ~12;
  409. mCamControllNew |= 8;
  410. } else if (Y == 1 ){
  411. m_nStepY = 0;
  412. m_nCursorY = Picture.rows / 2;
  413. mCamControllNew &= ~12;
  414. } else if (Y == 2){
  415. m_nStepY = m_aSegments[Y][X] - blobs.size() / 9;
  416. mCamControllNew &= ~12;
  417. mCamControllNew |= 4;
  418. };
  419. m_nCursorX += m_nStepX;
  420. m_nCursorY += m_nStepY;
  421. if( m_nCamControllCurrent ){
  422. int HW, HH;
  423. float s1, s2;
  424. HW = Picture.cols / 2;
  425. HH = Picture.rows / 2;
  426. s1 = (float)((m_nCursorX < HW) ? (HW - m_nCursorX) : (m_nCursorX - HW)) / (float)HW;
  427. s2 = (float)((m_nCursorY < HH) ? (HH - m_nCursorY) : (m_nCursorY - HH)) / (float)HH;
  428. //m_pProtocol->setPTZSpeed(s1,s2,0.);
  429. //if (m_pPTZ != NULL) m_pPTZ->setSpeed(s1);
  430. }
  431. if (m_nCursorX < 0)m_nCursorX = 0;
  432. if (m_nCursorX > Picture.cols)m_nCursorX = Picture.cols;
  433. if (m_nCursorY < 0)m_nCursorY = 0;
  434. if (m_nCursorY > Picture.rows)m_nCursorY = Picture.rows;
  435. if (m_nCamControllCurrent != mCamControllNew){
  436. if (m_pPTZ != NULL){
  437. switch (mCamControllNew){
  438. case 0:
  439. m_pPTZ->cmdEndManualTilt();
  440. m_pPTZ->cmdEndManualPan();
  441. break;
  442. case 1:
  443. m_pPTZ->cmdBeginPanRight();
  444. break;
  445. case 2:
  446. m_pPTZ->cmdBeginPanLeft();
  447. break;
  448. case 4:
  449. m_pPTZ->cmdBeginTiltDown();
  450. break;
  451. case 5:
  452. //m_pProtocol->goDownRight();
  453. m_pPTZ->cmdBeginPanRight();
  454. m_pPTZ->cmdBeginTiltDown();
  455. break;
  456. case 6:
  457. //m_pProtocol->goDownLeft();
  458. m_pPTZ->cmdBeginPanLeft();
  459. m_pPTZ->cmdBeginTiltDown();
  460. break;
  461. case 8:
  462. m_pPTZ->cmdBeginTiltUp();
  463. break;
  464. case 9:
  465. //m_pProtocol->goUpRight();
  466. m_pPTZ->cmdBeginPanRight();
  467. m_pPTZ->cmdBeginTiltUp();
  468. break;
  469. case 10:
  470. //m_pProtocol->goUpLeft();
  471. m_pPTZ->cmdBeginPanLeft();
  472. m_pPTZ->cmdBeginTiltUp();
  473. break;
  474. };
  475. };
  476. m_nCamControllCurrent = mCamControllNew;
  477. };
  478. };
  479. };
  480. // Cursor
  481. line(Picture, Point(m_nCursorX, 0), Point(m_nCursorX, Picture.rows), Scalar(255, 255, 0), 2);
  482. line(Picture, Point(0, m_nCursorY), Point(Picture.cols, m_nCursorY), Scalar(255, 255, 0), 2);
  483. //*/
  484. // PIP
  485. if (m_bPIP){
  486. Mat resizedSrc;
  487. Mat subView;
  488. Mat convertedSrc;
  489. /*
  490. if (!m_oFilter->filterFrame().empty()){
  491. resize(m_oFilter->filterFrame(), resizedSrc, Size(160, 120), 0.0, 0.0, INTER_AREA);
  492. subView = Picture(Rect((Picture.cols - 510), 50 + 120, 160, 120));
  493. cvtColor(resizedSrc, convertedSrc, CV_GRAY2BGR);
  494. convertedSrc.copyTo(subView);
  495. };
  496. if (!m_oFilter->gaussianFrame().empty()){
  497. resize(m_oFilter->gaussianFrame(), resizedSrc, Size(160, 120), 0.0, 0.0, INTER_AREA);
  498. subView = Picture(Rect((Picture.cols - 340), 50 + 120, 160, 120));
  499. cvtColor(resizedSrc, convertedSrc, CV_GRAY2BGR);
  500. convertedSrc.copyTo(subView);
  501. };*/
  502. if (m_nPrepMethod){
  503. if (!m_oFilter->thresholdFrame().empty()){
  504. resize(m_oFilter->thresholdFrame(), resizedSrc, Size(160, 120), 0.0, 0.0, INTER_AREA);
  505. subView = Picture(Rect((Picture.cols - 170), 50 + 120, 160, 120));
  506. cvtColor(resizedSrc, convertedSrc, CV_GRAY2BGR);
  507. convertedSrc.copyTo(subView);
  508. };
  509. } else {
  510. if (!tThresh.empty()){
  511. resize(tThresh, resizedSrc, Size(160, 120), 0.0, 0.0, INTER_AREA);
  512. subView = Picture(Rect((Picture.cols - 170), 50 + 120, 160, 120));
  513. cvtColor(resizedSrc, convertedSrc, CV_GRAY2BGR);
  514. convertedSrc.copyTo(subView);
  515. };
  516. };
  517. };
  518. };