Thanks.
It works well now, Not sure why. What I did is just : comment other parts of the code until only left the button…and bad access still happens.And then I close the file and reopen it. The crash never happen again. I put back other part of the code, works well.
Wired. So I still past my mess code here:
//--------------------------------------------------------------
void ofApp::setup(){
ofBackground(0,0,0);
w = 320;
h = 240;
movie.initGrabber(w, h, true);
//reserve memory for cv images
rgb.allocate(w, h);
hsb.allocate(w, h);
hue.allocate(w, h);
sat.allocate(w, h);
bri.allocate(w, h);
filtered.allocate(w, h);
//osc
sender.setup(HOST, PORT);
uIsRecord= true;
uIsPlay=false;
bIsPressing=false;
}
//--------------------------------------------------------------
void ofApp::update(){
movie.update();
if (movie.isFrameNew()) {
//copy webcam pixels to rgb image
rgb.setFromPixels(movie.getPixels(), w, h);
//mirror horizontal
rgb.mirror(false, true);
//duplicate rgb
hsb = rgb;
//convert to hsb
hsb.convertRgbToHsv();
//store the three channels as grayscale images
hsb.convertToGrayscalePlanarImages(hue, sat, bri);
//filter image based on the hue value were looking for
for (int i=0; i<w*h; i++) {
filtered.getPixels()[i] = ofInRange(hue.getPixels()[i],findHue-5,findHue+5) ? 255 : 0;
}
filtered.flagImageChanged();
//run the contour finder on the filtered image to find blobs with a certain hue
contours.findContours(filtered, 500, w*h/2, 1, false);
std:ofSort(contours.blobs,my_compare);//sort bolbs by x position
}
}
//--------------------------------------------------------------
void ofApp::draw(){
if(uIsRecord)
{
drawRecord();
}
else if (uIsPlay)
{
drawPlay();
}
}
void ofApp::drawRecord(){
// show camera view
ofSetColor(255,255,255);
//draw all cv images
rgb.draw(0,0);
filtered.draw(320,0);
contours.draw(320,0);
// recording button
ofSetColor(255, 0, 0);
ofFill();
int marginX = ofGetWidth() * 0.4;
int marginY = ofGetHeight() * 0.5;
recordButton = ofRectangle(marginX, marginY, ofGetWidth() * 0.15, ofGetWidth() * 0.05);
ofSetColor(200,0,0);
if (bIsPressing) {
ofNoFill();
}
ofRect(recordButton);
ofFill();
//draw red circles for found blobs
ofxOscMessage m;
m.setAddress("/test");
for (int i=0; i<contours.nBlobs; i++) {
ofCircle(contours.blobs[i].centroid.x, contours.blobs[i].centroid.y, 20);
ofDrawBitmapString ( "i =" + ofToString (i, 1), 4, 10*i+80);
ofDrawBitmapString ( "x =" + ofToString (contours.blobs[i].boundingRect.x, 5), 64, 10*i+80);
ofDrawBitmapString ( "y =" + ofToString (contours.blobs[i].boundingRect.y, 5), 264, 10*i+80);
bool flag=true;
bool flag2=true;
bool flag3=true;
int leftx=contours.blobs[0].boundingRect.x;
int rightx=contours.blobs[0].boundingRect.x+contours.blobs[0].boundingRect.width;
int point1x=leftx+ (rightx-leftx)/6;
int point2x=leftx+ (rightx-leftx)/2;
int point3x=leftx+ (rightx-leftx)/6*5;
for (int index=0; index <contours.blobs[0].nPts; index++)
{
if ((contours.blobs[0].pts[index].x >= point1x)&&(contours.blobs[0].pts[index].x <= point1x+5)&&(flag==true))
{ ofDrawBitmapString("minx[" + ofToString (contours.blobs[0].pts[index].x) + ofToString (contours.blobs[0].pts[index].y) + "]", 500, 200);
flag=false;
}
if ((contours.blobs[0].pts[index].x >= point2x)&&(contours.blobs[0].pts[index].x <= point2x+5)&&(flag2==true))
{ ofDrawBitmapString("minx[" + ofToString (contours.blobs[0].pts[index].x) + ofToString (contours.blobs[0].pts[index].y) + "]", 500, 250);
flag2=false;
}
if ((contours.blobs[0].pts[index].x >= point3x)&&(contours.blobs[0].pts[index].x <= point3x+5)&&(flag3==true))
{ ofDrawBitmapString("minx[" + ofToString (contours.blobs[0].pts[index].x) + ofToString (contours.blobs[0].pts[index].y) + "]", 500, 300);
flag3=false;
}
}
int Xmessage = contours.blobs[i].boundingRect.x;
int Ymessage = contours.blobs[i].boundingRect.y;
int Wmessage = contours.blobs[i].boundingRect.width;
m.addIntArg(Xmessage);
m.addIntArg(Ymessage);
m.addIntArg(Wmessage);
}
sender.sendMessage(m);
}
void ofApp::drawPlay(){
}
//--------------------------------------------------------------
void ofApp::keyPressed(int key){
if(key == 'a' || key == 'A'){
}
}
//--------------------------------------------------------------
void ofApp::keyReleased(int key){
}
//--------------------------------------------------------------
void ofApp::mouseMoved(int x, int y ){
}
//--------------------------------------------------------------
void ofApp::mouseDragged(int x, int y, int button){
}
//--------------------------------------------------------------
void ofApp::mousePressed(int x, int y, int button){
//calculate local mouse x,y in image
int mx = x % w;
int my = y % h;
//get hue value on mouse position
if ((x>0)&&(x<320)&&(y>0)&&(y<240))
{findHue = hue.getPixels()[my*w+mx];}
if (recordButton.inside(x, y)) {
bIsPressing = true;}
}
//--------------------------------------------------------------
void ofApp::mouseReleased(int x, int y, int button){
if (uIsRecord){
bIsPressing=false;
}
}
//--------------------------------------------------------------
void ofApp::windowResized(int w, int h){
}
//--------------------------------------------------------------
void ofApp::gotMessage(ofMessage msg){
}
//--------------------------------------------------------------
void ofApp::dragEvent(ofDragInfo dragInfo){
}