@alexandrosdrymonitis wrote:
I want to highlight specific parts of an image (a music score), in a row. I have created the same image twice, once in black and white and once with some grey parts included. I’m using ofxCvContourFinder to detect contours between the two images. The whole thing works, but the contours appear in an order that looks quite random to me. Maybe they are being sorted by their Y coordinate.
Here’s my code (ofApp.cpp):
#include "ofApp.h" //-------------------------------------------------------------- void ofApp::setup(){ // ofxOpenCV doesn't have image loading. // So first, load the .png file into a temporary ofImage. ofImage scoreOfImage; scoreOfImage.loadImage("test_score.png"); scoreOfImage.setImageType(OF_IMAGE_GRAYSCALE); ofImage scoreOfImage2; scoreOfImage2.loadImage("test_score2.png"); scoreOfImage2.setImageType(OF_IMAGE_GRAYSCALE); // Set the ofxCvImage from the pixels of this ofImage. int imgW = scoreOfImage.getWidth(); int imgH = scoreOfImage.getHeight(); unsigned char *scorePixels = scoreOfImage.getPixels().getData(); grayImage.setFromPixels(scorePixels, imgW, imgH); imgWglobal = imgW; imgHglobal = imgH; int imgW2 = scoreOfImage2.getWidth(); int imgH2 = scoreOfImage2.getHeight(); unsigned char *scorePixels2 = scoreOfImage2.getPixels().getData(); grayBg.setFromPixels(scorePixels2, imgW2, imgH2); grayDiff.allocate(imgW, imgH); grayDiff.absDiff(grayBg, grayImage); grayDiff.threshold(30); contourFinder.findContours(grayDiff, 5, (imgW*imgH)/4, 4, false, true); timeRef = ofGetElapsedTimeMillis(); timeDiff = 1000; idx = 0; } //-------------------------------------------------------------- void ofApp::update(){ timeStamp = ofGetElapsedTimeMillis(); if ((timeStamp - timeRef) > timeDiff) { idx++; if (idx >= contourFinder.nBlobs) { idx = 0; } timeRef = ofGetElapsedTimeMillis(); } } //-------------------------------------------------------------- void ofApp::draw(){ ofSetHexColor(0xffffff); grayBg.draw(0, 0, imgWglobal, imgHglobal); contourFinder.draw(imgWglobal, 0, imgWglobal, imgHglobal); ofColor c(255, 0, 0); ofRectangle r = contourFinder.blobs.at(idx).boundingRect; ofSetColor(c); ofDrawRectangle(r); }
And here are the two images I’m using:
The code is supposed to highlight the first note, then the first bar, then the rest, and lastly the second bar, but it highlights the first note, the rest, the second bar, and lastly the first bar.
Can someone help me out with sorting the contours in a desired way?
Posts: 1
Participants: 1