Add OpenCV test10

This commit is contained in:
daleclack 2022-07-28 13:53:04 +08:00
parent 95f6b2a80b
commit a2c601815f
3 changed files with 90 additions and 0 deletions

View File

@ -0,0 +1,3 @@
{
"C_Cpp.default.configurationProvider": "ms-vscode.cmake-tools"
}

View File

@ -0,0 +1,18 @@
cmake_minimum_required(VERSION 3.0.0)
project(OpenCV_test10 VERSION 1.0.0)
#Find the opencv package
find_package(OpenCV REQUIRED)
#include directories for opencv
include_directories(${OpenCV_INCLUDE_DIRS})
add_executable(${PROJECT_NAME} main.cpp)
#Add command to generate .gitignore
add_custom_command(TARGET ${PROJECT_NAME}
COMMAND echo \"*\" > ${CMAKE_BINARY_DIR}/.gitignore
COMMAND echo \"**/*\" > ${CMAKE_BINARY_DIR}/.hgignore)
#Add OpenCV Libraries
target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBS})

View File

@ -0,0 +1,69 @@
#include <opencv2/opencv.hpp>
#include <iostream>
#include <fstream>
using namespace cv;
int main(int argc, char **argv)
{
std::string filename;
Mat image, gray_image, thre_image, canny_image;
int brightless = 0;
// Get Filename
std::cout << "Input Filename:";
std::cin >> filename;
// Load the image
if (filename != "")
{
image = imread(filename, IMREAD_COLOR);
if (image.empty())
{
std::cout << "Failed to open the image!" << std::endl;
return -1;
}
}
// Convert the image to HSV color and filter the background color
cvtColor(image, gray_image, COLOR_BGR2GRAY);
namedWindow("Original Image", WINDOW_AUTOSIZE);
imshow("Original Image", image);
namedWindow("Gray Image", WINDOW_AUTOSIZE);
imshow("Gray Image", gray_image);
// Use cv::thershold to convert
threshold(gray_image, thre_image, 0, 255, THRESH_BINARY | THRESH_OTSU);
namedWindow("OTSU Threshold", WINDOW_AUTOSIZE);
imwrite("/dev/shm/thre_test.png", thre_image);
imshow("OTSU Threshold", thre_image);
// Canny Process
Canny(thre_image, canny_image, 80, 150);
namedWindow("Canny Image", WINDOW_AUTOSIZE);
imshow("Canny Image", canny_image);
std::fstream outfile;
outfile.open("test.txt", std::ios_base::out);
if (outfile.is_open())
{
outfile << canny_image;
}
outfile.close();
while (1)
{
int c = waitKey(1);
if (c != -1)
{
std::cout << c << std::endl;
}
if (c == 27)
{
break;
}
}
destroyAllWindows();
return 0;
}