//#include <stdio.h>
//#include <stdlib.h>
//#include <string.h>
//#include <math.h>
//#include "imageio.cpp"
//#include "helperMethods.cpp"
//#include "preProcessing.cpp"
//#include "postProcessing.cpp"
//#include "sizeFindingMethods.cpp"
#include "parasite.h"
//#include <conio.h>;
//#include <iostream>;
#include "shlobj.h"
//#include <time.h>
////////////////////////////////////////////////////////////////////////////////
/* Custom HPC Counter */
////////////////////////////////////////////////////////////////////////////////
//#define BENCHMARKING
//#define ELAPSED
//#define NUM_OF_PASSES 1
//#define DEBUG_OUTPUT
//#define TITLES
extern "C" {
#include "hr_time.h"
}
extern "C" {
#include "cputime.h"
}
//extern "C" {
// #include "leak_detector_c.h"
//}
////////////////////////////////////////////////////////////////////////////////
/* End Custom HPC Counter */
////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
///* Timeb headers */
//////////////////////////////////////////////////////////////////////////////////
//#include <sys/timeb.h>
//#include <time.h>
//// to get portable code we need this
//#ifdef WIN32 // _WIN32 is defined by all Windows 32 compilers, but not by others.
//#define timeb _timeb
//#define ftime _ftime
//#endif
//////////////////////////////////////////////////////////////////////////////////
///* End timeb headers */
//////////////////////////////////////////////////////////////////////////////////
//#include "mmgr.h"
// Global Variables
short* parasiteSize;
double* parasiteCompactness;
int noOfThreads;
int imagesPerThread;
void processImages(struct threadStats*);
int main(int argc, unsigned char* argv[])
{
HANDLE *threads;
struct threadStats *st;
int numberOfInputImages, extra;
// get the index of the image we want and make a unsigned char array of it then concat it to imageFile.
// Set the size of the variable 'imageFileName' to 255 characters
char* masterImageFileName = (char*)malloc(sizeof(char)*255);
char imageIndexString[10];
// Construct the file name
int imageIndex = 1;
itoa(imageIndex,imageIndexString,10);
strcpy(masterImageFileName, "images/colour");
strcat(masterImageFileName, imageIndexString);
strcat(masterImageFileName, ".bmp");
// Reads in one specific image defined by the second parameter.
//strcpy(masterImageFileName, "images/colour137.bmp");
// Set the size of the video name variable 30 characters.
//char* videoname = (char*)malloc(sizeof(char)*30);
//if (argc != 3) /* argc should be 3 for correct execution */
//{
// printf("no folder or video name specified:\n");
// return 1;
//}
//else
//{
// //printf("path: %s\n", argv[1]);
// // copy the first argument to the 'masterImageFileName' variable.
// strcpy(masterImageFileName, (char*) argv[1]);
// // copy the second argument to the 'videoname' variable.
// strcpy(videoname, (char*) argv[2]);
// //masterImageFileName = (char*) argv[1];
//}
// Set the integer 'numberOfInputImages'.
//numberOfInputImages = getNumberOfImages((char*) argv[1]);
int numCores = 1;
SYSTEM_INFO siSysInfo;
GetSystemInfo(&siSysInfo);
numCores = siSysInfo.dwNumberOfProcessors;
printf("////////////////////////////////////////\n");
printf("/ MULTITHREADED PARASITE ANALYSIS /\n");
printf("////////////////////////////////////////\n");
printf("Starting program...\n\n");
printf("%d Cores available.\n", numCores);
printf("___________________\n\n");
numberOfInputImages = 140;
parasiteSize = (short *)malloc(sizeof(short) * numberOfInputImages);
parasiteCompactness = (double *)malloc(sizeof(double) * numberOfInputImages);
printf("Enter the number of threads: ");
scanf("%d",&noOfThreads);
while(noOfThreads>numCores)
{
printf("There is only %d cores available - therefore cannot create more than %d thread(s)\n", numCores, numCores);
printf("\n");
printf("Enter the number of threads: ");
scanf("%d",&noOfThreads);
}
while(noOfThreads>numberOfInputImages)
{
printf("There is only %d images - therefore cannot create more than %d thread(s)\n", numberOfInputImages, numberOfInputImages);
printf("\n");
printf("Enter the number of threads: ");
scanf("%d",&noOfThreads);
}
imagesPerThread = numberOfInputImages/noOfThreads;
extra = numberOfInputImages % noOfThreads;
threads = (HANDLE*) malloc(sizeof(HANDLE)*(noOfThreads-1)); // 1 handle for each spawned thread not counting master thread
st = (struct threadStats*) malloc(sizeof(struct threadStats)*noOfThreads); // struct for each thread
double processElapsedTime;
int offset = imagesPerThread;
stopWatch multi_stopwatch;
if(initialiseTimer(&multi_stopwatch)==0)
{
startTimer(&multi_stopwatch, 1);
int noOfSlaves = noOfThreads-1;
for(int i=1;i<=noOfSlaves;i++)
{
st[i].id = i;
st[i].imagesPerThread = imagesPerThread;
if (extra > 0) {
st[i].imagesPerThread += 1;
extra--;
}
st[i].imageOffset = offset;
offset += st[i].imagesPerThread;
//threads[i] = (HANDLE) _beginthreadex(NULL,0,processImages,(void*)&st[i],0,NULL);
threads[i-1] = (HANDLE) _beginthread((void(*)(void*))processImages, 0, (void*)&st[i]);
}
st[0].id = 0;
st[0].imagesPerThread = imagesPerThread;
st[0].imageOffset = 0;
processImages(&st[0]);
WaitForMultipleObjects(noOfSlaves, threads, TRUE, INFINITE);
stopTimer(&multi_stopwatch, 1);
processElapsedTime += getElapsedTimeInMilli(&multi_stopwatch);
printf("\n");
printf("Free thread handles and structs\n");
free(st);
free(threads);
//getchar(); // last character in stream will be a return
printf("All threads have completed.\n\n");
}
else { printf("System timer did not initialise\n"); }
printf("\nProcess Elapsed Time: %f(ms)\n\n", processElapsedTime);
printf("# Cores: %d\n", siSysInfo.dwNumberOfProcessors);
printf("# Images: %d\n", numberOfInputImages);
printf("# Threads: %d\n", noOfThreads);
printf("# Images per thread: %d\n", imagesPerThread);
//printf("Size of Struct: %d\n", sizeof(struct threadStats)*noOfThreads);
printf("\n");
getchar();
fflush(NULL);
printf("Please type enter to obtain results\n");
getchar();
//system("pause");
//Compensate for being too small
compensateForSmallSize(parasiteSize, numberOfInputImages);
//findParasiteFlux(numberOfInputImages, parasiteSize);
printf("--------------- Flux Unsorted ---------------\n");
for(int i = 0; i < numberOfInputImages; i++)
{
if (i%imagesPerThread == 0)
printf("++++++++++++++++\n");
printf("Array index %d: size = %d \t compactness = %f\n" , i, parasiteSize[i], parasiteCompactness[i]);
}
//findParasiteFluxAlt(numberOfInputImages, parasiteCompactness);
// Use this one.
int flux;
if (numberOfInputImages >= 3) {
flux = findParasiteFlux3(numberOfInputImages, parasiteSize);
printf("Number of head projections: %d\n", flux);
//// Write the results to the results text file.
//FILE* resultsFile;
//resultsFile = fopen("Results.txt", "a");
//fprintf(resultsFile, "%s, %d,\n", videoName, flux);
//fclose(resultsFile);
}
else
printf("Not enough images to analyse.\n");
free(parasiteSize);
free(parasiteCompactness);
free(masterImageFileName);
printf("___________________\n");
printf("PROGRAM COMPLETED\n\n");
printf("PRESS ANY KEY TO EXIT\n");
fflush(stdout);
//getchar();
getchar();
return 0;
}
void processImages(struct threadStats* s)
{
int threadID = (int)(struct threadStats*)(s)->id;
int totalThreadImages = (int)(struct threadStats*)(s)->imagesPerThread;
int threadImageOffset = (int)(struct threadStats*)(s)->imageOffset;
unsigned char processorMask = 0x01;
processorMask = processorMask << threadID;
printf("Thread %d gets processor %d with %d images\n", threadID, processorMask, totalThreadImages);
// Calculate the starting image offset for the current thread
int imageIndex = threadImageOffset;
//int imageIndex = 11;
// Cannot set affinity before creating thread.
// SetThreadAffinityMask uses a bitmask for assigning processor cores.
if (!SetThreadAffinityMask(GetCurrentThread(), (DWORD)processorMask)) {
printf("Could not set affinity\n");
printf("Err: %d\n", GetLastError());
//getchar();
_endthread();
}
/* To double check processor affinity
//DWORD proNum = 9;
//printf("proNum B: %d\n", proNum);
//proNum = GetCurrentProcessorNumber(); // returns a number between 0..N
//printf("proNum A: %d\n", proNum);
*/
/* Small test to check processor affinity
int o=0;
int count = 0;
while(o<20000000)
{
t_bmp* img = (t_bmp*)malloc(sizeof(t_bmp));
free(img);
o++;
}
_endthread();
*/
unsigned char* imageArray = (unsigned char *)malloc(sizeof(unsigned char) * 640 * 480);
t_bmp* img = (t_bmp*)malloc(sizeof(t_bmp));
char* imageFileName = (char*)malloc(sizeof(char)*255);
memset(imageFileName, '-', sizeof(char)*255);
sprintf(imageFileName,"images/colour%d.bmp", (imageIndex+1));
//printf("%d\tThread is Running\t%d: output file %s\n", threadID, totalThreadImages, imageFileName);
#ifdef BENCHMARKING
#ifdef ELAPSED
FILE *benchMarkFileElapsed;
char outputName [30];
sprintf (outputName, "benchMarkFileElapsed Thread_%d.txt", threadID);
benchMarkFileElapsed = fopen(outputName, "w");
fprintf(benchMarkFileElapsed, "------------Benchmarking Algorithms (Elapsed)------------\n");
fprintf(benchMarkFileElapsed, "Thread ID %d.\n", threadID);
fprintf(benchMarkFileElapsed, "Processor ID (start at 0) %d. \n", GetCurrentProcessorNumber());
fprintf(benchMarkFileElapsed, "Number of Images %d.\n", totalThreadImages);
fprintf(benchMarkFileElapsed, "Average elapsed times in ms.\n");
fprintf(benchMarkFileElapsed, "Number of Passes:%d\n\n", NUM_OF_PASSES);
double aggregatedElapsedTime = 0;
#else
FILE *benchMarkFileCPU;
char outputName [30];
sprintf (outputName, "benchMarkFileCPU Thread_%d.txt", threadID);
benchMarkFileCPU = fopen(outputName, "w");
fprintf(benchMarkFileCPU, "------------Benchmarking Algorithms (CPU)------------\n");
fprintf(benchMarkFileElapsed, "Thread ID %d.\n", threadID);
fprintf(benchMarkFileElapsed, "Processor ID %d.\n", GetCurrentProcessorNumber());
fprintf(benchMarkFileElapsed, "Number of Images %d.\n", totalThreadImages);
fprintf(benchMarkFileCPU, "Average CPU times in ms.\n");
fprintf(benchMarkFileCPU, "Number of Passes:%d\n\n", NUM_OF_PASSES);
double aggregatedCPUTime = 0;
#endif
#endif
// While there are no more images to load.
// (imageIndex-1) = starting image index
// ((threadID*totalThreadImages)+totalThreadImages)) = final image index
while((imageIndex<(threadImageOffset+totalThreadImages)) && (libbmp_load(imageFileName, img) != 0))
//while(libbmp_load(imageFileName, img) != 0)
{
#ifdef TITLES
printf("------ Processing Frame ------\n");
#endif
//printf("THREAD %d IMAGE %d\n", threadID, imageIndex);
// Moy is black and white intensity
// moy of 0 is black
// moy of 255 is white
// height goes from bottom up not top down!!!!
// initialize all necessary variables
////////////////////////////////////////////////////////////////////////////////
/* Create Histogram */
////////////////////////////////////////////////////////////////////////////////
short* histogram;
histogram = createHistogram(img);
#ifdef DEBUG_OUTPUT
//libbmp_write("image.bmp", img);
libbmp_write("image.bmp", img);
#endif
////////////////////////////////////////////////////////////////////////////////
/* End Create Histogram */
////////////////////////////////////////////////////////////////////////////////
/**************************************************************************************************************/
////////////////////////////////////////////////////////////////////////////////
/* Enhance Contrast */
////////////////////////////////////////////////////////////////////////////////
#ifdef TITLES
printf("-------------------Enhance Contrast\n");
#endif
#ifdef BENCHMARKING
t_bmp* initialImg;
initialImg = (t_bmp*) malloc(sizeof(t_bmp));
libbmp_copyAndCreateImg(img, initialImg);
#ifdef ELAPSED
stopWatch s;
if(initialiseTimer(&s)==0) {
#endif
for (int i = 0; i < NUM_OF_PASSES; i++) {
libbmp_copyImgData(initialImg, img);
#ifdef ELAPSED
startTimer(&s, processorMask);
#else
initCPUTime();
#endif
#endif
enhanceContrast(img, histogram);
#ifdef BENCHMARKING
#ifdef ELAPSED
stopTimer(&s, processorMask);
#else
double endTime = getCPUTimeSinceStart();
#endif
#ifdef ELAPSED
printf("Elapsed Time: %f(s), %f(ms)\n", getElapsedTime(&s), getElapsedTimeInMilli(&s));
aggregatedElapsedTime += getElapsedTimeInMilli(&s);
#else
printf("CPU Time: %f(ms)\n", endTime);
aggregatedCPUTime += endTime;
#endif
}
#ifdef ELAPSED
} else { printf("Enhance constrast timer didnt initialise\n"); }
#endif
// Print results to file and screen and clean up
#ifdef ELAPSED
fprintf(benchMarkFileElapsed, "Enhance Contrast : ");
fprintf(benchMarkFileElapsed, "%f\n", (aggregatedElapsedTime/NUM_OF_PASSES));
printf("\nAverage Elapsed Time: %f(ms)\n\n", (aggregatedElapsedTime/NUM_OF_PASSES));
aggregatedElapsedTime = 0.0;
#else
fprintf(benchMarkFileCPU, "Enhance Contrast : ");
fprintf(benchMarkFileCPU, "%f\n", (aggregatedCPUTime/NUM_OF_PASSES));
printf("\nAverage CPU Time: %f(ms)\n\n", (aggregatedCPUTime/NUM_OF_PASSES));
aggregatedCPUTime = 0.0;
#endif
for(int i = 0; i < initialImg->height; i++)
free(initialImg->data[i]);
free(initialImg->data);
free(initialImg);
#endif
// free histogram data structure
free(histogram);
#ifdef DEBUG_OUTPUT
//enhanceContrast(img, histogram);
//libbmp_write("imageEnhanceContrast.bmp", img);
libbmp_write("imageStage1.bmp", img);
#endif
////////////////////////////////////////////////////////////////////////////////
/* End Enhance Contrast */
////////////////////////////////////////////////////////////////////////////////
/**************************************************************************************************************/
////////////////////////////////////////////////////////////////////////////////
/* Filtering */
////////////////////////////////////////////////////////////////////////////////
#ifdef TITLES
printf("-------------------Filtering\n");
#endif
t_bmp* outputImg = (t_bmp*)malloc(sizeof(t_bmp));
libbmp_copyAndCreateImg(img, outputImg); // initialise outputImage by copying
#ifdef BENCHMARKING
initialImg = (t_bmp*) malloc(sizeof(t_bmp));
libbmp_copyAndCreateImg(img, initialImg);
/* Updated the low pass filter sequential to take into account the border pixels.
Also removed a major bug where the convolution operation output the results of each pixel calculation to the same image instead of a new image.
*/
#ifdef ELAPSED
if(initialiseTimer(&s)==0) {
#endif
for (int i = 0; i < NUM_OF_PASSES; i++) {
libbmp_copyImgData(initialImg, img);
#ifdef ELAPSED
startTimer(&s, processorMask);
#else
initCPUTime();
#endif
#endif
lowPassFilterImage(img, outputImg);
//lowPassFilterImage(img);
#ifdef BENCHMARKING
#ifdef ELAPSED
stopTimer(&s, processorMask);
#else
double endTime = getCPUTimeSinceStart();
#endif
#ifdef ELAPSED
printf("Elapsed Time: %f(s), %f(ms)\n", getElapsedTime(&s), getElapsedTimeInMilli(&s));
aggregatedElapsedTime += getElapsedTimeInMilli(&s);
#else
printf("CPU Time: %f(ms)\n", endTime);
aggregatedCPUTime += endTime;
#endif
}
#ifdef ELAPSED
} else { printf("Filtering timer didnt initialise\n"); }
#endif
#ifdef ELAPSED
fprintf(benchMarkFileElapsed, "Filtering : ");
fprintf(benchMarkFileElapsed, "%f\n", (aggregatedElapsedTime/NUM_OF_PASSES));
printf("\nAverage Elapsed Time: %f(ms)\n\n", (aggregatedElapsedTime/NUM_OF_PASSES));
aggregatedElapsedTime = 0.0;
#else
fprintf(benchMarkFileCPU, "Filtering : ");
fprintf(benchMarkFileCPU, "%f\n", (aggregatedCPUTime/NUM_OF_PASSES));
printf("\nAverage CPU Time: %f(ms)\n\n", (aggregatedCPUTime/NUM_OF_PASSES));
aggregatedCPUTime = 0.0;
#endif
for(int i = 0; i < initialImg->height; i++)
free(initialImg->data[i]);
free(initialImg->data);
free(initialImg);
#endif
// Switch pointers so img points to outputImg (the result of the low pass filter operation) and outputImg points to the old data (img), which is then removed.
t_bmp* temp;
//printf("img: %x OutputImage: %x temp: %xd\n\n", img, outputImg, temp);
temp = outputImg;
//printf("temp = outputImage\n");
//printf("img: %x OutputImage: %x temp: %xd\n", img, outputImg, temp);
outputImg = img;
//printf("outputImage = img\n");
//printf("img: %x OutputImage: %x temp: %xd\n", img, outputImg, temp);
img = temp;
//printf("img = temp\n");
//printf("img: %x OutputImage: %x temp: %xd\n", img, outputImg, temp);
for(int i = 0; i < outputImg->height; i++)
free(outputImg->data[i]);
free(outputImg->data);
free(outputImg);
#ifdef DEBUG_OUTPUT
//lowPassFilterImage(img);
//medianFilterImage(img);
//libbmp_write("imageMedianFilter.bmp", img);
libbmp_write("imageStage2.bmp", img);
#endif
////////////////////////////////////////////////////////////////////////////////
/* End Filtering */
////////////////////////////////////////////////////////////////////////////////
/**************************************************************************************************************/
////////////////////////////////////////////////////////////////////////////////
/* Segment Image */
////////////////////////////////////////////////////////////////////////////////
#ifdef TITLES
printf("-------------------Segment Image\n");
#endif
#ifdef BENCHMARKING
initialImg = (t_bmp*) malloc(sizeof(t_bmp));
libbmp_copyAndCreateImg(img, initialImg);
#ifdef ELAPSED
if(initialiseTimer(&s)==0) {
#endif
for (int i = 0; i < NUM_OF_PASSES; i++) {
libbmp_copyImgData(initialImg, img);
#ifdef ELAPSED
startTimer(&s, processorMask);
#else
initCPUTime();
#endif
#endif
segmentImage(img);
#ifdef BENCHMARKING
#ifdef ELAPSED
stopTimer(&s, processorMask);
#else
double endTime = getCPUTimeSinceStart();
#endif
#ifdef ELAPSED
printf("Elapsed Time: %f(s), %f(ms)\n", getElapsedTime(&s), getElapsedTimeInMilli(&s));
aggregatedElapsedTime += getElapsedTimeInMilli(&s);
#else
printf("CPU Time: %f(ms)\n", endTime);
aggregatedCPUTime += endTime;
#endif
}
#ifdef ELAPSED
} else { printf("Segment timer didnt initialise\n"); }
#endif
#ifdef ELAPSED
fprintf(benchMarkFileElapsed, "Segment Image : ");
fprintf(benchMarkFileElapsed, "%f\n", (aggregatedElapsedTime/NUM_OF_PASSES));
printf("\nAverage Elapsed Time: %f(ms)\n\n", (aggregatedElapsedTime/NUM_OF_PASSES));
aggregatedElapsedTime = 0.0;
#else
fprintf(benchMarkFileCPU, "Segment Image : ");
fprintf(benchMarkFileCPU, "%f\n", (aggregatedCPUTime/NUM_OF_PASSES));
printf("\nAverage CPU Time: %f(ms)\n\n", (aggregatedCPUTime/NUM_OF_PASSES));
aggregatedCPUTime = 0.0;
#endif
for(int i = 0; i < initialImg->height; i++)
free(initialImg->data[i]);
free(initialImg->data);
free(initialImg);
#endif
#ifdef DEBUG_OUTPUT
//segmentImage(img);
//libbmp_write("imageSegmented.bmp", img);
libbmp_write("imageStage3.bmp", img);
#endif
////////////////////////////////////////////////////////////////////////////////
/* End Segment Image */
////////////////////////////////////////////////////////////////////////////////
/**************************************************************************************************************/
//Convert image to a 1D array
imageToArray(img, imageArray);
#ifdef DEBUG_OUTPUT
createImage(img, imageArray);
//libbmp_write("imageWith1DArray.bmp", img);
libbmp_write("imageStage4.bmp", img);
#endif
/**************************************************************************************************************/
////////////////////////////////////////////////////////////////////////////////
/* Dilate Image three times */
////////////////////////////////////////////////////////////////////////////////
#ifdef TITLES
printf("-------------------Dilate Image\n");
#endif
#ifdef BENCHMARKING
unsigned char* initialImgArray;
initialImgArray = (unsigned char *)malloc(sizeof(unsigned char) * 640 * 480);
copyArray(imageArray, initialImgArray);
#ifdef ELAPSED
if(initialiseTimer(&s)==0) {
#endif
for (int i = 0; i < NUM_OF_PASSES; i++) {
copyArray(initialImgArray, imageArray);
#ifdef ELAPSED
startTimer(&s, processorMask);
#else
initCPUTime();
#endif
#endif
dilateImage(img->height, img->width, imageArray);
dilateImage(img->height, img->width, imageArray);
dilateImage(img->height, img->width, imageArray);
dilateImage(img->height, img->width, imageArray);
dilateImage(img->height, img->width, imageArray);
dilateImage(img->height, img->width, imageArray);
#ifdef BENCHMARKING
#ifdef ELAPSED
stopTimer(&s, processorMask);
#else
double endTime = getCPUTimeSinceStart();
#endif
#ifdef ELAPSED
printf("Elapsed Time: %f(s), %f(ms)\n", getElapsedTime(&s), getElapsedTimeInMilli(&s));
aggregatedElapsedTime += getElapsedTimeInMilli(&s);
#else
printf("CPU Time: %f(ms)\n", endTime);
aggregatedCPUTime += endTime;
#endif
}
#ifdef ELAPSED
} else { printf("Dilate timer didnt initialise\n"); }
#endif
#ifdef ELAPSED
fprintf(benchMarkFileElapsed, "Dilate Image : ");
fprintf(benchMarkFileElapsed, "%f\n", (aggregatedElapsedTime/NUM_OF_PASSES));
printf("\nAverage Elapsed Time: %f(ms)\n\n", (aggregatedElapsedTime/NUM_OF_PASSES));
aggregatedElapsedTime = 0.0;
#else
fprintf(benchMarkFileCPU, "Dilate Image : ");
fprintf(benchMarkFileCPU, "%f\n", (aggregatedCPUTime/NUM_OF_PASSES));
printf("\nAverage CPU Time: %f(ms)\n\n", (aggregatedCPUTime/NUM_OF_PASSES));
aggregatedCPUTime = 0.0;
#endif
free(initialImgArray);
#endif
//dilateImage(img->height, img->width, imageArray);
//dilateImage(img->height, img->width, imageArray);
//dilateImage(img->height, img->width, imageArray);
//dilateImage(img->height, img->width, imageArray);
//dilateImage(img->height, img->width, imageArray);
//dilateImage(img->height, img->width, imageArray);
#ifdef DEBUG_OUTPUT
createImage(img, imageArray);
//libbmp_write("imageDilated.bmp", img);
libbmp_write("imageStage5.bmp", img);
#endif
////////////////////////////////////////////////////////////////////////////////
/* End Dilate Image three times */
////////////////////////////////////////////////////////////////////////////////
// The following code was a template for creating a method to fill in the black gaps
// in the worm segmentation.
//int *fillDetails = findLargestArea(img->height, img->width, imageArray);
////fill(imageArray, fillDetails[1], fillDetails[2]);
//createImage(img, imageArray);
//libbmp_write("imageStage5i.bmp", img);
//free(fillDetails);
/**************************************************************************************************************/
////////////////////////////////////////////////////////////////////////////////
/* Erode Image three times */
////////////////////////////////////////////////////////////////////////////////
#ifdef TITLES
printf("-------------------Erode Image\n");
#endif
#ifdef BENCHMARKING
initialImgArray = (unsigned char *)malloc(sizeof(unsigned char) * 640 * 480);
copyArray(imageArray, initialImgArray);
#ifdef ELAPSED
if(initialiseTimer(&s)==0) {
#endif
for (int i = 0; i < NUM_OF_PASSES; i++) {
copyArray(initialImgArray, imageArray);
#ifdef ELAPSED
startTimer(&s, processorMask);
#else
initCPUTime();
#endif
#endif
erodeImage(img->height, img->width, imageArray);
erodeImage(img->height, img->width, imageArray);
erodeImage(img->height, img->width, imageArray);
erodeImage(img->height, img->width, imageArray);
erodeImage(img->height, img->width, imageArray);
erodeImage(img->height, img->width, imageArray);
#ifdef BENCHMARKING
#ifdef ELAPSED
stopTimer(&s, processorMask);
#else
double endTime = getCPUTimeSinceStart();
#endif
#ifdef ELAPSED
printf("Elapsed Time: %f(s), %f(ms)\n", getElapsedTime(&s), getElapsedTimeInMilli(&s));
aggregatedElapsedTime += getElapsedTimeInMilli(&s);
#else
printf("CPU Time: %f(ms)\n", endTime);
aggregatedCPUTime += endTime;
#endif
}
#ifdef ELAPSED
} else { printf("Erode timer didnt initialise\n"); }
#endif
#ifdef ELAPSED
fprintf(benchMarkFileElapsed, "Erode Image : ");
fprintf(benchMarkFileElapsed, "%f\n", (aggregatedElapsedTime/NUM_OF_PASSES));
printf("\nAverage Elapsed Time: %f(ms)\n\n", (aggregatedElapsedTime/NUM_OF_PASSES));
aggregatedElapsedTime = 0.0;
#else
fprintf(benchMarkFileCPU, "Erode Image : ");
fprintf(benchMarkFileCPU, "%f\n", (aggregatedCPUTime/NUM_OF_PASSES));
printf("\nAverage CPU Time: %f(ms)\n\n", (aggregatedCPUTime/NUM_OF_PASSES));
aggregatedCPUTime = 0.0;
#endif
free(initialImgArray);
#endif
//erodeImage(img->height, img->width, imageArray);
//erodeImage(img->height, img->width, imageArray);
//erodeImage(img->height, img->width, imageArray);
//erodeImage(img->height, img->width, imageArray);
//erodeImage(img->height, img->width, imageArray);
//erodeImage(img->height, img->width, imageArray);
#ifdef DEBUG_OUTPUT
createImage(img, imageArray);
//libbmp_write("imageEroded.bmp", img);
libbmp_write("imageStage6.bmp", img);
#endif
////////////////////////////////////////////////////////////////////////////////
/* Erode Image three times */
////////////////////////////////////////////////////////////////////////////////
/**************************************************************************************************************/
////////////////////////////////////////////////////////////////////////////////
/* Remove Noise */
////////////////////////////////////////////////////////////////////////////////
#ifdef TITLES
printf("-------------------Remove Noise\n");
#endif
int maskSize = 3;
#ifdef BENCHMARKING
initialImgArray = (unsigned char *)malloc(sizeof(unsigned char) * 640 * 480);
copyArray(imageArray, initialImgArray);
#ifdef ELAPSED
if(initialiseTimer(&s)==0) {
#endif
for (int i = 0; i < NUM_OF_PASSES; i++) {
copyArray(initialImgArray, imageArray);
#ifdef ELAPSED
startTimer(&s, processorMask);
#else
initCPUTime();
#endif
#endif
removeUnwantedNoiseHeight(img->height, img->width, imageArray, maskSize);
removeUnwantedNoiseWidth(img->height, img->width, imageArray, maskSize);
removeUnwantedNoiseHeight(img->height, img->width, imageArray, maskSize);
removeUnwantedNoiseWidth(img->height, img->width, imageArray, maskSize);
#ifdef BENCHMARKING
#ifdef ELAPSED
stopTimer(&s, processorMask);
#else
double endTime = getCPUTimeSinceStart();
#endif
#ifdef ELAPSED
printf("Elapsed Time: %f(s), %f(ms)\n", getElapsedTime(&s), getElapsedTimeInMilli(&s));
aggregatedElapsedTime += getElapsedTimeInMilli(&s);
#else
printf("CPU Time: %f(ms)\n", endTime);
aggregatedCPUTime += endTime;
#endif
}
#ifdef ELAPSED
} else { printf("Unwanted noise timer didnt initialise\n"); }
#endif
#ifdef ELAPSED
fprintf(benchMarkFileElapsed, "Remove Noise : ");
fprintf(benchMarkFileElapsed, "%f\n", (aggregatedElapsedTime/NUM_OF_PASSES));
printf("\nAverage Elapsed Time: %f(ms)\n\n", (aggregatedElapsedTime/NUM_OF_PASSES));
aggregatedElapsedTime = 0.0;
#else
fprintf(benchMarkFileCPU, "Remove Noise : ");
fprintf(benchMarkFileCPU, "%f\n", (aggregatedCPUTime/NUM_OF_PASSES));
printf("\nAverage CPU Time: %f(ms)\n\n", (aggregatedCPUTime/NUM_OF_PASSES));
aggregatedCPUTime = 0.0;
#endif
free(initialImgArray);
#endif
//removeUnwantedNoiseHeight(img->height, img->width, imageArray, maskSize);
//removeUnwantedNoiseWidth(img->height, img->width, imageArray, maskSize);
//removeUnwantedNoiseHeight(img->height, img->width, imageArray, maskSize);
//removeUnwantedNoiseWidth(img->height, img->width, imageArray, maskSize);
#ifdef DEBUG_OUTPUT
createImage(img, imageArray);
//libbmp_write("imageNoiseRemoval.bmp", img);
libbmp_write("imageStage7.bmp", img);
#endif
////////////////////////////////////////////////////////////////////////////////
/* End Remove Noise */
////////////////////////////////////////////////////////////////////////////////
/**************************************************************************************************************/
////////////////////////////////////////////////////////////////////////////////
/* Find Centre Points */
////////////////////////////////////////////////////////////////////////////////
//getLineSizeWidth(img->height, img->width, imageArray);
//getLineSizeHeight(img->height, img->width, imageArray);
//createImage(img, imageArray);
//libbmp_write("imageCentrePoints.bmp", img);
////////////////////////////////////////////////////////////////////////////////
/* End Find Centre Points */
////////////////////////////////////////////////////////////////////////////////
/**************************************************************************************************************/
////////////////////////////////////////////////////////////////////////////////
/* Calculate Area */
////////////////////////////////////////////////////////////////////////////////
#ifdef TITLES
printf("-------------------Find Pixels\n");
#endif
//libbmp_load("images/colourprocessed1CircleTest.bmp", img);
//imageToArray(img, imageArray);
int *details = (int*) malloc(sizeof(int) * 3);
#ifdef BENCHMARKING
initialImgArray = (unsigned char *)malloc(sizeof(unsigned char) * 640 * 480);
copyArray(imageArray, initialImgArray);
#ifdef ELAPSED
if(initialiseTimer(&s)==0) {
#endif
for (int i = 0; i < NUM_OF_PASSES; i++) {
copyArray(initialImgArray, imageArray);
#ifdef ELAPSED
startTimer(&s, processorMask);
#else
initCPUTime();
#endif
#endif
free(details);
details = findPixels(img->height, img->width, imageArray);
#ifdef BENCHMARKING
#ifdef ELAPSED
stopTimer(&s, processorMask);
#else
double endTime = getCPUTimeSinceStart();
#endif
#ifdef ELAPSED
printf("Elapsed Time: %f(s), %f(ms)\n", getElapsedTime(&s), getElapsedTimeInMilli(&s));
aggregatedElapsedTime += getElapsedTimeInMilli(&s);
#else
printf("CPU Time: %f(ms)\n", endTime);
aggregatedCPUTime += endTime;
#endif
}
#ifdef ELAPSED
} else { printf("Find pixels timer didnt initialise\n"); }
#endif
#ifdef ELAPSED
fprintf(benchMarkFileElapsed, "Find Pixels : ");
fprintf(benchMarkFileElapsed, "%f\n", (aggregatedElapsedTime/NUM_OF_PASSES));
printf("\nAverage Elapsed Time: %f(ms)\n\n", (aggregatedElapsedTime/NUM_OF_PASSES));
aggregatedElapsedTime = 0.0;
#else
fprintf(benchMarkFileCPU, "Find Pixels : ");
fprintf(benchMarkFileCPU, "%f\n", (aggregatedCPUTime/NUM_OF_PASSES));
printf("\nAverage CPU Time: %f(ms)\n\n", (aggregatedCPUTime/NUM_OF_PASSES));
aggregatedCPUTime = 0.0;
#endif
free(initialImgArray);
#endif
//details = findPixels(img->height, img->width, imageArray);
//printf("**************\n");
//printf("details[0] %d : details[1] %d : details[2] %d\n", details[0], details[1], details[2]); // largestArea, blobCoordI, blobCoordJ
//printf("**************\n");
#ifdef TITLES
printf("-------------------Find and Replace Pixels\n");
#endif
#ifdef BENCHMARKING
initialImgArray = (unsigned char *)malloc(sizeof(unsigned char) * 640 * 480);
copyArray(imageArray, initialImgArray);
//outputArrays(imageArray, "a", initialImgArray, "b");
#ifdef ELAPSED
if(initialiseTimer(&s)==0) {
#endif
for (int i = 0; i < NUM_OF_PASSES; i++) {
copyArray(initialImgArray, imageArray);
#ifdef ELAPSED
startTimer(&s, processorMask);
#else
initCPUTime();
#endif
#endif
//outputArray(imageArray, "b1");
findAndReplace(imageArray, details[1], details[2], 4, 1);
//outputArray(imageArray, "b2");
#ifdef BENCHMARKING
#ifdef ELAPSED
stopTimer(&s, processorMask);
#else
double endTime = getCPUTimeSinceStart();
#endif
#ifdef ELAPSED
printf("Elapsed Time: %f(s), %f(ms)\n", getElapsedTime(&s), getElapsedTimeInMilli(&s));
aggregatedElapsedTime += getElapsedTimeInMilli(&s);
#else
printf("CPU Time: %f(ms)\n", endTime);
aggregatedCPUTime += endTime;
#endif
}
#ifdef ELAPSED
} else { printf("Find and replace timer didnt initialise\n"); }
#endif
//outputArrays(imageArray, "e", initialImgArray, "f");
#ifdef ELAPSED
fprintf(benchMarkFileElapsed, "Find and Replace Pixels: ");
fprintf(benchMarkFileElapsed, "%f\n", (aggregatedElapsedTime/NUM_OF_PASSES));
printf("\nAverage Elapsed Time: %f(ms)\n\n", (aggregatedElapsedTime/NUM_OF_PASSES));
aggregatedElapsedTime = 0.0;
#else
fprintf(benchMarkFileCPU, "Find and Replace Pixels: ");
fprintf(benchMarkFileCPU, "%f\n", (aggregatedCPUTime/NUM_OF_PASSES));
printf("\nAverage CPU Time: %f(ms)\n\n", (aggregatedCPUTime/NUM_OF_PASSES));
aggregatedCPUTime = 0.0;
#endif
free(initialImgArray);
free(details);
#endif
#ifdef DEBUG_OUTPUT
// Redraw the noise removal image once the noise and fluke have been distinguished.
createImage(img, imageArray);
//libbmp_write("imageNoiseRemoval.bmp", img);
libbmp_write("imageStage8.bmp", img);
#endif
double area = calculateArea(imageArray);
//printf("Area: %g \n", area);
//createImage(img, imageArray);
//sprintf(imageFileName, "images/colourprocessedi%d.bmp", (imageIndex+1));
//libbmp_write(imageFileName, img);
//outputArray(imageArray, "b3");
//double perimiter = calculatePerimeter("imageNoiseRemoval.bmp", img, imageArray);
//if (imageIndex == 11)
// printf("ThreadID: %d imageIndex: %d image: %d\n", threadID, imageIndex, (imageIndex+1));
double perimiter = calculatePerimeter(img, imageArray);
double compactness = calculateCompactness(perimiter, area);
//printf("Area: %g \n", area);
//printf("Perimiter: %g \n", perimiter);
//printf("Compactness: %g \n", compactness); // printing the compactness value before adding it to the array
// seems to cause assignment problems - some values are set to
// -0.0
//if (threadID == 2)
parasiteCompactness[imageIndex] = (double) compactness;
//// Calculate bounding box
//findBoundingBox(img->height, img->width, imageArray);
//createImage(img, imageArray);
//char* boundingImageFileName = (char*)malloc(sizeof(char)*19);
//strcpy(boundingImageFileName, "images/imageNoiseBounding");
//strcat(boundingImageFileName, imageIndexString);
//strcat(boundingImageFileName, ".bmp");
//libbmp_write(boundingImageFileName, img);
////////////////////////////////////////////////////////////////////////////////
/* End Calculate Area */
////////////////////////////////////////////////////////////////////////////////
/**************************************************************************************************************/
////////////////////////////////////////////////////////////////////////////////
/* Find Parasite Size */
////////////////////////////////////////////////////////////////////////////////
parasiteSize[imageIndex] = (short) getSizeOfBlobCircleTest(img->height, img->width, imageArray);
////////////////////////////////////////////////////////////////////////////////
/* End Find Parasite Size */
////////////////////////////////////////////////////////////////////////////////
/**************************************************************************************************************/
createImage(img, imageArray);
//strcpy(imageFileName, (char*) argv[1]);
//strcat(imageFileName, "\\colour");
//strcat(imageFileName, "processed");
//strcat(imageFileName, imageIndexString);
//strcat(imageFileName, ".bmp");
sprintf(imageFileName, "images/colourprocessed%d.bmp", (imageIndex+1));
libbmp_write(imageFileName, img);
/**************************************************************************************************************/
////////////////////////////////////////////////////////////////////////////////
/* Create new image name for next image */
////////////////////////////////////////////////////////////////////////////////
//printf("image index: %d \n", imageIndex);
imageIndex++;
//itoa(imageIndex,imageIndexString,10); // void itoa(int input, char *buffer, int radix) - base radix: 10 (decimal)
//strcpy(imageFileName, "images/colour");
//strcat(imageFileName, imageIndexString);
//strcat(imageFileName, ".bmp");
sprintf(imageFileName,"images/colour%d.bmp", (imageIndex+1));
//strcpy(imageFileName, (char*) argv[1]);
//strcat(imageFileName, "\\colour");
//strcat(imageFileName, imageIndexString);
//strcat(imageFileName, ".bmp");
////////////////////////////////////////////////////////////////////////////////
/* End Create new image name for next image */
////////////////////////////////////////////////////////////////////////////////
#ifdef BENCHMARKING
#ifdef ELAPSED
fprintf(benchMarkFileElapsed, "\n");
#else
fprintf(benchMarkFileCPU, "\n");
#endif
#endif
// Free all allocated memory
for(int i = 0; i < img->height; i++)
free(img->data[i]);
free(img->data);
fflush(stdout);
}
#ifdef BENCHMARKING
#ifdef ELAPSED
fclose(benchMarkFileElapsed);
#else
fclose(benchMarkFileCPU);
#endif
#endif
printf("ThreadID %d cleaning up\n", threadID);
free(img);
free(imageArray);
free(imageFileName);
fflush(NULL);
//printf("***********************************************************************\n\n");
if (threadID != 0)
{
printf("ThreadID %d has ended\n", threadID);
fflush(stdout);
_endthread();
}
}