Click here to Skip to main content
15,867,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to add image processing to my app and also want to calculate the time it takes to finish rendering an image. I also need some ideas on how I can store images.
Posted
Updated 17-Jan-11 2:59am
v2
Comments
Richard MacCutchan 17-Jan-11 6:59am    
OK, and what are you having difficulty with? Your question is so general it is impossible to suggest anything that might be of use.
Andrew Brock 17-Jan-11 7:00am    
Please elaborate.
What do you want to add images too, and what are you wanting to get the time of?

There are a vast number of image processing libraries that are free and a lot of them are open source.

My top 2 would be:
CxImage[^] - Very comprehensive
GDI+[^] - pretty easy to use, well documented and heaps of samples

As for the timing, that is simple. You just need to use a timing function like timeGetTime or if you want some serious precision QueryPerformanceCounter

#include <StdIO.h>
#include <Windows.h>
#include <MmSystem.h>

#pragma comment(lib, "WinMm.lib") //link with WinMM.dll for timeGetTime()

int main() {
    //timeGetTime() returns an arbritary time, so we need to get the starting time
    DWORD nStart = timeGetTime();
    Sleep(2000); //Process your images here
    DWORD nEndTime = timeGetTime();
    printf("Operation took %ums\n", nEndTime - nStart);
    return 0;
}
 
Share this answer
 
v2
Comments
Espen Harlinn 17-Jan-11 10:03am    
5+ GDI+ can easily be accessed from c, it's just not a "well" documented feature :)
jidankhan 17-Jan-11 23:50pm    
I am using ubuntu ,while I run the program it saw error due to Winmm.lib , HOw can I add this library ?
Andrew Brock 18-Jan-11 0:27am    
Sorry, most of the things here are Windows, so I assumed your question was too.
I will add another answer shortly with a unix/linux solution
This code works properly on my Ubuntu virtual machine, but not on Windows as Windows only provides precision to the millisecond unless you use the performance counters.
Windows will show the time as either 3000, 2000, 1000 or occasionally 0 microseconds because of this.

Keep this in mind if you are considering on porting to Windows at some stage.

#include <stdlib.h>
#include <sys/time.h>
#include <stdio.h>
#include <unistd.h>

unsigned int GetTimeMilli() {
	struct timeval sTime;
	struct timezone sTimeZone;
	struct tm *tmNow;
	unsigned int nTime;
	gettimeofday(&sTime, &sTimeZone);
	tmNow = localtime(&sTime.tv_sec);
	nTime = tmNow->tm_hour;
	nTime = nTime * 60 + tmNow->tm_min;
	nTime = nTime * 60 + tmNow->tm_sec;
	nTime = nTime * 1000 + sTime.tv_usec / 1000; //convert micro to milli
	return nTime;
}

unsigned long long GetTimeMicro() {
	struct timeval sTime;
	struct timezone sTimeZone;
	struct tm *tmNow;
	unsigned long long nTime;
	gettimeofday(&sTime, &sTimeZone);
	tmNow = localtime(&sTime.tv_sec);
	nTime = tmNow->tm_hour;
	nTime = nTime * 60 + tmNow->tm_min;
	nTime = nTime * 60 + tmNow->tm_sec;
	nTime = nTime * 1000000 + sTime.tv_usec; //convert micro to milli
	return nTime;
}

unsigned int GetTimeDiffMilli(unsigned int nStart, unsigned int nEnd) {
	if (nEnd < nStart) { //we ended in a different day than we started in
		return nEnd + (24 * 60 * 60 * 1000000) - nStart;
	}
	return nEnd - nStart;
}

unsigned long long GetTimeDiffMicro(unsigned long long nStart, unsigned long long nEnd) {
	if (nEnd < nStart) { //we ended in a different day than we started in
		return nEnd + (24 * 60 * 60 * 1000000) - nStart;
	}
	return nEnd - nStart;
}

int main() {
	unsigned long long nStart, nEnd;
	nStart = GetTimeMicro();
	usleep(2000000); //Sleep for 2,000,000 microseconds = 2 seconds
	nEnd = GetTimeMicro();
	printf("%llu, %llu\n", nStart, nEnd);
	printf("Sleep took %llu microseconds\n", GetTimeDiffMicro(nStart, nEnd));
	return 0;
}
 
Share this answer
 
Thanks all to all of you frndz....

I done with it using this code and it is for FITS format....



#include <stdio.h>
#include <fitsio.h>
#include<time.h>
//#include <Windows.h>
//#include <MmSystem.h>
//#pragma comment(lib, "WinMm.lib") //link with WinMM.dll for timeGetTime()

int main(int argc, char **argv)
{
fitsfile *afptr, *bfptr, *outfptr; /* FITS file pointers */
int status = 0; /* CFITSIO status value MUST be initialized to zero! */
int atype, btype, anaxis, bnaxis, check = 1, ii, op;
long npixels = 1, firstpix[3] = {1,1,1}, ntodo;
long anaxes[3] = {1,1,1}, bnaxes[3]={1,1,1};
double *apix, *bpix, value;
int image2=1;
int t=clock();
int s,e,d,i;
if (argc = 2) {
printf("Usage: imarith image1 { image2 | value } oper outimage \n");
printf("\n");
// printf("xyz\n");
printf("Perform 'image1 oper image2' or 'image1 oper value'\n");
printf("creating a new output image. Supported arithmetic\n");
printf("operators are add, sub, mul, div (first character required\n");
printf("\n");
printf("Examples: \n");
printf(" imarith in1.fits in2.fits a out.fits - add the 2 files\n");
printf(" imarith in1.fits 1000.0 mul out.fits - mult in1 by 1000\n");
}
fits_open_file(&afptr, argv[1], READONLY, &status); /* open input images */
// status=0;
printf("\n1\n");
if (status) {
fits_report_error(stderr, status); /* print error message */
printf("\n2\n");
return(status);
}
printf("\n3\n");
status=0;
//fits_close_file(afptr,&status);
fits_open_file(&bfptr, argv[2], READONLY, &status);
printf("Status: %d Test\n",status);
if (status) {
value = atof(argv[2]);
if (value == 0.0) {
printf("Error: second argument is neither an image name"
" nor a valid numerical value.\n");
return(status);
}
image2 = 0;
status = 0;
}
// status=5;

fits_get_img_dim(afptr, &anaxis, &status); /* read dimensions */
if (image2) fits_get_img_dim(bfptr, &bnaxis, &status);
fits_get_img_size(afptr, 3, anaxes, &status);
if (image2) fits_get_img_size(bfptr, 3, bnaxes, &status);
//printf("\nStatus 2 : %d\n",status);
// status=0;
if (status) {
fits_report_error(stderr, status); /* print error message */
printf("\nIn if %d\n",status);
return(status);
printf("\n%d\n",status);
}
printf("\n anaxis : %d\n",anaxis);
if (anaxis > 3) {
printf("Error: images with > 3 dimensions are not supported\n");
check = 0;
}
/* check that the input 2 images have the same size */
else if ( image2 && ( anaxes[0] != bnaxes[0] ||
anaxes[1] != bnaxes[1] ||
anaxes[2] != bnaxes[2] ) ) {
printf("Error: input images don't have same size\n");
check = 0;
}
// printf("\n Bfor opr");
if (*argv[3] == 'a' || *argv[3] == 'A')
op = 1;
else if (*argv[3] == 's' || *argv[3] == 'S')
op = 2;
else if (*argv[3] == 'm' || *argv[3] == 'M')
op = 3;
else if (*argv[3] == 'd' || *argv[3] == 'D')
op = 4;
else {
printf("Error: unknown arithmetic operator\n");
check = 0;
}
//printf("\nAfter opern\n");
/* create the new empty output file if the above checks are OK */
if (check && !fits_create_file(&outfptr, argv[4], &status) )
{
/* copy all the header keywords from first image to new output file */
fits_copy_header(afptr, outfptr, &status);
npixels = anaxes[0]; /* no. of pixels to read in each row */
apix = (double *) malloc(npixels * sizeof(double)); /* mem for 1 row */
if (image2) bpix = (double *) malloc(npixels * sizeof(double));
if (apix == NULL || (image2 && bpix == NULL)) {
printf("Memory allocation error\n");
return(1);
}
/* loop over all planes of the cube (2D images have 1 plane) */
for (firstpix[2] = 1; firstpix[2] <= anaxes[2]; firstpix[2]++)
{
/* loop over all rows of the plane */
for (firstpix[1] = 1; firstpix[1] <= anaxes[1]; firstpix[1]++)
{
/* Read both images as doubles, regardless of actual datatype. */
/* Give starting pixel coordinate and no. of pixels to read. */
/* This version does not support undefined pixels in the image. */
if (fits_read_pix(afptr, TDOUBLE, firstpix, npixels, NULL, apix,
NULL, &status))
break; /* jump out of loop on error */
if (image2 && fits_read_pix(bfptr, TDOUBLE, firstpix, npixels,
NULL, bpix, NULL, &status))
break; /* jump out of loop on error */
switch (op) {
case 1:
for(ii=0; ii< npixels; ii++)
if (image2)
apix[ii] += bpix[ii];
else
apix[ii] += value;
break;
case 2:
for(ii=0; ii< npixels; ii++)
if (image2)
apix[ii] -= bpix[ii];
else
apix[ii] -= value;
break;
case 3:
for(ii=0; ii< npixels; ii++)
if (image2)
apix[ii] *= bpix[ii];
else
apix[ii] *= value;
break;
case 4:
for(ii=0; ii< npixels; ii++) {
if (image2) {
if (bpix[ii] !=0.)
apix[ii] /= bpix[ii];
else
apix[ii] = 0.;
}
else {
apix[ii] /= value;
}
}
}
fits_write_pix(outfptr, TDOUBLE, firstpix, npixels,
apix, &status); /* write new values to output image */
}
} /* end of loop over planes */
fits_close_file(outfptr, &status);
free(apix);
if (image2) free(bpix);
}
//printf("\nHi\n");
fits_close_file(afptr, &status);
if (image2) fits_close_file(bfptr, &status);
if (status) fits_report_error(stderr, status); /* print any error message */
return(status);
printf("\n1");
s=clock();
d=s-t;
printf("\nTime taken for execution : %d\n",d);
printf("\n2");
// DWORD nEndTime = timeGetTime();
// printf("Operation took %ums\n", nEndTime - nStart);
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900