Image proccessing in PROGRAMING C NO MAIN FUNCTION NEEDED Co
Image proccessing in PROGRAMING C
NO MAIN FUNCTION NEEDED!
Color-Filter an image:
1. All pixels in the picture with color in the chosen range will be replaced with new color. The following shows the pseudo code for the color filter.
if (R in the range of [target_r - threshold, target_r + threshold]) and (G in the range of [target_g - threshold, target_g + threshold]) and (B in the range of [target_b - threshold, target_b + threshold])
R = replace_r ;
G = replace_g ;
B = replace_b ;
else keep the current color
Given: void ColorFilter(unsigned char R[WIDTH][HEIGHT], unsigned char G[WIDTH][HEIGHT], unsigned char B[WIDTH][HEIGHT], int target_r, int target_g, int target_b, int threshold, int replace_r, int replace_g, int replace_b);
2. To mirror an image vertically, the intensity values in vertical direction at the top should be reversed and copied to the bottom.
Given: /* mirror image vertically */ void VMirror(unsigned char R[WIDTH][HEIGHT], unsigned char G[WIDTH][HEIGHT], unsigned char B[WIDTH][HEIGHT]);
3. This operation will add borders to the current image. The border color and width (in pixels) of the borders are parameters given by the user. Within your implementation, you should define an aspect ratio of 16:9 for your horizontal to vertical border. This should make your horizontal border look thicker than the vertical one.
Given: void AddBorder(unsigned char R[WIDTH][HEIGHT], unsigned char G[WIDTH][HEIGHT], unsigned char B[WIDTH][HEIGHT], char color[SLEN], int border_width);
4.The edge detection works this way: the intensity value at each pixel is mapped to a new value, which is the sum of itself and its 8 neighbors with different parameters. Note the sum of all parameters is 0, which will result in a vary dark image where only the edges are detected are colored. The following shows an example of the filter and the applied pixel:
You need to define and implement a function to do this DIP. Note that you have to set the boundary for the newly generated pixel value, i.e., the value should be within the range of [0,255]. Hint (the following code can be used to handle pixel intensity under- or overflow): if (v<0) v=0; else if (v>255) v=255;
Given: void Edge(unsigned char R[WIDTH][HEIGHT], unsigned char G[WIDTH][HEIGHT], unsigned char B[WIDTH][HEIGHT]);
Solution
1) color filter
#define filterWidth 3
#define filterHeight 3
double filter[filterHeight][filterWidth] =
{
0, 0, 0,
0, 1, 0,
0, 0, 0
};
double factor = 1.0;
double bias = 0.0;
int main(int argc, char *argv[])
{
//load the image into the buffer
unsigned long w = 0, h = 0;
std::vector<ColorRGB> image;
loadImage(image, w, h, \"pics/photo3.png\");
std::vector<ColorRGB> result(image.size());
//set up the screen
screen(w, h, 0, \"Filters\");
ColorRGB color; //the color for the pixels
//apply the filter
for(int x = 0; x < w; x++)
for(int y = 0; y < h; y++)
{
double red = 0.0, green = 0.0, blue = 0.0;
//multiply every value of the filter with corresponding image pixel
for(int filterY = 0; filterY < filterHeight; filterY++)
for(int filterX = 0; filterX < filterWidth; filterX++)
{
int imageX = (x - filterWidth / 2 + filterX + w) % w;
int imageY = (y - filterHeight / 2 + filterY + h) % h;
red += image[imageY * w + imageX].r * filter[filterY][filterX];
green += image[imageY * w + imageX].g * filter[filterY][filterX];
blue += image[imageY * w + imageX].b * filter[filterY][filterX];
}
//truncate values smaller than zero and larger than 255
result[y * w + x].r = min(max(int(factor * red + bias), 0), 255);
result[y * w + x].g = min(max(int(factor * green + bias), 0), 255);
result[y * w + x].b = min(max(int(factor * blue + bias), 0), 255);
}
//draw the result buffer to the screen
for(int y = 0; y < h; y++)
for(int x = 0; x < w; x++)
{
pset(x, y, result[y * w + x]);
}
//redraw & sleep
redraw();
sleep();
}
2) mirror image
BOOL BMPFile::VertFlipBuf(unsigned char * inbuf,
UINT widthBytes,
__int16 height)
{
BYTE *tb1;
BYTE *tb2;
if (inbuf==NULL)
return FALSE;
UINT bufsize;
bufsize=widthBytes;
tb1= new unsigned char[bufsize];
if (tb1==NULL) {
return FALSE;
}
tb2= new unsigned char [bufsize];
if (tb2==NULL) {
delete [] tb1;
return FALSE;
}
__int16 row_cnt;
ULONG off1=0;
ULONG off2=0;
for (row_cnt=0;row_cnt<(height+1)/2;row_cnt++)
{
off1=row_cnt*bufsize;
off2=((height-1)-row_cnt)*bufsize;
memcpy(tb1,inbuf+off1,bufsize*sizeof(unsigned char));
memcpy(tb2,inbuf+off2,bufsize*sizeof(unsigned char));
memcpy(inbuf+off1,tb2,bufsize*sizeof(unsigned char));
memcpy(inbuf+off2,tb1,bufsize*sizeof(unsigned char));
}
delete [] tb1;
delete [] tb2;
return TRUE;
}
3) putting boder
Edit & Run
HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE); COORD CursorPosition; void gotoXY(int x, int y); void gotoXY(int x, int y, string text); void Draw(int style, int col, int row, int length,int amount, bool filled, int shadow ); int main(int) { // Draw routine // first number = 1 for single line, 2 for a double lines // second number = column\'s across // third number = row\'s down // fourth number = box total length // fifth number = box total height // sixth number = 0 for not filled, 1 for filled //seventh number = 0 for no shadow, 1 for a light shadow, 2 for medium shadow, 3 for a dark shadow and 4 for a solid shadow. string PressKey = \"- Programmed by whitenite1 - Added flashing box frame on September 14th, 2011 - Press any key to end program...\"; int x, y, len, frame; y = 1; frame = 2; char holder; Draw(1,2,1,76,23,0,0); // Box, 1 line, around screen Draw(2,12,4,56,16,1,4); // Box, 2 lines, around screen Draw(1,28,6,25,3,0,2); // Box, 1 line, for Title gotoXY(30,7,\"Scrolling Text Demo 2\"); Draw(2,23,13,36,3,0,2); // Box for moving text len = PressKey.length(); do { gotoXY( 25,14); for (x=0;x<32;x++) cout << PressKey[x]; // scrolling the string called PressKey holder = PressKey[0]; for ( x=0;x<len;x++) PressKey[x] = PressKey[x+1]; PressKey[len] = holder; Sleep(120); Draw(frame,23,13,36,3,0,0); y++; if (y>5) { Draw(frame,28,6,25,3,0,0); frame--; y=0; } if ( frame==0) frame=+2; } while (_kbhit() == \'\\0\' ); gotoXY(25,14,\" Good-Bye \"); gotoXY(25,14); Sleep(2500); return 0; } void gotoXY(int x, int y) { CursorPosition.X = x; CursorPosition.Y = y; SetConsoleCursorPosition(console,CursorPosition); } void gotoXY(int x, int y, string text) { CursorPosition.X = x; CursorPosition.Y = y; SetConsoleCursorPosition(console,CursorPosition); cout << text; } void Draw(int style, int col, int row, int length,int amount, bool fill, int sw ) { // Draws a 1 or 2 line box int a; if ( sw >4) sw = 4; style=(style-1)*6; char box[12]; char shdw[5]; box[0] = \'\\xDA\';// box[1] = \'\\xBF\';// box[2] = \'\\xC0\';// box[3] = \'\\xD9\';// box[4] = \'\\xB3\';// box[5] = \'\\xC4\';// box[6] = \'\\xC9\';// box[7] = \'\\xBB\';// box[8] = \'\\xC8\';// box[9] = \'\\xBC\';// box[10] = \'\\xBA\';// box[11] = \'\\xCD\';// shdw[1] = \'\\xB0\';// shdw[2] = \'\\xB1\';// shdw[3] = \'\\xB2\';// shdw[4] = \'\\xDB\';// char tl,tr,bl,br,side,edge,shadow; tl = box[style]; tr = box[style+1]; bl = box[style+2]; br = box[style+3]; side = box[style+4]; edge = box[style+5]; shadow = shdw[sw]; string Line(length-2,edge); string Shadow(length,shadow); string Fill(length-2, \' \'); gotoXY(col,row); cout << tl << Line << tr; for (a = 1; a <amount-1;a++) { gotoXY(col,row+a); cout << side; if (fill) cout << Fill; else gotoXY(col+length-1,row+a); cout << side; if (sw) cout << shadow; } gotoXY(col,(amount+row)-1); cout << bl << Line << br; if (sw) { cout << shadow; gotoXY(col+1,row+amount , Shadow ); } } | Edit & Run |


