图像处理
1 #include<graphics.h> 2 #include<conio.h> 3 4 void Mosaic(IMAGE *img, int size, int sx, int sy) 5 { 6 int width = img->getheight();//获取图形的宽 7 int height = img->getwidth(); 8 int redsum;//红色的值的和 9 int bluesum;//蓝色值的和 10 int greensum;//绿色值和 11 int counnt;//每个小方块内的像素数量 12 int color;//每个像素的颜色 13 int x, y, tx, ty; 14 DWORD* p = GetImageBuffer(img);//获取指向显存的指针 15 16 //求左上角第一个樊哙的坐标 17 sx = (sx%size==0?0:sx%size-size); 18 sy = (sy%size == 0 ? 0 : sy%size - size); 19 //循环小方块 20 for (y = sy; y < height;y+=size) 21 { 22 for (x = sx; x < width;x+=size) 23 { 24 redsum = greensum = bluesum = counnt = 0; 25 //遍历像素点 26 for (ty = min(y + size, height) - 1; ty >= max(y, 0); ty--) 27 { 28 for (tx = min(x + size, width)-1; tx >= max(x, 0); tx--) 29 { 30 color = p[ty*width + tx]; 31 redsum += GetRValue(color); 32 greensum+=GetGValue(color); 33 bluesum += GetBValue(color); 34 counnt++; 35 } 36 } 37 38 39 redsum /= counnt; 40 greensum /= counnt; 41 bluesum /= counnt; 42 color = RGB(redsum, greensum, bluesum); 43 for (ty = min(y + size, height) - 1; ty >= max(y, 0); ty--) 44 { 45 for (tx = min(x + size, width) - 1; tx >= max(x, 0); tx--) 46 { 47 p[ty*width + tx] = color; 48 } 49 } 50 51 } 52 } 53 54 } 55 56 int main() 57 { 58 //搭建绘图环境 59 initgraph(640, 480); 60 //setbkcolor(RED); 61 //cleardevice(); 62 IMAGE img; 63 loadimage(&img,L"11.jpg", 640, 480); 64 putimage(0, 0, &img); 65 _getch(); 66 Mosaic(&img, 5, 0, 0); 67 putimage(0,0,&img); 68 //loadimage(NULL,L"11.jpg"); 69 _getch(); 70 return 0; 71 }