Code:
#include "testApp.h"
//--------------------------------------------------------------
struct coordinate
{
unsigned int x, y;
void * data;
};
struct blob
{
coordinate min, max;
coordinate center;
int area;
};
void checkblob(unsigned int row,unsigned int coloumn,unsigned int karen[][448]);
void testApp::setup(){
//loading and converting the image to grayscale
testImage.loadImage("fabia.jpg");
farveTest.allocate(testImage.getWidth(),testImage.getHeight());
farveTest.setFromPixels(testImage.getPixels(),testImage.getWidth(),testImage.getHeight());
shTest.allocate(testImage.getWidth(),testImage.getHeight());
shTest = farveTest;
tresholdTest.allocate(testImage.getWidth(),testImage.getHeight());
outputPic.allocate(testImage.getWidth(),testImage.getHeight());
IplImage* cvImg = shTest.getCvImage();
IplImage* cvImg2 = tresholdTest.getCvImage();
IplImage* cvImg3 = outputPic.getCvImage();
int hoejde = shTest.getHeight();
int bredde = shTest.getWidth();
//tresholding and converting my image to a 2d array
unsigned int myArray[336][448];
for(int y = 0; y < cvImg->height; y ++)
{
uchar* ptr = (uchar*)(cvImg->imageData + y * cvImg->widthStep);
uchar* ptr2 = (uchar*)(cvImg2->imageData + y * cvImg2->widthStep);
for(int x = 0; x <cvImg->width; x ++)
{
if(ptr[x] < 200)
{
ptr2[x]= 0;
myArray[y][x] = 0;
}
else
{
ptr2[x] = 255;
myArray[y][x] = 255;
}
}
}
//running checkblob function on white pixels
for(unsigned int y=0;y<336;y++)
{
for(unsigned int x = 0;x<448;x++)
{
if(myArray[y][x] == 255)
{
myArray[y][x]=5;
checkblob(y,x,myArray);
}
}
}
}
//--------------------------------------------------------------
void testApp::update(){
}
//--------------------------------------------------------------
void testApp::draw(){
shTest.draw(testImage.getWidth(),0);
farveTest.draw(0,0);
tresholdTest.draw(0,testImage.getHeight());
outputPic.draw(testImage.getWidth(),testImage.getHeight());
}
//--------------------------------------------------------------
void testApp::keyPressed(int key){
}
//--------------------------------------------------------------
void testApp::keyReleased(int key){
}
//--------------------------------------------------------------
void testApp::mouseMoved(int x, int y ){
}
//--------------------------------------------------------------
void testApp::mouseDragged(int x, int y, int button){
}
//--------------------------------------------------------------
void testApp::mousePressed(int x, int y, int button){
}
//--------------------------------------------------------------
void testApp::mouseReleased(int x, int y, int button){
}
//--------------------------------------------------------------
void testApp::windowResized(int w, int h){
}
void checkblob(unsigned int row,unsigned int coloumn,unsigned int karen[][448])
{
int blobid;
vector< vector<coordinate> > blobs(20);
if(karen[row][coloumn]==255)
{
karen[row][coloumn]=5;
coordinate coordData = {coloumn,row};
blobs[blobid].push_back(coordData);
}
int treshold = 255;
if(karen[row][(coloumn+1)] == 255)
{
karen[row][(coloumn+1)] = 1;
coordinate coordData = {coloumn+1,row};
blobs[blobid].push_back(coordData);
checkblob(row,(coloumn+1),karen);
}
else if(karen[(row+1)][coloumn] == treshold)
{
karen[(row+1)][coloumn] = 2;
coordinate coordData = {coloumn,row+1};
blobs[blobid].push_back(coordData);
checkblob((row+1),coloumn,karen);
}
else if(karen[row][(coloumn-1)] == treshold)
{
karen[row][(coloumn-1)] = 3;
coordinate coordData = {coloumn-1,row};
blobs[blobid].push_back(coordData);
checkblob(row,(coloumn-1),karen);
}
else if(karen[(row-1)][coloumn] == treshold)
{
karen[(row-1)][coloumn] = 4;
coordinate coordData = {coloumn,row-1};
blobs[blobid].push_back(coordData);
checkblob((row-1),coloumn,karen);
}
else
{
//backtracking to find every pixel in the blob.
switch(karen[row][coloumn])
{
case 1:
checkblob(row,(coloumn-1),karen);
break;
case 2:
checkblob(row-1,coloumn,karen);
break;
case 3:
checkblob(row,(coloumn+1),karen);
break;
case 4:
checkblob(row+1,coloumn,karen);
break;
case 5:
blobid++;
break;
default:
blobid++;
cout<<blobid<<endl;
break;
}
}
}