findFile() Cannot find sample image

i am trying to learn to detect colors with opencv in c++ but the img i wanna read does not work.

can someone help

the cpp file and the Cmakelist is below

#include <iostream>
#include "opencv2/objdetect.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/videoio.hpp"
#include <opencv2/imgcodecs.hpp>
#include <string>
#include <vector>
#include <opencv2/core.hpp>
using namespace cv;
using namespace std;
char s = 's';
int min_blue = (110,50,50);
int  max_blue=  (130,255,255);
    
int   min_red = (0,150,127);
int  max_red = (178,255,255);
   Mat img;
int main(){

    String path =  samples::findFile("baboon.jpg"); // img to read
   
  img   = imread(path,IMREAD_COLOR); // reading img
      if(img.empty())
    {
        cout << "Could not read the image: " << img << endl;
        return 1;
    }
    Mat background;

Mat mask, imghsv;
cvtColor(img,imghsv,COLOR_BGR2HSV);
inRange(img,min_red,max_red,mask);
vector < vector < Point>> contours;
findContours(mask,contours,RETR_EXTERNAL,CHAIN_APPROX_SIMPLE);
 // Draw contours and labels
        for (const auto& red_contour : contours) {
            if (contourArea(red_contour) > 300) {
            Rect boundingBox_red = boundingRect(red_contour);
            rectangle(img, boundingBox_red, Scalar(0, 0, 255), 2);
            putText(img, "Red", boundingBox_red.tl(), cv::FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2);
        cout << "Red_contours values " << red_contour << endl;
            }
        }

imshow("mask",mask);
waitKey(0);
destroyAllWindows();
}

then here is the cmakelist:

cmake_minimum_required(VERSION 3.28.1) # is the current version were it is made.
project(color_detect)

find_package(OpenCV REQUIRED) # looks for the opencv package
include_directories( ${OpenCV_INCLUDE_DIRECTORIES}) # include opencv directories

add_executable(red src/red1.cpp) # the cpp file

target_link_libraries(red ${OpenCV_LIBS} ) 

can anyone help because i get the error:

/home/d22/Documents/cv_projects/opencv_colordetectionv2/build/red
[ WARN:0@0.000] global ./modules/core/src/utils/samples.cpp (61) findFile cv::samples::findFile(‘baboon.jpg’) => ‘’
terminate called after throwing an instance of ‘cv::Exception’
what(): OpenCV(4.6.0) ./modules/core/src/utils/samples.cpp:64: error: (-2:Unspecified error) OpenCV samples: Can’t find required data file: baboon.jpg in function ‘findFile’

Aborted (core dumped)

baboon.jpg should originally be in samples/data of the source directory. opencv/samples/data/baboon.jpg at 4.x · opencv/opencv · GitHub

docs for findFile describe how the function looks for files: OpenCV: Utility functions for OpenCV samples

if you have the file, then you have a few options for making it findable.

personally, I mostly use opencv-python, which doesn’t come with these sample files, but then I set OPENCV_SAMPLES_DATA_PATH to point to the samples/data directory of either a clone of the repo or the extracted files of the binary release (downloaded from opencv.org or github).

1 Like

thanks i get it now now i gonna have find out how to detect colors but thank you