forked from jesusgollonet/ofxCvWatershed
/
ofxCvWatershed.cpp
135 lines (104 loc) · 4.26 KB
/
ofxCvWatershed.cpp
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
//
// ofxCvWatershed.cpp
// watershedTest
//
// Created by Jesus Gollonet on 5/4/11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#include "ofxCvWatershed.h"
void ofxCvWatershed::setup(unsigned char * px, int _w, int _h){
bCurrentlyMarking = false;
storage = cvCreateMemStorage(1000);
contours = 0;
iplTargetImg = cvCreateImage(cvSize(_w, _h), IPL_DEPTH_8U, 3);
iplMarkersTempImg = cvCreateImage(cvSize(_w, _h), IPL_DEPTH_8U, 1);
iplMarkers32sImg = cvCreateImage(cvSize(_w, _h), IPL_DEPTH_32S, 1);
cvZero(iplMarkersTempImg);
memcpy( iplTargetImg->imageData, px, _w*_h*3);
targetImg.allocate(_w,_h);
targetImg.setFromPixels(px, _w, _h);
markersImg.allocate(_w,_h);
watershedImg.allocate(_w,_h);
watershedGrayImg.allocate(_w,_h);
markersImg.set(0);
watershedGrayImg.set(0);
bCanDraw = true;
}
void ofxCvWatershed::addToCurrentMarker(int _x, int _y) {
if (!bCurrentlyMarking) {
bCurrentlyMarking = true;
currentMarkers.clear();
currentMarkers.push_back(ofPoint(_x, _y));
}
CvPoint prevPt = cvPoint(currentMarkers.back().x, currentMarkers.back().y);
CvPoint currentPt = cvPoint(_x, _y);
cvLine( iplMarkersTempImg, prevPt, currentPt, cvScalarAll(255), 5, 8, 0 );
markersImg = iplMarkersTempImg;
currentMarkers.push_back(ofPoint(_x, _y));
}
void ofxCvWatershed::finishCurrentMarker() {
bCurrentlyMarking = false;
}
void ofxCvWatershed::segment() {
reset();
int nContours = cvFindContours( iplMarkersTempImg, storage, &contours, sizeof(CvContour),
CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE );
int i, j, compCount = 0;
cvZero( iplMarkers32sImg );
for( ; contours != 0; contours = contours->h_next, compCount++ ) {
cvDrawContours(
iplMarkers32sImg,
contours,
cvScalarAll(compCount+1),
cvScalarAll(compCount+1),
-1, -1, 8,
cvPoint(0,0)
);
}
CvRNG rng = cvRNG(-1);
colors = cvCreateMat( 1, compCount, CV_8UC3 );
for( i = 0; i < compCount; i++ ) {
uchar* ptr = colors->data.ptr + i*3;
// no colors for now.
ptr[0] = (uchar)0;//(cvRandInt(&rng)%180 + 50);
ptr[1] = (uchar)0;//(cvRandInt(&rng)%180 + 50);
ptr[2] = (uchar)0;//(cvRandInt(&rng)%180 + 50);
}
cvWatershed( iplTargetImg, iplMarkers32sImg );
// paint the watershed image
for( i = 0; i < iplMarkers32sImg->height; i++ ) {
for( j = 0; j < iplMarkers32sImg->width; j++ ) {
int idx = CV_IMAGE_ELEM( iplMarkers32sImg, int, i, j );
uchar* dst = &CV_IMAGE_ELEM( iplTargetImg, uchar, i, j*3 );
if( idx == -1 ) {
dst[0] = dst[1] = dst[2] = (uchar)255;
} else if( idx <= 0 || idx > compCount ){
dst[0] = dst[1] = dst[2] = (uchar)0; // should not get here
}else {
uchar* ptr = colors->data.ptr + (idx-1)*3;
dst[0] = ptr[0]; dst[1] = ptr[1]; dst[2] = ptr[2];
}
}
}
//cvAddWeighted( watershed, 0.5, colorImg.getCvImage(), 0.5, 0, watershed );
watershedImg = iplTargetImg;
watershedGrayImg = watershedImg;
watershedGrayImg.threshold(140);
//watershedGrayImg.invert();
printf("contorus %i", contourFinder.findContours( watershedGrayImg, 10,
(watershedImg.width * watershedImg.height)/ 2.f,
20, true));
}
void ofxCvWatershed::draw(int _x, int _y) {
if(bCanDraw) {
if(targetImg.bAllocated) targetImg.draw(_x, _y);
if(markersImg.bAllocated) markersImg.draw(_x + targetImg.width, _y);
if(watershedImg.bAllocated) watershedImg.draw(_x + targetImg.width * 2, _y);
if(watershedGrayImg.bAllocated) watershedGrayImg.draw(_x + targetImg.width * 2, _y + targetImg.height);
contourFinder.draw(_x + targetImg.width * 2, _y + targetImg.height);
}
}
void ofxCvWatershed::reset() {
// cvReleaseImage( &cvImage );
// cvReleaseImage( &cvImageTemp );
}