Created
March 21, 2012 02:57
-
-
Save atneik/2143929 to your computer and use it in GitHub Desktop.
Image processing A1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <cv.h> | |
#include <highgui.h> | |
#define MAG 12 | |
struct ratio | |
{ | |
float value; | |
char alpha; | |
}; | |
int compare (const void *a, const void *b) | |
{ | |
struct ratio *ia = (struct ratio *)a; | |
struct ratio *ib = (struct ratio *)b; | |
if(ia->value > ib->value) return 1; | |
if(ia->value < ib->value) return -1; | |
return 0; | |
} | |
struct ratio b_ratio[95]; | |
void reverseTable() | |
{ | |
float temp; | |
for (int i=0; i<47; i++) { | |
temp=b_ratio[i].value; | |
b_ratio[i].value=b_ratio[94-i].value; | |
b_ratio[94-i].value=temp; | |
} | |
} | |
int getAlpha(float value) | |
{ | |
float diff; | |
int c; | |
float prevDiff=255; | |
int prevC=' '; | |
for (int i=0; i<95; i++) { | |
if(b_ratio[i].value>=value) | |
{ | |
diff=b_ratio[i].value - value; | |
c=b_ratio[i].alpha; | |
} | |
else if(b_ratio[i].value<value) | |
{ | |
diff=value-b_ratio[i].value; | |
c=b_ratio[i].alpha; | |
} | |
if(diff>prevDiff) | |
{ | |
return prevC; | |
} | |
prevDiff=diff; | |
prevC=c; | |
} | |
return c; | |
} | |
int main(int argc, char** argv) | |
{ | |
CvScalar point_color; | |
CvSize frame_size, newframe_size; | |
CvFont font; | |
cvInitFont(&font, CV_FONT_HERSHEY_SIMPLEX, 0.5, 0.5, 0, 1, CV_AA); | |
IplImage *frame,*picFrame, *newFrame; | |
char c[2]; | |
c[1]='\0'; | |
float avg; | |
size_t len=95; | |
printf("\nCreating Text Table...\n"); | |
for (int z=32; z<127; z++) { //95 printable | |
c[0]=z; | |
frame = cvCreateImage(cvSize(64,64),IPL_DEPTH_8U,1); | |
cvPutText(frame, c, cvPoint(2,32),&font, cvScalar(255,255,255)); | |
avg=0; | |
for (int i=0; i<64; i++) { | |
for (int j=0; j<64; j++) { | |
point_color=cvGet2D(frame,i,j); | |
avg=avg+point_color.val[0]; | |
} | |
} | |
avg=avg/(16*16); | |
b_ratio[z-32].alpha=z; | |
b_ratio[z-32].value=avg; | |
} | |
qsort (b_ratio,len, sizeof (ratio), compare); | |
float scale = (255.00/b_ratio[94].value); | |
printf("\n%f\n",scale); | |
reverseTable(); | |
for (int i=0; i<95; i++) { | |
b_ratio[i].value=b_ratio[i].value*scale; | |
printf("%c: %f\n",b_ratio[i].alpha,b_ratio[i].value); | |
} | |
cvReleaseImage(&frame); | |
printf("\nText Table created. Now loading %s\n",argv[1]); | |
picFrame=cvLoadImage(argv[1],0); | |
if(!picFrame) | |
{ | |
printf("ERROR loading file"); | |
return 1; | |
} | |
frame_size.height = picFrame->height; | |
frame_size.width = picFrame->width; | |
printf("Loaded image (%d x %d) ",frame_size.height,frame_size.width); | |
printf("\n"); | |
newframe_size=frame_size; | |
newframe_size.width*=MAG; | |
newframe_size.height*=MAG; | |
newFrame=cvCreateImage(newframe_size,IPL_DEPTH_8U,1); | |
CvScalar s; | |
s.val[0]=255; | |
for (int i=0; i<newframe_size.width; i++) { | |
for (int j=0; j<newframe_size.height; j++) { | |
cvSet2D(newFrame,j,i,s); | |
} | |
} | |
printf("New Image cleared!\n"); | |
CvFont newfont; | |
double hScale=0.5; | |
double vScale=0.5; | |
int lineWidth=1; | |
cvInitFont(&newfont,CV_FONT_HERSHEY_SIMPLEX, hScale,vScale,0,lineWidth); | |
//cvPutText (newFrame,"A",cvPoint(10,10), &newfont, cvScalar(255,255,255)); | |
cvNamedWindow("Window", CV_WINDOW_AUTOSIZE); | |
char temp[2]; | |
for (int i=0; i<frame_size.width; i++) { | |
for (int j=0; j<frame_size.height; j++) { | |
//printf("(%d,%d) ",i,j); | |
point_color=cvGet2D(picFrame,j,i); | |
printf("%c",getAlpha(point_color.val[0])); | |
sprintf(temp,"%c",getAlpha(point_color.val[0])); | |
cvPutText (newFrame,temp,cvPoint(i*MAG,j*MAG), &newfont, cvScalar(0,0,0)); | |
//cvShowImage("Window", newFrame); | |
} | |
printf("\n"); | |
} | |
cvShowImage("Window", newFrame); | |
cvWaitKey(0); | |
if(!cvSaveImage("NEWimage.jpg",newFrame)) printf("Could not save"); | |
cvDestroyWindow("Window"); | |
cvReleaseImage(&picFrame); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment