Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F80558035
BlobDetection.pde
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Sun, Sep 1, 09:25
Size
3 KB
Mime Type
text/x-c++
Expires
Tue, Sep 3, 09:25 (1 d, 23 h)
Engine
blob
Format
Raw Data
Handle
20394075
Attached To
rBAFOURPROJECT InfoVisuGit
BlobDetection.pde
View Options
import java.util.ArrayList;
import java.util.List;
import java.util.TreeSet;
class BlobDetection {
PImage findConnectedComponents(PImage img, boolean onlyBiggest) {
PImage img0=img.copy();
int [] labels= new int [img0.width*img0.height];
List<TreeSet<Integer>> labelsEquivalences= new ArrayList<TreeSet<Integer>>();
ArrayList<Integer> count=new ArrayList<Integer>();
int currentLabel=0;
for (int line=0; line < img0.height; ++line) {
TreeSet<Integer> adjColor=new TreeSet<Integer>();
for (int x=0; x < img0.width; x++) {
if (img0.pixels[line*img0.width+x]==color(255)) {
adjColor.clear();
if (line>0) {
for (int i=x-1; i <= x+1; i++) {//check the three
if (0<=i && i<img0.width && labels[(line-1)*img0.width+i]!=0) {
adjColor.add(labels[(line-1)*img0.width+i]);
}
}
}
if (0<x && labels[line*img0.width+x-1]!=0) {
adjColor.add(labels[line*img0.width+x-1]);
}
//println(minColor==1000?1:minColor);
if (adjColor.isEmpty()) {
TreeSet ts=new TreeSet<Integer>();
ts.add(++currentLabel);
labelsEquivalences.add(ts);
count.add(1);
labels[line*img0.width+x]=currentLabel;
} else {
if (adjColor.size()>1) {
for (Integer i : adjColor) {
labelsEquivalences.get(i-1).addAll(adjColor);
}
}
int first=adjColor.first();
count.set(first-1, count.get(first-1)+1);
labels[line*img0.width+x]=first;
}
}
}
}
//merge labelsEquivalences
for (int j=0; j<labelsEquivalences.size(); ++j) {
TreeSet<Integer> ts=labelsEquivalences.get(j);
if (ts.size()>1) {
TreeSet<Integer> acc=new TreeSet<Integer>();
for (Integer i : ts) {
TreeSet<Integer> other=labelsEquivalences.get(i-1);
if(ts!=other){ acc.addAll(other);}
}
ts.addAll(acc);
for (Integer i : ts) {
labelsEquivalences.set(i-1,ts);
}
}
}
//assess blob size
int[] blobSize=new int[labelsEquivalences.size()];
for (int j=0; j<labelsEquivalences.size(); ++j) {
TreeSet<Integer> ts=labelsEquivalences.get(j);
int sum=0;
for (Integer i : ts) {
sum+=count.get(i-1);
}
blobSize[j]=sum;
}
//create ColorMap
int[] colorMap=new int[blobSize.length];
if (onlyBiggest) {
int max =-1;
for(int i=0; i < blobSize.length; i++){
max=max(max,blobSize[i]);
}
for(int i=0; i < blobSize.length; i++){
colorMap[i]=blobSize[i]==max?color(255):color(0);
}
} else {//List<TreeSet<Integer>> labelsEquivalences
for(TreeSet<Integer> ts:labelsEquivalences){
int rndColor=color(random(255),255,200);
for(Integer i:ts){
colorMap[i-1]=rndColor;
}
}
}
//colorize
for (int i=0; i < img0.width*img0.height; ++i) {
//int co=(labels[i])*256/7;
//labels[i]=color(co, 255, 200);
if(labels[i]!=0){
img0.pixels[i]=colorMap[labels[i]-1];
}
}
return img0;
}
}
Event Timeline
Log In to Comment