diff options
Diffstat (limited to 'store/works/life/digital-image-process-lab')
6 files changed, 244 insertions, 0 deletions
diff --git a/store/works/life/digital-image-process-lab/.gitignore b/store/works/life/digital-image-process-lab/.gitignore new file mode 100644 index 0000000..d8c2a8f --- /dev/null +++ b/store/works/life/digital-image-process-lab/.gitignore @@ -0,0 +1,2 @@ +.clangd +build
\ No newline at end of file diff --git a/store/works/life/digital-image-process-lab/CMakeLists.txt b/store/works/life/digital-image-process-lab/CMakeLists.txt new file mode 100644 index 0000000..8a00ca5 --- /dev/null +++ b/store/works/life/digital-image-process-lab/CMakeLists.txt @@ -0,0 +1,20 @@ +cmake_minimum_required(VERSION 3.20) + +project(digital-image-process-lab) + +set(CMAKE_CXX_STANDARD 17) + +find_package(OpenCV REQUIRED) +include_directories(${OpenCV_INCLUDE_DIRS}) + +add_executable(HistogramEqulization HistogramEqulization.cpp) +target_link_libraries(HistogramEqulization ${OpenCV_LIBS}) + +add_executable(DiscreteFourierTransform DiscreteFourierTransform.cpp) +target_link_libraries(DiscreteFourierTransform ${OpenCV_LIBS}) + +add_executable(MorphologicalTransformation MorphologicalTransformation.cpp) +target_link_libraries(MorphologicalTransformation ${OpenCV_LIBS}) + +add_executable(HarrisCornerDetector HarrisCornerDetector.cpp) +target_link_libraries(HarrisCornerDetector ${OpenCV_LIBS}) diff --git a/store/works/life/digital-image-process-lab/DiscreteFourierTransform.cpp b/store/works/life/digital-image-process-lab/DiscreteFourierTransform.cpp new file mode 100644 index 0000000..31108bb --- /dev/null +++ b/store/works/life/digital-image-process-lab/DiscreteFourierTransform.cpp @@ -0,0 +1,63 @@ +#include <opencv2/core.hpp> +#include <opencv2/highgui.hpp> +#include <opencv2/imgcodecs.hpp> +#include <opencv2/imgproc.hpp> + +#include <iostream> + +using namespace cv; + +int main(int argc, char **argv) { + if (argc != 2) { + std::cerr << "Please input an image file path as the only arg." + << std::endl; + return -1; + } + + const char *filename = argv[1]; + Mat I = imread(samples::findFile(filename), IMREAD_GRAYSCALE); + if (I.empty()) { + std::cout << "Error opening image" << std::endl; + return EXIT_FAILURE; + } + Mat padded; // expand input image to optimal size + int m = getOptimalDFTSize(I.rows); + int n = getOptimalDFTSize(I.cols); // on the border add zero values + copyMakeBorder(I, padded, 0, m - I.rows, 0, n - I.cols, BORDER_CONSTANT, + Scalar::all(0)); + Mat planes[] = {Mat_<float>(padded), Mat::zeros(padded.size(), CV_32F)}; + Mat complexI; + merge(planes, 2, complexI); // Add to the expanded another plane with zeros + dft(complexI, complexI); // this way the result may fit in the source matrix + // compute the magnitude and switch to logarithmic scale + // => log(1 + sqrt(Re(DFT(I))^2 + Im(DFT(I))^2)) + split(complexI, planes); // planes[0] = Re(DFT(I), planes[1] = Im(DFT(I)) + magnitude(planes[0], planes[1], planes[0]); // planes[0] = magnitude + Mat magI = planes[0]; + magI += Scalar::all(1); // switch to logarithmic scale + log(magI, magI); + // crop the spectrum, if it has an odd number of rows or columns + magI = magI(Rect(0, 0, magI.cols & -2, magI.rows & -2)); + // rearrange the quadrants of Fourier image so that the origin is at the + // image center + int cx = magI.cols / 2; + int cy = magI.rows / 2; + Mat q0(magI, Rect(0, 0, cx, cy)); // Top-Left - Create a ROI per quadrant + Mat q1(magI, Rect(cx, 0, cx, cy)); // Top-Right + Mat q2(magI, Rect(0, cy, cx, cy)); // Bottom-Left + Mat q3(magI, Rect(cx, cy, cx, cy)); // Bottom-Right + Mat tmp; // swap quadrants (Top-Left with Bottom-Right) + q0.copyTo(tmp); + q3.copyTo(q0); + tmp.copyTo(q3); + q1.copyTo(tmp); // swap quadrant (Top-Right with Bottom-Left) + q2.copyTo(q1); + tmp.copyTo(q2); + normalize(magI, magI, 0, 1, + NORM_MINMAX); // Transform the matrix with float values into a + // viewable image form (float between values 0 and 1). + imshow("Input Image", I); // Show the result + imshow("spectrum magnitude", magI); + waitKey(); + return EXIT_SUCCESS; +} diff --git a/store/works/life/digital-image-process-lab/HarrisCornerDetector.cpp b/store/works/life/digital-image-process-lab/HarrisCornerDetector.cpp new file mode 100644 index 0000000..c7013be --- /dev/null +++ b/store/works/life/digital-image-process-lab/HarrisCornerDetector.cpp @@ -0,0 +1,49 @@ +#include <opencv2/highgui.hpp> +#include <opencv2/imgproc.hpp> + +#include <iostream> + +cv::Mat src, src_gray; +int thresh = 100; +int max_thresh = 255; +const char *source_window = "Source image"; +const char *corners_window = "Corners detected"; +void cornerHarris_demo(int, void *); +int main(int argc, char **argv) { + cv::CommandLineParser parser(argc, argv, + "{@input | building.jpg | input image}"); + src = cv::imread(cv::samples::findFile(parser.get<cv::String>("@input"))); + if (src.empty()) { + std::cout << "Could not open or find the image!\n" << std::endl; + std::cout << "Usage: " << argv[0] << " <Input image>" << std::endl; + return -1; + } + cvtColor(src, src_gray, cv::COLOR_BGR2GRAY); + cv::namedWindow(source_window); + cv::createTrackbar("Threshold: ", source_window, &thresh, max_thresh, + cornerHarris_demo); + cv::imshow(source_window, src); + cornerHarris_demo(0, 0); + cv::waitKey(); + return 0; +} + +void cornerHarris_demo(int, void *) { + int blockSize = 2; + int apertureSize = 3; + double k = 0.04; + cv::Mat dst = cv::Mat::zeros(src.size(), CV_32FC1); + cv::cornerHarris(src_gray, dst, blockSize, apertureSize, k); + cv::Mat dst_norm, dst_norm_scaled; + normalize(dst, dst_norm, 0, 255, cv::NORM_MINMAX, CV_32FC1, cv::Mat()); + convertScaleAbs(dst_norm, dst_norm_scaled); + for (int i = 0; i < dst_norm.rows; i++) { + for (int j = 0; j < dst_norm.cols; j++) { + if ((int)dst_norm.at<float>(i, j) > thresh) { + circle(dst_norm_scaled, cv::Point(j, i), 5, cv::Scalar(0), 2, 8, 0); + } + } + } + cv::namedWindow(corners_window); + cv::imshow(corners_window, dst_norm_scaled); +} diff --git a/store/works/life/digital-image-process-lab/HistogramEqulization.cpp b/store/works/life/digital-image-process-lab/HistogramEqulization.cpp new file mode 100644 index 0000000..951a966 --- /dev/null +++ b/store/works/life/digital-image-process-lab/HistogramEqulization.cpp @@ -0,0 +1,34 @@ +#include <iostream> +#include <string> + +#include <opencv2/highgui.hpp> +#include <opencv2/imgcodecs.hpp> +#include <opencv2/imgproc.hpp> + +int main(int argc, char **argv) { + if (argc != 2) { + std::cerr << "Please input an image file path as the only arg." + << std::endl; + return -1; + } + + std::string file_name(argv[1]); + + cv::Mat src, dst; + + src = cv::imread(file_name, cv::IMREAD_COLOR); + if (src.empty()) { + std::cerr << "Failed to load image file: " << file_name << std::endl; + return -2; + } + + cv::cvtColor(src, src, cv::COLOR_BGR2GRAY); + + cv::equalizeHist(src, dst); + + cv::imshow("Source Image", src); + cv::imshow("Equalized Image", dst); + cv::waitKey(); + + return 0; +} diff --git a/store/works/life/digital-image-process-lab/MorphologicalTransformation.cpp b/store/works/life/digital-image-process-lab/MorphologicalTransformation.cpp new file mode 100644 index 0000000..9b1b097 --- /dev/null +++ b/store/works/life/digital-image-process-lab/MorphologicalTransformation.cpp @@ -0,0 +1,76 @@ +#include <opencv2/highgui.hpp> +#include <opencv2/imgproc.hpp> + +#include <iostream> + +cv::Mat src, erosion_dst, dilation_dst; + +int erosion_elem = 0; +int erosion_size = 0; +int dilation_elem = 0; +int dilation_size = 0; +int const max_elem = 2; +int const max_kernel_size = 21; + +void Erosion(int, void *); +void Dilation(int, void *); + +int main(int argc, char **argv) { + + cv::CommandLineParser parser(argc, argv, + "{@input | LinuxLogo.jpg | input image}"); + src = imread(cv::samples::findFile(parser.get<cv::String>("@input")), + cv::IMREAD_COLOR); + if (src.empty()) { + std::cout << "Could not open or find the image!\n" << std::endl; + std::cout << "Usage: " << argv[0] << " <Input image>" << std::endl; + return -1; + } + namedWindow("Erosion Demo", cv::WINDOW_AUTOSIZE); + namedWindow("Dilation Demo", cv::WINDOW_AUTOSIZE); + cv::moveWindow("Dilation Demo", src.cols, 0); + cv::createTrackbar("Element:\n 0: Rect \n 1: Cross \n 2: Ellipse", + "Erosion Demo", &erosion_elem, max_elem, Erosion); + cv::createTrackbar("Kernel size:\n 2n +1", "Erosion Demo", &erosion_size, + max_kernel_size, Erosion); + cv::createTrackbar("Element:\n 0: Rect \n 1: Cross \n 2: Ellipse", + "Dilation Demo", &dilation_elem, max_elem, Dilation); + cv::createTrackbar("Kernel size:\n 2n +1", "Dilation Demo", &dilation_size, + max_kernel_size, Dilation); + Erosion(0, 0); + Dilation(0, 0); + cv::waitKey(0); + return 0; +} + +void Erosion(int, void *) { + int erosion_type = 0; + if (erosion_elem == 0) { + erosion_type = cv::MORPH_RECT; + } else if (erosion_elem == 1) { + erosion_type = cv::MORPH_CROSS; + } else if (erosion_elem == 2) { + erosion_type = cv::MORPH_ELLIPSE; + } + cv::Mat element = getStructuringElement( + erosion_type, cv::Size(2 * erosion_size + 1, 2 * erosion_size + 1), + cv::Point(erosion_size, erosion_size)); + cv::erode(src, erosion_dst, element); + imshow("Erosion Demo", erosion_dst); +} + +void Dilation(int, void *) { + int dilation_type = 0; + if (dilation_elem == 0) { + dilation_type = cv::MORPH_RECT; + } else if (dilation_elem == 1) { + dilation_type = cv::MORPH_CROSS; + } else if (dilation_elem == 2) { + dilation_type = cv::MORPH_ELLIPSE; + } + cv::Mat element = getStructuringElement( + dilation_type, cv::Size(2 * dilation_size + 1, 2 * dilation_size + 1), + cv::Point(dilation_size, dilation_size)); + dilate(src, dilation_dst, element); + imshow("Dilation Demo", dilation_dst); +} |