mirror of
https://gitcode.com/gh_mirrors/es/esp32-opencv.git
synced 2025-08-14 18:50:49 +08:00
Benchmark formatting + on different images sizes
TODO: - markdown formatting ? - header/footer
This commit is contained in:
@ -21,112 +21,52 @@ using namespace cv;
|
||||
using namespace std;
|
||||
|
||||
|
||||
char* TAG="opencv_tests";
|
||||
const String imageFileName = "/spiffs/jack.png";
|
||||
const char* TAG="opencv_tests";
|
||||
const int REPEAT = 3; // number of times to repeat the function call for the average
|
||||
const int NB_IMAGES = 3; // number of images with different resolutions for the tests
|
||||
const String images_res[] = {"160x120", "320x240", "640x480"};
|
||||
|
||||
|
||||
extern "C" {
|
||||
void app_main(void);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Function that tests the basics of OpenCV:
|
||||
* - Matrices creation
|
||||
* - Arithmetic operations
|
||||
* -
|
||||
*/
|
||||
void test_basics()
|
||||
Mat read_image_specific_res(const String &fileName)
|
||||
{
|
||||
ESP_LOGI(TAG, "Doing basic matrices test");
|
||||
Mat img = imread(fileName, IMREAD_GRAYSCALE);
|
||||
|
||||
/* Matrices initialization tests */
|
||||
Mat M1(2,2, CV_8UC3, Scalar(0,0,255));
|
||||
cout << "M1 = " << endl << " " << M1 << endl << endl;
|
||||
|
||||
Mat M2(2,2, CV_8UC3, Scalar(0,0,111));
|
||||
cout << "M2 = " << endl << " " << M2 << endl << endl;
|
||||
|
||||
Mat eye = Mat::eye(10, 10, CV_32F) * 0.1;
|
||||
cout << "eye = " << endl << " " << eye << endl << endl;
|
||||
|
||||
Mat ones = Mat::ones(15, 4, CV_8U)*3;
|
||||
cout << "ones = " << endl << " " << ones << endl << endl;
|
||||
|
||||
vector<float> v;
|
||||
v.push_back((float)CV_PI);
|
||||
v.push_back(2);
|
||||
v.push_back(3.01f);
|
||||
cout << "floats vector = " << endl << " " << Mat(v) << endl << endl;
|
||||
|
||||
uint8_t data[15] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
|
||||
Mat M3 = Mat(3, 5, CV_8UC1, data);
|
||||
cout << "Gray matrix = " << endl << " " << M3 << endl << endl;
|
||||
}
|
||||
|
||||
/**
|
||||
* Function that reads and write on the SPI Flash File System
|
||||
*/
|
||||
void test_spiffs()
|
||||
{
|
||||
ESP_LOGI(TAG, "Doing SPIFFS read/write tests");
|
||||
|
||||
/* SPIFFS file reading test */
|
||||
FILE* f = fopen("/spiffs/hello.txt", "r");
|
||||
if (f == NULL) {
|
||||
ESP_LOGE(TAG, "Failed to open file for reading");
|
||||
return;
|
||||
if(img.empty()) {
|
||||
ESP_LOGW(TAG, "cannot read the image: %s", fileName.c_str());
|
||||
return img;
|
||||
}
|
||||
char line[64];
|
||||
fgets(line, sizeof(line), f);
|
||||
fclose(f);
|
||||
|
||||
printf("Read from file: '%s'\n", line);
|
||||
|
||||
/* Images reading/writing tests */
|
||||
Mat src;
|
||||
src = imread(imageFileName, IMREAD_COLOR);
|
||||
if(src.empty()) {
|
||||
ESP_LOGW(TAG, "cannot read the image: %s", imageFileName.c_str());
|
||||
return;
|
||||
}
|
||||
|
||||
imwrite("/spiffs/dst.png", src);
|
||||
ESP_LOGI(TAG, "Image written!");
|
||||
ESP_LOGI(TAG, "Image read of %dx%dx%d, with %d pixel depth", img.rows, img.cols, img.channels(), img.depth());
|
||||
return img;
|
||||
}
|
||||
|
||||
void test_thresholds(const Mat &src)
|
||||
{
|
||||
ESP_LOGI(TAG, "==================== Thresholding tests ====================");
|
||||
|
||||
Mat gray, dst;
|
||||
|
||||
// convert to grayscale
|
||||
cvtColor(src, gray, COLOR_BGR2GRAY);
|
||||
Mat dst;
|
||||
|
||||
// apply thresholds
|
||||
BENCHMARK_MS("Binary threshold", REPEAT, threshold, gray, dst, 128, 255, THRESH_BINARY);
|
||||
BENCHMARK_MS("Triangle threshold", REPEAT, threshold, gray, dst, 0, 255, THRESH_BINARY | THRESH_TRIANGLE);
|
||||
BENCHMARK_MS("OTSU threshold", REPEAT, threshold, gray, dst, 0, 255, THRESH_BINARY | THRESH_OTSU);
|
||||
BENCHMARK_MS("To zero threshold", REPEAT, threshold, gray, dst, 128, 255, THRESH_TOZERO);
|
||||
BENCHMARK_MS("Binary threshold", REPEAT, threshold, src, dst, 128, 255, THRESH_BINARY);
|
||||
BENCHMARK_MS("Triangle threshold", REPEAT, threshold, src, dst, 0, 255, THRESH_BINARY | THRESH_TRIANGLE);
|
||||
BENCHMARK_MS("OTSU threshold", REPEAT, threshold, src, dst, 0, 255, THRESH_BINARY | THRESH_OTSU);
|
||||
BENCHMARK_MS("To zero threshold", REPEAT, threshold, src, dst, 128, 255, THRESH_TOZERO);
|
||||
}
|
||||
|
||||
void test_blurring(const Mat &src)
|
||||
{
|
||||
ESP_LOGI(TAG, "==================== Blurring tests ====================");
|
||||
|
||||
Mat dst;
|
||||
|
||||
// apply blurs
|
||||
BENCHMARK_MS("GaussianBlur", REPEAT, GaussianBlur, src, dst, Size(9, 9), 0, 0, BORDER_DEFAULT);
|
||||
BENCHMARK_MS("medianBlur", REPEAT, medianBlur, src, dst, 9);
|
||||
BENCHMARK_MS("GaussianBlur9x9", REPEAT, GaussianBlur, src, dst, Size(9, 9), 0, 0, BORDER_DEFAULT);
|
||||
BENCHMARK_MS("medianBlur9x9", REPEAT, medianBlur, src, dst, 9);
|
||||
BENCHMARK_MS("bilateralFilter", REPEAT, bilateralFilter, src, dst, 9, 18, 5, BORDER_DEFAULT);
|
||||
}
|
||||
|
||||
void test_morphology_transform(const Mat &src)
|
||||
{
|
||||
ESP_LOGI(TAG, "=========== Morphology transformations tests ===========");
|
||||
|
||||
Mat dst;
|
||||
|
||||
// create a kernel for the transformation
|
||||
@ -140,8 +80,6 @@ void test_morphology_transform(const Mat &src)
|
||||
|
||||
void test_resize(const Mat &src)
|
||||
{
|
||||
ESP_LOGI(TAG, "==================== Resizing tests ====================");
|
||||
|
||||
Mat dst;
|
||||
|
||||
BENCHMARK_MS("linear resize", REPEAT, resize, src, dst, Size(), 0.75, 0.75, INTER_LINEAR);
|
||||
@ -153,42 +91,34 @@ void test_resize(const Mat &src)
|
||||
|
||||
void test_edge_detect(const Mat &src)
|
||||
{
|
||||
ESP_LOGI(TAG, "================= Edge detection tests =================");
|
||||
Mat dst;
|
||||
|
||||
Mat dst, gray;
|
||||
|
||||
// convert to grayscale
|
||||
cvtColor(src, gray, COLOR_BGR2GRAY);
|
||||
|
||||
BENCHMARK_MS("Sobel", REPEAT, Sobel, gray, dst, 2, 2, 1, 3, 1, 0, BORDER_DEFAULT);
|
||||
//BENCHMARK_MS("Canny", REPEAT, Canny, gray, dst, 8, 24, 3, false); // FIXME: can't deduce template parameter 'F'
|
||||
BENCHMARK_MS("Sobel", REPEAT, Sobel, src, dst, 2, 2, 1, 3, 1, 0, BORDER_DEFAULT);
|
||||
//BENCHMARK_MS("Canny", REPEAT, Canny, src, dst, 8, 24, 3, false); // FIXME: can't deduce template parameter 'F'
|
||||
Canny(src, dst, 8, 24, 3, false); // cache warm up
|
||||
auto start = chrono::system_clock::now();
|
||||
Canny(gray, dst, 8, 24, 3, false);
|
||||
Canny(src, dst, 8, 24, 3, false);
|
||||
auto duration = chrono::duration_cast<chrono::milliseconds >(chrono::system_clock::now() - start);
|
||||
std::cout << "Time taken by " << "Canny" << ": " << duration.count() << " [" << "ms" << "]" << std::endl;
|
||||
std::cout << "Canny" << ": " << duration.count() << " [" << "ms" << "]" << std::endl;
|
||||
}
|
||||
|
||||
void test_hough(const Mat &src)
|
||||
{
|
||||
ESP_LOGI(TAG, "================= Hough transform tests =================");
|
||||
Mat dst, blurred, edge;
|
||||
|
||||
Mat dst, gray, edge;
|
||||
|
||||
// Grayscale and edge detection
|
||||
cvtColor(src, gray, COLOR_BGR2GRAY);
|
||||
Canny(gray, edge, 8, 24, 3, false);
|
||||
blur(src, blurred, Size(3,3));
|
||||
Canny(blurred, edge, 10, 30, 3, false);
|
||||
|
||||
// hough
|
||||
vector<Vec2f> lines;
|
||||
BENCHMARK_MS("HoughLines", REPEAT, HoughLines, edge, lines, 1, CV_PI/180, 200, 0, 0, 0, CV_PI);
|
||||
cout << lines.size() << " lines were detected" << endl;
|
||||
BENCHMARK_MS("HoughLines", REPEAT, HoughLines, edge, lines, 1, CV_PI/180, 100, 0, 0, 0, CV_PI);
|
||||
|
||||
// probabilistic hough
|
||||
vector<Vec4i> linesP;
|
||||
BENCHMARK_MS("HoughLineP", REPEAT, HoughLinesP, edge, linesP, 1, CV_PI/180, 200, 30, 10);
|
||||
cout << linesP.size() << " lines were detected" << endl;
|
||||
BENCHMARK_MS("HoughLineP", REPEAT, HoughLinesP, edge, linesP, 1, CV_PI/180, 100, 80, 0);
|
||||
}
|
||||
|
||||
|
||||
void app_main(void)
|
||||
{
|
||||
ESP_LOGI(TAG, "Starting main");
|
||||
@ -197,46 +127,59 @@ void app_main(void)
|
||||
/* SPIFFS init */
|
||||
init_spiffs();
|
||||
|
||||
/* Basics Matrices tests */
|
||||
test_basics();
|
||||
|
||||
/* Test the spiffs read/write */
|
||||
test_spiffs();
|
||||
disp_mem_infos();
|
||||
/* Read the images for the tests */
|
||||
Mat src = read_image_specific_res("/spiffs/"+images_res[0]+".png");
|
||||
Mat src2 = read_image_specific_res("/spiffs/"+images_res[1]+".png");
|
||||
Mat src3 = read_image_specific_res("/spiffs/"+images_res[2]+".png");
|
||||
vector<Mat> matrices;
|
||||
matrices.push_back(src);
|
||||
matrices.push_back(src2);
|
||||
matrices.push_back(src3);
|
||||
|
||||
/* Read an image for the next tests */
|
||||
Mat src = imread(imageFileName, IMREAD_COLOR);
|
||||
if(src.empty()) {
|
||||
ESP_LOGW(TAG, "cannot read the image: %s", imageFileName.c_str());
|
||||
return;
|
||||
}
|
||||
ESP_LOGI(TAG, "Image read of %dx%dx%d, with %d pixel depth", src.rows, src.cols, src.channels(), src.depth());
|
||||
// TODO: rename measure.hpp in benchmark.hpp and add printHeader method
|
||||
// TODO: make an array of function and call them from a single loop
|
||||
|
||||
/* Conversions and thresholds tests */
|
||||
test_thresholds(src);
|
||||
disp_mem_infos();
|
||||
printf("============================== Thresholding tests ==============================\n");
|
||||
for(int i = 0; i < NB_IMAGES; i++) {
|
||||
printf("Image %s -------------------------\n", images_res[i].c_str());
|
||||
test_thresholds(matrices[i]);
|
||||
}
|
||||
|
||||
/* Blurring tests */
|
||||
test_blurring(src);
|
||||
disp_mem_infos();
|
||||
printf("================================ Blurring tests ================================\n");
|
||||
for(int i = 0; i < NB_IMAGES; i++) {
|
||||
printf("Image %s -------------------------\n", images_res[i].c_str());
|
||||
test_blurring(matrices[i]);
|
||||
}
|
||||
|
||||
/* Morphology transformations */
|
||||
test_morphology_transform(src);
|
||||
disp_mem_infos();
|
||||
printf("======================= Morphology transformations tests =======================\n");
|
||||
for(int i = 0; i < NB_IMAGES; i++) {
|
||||
printf("Image %s -------------------------\n", images_res[i].c_str());
|
||||
test_morphology_transform(matrices[i]);
|
||||
}
|
||||
|
||||
/* Edge detection */
|
||||
test_edge_detect(src);
|
||||
disp_mem_infos();
|
||||
printf("================================ Resizing tests ================================\n");
|
||||
for(int i = 0; i < NB_IMAGES; i++) {
|
||||
printf("Image %s -------------------------\n", images_res[i].c_str());
|
||||
test_resize(matrices[i]);
|
||||
}
|
||||
|
||||
/* Image resizing */
|
||||
test_resize(src);
|
||||
disp_mem_infos();
|
||||
|
||||
wait_msec(500);
|
||||
printf("============================= Edge detection tests =============================\n");
|
||||
for(int i = 0; i < NB_IMAGES; i++) {
|
||||
printf("Image %s -------------------------\n", images_res[i].c_str());
|
||||
test_edge_detect(matrices[i]);
|
||||
}
|
||||
|
||||
/* Hough transform */
|
||||
test_hough(src);
|
||||
disp_mem_infos();
|
||||
printf("============================= Hough transform tests ============================\n");
|
||||
for(int i = 0; i < NB_IMAGES; i++) {
|
||||
printf("Image %s -------------------------\n", images_res[i].c_str());
|
||||
test_hough(matrices[i]);
|
||||
}
|
||||
|
||||
ESP_LOGI(TAG, "End of main");
|
||||
}
|
Reference in New Issue
Block a user