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)