//-------------------------------------------------------------- void testApp::mousePressed(int x, int y, int button){ if (button == OF_MOUSE_BUTTON_1) { // ofSendMessage("Hello"); // Catch the Bot! for (vector<ofxCvBlob>::iterator blob_it = blobs.begin(); blob_it != blobs.end(); blob_it++) { if (blob_it->boundingRect.inside(x, y)) { bots.insert(pair<int, Bot>(potential_bot_id, Bot(*blob_it, potential_bot_id, colors[botColorNum]))); botColorNum = (botColorNum + 1) % 18; potential_bot_id++; blobs.erase(blob_it); break; } } } }
int main(void) { Bot a = Bot(); float c_rgb[3]; GLFWwindow* window; glfwSetErrorCallback(error_callback); if (!glfwInit()) exit(EXIT_FAILURE); window = glfwCreateWindow(640, 480, "Simple example", NULL, NULL); if (!window) { glfwTerminate(); exit(EXIT_FAILURE); } glfwMakeContextCurrent(window); glfwSwapInterval(1); glfwSetKeyCallback(window, key_callback); while (!glfwWindowShouldClose(window)) { float ratio; int width, height; glfwGetFramebufferSize(window, &width, &height); ratio = width / (float)height; glViewport(0, 0, width, height); glClear(GL_COLOR_BUFFER_BIT); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-ratio, ratio, -1.f, 1.f, 1.f, -1.f); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glRotatef((float)glfwGetTime() * 50.f, 0.f, 0.f, 1.f); glBegin(GL_TRIANGLES); a.getColor(c_rgb); glColor3f(c_rgb[0], c_rgb[1], c_rgb[2]); glVertex3f(-0.6f, -0.4f, 0.f); //glColor3f(0.f, 1.f, 0.f); glVertex3f(0.6f, -0.4f, 0.f); //glColor3f(0.f, 0.f, 1.f); glVertex3f(0.f, 0.6f, 0.f); glEnd(); glfwSwapBuffers(window); glfwPollEvents(); } glfwDestroyWindow(window); glfwTerminate(); exit(EXIT_SUCCESS); }
//-------------------------------------------------------------- void testApp::update() { bool bNewFrame = false; #ifdef _USE_LIVE_VIDEO vidGrabber.update(); bNewFrame = vidGrabber.isFrameNew(); #else vidPlayer.update(); bNewFrame = vidPlayer.isFrameNew(); #endif if (bNewFrame) { #ifdef _USE_LIVE_VIDEO colorImg.setFromPixels(vidGrabber.getPixels(), vidSize.x, vidSize.y); #else colorImg.setFromPixels(vidPlayer.getPixels(), vidPlayer.width,vidPlayer.height); #endif grayImage = colorImg; if (shouldSetNewBackGround == true) { grayBg = grayImage; // the = sign copys the pixels from grayImage into grayBg bots.clear(); shouldSetNewBackGround = false; } // take the abs value of the difference between background and incoming and then threshold: grayDiff.absDiff(grayBg, grayImage); grayDiff.threshold(thresholdSlider); // dilate the blobs on the screen; compensating for blob-splitting by lines on grid for (int i = 0; i < dilateSlider; i++) grayDiff.dilate(); // find contours which are between the size of 1/300 pixels and 1/3 the w*h pixels. contourFinder.findContours(grayDiff, (colorImg.height * colorImg.width)/300, (colorImg.height * colorImg.width)/3, 10, false); // find bots // get all the blobs found in the contourFinder blobs = vector<ofxCvBlob>(contourFinder.blobs.begin(), contourFinder.blobs.end()); // if the contourFinder didn't find any blobs, remove all the bots stored. if (blobs.size() == 0) { bots.clear(); potential_bot_id = 0; botColorNum = 0; } else { // vector iterator that will contain the pointer to the beginning of blobs vector<ofxCvBlob>::iterator vect_it; // remove any elements that may resise in black_list black_list.clear(); // updated the positions of the blobs for (map<int, Bot>::iterator it = bots.begin(); it != bots.end(); it++) { // this will contain the offset of one pointer to the pointer of first element int offset = it->second.updatePosition(blobs); vect_it = blobs.begin(); std::advance(vect_it, offset); if (offset == -1) { // push key to bot that no longer exist black_list.push_back(it->first); } else { blobs.erase(vect_it); } } // if there are any keys in black_list, remove them from bots map for (vector<int>::iterator it = black_list.begin(); it != black_list.end(); it++) { bots.erase(*it); } if (shouldCaptureNewBot) { if (blobs.size() > 0) { bots.insert(pair<int, Bot>(newBotID, Bot(*blobs.begin(), newBotID, colors[botColorNum]))); botColorNum = (botColorNum + 1) % 18; shouldCaptureNewBot = false; } } else if (autoFindBots) { // insert newly-found blobs, if there are any for (vector<ofxCvBlob>::iterator it = blobs.begin(); it != blobs.end(); it++) { bots.insert(pair<int, Bot>(potential_bot_id, Bot(*it, potential_bot_id, colors[botColorNum]))); botColorNum = (botColorNum + 1) % 18; potential_bot_id++; } } if (shouldBeginSendingBotInfo) { zmqThread.setBots(bots); zmqThread.setBlobs(blobs); } } ofPoint size; size.x = colorImg.width; size.y = colorImg.height; zmqThread.setFrameDimensions(size); } switch (vidID) { case 0: displayedImage = colorImg; label = "Color Image"; break; case 1: displayedImage = grayBg; label = "Gray Bg Image"; break; case 2: displayedImage = grayImage; label = "Gray Image"; break; case 3: displayedImage = grayDiff; label = "Subtracted Image"; break; default: displayedImage = colorImg; label = "Color Image"; break; } }