1. 800x600の白線画像を1/13.333...(0.075)の60x45に縮める。
2. 60x45のうちの下から60x14を中間BMPファイルとしてセーブ
3. 60x14のうちの56x10を切り出してBMPファイルに。1つにつき5x5の25個できる。
4. 左右直進をやる。
// curve_dataset_bmp.h
// 2017/11/29 by marsee
//
#ifndef __CURVE_DATASET_BMP_H__
#define __CURVE_DATASET_BMP_H__
#include "hls_video.h"
#define BMP_HEIGHT 600
#define BMP_WIDTH 800
#define REDUCTION_RATIO 0.075 // 1/13.3333... 60x45
#define DATASET_HEIGHT 10
#define DATASET_WIDTH 56
#define STRAIGHT_BMP_FILE_NAME straight
#define LEFT_TURN_BMP_FILE_NAME left_turn
#define RIGHT_TURN_BMP_FILE_NAME right_turn
#define STRAIGHT_NUM_OF_IMAGE 41
#define LEFT_TURN_NUM_OF_IMAGE 18
#define RIGHT_TURN_NUM_OF_IMAGE 18
typedef hls::Scalar<3, unsigned char> RGB_PIXEL;
typedef hls::Mat<BMP_HEIGHT, BMP_WIDTH, HLS_8UC3> RGB_IMAGE;
typedef hls::Mat<BMP_HEIGHT, BMP_WIDTH, HLS_8UC1> GRAY_IMAGE;
#endif
// curve_dataset_bmp.cpp
// 2017/11/29 by marsee
//
#include <iostream>
#include "hls_opencv.h"
#include "curve_dataset_bmp.h"
#include <arpa/inet.h>
int main(){
char straight_fn[256] = "straight";
char left_turn_fn[256] = "left_turn";
char right_turn_fn[256] = "right_turn";
char bmp_file[256];
FILE *ftin, *ftln;
char train_image_name[256] = "train_curve_run_image";
char train_label_name[256] = "train_curve_run_label";
uint32_t buf[5];
uint8_t bufchar[100];
if ((ftin = fopen(train_image_name, "wb")) == NULL){
fprintf(stderr, "Can't open %s\n", train_image_name);
exit(1);
}
if ((ftln = fopen(train_label_name, "wb")) == NULL){
fprintf(stderr, "Can't open %s\n", train_label_name);
exit(1);
}
// Writed header
buf[0] = htonl(0x803); // magic number
buf[1] = htonl((STRAIGHT_NUM_OF_IMAGE+LEFT_TURN_NUM_OF_IMAGE+RIGHT_TURN_NUM_OF_IMAGE)*25); // number of image
buf[2] = htonl(10); // number of rows (10)
buf[3] = htonl(56); // number of columns (56)
fwrite(buf, sizeof(uint32_t), 4, ftin);
buf[0] = htonl(0x801); // magic number
buf[1] = htonl((STRAIGHT_NUM_OF_IMAGE+LEFT_TURN_NUM_OF_IMAGE+RIGHT_TURN_NUM_OF_IMAGE)*25); // number of image
fwrite(buf, sizeof(uint32_t), 2, ftln);
// refereed to http://opencv.jp/cookbook/opencv_img.html
// straight
for(int i=0; i<STRAIGHT_NUM_OF_IMAGE; i++){
sprintf(bmp_file, "%s%d.bmp", straight_fn, i);
cv::Mat straight_img = cv::imread(bmp_file,1);
if(straight_img.empty()){
fprintf(stderr,"Error: %s\n", bmp_file);
return(-1);
}
cv::Mat reduct_img(straight_img.rows*0.075, straight_img.cols*0.075, straight_img.type());
cv::resize(straight_img, reduct_img, reduct_img.size(), cv::INTER_LINEAR);
cv::Mat gray_img;
cv::cvtColor(reduct_img, gray_img, CV_BGR2GRAY);
sprintf(bmp_file, "%s_RED%d.bmp", straight_fn, i);
cv::imwrite(bmp_file, gray_img);
for(int y=0; y<5; y++){
for(int x=0; x<5; x++){
cv::Rect rect_center(x, 30+y, 56, 10);
cv::Mat img_rect(gray_img, rect_center);
sprintf(bmp_file, "%s_RED_rect%d_%d%d.bmp", straight_fn, i, y, x);
cv::imwrite(bmp_file, img_rect);
for(int iy=0; iy<img_rect.rows; iy++){
for(int ix=0; ix<img_rect.cols; ix++){
bufchar[ix] = img_rect.at<uchar>(iy, ix);
}
fwrite(bufchar, sizeof(uint8_t), img_rect.cols, ftin); // image write
}
bufchar[0] = 0x1;
fwrite(bufchar, sizeof(uint8_t), 1, ftln); // label write
}
}
}
// left turn
for(int i=0; i<LEFT_TURN_NUM_OF_IMAGE; i++){
sprintf(bmp_file, "%s%d.bmp", left_turn_fn, i);
cv::Mat left_turn_img = cv::imread(bmp_file,1);
if(left_turn_img.empty()){
fprintf(stderr,"Error: %s\n", bmp_file);
return(-1);
}
cv::Mat reduct_img(left_turn_img.rows*0.075, left_turn_img.cols*0.075, left_turn_img.type());
cv::resize(left_turn_img, reduct_img, reduct_img.size(), cv::INTER_LINEAR);
cv::Mat gray_img;
cv::cvtColor(reduct_img, gray_img, CV_BGR2GRAY);
sprintf(bmp_file, "%s_RED%d.bmp", left_turn_fn, i);
cv::imwrite(bmp_file, gray_img);
for(int y=0; y<5; y++){
for(int x=0; x<5; x++){
cv::Rect rect_center(x, 30+y, 56, 10);
cv::Mat img_rect(gray_img, rect_center);
sprintf(bmp_file, "%s_RED_rect%d_%d%d.bmp", left_turn_fn, i, y, x);
cv::imwrite(bmp_file, img_rect);
for(int iy=0; iy<img_rect.rows; iy++){
for(int ix=0; ix<img_rect.cols; ix++){
bufchar[ix] = img_rect.at<uchar>(iy, ix);
}
fwrite(bufchar, sizeof(uint8_t), img_rect.cols, ftin); // image write
}
bufchar[0] = 0x0;
fwrite(bufchar, sizeof(uint8_t), 1, ftln); // label write
}
}
}
// right turn
for(int i=0; i<RIGHT_TURN_NUM_OF_IMAGE; i++){
sprintf(bmp_file, "%s%d.bmp", right_turn_fn, i);
cv::Mat right_turn_img = cv::imread(bmp_file,1);
if(right_turn_img.empty()){
fprintf(stderr,"Error: %s\n", bmp_file);
return(-1);
}
cv::Mat reduct_img(right_turn_img.rows*0.075, right_turn_img.cols*0.075, right_turn_img.type());
cv::resize(right_turn_img, reduct_img, reduct_img.size(), cv::INTER_LINEAR);
cv::Mat gray_img;
cv::cvtColor(reduct_img, gray_img, CV_BGR2GRAY);
sprintf(bmp_file, "%s_RED%d.bmp", right_turn_fn, i);
cv::imwrite(bmp_file, gray_img);
for(int y=0; y<5; y++){
for(int x=0; x<5; x++){
cv::Rect rect_center(x, 30+y, 56, 10);
cv::Mat img_rect(gray_img, rect_center);
sprintf(bmp_file, "%s_RED_rect%d_%d%d.bmp", right_turn_fn, i, y, x);
cv::imwrite(bmp_file, img_rect);
for(int iy=0; iy<img_rect.rows; iy++){
for(int ix=0; ix<img_rect.cols; ix++){
bufchar[ix] = img_rect.at<uchar>(iy, ix);
}
fwrite(bufchar, sizeof(uint8_t), img_rect.cols, ftin); // image write
}
bufchar[0] = 0x2;
fwrite(bufchar, sizeof(uint8_t), 1, ftln); // label write
}
}
}
fclose(ftin);
fclose(ftln);
return(0);
}
日 | 月 | 火 | 水 | 木 | 金 | 土 |
---|---|---|---|---|---|---|
- | - | - | - | - | 1 | 2 |
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |