# include <stdio.h>
# include<string.h>
# include<stdlib.h>
# include<opencv2/opencv.hpp>
# include<sstream>
# include<iostream>
using namespace std;
using namespace cv;
void get_cursor_location(char * p, int len, char sign,int *first_loc,int * second_loc )
{
int flag = 1;
for (int i = 0; i < len; i++)
{
if (p[i] == sign)
{
if (flag)
{
*first_loc = i;
flag = 0;
}
else
*second_loc = i;
}
}
}
int get_string(int start, int end, char*p)//获取字符串并将其转化为int类型;
{
int i = 0;
char *pp=(char *)calloc(20,1);
for (i = start; i < end; i++)
{
*(pp+i-start) = *(p+i);
}
//获得字符串pp,
int num;
stringstream convert;
convert << pp;
convert >> num;
return num;//将字符串转化为int类型并返回
}
typedef struct strction
{
int image;
CvPoint position;
int flag;
}image_and_position;
void int_to_str(int num, string *str_num)
{
stringstream convert;
convert << num;
convert >> *str_num;
}
int main()
{
FILE *fp = fopen("C:\\Users\\93970\\Desktop\\1.txt", "r+");
char buf[100];
int line_count = 0;
int image_row = 608, image_col = 608;
vector<image_and_position> info;
int count_1_num = 0;
while (fgets(buf, 100, fp))
{
printf("%s", buf);
line_count++;
int first_ = 0, second_ = 0, first_douhao = 0, second_douhao = 0;
get_cursor_location(buf,strlen(buf),'_',&first_,&second_);//获取标志“_”的位置
get_cursor_location(buf, strlen(buf), ',', &first_douhao, &second_douhao);//获取标志“,”的位置
//printf("first_=%d\tsecond_=%d\n", first_, second_);
int image_num=get_string(0, first_, buf);
//printf("图片%d\t", image_num);
int row_num = get_string(first_+1 , second_, buf);
//printf("第%d行\t", row_num);
int col_num = get_string(second_ + 1, strlen(buf)-3, buf);
//printf("第%d列\t", col_num);
int flag = get_string(first_douhao+1, strlen(buf), buf);
//printf("是否绘点:%d\n", flag);
image_and_position a;
if (image_num == 1)
count_1_num++;
a.image = image_num;
a.position = cvPoint(row_num, col_num);
a.flag = flag;
info.push_back(a);//printf("%d", info[0].image);
}
printf("line_count=%d\n", line_count);
for (int i = 1; i <= 60; i++)
{
Mat binary_image(image_row, image_col, CV_8UC1,Scalar(0));
for (int image_row_count = 0; image_row_count < count_1_num; image_row_count++)
{
if (i == info[image_row_count+ count_1_num*(i-1)].image && info[image_row_count+ count_1_num*(i - 1)].flag == 1)
{
Rect r(info[image_row_count + count_1_num*(i - 1)].position.x, info[image_row_count + count_1_num*(i - 1)].position.y, 4, 4);
rectangle(binary_image, r, Scalar(255), 1);
binary_image(r).setTo(255);
}
}
//imshow("绘图", binary_image);
string str_i;
int_to_str(i, &str_i);
imwrite(str_i + ".png", binary_image);
binary_image.release();
}
return 0;
}
下面是本人txt文件中部分数据:
001_0_0,0
001_0_4,0
001_0_8,0
001_0_12,0
001_0_16,0
001_0_20,0
001_0_24,0
001_0_28,0
001_0_32,0
001_0_36,0
001_0_40,0
001_0_44,0
001_0_48,0
001_0_52,0
001_0_56,0
001_0_60,0
001_0_64,0
001_0_68,0
001_0_72,0
001_0_76,0
001_0_80,0
001_0_84,0
001_0_88,0
001_0_92,0
001_0_96,0
001_0_100,0
001_0_104,0
001_0_108,0
001_0_112,0
001_0_116,0
001_0_120,0
001_0_124,0
001_0_128,0
001_0_132,0
001_0_136,0
001_0_140,0
001_0_144,0
001_0_148,0
001_0_152,0
001_0_156,0
001_0_160,0
001_0_164,0
001_0_168,0
001_0_172,0
001_0_176,0
001_0_180,0
001_0_184,0
001_0_188,0
001_0_192,0
001_0_196,0
001_0_200,0
001_0_204,0
001_0_208,0
001_0_212,0
001_0_216,0
001_0_220,0
001_0_224,0
001_0_228,0
001_0_232,0
001_0_236,0
001_0_240,0
001_0_244,0
001_0_248,0
001_0_252,0
001_0_256,0
001_0_260,0
001_0_264,0
001_0_268,0
001_0_272,0
001_0_276,0
001_0_280,0
001_0_284,0
001_0_288,0
001_0_292,0
001_0_296,0
001_0_300,0
001_0_304,0
001_0_308,0
001_0_312,0
001_0_316,0
001_0_320,0
001_0_324,0
001_0_328,0
001_0_332,0
001_0_336,0
001_0_340,0
001_0_344,0
001_0_348,0
001_0_352,0
001_0_356,0
001_0_360,0
001_0_364,0
001_0_368,0
001_0_372,0
001_0_376,0
001_0_380,0
001_0_384,0
001_0_388,0
001_0_392,0
001_0_396,0
001_0_400,0
001_0_404,0
001_0_408,0
001_0_412,0
001_0_416,0
001_0_420,0
001_0_424,0
001_0_428,0
001_0_432,0
001_0_436,0
001_0_440,0
001_0_444,0
001_0_448,0
001_0_452,0
001_0_456,0
001_0_460,0
001_0_464,0
001_0_468,0
001_0_472,0
001_0_476,0
001_0_480,0
001_0_484,0
001_0_488,0
001_0_492,0
001_0_496,0
001_0_500,0
001_0_504,0
001_0_508,0
001_0_512,0
001_0_516,0
001_0_520,0
001_0_524,0
001_0_528,0
001_0_532,0
001_0_536,0
001_0_540,0
001_0_544,0
001_0_548,0
001_0_552,0
001_0_556,0
001_0_560,0
001_0_564,0
001_0_568,0
001_0_572,1
001_0_576,1
001_0_580,1
001_0_584,1
001_0_588,1
001_0_592,1
001_0_596,1
001_0_600,1
001_0_604,1
001_4_0,0
001_4_4,0
001_4_8,0
001_4_12,0
001_4_16,0
001_4_20,0
001_4_24,0
001_4_28,0
001_4_32,0
001_4_36,0
001_4_40,0
001_4_44,0
001_4_48,0
001_4_52,0
001_4_56,0
001_4_60,0
001_4_64,0
001_4_68,0
001_4_72,0
001_4_76,0
001_4_80,0
001_4_84,0
001_4_88,0
001_4_92,0
001_4_96,0
001_4_100,0
001_4_104,0
001_4_108,0
001_4_112,0
001_4_116,0
001_4_120,0
001_4_124,0
001_4_128,0
001_4_132,0
001_4_136,0
001_4_140,0
001_4_144,0
001_4_148,0
001_4_152,0
001_4_156,0
001_4_160,0
001_4_164,0
001_4_168,0
001_4_172,0
001_4_176,0
001_4_180,0
001_4_184,0
001_4_188,0
001_4_192,0
001_4_196,0
001_4_200,0
001_4_204,0
001_4_208,0
001_4_212,0
001_4_216,0
001_4_220,0
001_4_224,0
001_4_228,0
001_4_232,0
001_4_236,0
001_4_240,0
001_4_244,0
001_4_248,0
001_4_252,0
001_4_256,0
001_4_260,0
001_4_264,0
001_4_268,0
001_4_272,0
001_4_276,0
001_4_280,0
001_4_284,0
001_4_288,0
001_4_292,0
001_4_296,0
001_4_300,0
001_4_304,0
001_4_308,0
001_4_312,0
001_4_316,0
001_4_320,0
001_4_324,0
001_4_328,0
001_4_332,0
001_4_336,0
001_4_340,0
001_4_344,0
001_4_348,0
001_4_352,0
001_4_356,0
001_4_360,0
001_4_364,0
001_4_368,0
001_4_372,0
001_4_376,0
001_4_380,0
001_4_384,0
001_4_388,0
001_4_392,0
001_4_396,0
001_4_400,0
001_4_404,0
001_4_408,0
001_4_412,0
001_4_416,0
001_4_420,0
001_4_424,0
001_4_428,0
001_4_432,0
001_4_436,0
001_4_440,0
001_4_444,0
001_4_448,0
001_4_452,0
001_4_456,0
001_4_460,0
001_4_464,0
001_4_468,0
001_4_472,0
001_4_476,0
001_4_480,0
001_4_484,0
001_4_488,0
001_4_492,0
001_4_496,0
001_4_500,0
001_4_504,0
001_4_508,0
001_4_512,0
001_4_516,0
001_4_520,0
001_4_524,0
001_4_528,0
001_4_532,0
001_4_536,0
001_4_540,0
001_4_544,0
001_4_548,0
001_4_552,0
001_4_556,0
001_4_560,0
001_4_564,0
001_4_568,0
001_4_572,0
001_4_576,1
001_4_580,1
001_4_584,1
001_4_588,1
001_4_592,1
001_4_596,1
001_4_600,1
001_4_604,1
001_8_0,0
001_8_4,0
001_8_8,0
001_8_12,0
001_8_16,0
001_8_20,0
001_8_24,0
001_8_28,0
001_8_32,0
001_8_36,0
001_8_40,0
001_8_44,0
001_8_48,0
001_8_52,0
001_8_56,0
001_8_60,0
001_8_64,0
001_8_68,0
001_8_72,0
001_8_76,0
001_8_80,0
001_8_84,0
001_8_88,0
001_8_92,0
001_8_96,0