标签:
“ACKing”同学以前做一个图像处理的项目时,遇到了一个问题,他需要摘取出图片中某个黑色线圏成的区域以内的图片,现在请你来帮助他完成第一步,把黑色线圏外的区域全部变为黑色。
图1 图2
已知黑线各处不会出现交叉(如图2),并且,除了黑线上的点外,图像中没有纯黑色(即像素为0的点)。
1 5 5 100 253 214 146 120 123 0 0 0 0 54 0 33 47 0 255 0 0 78 0 14 11 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 33 47 0 0 0 0 78 0 0 0 0 0 0
不知道为何提交程序测试会出错,没找到错误。。
#include <stdio.h> #include <stdlib.h> int main() { int N = 0, W = 0, H = 0, i = 0, j = 0, k = 0, l = 0; scanf("%d", &N); while(N--) { scanf("%d%d", &W, &H); int** a = (int**)malloc(sizeof(int*) * H); i = H; while(i--) { a[i] = (int*)malloc(sizeof(int) * W); } i = 0; while(i < H) { j = 0; while(j < W) { scanf("%d", &a[i][j]); j++; } i++; } i = 0; while(i < H) { int flagJ = 0; int flagK = 0; j = 0; k = W-1; while(k - j > -1) { if(a[i][j] == 0 && flagJ == 0) flagJ = 1; if(flagJ == 0) a[i][j] = -1; if(a[i][k] == 0 && flagK == 0) flagK = 1; if(flagK == 0) a[i][k] = -1; j++; k--; } i++; } i = 0; while(i < W) { int flagJ = 0; int flagK = 0; j = 0; k = H-1; while(k - j > -1) { if(a[j][i] == 0 && flagJ == 0) flagJ = 1; if(flagJ == 0 || a[j][i] == -1) a[j][i] = 0; if(a[k][i] == 0 && flagK == 0) flagK = 1; if(flagK == 0 || a[k][i] == -1) a[k][i] = 0; j++; k--; } i++; } i = 0; while(i < H) { j = 0; while(j < W) { printf("%d ", a[i][j]); j++; } printf("\n"); i++; } } return 1; }
标签:
原文地址:http://www.cnblogs.com/sdlwlxf/p/4553005.html