Mọi người cho em hỏi là em đang dùng openCv 3.4.1 muốn khai báo một class trong file .hpp.Thì nó báo lỗi như vậy line 1419 file mat.inl.hpp
File .hpp
#pragma once
#include "Header_1.h"
class GUI_1
{
private:
//Create a black image
Mat imgBlank = Mat::zeros(original.size(), CV_8UC3);
//Take size original to 2 part
int height = original.size().height;
int width = original.size().width;
public:
//Sources img (input)
Mat original;
//extension coordinate variable for tracking object
int posX;
int posY;
//last line of input
//color for cross hair and coordinate
double red = 0;
double green = 255;
double blue = 255;
//Create imgGUI for show (output)
Mat imgGUI= imgBlank + crosshair() + imgCoordinate() ;
//function
Mat crosshair()
{
// Crate blank img
Mat img = imgBlank;
//properties
int length = 10;
//draw crosshair
//horizon
line(img, Point(posX - length, posY), Point(posX + length, posY), Scalar(blue, green, red), 1);
//vertical
line(img, Point(posX, posY - length), Point(posX, posY + length), Scalar(blue, green, red), 1);
return img;
}
Mat imgCoordinate()
{
//blank img
Mat img = imgBlank;
//coordinate of text with object
const int subX = 2;
const int subY =2;
//put coordinate
string Text = "X=" + to_string(posX) + "Y=" + to_string(posY);
putText(img, Text, Point(posX + subX, posY + subY), FONT_HERSHEY_COMPLEX, 1, Scalar(blue, green, red), 1);
return img;
}
};
Sau đó em nạp class này vào file tracking.cpp.Em thêm vào các member class để nó lấy dữ liệu ở dòng 57,115 105,106.
Cụ thể em nạp khai báo class GUI_1 GUI;
GUI.original = imgOriginal //la một Mat
GUI.posX = posX;
imgOriginal = imgOriginal + imgLines + GUI.imgGUI;
//sau do show imgOriginal
Code file tracking
#include "Header_1.h"
#include "GUI add on 1.hpp"
int main()
{
VideoCapture cap(1); //capture the video from webcam
if (!cap.isOpened()) // if not success, exit program
{
cout << "Cannot open the web cam" << endl;
return -1;
}
//create control window
namedWindow("Control", CV_WINDOW_AUTOSIZE); //create a window called "Control"
int iLowH = 170;
int iHighH = 179;
int iLowS = 150;
int iHighS = 255;
int iLowV = 60;
int iHighV = 255;
//Create trackbars in "Control" window
createTrackbar("LowH", "Control", &iLowH, 179); //Hue (0 - 179)
createTrackbar("HighH", "Control", &iHighH, 179);
createTrackbar("LowS", "Control", &iLowS, 255); //Saturation (0 - 255)
createTrackbar("HighS", "Control", &iHighS, 255);
createTrackbar("LowV", "Control", &iLowV, 255);//Value (0 - 255)
createTrackbar("HighV", "Control", &iHighV, 255);
int iLastX = -1;
int iLastY = -1;
//Capture a temporary image from the camera
Mat imgTmp;
cap.read(imgTmp);
//Create a black image
Mat imgBlank = Mat::zeros(imgTmp.size(), CV_8UC3);
//intialize lineimg
Mat imgLines = imgBlank;
while (true)
{
//read original sources
Mat imgOriginal;
bool bSuccess = cap.read(imgOriginal); // read a new frame from video
//intialize GUI
GUI_1 GUI;
GUI.original = imgOriginal;
if (!bSuccess) //if not success, break loop
{
cout << "Cannot read a frame from video stream" << endl;
break;
}
Mat imgHSV;
cvtColor(imgOriginal, imgHSV, COLOR_BGR2HSV); //Convert the captured frame from BGR to HSV
Mat imgThresholded;
inRange(imgHSV, Scalar(iLowH, iLowS, iLowV), Scalar(iHighH, iHighS, iHighV), imgThresholded); //Threshold the image
//morphological opening (removes small objects from the foreground)
erode(imgThresholded, imgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)));
dilate(imgThresholded, imgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)));
//morphological closing (removes small holes from the foreground)
dilate(imgThresholded, imgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)));
erode(imgThresholded, imgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)));
//Calculate the moments of the thresholded image
Moments oMoments = moments(imgThresholded);
double dM01 = oMoments.m01;
double dM10 = oMoments.m10;
double dArea = oMoments.m00;
// if the area <= 10000, I consider that the there are no object in the image and it's because of the noise, the area is not zero
if (dArea > 10000)
{
//calculate the position of the ball
int posX = dM10 / dArea;
int posY = dM01 / dArea;
if (iLastX >= 0 && iLastY >= 0 && posX >= 0 && posY >= 0)
{
//Draw a red line from the previous point to the current point
line(imgLines, Point(posX, posY), Point(iLastX, iLastY), Scalar(0, 0, 255), 2);
//add object loaction for GUI
GUI.posX = posX;
GUI.posY = posY;
}
iLastX = posX;
iLastY = posY;
}
imshow("Thresholded Image", imgThresholded); //show the thresholded image
imgOriginal = imgOriginal + imgLines + GUI.imgGUI;
imshow("Original", imgOriginal); //show the original image
if (waitKey(30) == 27) //wait for 'esc' key press for 30ms. If 'esc' key is pressed, break loop
{
cout << "esc key is pressed by user" << endl;
break;
}
}
return 0;
}