From 9742c732544ca822fe353841c8d3cb38c8c4cd49 Mon Sep 17 00:00:00 2001 From: Laurent Berger <3591626+LaurentBerger@users.noreply.github.com> Date: Thu, 6 Apr 2023 12:02:00 +0200 Subject: [PATCH] Merge pull request #23442 from LaurentBerger:tuto_findFile Fix image loading in tutorials code #23442 Fixes https://github.com/opencv/opencv/issues/23378 ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [X] I agree to contribute to the project under Apache 2 License. - [X] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [X] The PR is proposed to the proper branch - [X] There is a reference to the original bug report and related work - [X] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [X] The feature is well documented and sample code can be built with the project CMake --- .../MatchTemplate_Demo.cpp | 20 ++++++++++--------- .../calcBackProject_Demo1.cpp | 5 +++-- .../Histograms_Matching/compareHist_Demo.cpp | 13 ++++++------ .../anisotropic_image_segmentation.cpp | 17 +++++++++++----- .../out_of_focus_deblur_filter.cpp | 15 +++++++++----- .../periodic_noise_removing_filter.cpp | 19 +++++++++++++++--- .../ImgTrans/generalizedHoughTransform.cpp | 7 ++++--- 7 files changed, 63 insertions(+), 33 deletions(-) diff --git a/samples/cpp/tutorial_code/Histograms_Matching/MatchTemplate_Demo.cpp b/samples/cpp/tutorial_code/Histograms_Matching/MatchTemplate_Demo.cpp index f9abbae945..157e9209dc 100644 --- a/samples/cpp/tutorial_code/Histograms_Matching/MatchTemplate_Demo.cpp +++ b/samples/cpp/tutorial_code/Histograms_Matching/MatchTemplate_Demo.cpp @@ -26,26 +26,28 @@ int max_Trackbar = 5; /// Function Headers void MatchingMethod( int, void* ); +const char* keys = +"{ help h| | Print help message. }" +"{ @input1 | Template_Matching_Original_Image.jpg | image_name }" +"{ @input2 | Template_Matching_Template_Image.jpg | template_name }" +"{ @input3 | | mask_name }"; + /** * @function main */ int main( int argc, char** argv ) { - if (argc < 3) - { - cout << "Not enough parameters" << endl; - cout << "Usage:\n" << argv[0] << " []" << endl; - return -1; - } + CommandLineParser parser( argc, argv, keys ); + samples::addSamplesDataSearchSubDirectory( "doc/tutorials/imgproc/histograms/template_matching/images" ); //! [load_image] /// Load image and template - img = imread( argv[1], IMREAD_COLOR ); - templ = imread( argv[2], IMREAD_COLOR ); + img = imread( samples::findFile( parser.get("@input1") ) ); + templ = imread( samples::findFile( parser.get("@input2") ), IMREAD_COLOR ); if(argc > 3) { use_mask = true; - mask = imread( argv[3], IMREAD_COLOR ); + mask = imread(samples::findFile( parser.get("@input3") ), IMREAD_COLOR ); } if(img.empty() || templ.empty() || (use_mask && mask.empty())) diff --git a/samples/cpp/tutorial_code/Histograms_Matching/calcBackProject_Demo1.cpp b/samples/cpp/tutorial_code/Histograms_Matching/calcBackProject_Demo1.cpp index bcb547a2fb..ed2c067b54 100644 --- a/samples/cpp/tutorial_code/Histograms_Matching/calcBackProject_Demo1.cpp +++ b/samples/cpp/tutorial_code/Histograms_Matching/calcBackProject_Demo1.cpp @@ -26,8 +26,9 @@ void Hist_and_Backproj(int, void* ); int main( int argc, char* argv[] ) { //! [Read the image] - CommandLineParser parser( argc, argv, "{@input | | input image}" ); - Mat src = imread( parser.get( "@input" ) ); + CommandLineParser parser( argc, argv, "{@input |Back_Projection_Theory0.jpg| input image}" ); + samples::addSamplesDataSearchSubDirectory("doc/tutorials/imgproc/histograms/back_projection/images"); + Mat src = imread(samples::findFile(parser.get( "@input" )) ); if( src.empty() ) { cout << "Could not open or find the image!\n" << endl; diff --git a/samples/cpp/tutorial_code/Histograms_Matching/compareHist_Demo.cpp b/samples/cpp/tutorial_code/Histograms_Matching/compareHist_Demo.cpp index aa5fce2091..dcbcf8d568 100644 --- a/samples/cpp/tutorial_code/Histograms_Matching/compareHist_Demo.cpp +++ b/samples/cpp/tutorial_code/Histograms_Matching/compareHist_Demo.cpp @@ -14,9 +14,9 @@ using namespace cv; const char* keys = "{ help h| | Print help message. }" - "{ @input1 | | Path to input image 1. }" - "{ @input2 | | Path to input image 2. }" - "{ @input3 | | Path to input image 3. }"; + "{ @input1 |Histogram_Comparison_Source_0.jpg | Path to input image 1. }" + "{ @input2 |Histogram_Comparison_Source_1.jpg | Path to input image 2. }" + "{ @input3 |Histogram_Comparison_Source_2.jpg | Path to input image 3. }"; /** * @function main @@ -25,9 +25,10 @@ int main( int argc, char** argv ) { //! [Load three images with different environment settings] CommandLineParser parser( argc, argv, keys ); - Mat src_base = imread( parser.get("input1") ); - Mat src_test1 = imread( parser.get("input2") ); - Mat src_test2 = imread( parser.get("input3") ); + samples::addSamplesDataSearchSubDirectory( "doc/tutorials/imgproc/histograms/histogram_comparison/images" ); + Mat src_base = imread(samples::findFile( parser.get( "@input1" ) ) ); + Mat src_test1 = imread(samples::findFile( parser.get( "@input2" ) ) ); + Mat src_test2 = imread(samples::findFile( parser.get( "@input3" ) ) ); if( src_base.empty() || src_test1.empty() || src_test2.empty() ) { cout << "Could not open or find the images!\n" << endl; diff --git a/samples/cpp/tutorial_code/ImgProc/anisotropic_image_segmentation/anisotropic_image_segmentation.cpp b/samples/cpp/tutorial_code/ImgProc/anisotropic_image_segmentation/anisotropic_image_segmentation.cpp index a402e1de4a..382303de5d 100644 --- a/samples/cpp/tutorial_code/ImgProc/anisotropic_image_segmentation/anisotropic_image_segmentation.cpp +++ b/samples/cpp/tutorial_code/ImgProc/anisotropic_image_segmentation/anisotropic_image_segmentation.cpp @@ -1,9 +1,10 @@ -/** +/** * @brief You will learn how to segment an anisotropic image with a single local orientation by a gradient structure tensor (GST) * @author Karpushin Vladislav, karpushin@ngs.ru, https://github.com/VladKarpushin */ #include +#include "opencv2/highgui.hpp" #include "opencv2/imgproc.hpp" #include "opencv2/imgcodecs.hpp" @@ -21,7 +22,8 @@ int main() int LowThr = 35; // threshold1 for orientation, it ranges from 0 to 180 int HighThr = 57; // threshold2 for orientation, it ranges from 0 to 180 - Mat imgIn = imread("input.jpg", IMREAD_GRAYSCALE); + samples::addSamplesDataSearchSubDirectory("doc/tutorials/imgproc/anisotropic_image_segmentation/images"); + Mat imgIn = imread(samples::findFile("gst_input.jpg"), IMREAD_GRAYSCALE); if (imgIn.empty()) //check whether the image is loaded or not { cout << "ERROR : Image cannot be loaded..!!" << endl; @@ -46,13 +48,18 @@ int main() //! [combining] //! [main] - normalize(imgCoherency, imgCoherency, 0, 255, NORM_MINMAX); - normalize(imgOrientation, imgOrientation, 0, 255, NORM_MINMAX); + normalize(imgCoherency, imgCoherency, 0, 255, NORM_MINMAX, CV_8U); + normalize(imgOrientation, imgOrientation, 0, 255, NORM_MINMAX, CV_8U); + imshow("Original", imgIn); + imshow("Result", 0.5 * (imgIn + imgBin)); + imshow("Coherency", imgCoherency); + imshow("Orientation", imgOrientation); imwrite("result.jpg", 0.5*(imgIn + imgBin)); imwrite("Coherency.jpg", imgCoherency); imwrite("Orientation.jpg", imgOrientation); - //! [main_extra] + waitKey(0); + //! [main_extra] return 0; } //! [calcGST] diff --git a/samples/cpp/tutorial_code/ImgProc/out_of_focus_deblur_filter/out_of_focus_deblur_filter.cpp b/samples/cpp/tutorial_code/ImgProc/out_of_focus_deblur_filter/out_of_focus_deblur_filter.cpp index 059df8bd55..30e96ff05e 100644 --- a/samples/cpp/tutorial_code/ImgProc/out_of_focus_deblur_filter/out_of_focus_deblur_filter.cpp +++ b/samples/cpp/tutorial_code/ImgProc/out_of_focus_deblur_filter/out_of_focus_deblur_filter.cpp @@ -1,8 +1,9 @@ -/** +/** * @brief You will learn how to recover an out-of-focus image by Wiener filter * @author Karpushin Vladislav, karpushin@ngs.ru, https://github.com/VladKarpushin */ #include +#include "opencv2/highgui.hpp" #include "opencv2/imgproc.hpp" #include "opencv2/imgcodecs.hpp" @@ -17,9 +18,9 @@ void calcWnrFilter(const Mat& input_h_PSF, Mat& output_G, double nsr); const String keys = "{help h usage ? | | print this message }" -"{image |original.JPG | input image name }" -"{R |53 | radius }" -"{SNR |5200 | signal to noise ratio}" +"{image |original.jpg | input image name }" +"{R |5 | radius }" +"{SNR |100 | signal to noise ratio}" ; int main(int argc, char *argv[]) @@ -35,6 +36,7 @@ int main(int argc, char *argv[]) int R = parser.get("R"); int snr = parser.get("SNR"); string strInFileName = parser.get("image"); + samples::addSamplesDataSearchSubDirectory("doc/tutorials/imgproc/out_of_focus_deblur_filter/images"); if (!parser.check()) { @@ -43,7 +45,7 @@ int main(int argc, char *argv[]) } Mat imgIn; - imgIn = imread(strInFileName, IMREAD_GRAYSCALE); + imgIn = imread(samples::findFile( strInFileName ), IMREAD_GRAYSCALE); if (imgIn.empty()) //check whether the image is loaded or not { cout << "ERROR : Image cannot be loaded..!!" << endl; @@ -69,7 +71,10 @@ int main(int argc, char *argv[]) imgOut.convertTo(imgOut, CV_8U); normalize(imgOut, imgOut, 0, 255, NORM_MINMAX); + imshow("Original", imgIn); + imshow("Debluring", imgOut); imwrite("result.jpg", imgOut); + waitKey(0); return 0; } diff --git a/samples/cpp/tutorial_code/ImgProc/periodic_noise_removing_filter/periodic_noise_removing_filter.cpp b/samples/cpp/tutorial_code/ImgProc/periodic_noise_removing_filter/periodic_noise_removing_filter.cpp index 0f49b698d5..245906abd3 100644 --- a/samples/cpp/tutorial_code/ImgProc/periodic_noise_removing_filter/periodic_noise_removing_filter.cpp +++ b/samples/cpp/tutorial_code/ImgProc/periodic_noise_removing_filter/periodic_noise_removing_filter.cpp @@ -1,8 +1,9 @@ -/** +/** * @brief You will learn how to remove periodic noise in the Fourier domain * @author Karpushin Vladislav, karpushin@ngs.ru, https://github.com/VladKarpushin */ #include +#include "opencv2/highgui.hpp" #include #include @@ -14,15 +15,25 @@ void filter2DFreq(const Mat& inputImg, Mat& outputImg, const Mat& H); void synthesizeFilterH(Mat& inputOutput_H, Point center, int radius); void calcPSD(const Mat& inputImg, Mat& outputImg, int flag = 0); -int main() +const String keys = +"{help h usage ? | | print this message }" +"{@image |period_input.jpg | input image name }" +; + +int main(int argc, char* argv[]) { - Mat imgIn = imread("input.jpg", IMREAD_GRAYSCALE); + CommandLineParser parser(argc, argv, keys); + string strInFileName = parser.get("@image"); + samples::addSamplesDataSearchSubDirectory("doc/tutorials/imgproc/periodic_noise_removing_filter/images"); + + Mat imgIn = imread(samples::findFile(strInFileName), IMREAD_GRAYSCALE); if (imgIn.empty()) //check whether the image is loaded or not { cout << "ERROR : Image cannot be loaded..!!" << endl; return -1; } + imshow("Image corrupted", imgIn); imgIn.convertTo(imgIn, CV_32F); //! [main] @@ -57,7 +68,9 @@ int main() imwrite("PSD.jpg", imgPSD); fftshift(H, H); normalize(H, H, 0, 255, NORM_MINMAX); + imshow("Debluring", imgOut); imwrite("filter.jpg", H); + waitKey(0); return 0; } diff --git a/samples/cpp/tutorial_code/ImgTrans/generalizedHoughTransform.cpp b/samples/cpp/tutorial_code/ImgTrans/generalizedHoughTransform.cpp index dca52b1339..06a7d73515 100644 --- a/samples/cpp/tutorial_code/ImgTrans/generalizedHoughTransform.cpp +++ b/samples/cpp/tutorial_code/ImgTrans/generalizedHoughTransform.cpp @@ -13,8 +13,9 @@ using namespace std; int main() { //! [generalized-hough-transform-load-and-setup] // load source image and grayscale template - Mat image = imread("images/generalized_hough_mini_image.jpg"); - Mat templ = imread("images/generalized_hough_mini_template.jpg", IMREAD_GRAYSCALE); + samples::addSamplesDataSearchSubDirectory("doc/tutorials/imgproc/generalized_hough_ballard_guil"); + Mat image = imread(samples::findFile("images/generalized_hough_mini_image.jpg")); + Mat templ = imread(samples::findFile("images/generalized_hough_mini_template.jpg"), IMREAD_GRAYSCALE); // create grayscale image Mat grayImage; @@ -105,4 +106,4 @@ int main() { //! [generalized-hough-transform-draw-results] return EXIT_SUCCESS; -} \ No newline at end of file +}