1 | initial version |
"video frame must be in color", thank you LBerger. This is what did not work.
But now is the second question. How can I edit this video in such a way that it detects the occurrence of a given color and permanently changes the color of this pixel, eg black?
I detect a particular color, but with the new frame, color detection is anew in a new situation. I would like the previously detected pixel to be still in the given color.
int main(int argc, char* argv[]) {
bool recording = false;
bool startNewRecording = false;
int inc = 0;
bool firstRun = true;
VideoCapture cap("F:/WAŻNE/Praca magisterska/Projekt pokrycie powierzchni/pokrycie powierzchni2/Data/44.mp4");
VideoWriter oVideoWriter;//create videoWriter object, not initialized yet
if (!cap.isOpened()) // if not success, exit program
{
cout << "ERROR: Cannot open the video file" << endl;
return -1;
}
namedWindow("MyVideo", CV_WINDOW_AUTOSIZE); //create a window called "MyVideo"
double dWidth = cap.get(CV_CAP_PROP_FRAME_WIDTH); //get the width of frames of the video
double dHeight = cap.get(CV_CAP_PROP_FRAME_HEIGHT); //get the height of frames of the video
cout << "Frame Size = " << dWidth << "x" << dHeight << endl;
//set framesize for use with videoWriter
Size frameSize(static_cast<int>(dWidth), static_cast<int>(dHeight));
for (;;) {
Mat frame;
bool bSuccess = cap.read(frame); // read a new frame from video
cap >> frame; // get a new frame from camera
//Analiza obrazu, i wpisanie elementow obrazu do macierzy pozycji
for (int i = 0; i < frame.rows; i++) {
// Podział obrazu na czesci, jesli w wyznaczonej czesci znaleziono ktorys z kolorow robotow to przyporzadkowanie kolejno warto 2,3,4 w macierzy pozycji
for (int j = 0; j < frame.cols; j++) {
if (frame.at<Vec3b>(i, j)[0] >= 192 && frame.at<Vec3b>(i, j)[1] <= 130 && frame.at<Vec3b>(i, j)[2] <= 100) {
frame.at<Vec3b>(i, j)[0] = 255;
frame.at<Vec3b>(i, j)[1] = 255;
frame.at<Vec3b>(i, j)[2] = 255;
break;
}
if (frame.at<Vec3b>(i, j)[1] >= 150 && frame.at<Vec3b>(i, j)[0] <= 130 && frame.at<Vec3b>(i, j)[2] <= 100) {
frame.at<Vec3b>(i, j)[0] = 255;
frame.at<Vec3b>(i, j)[1] = 255;
frame.at<Vec3b>(i, j)[2] = 255;
break;
}
if (frame.at<Vec3b>(i, j)[2] >= 160 && frame.at<Vec3b>(i, j)[0] <= 130 && frame.at<Vec3b>(i, j)[1] <= 80) {
frame.at<Vec3b>(i, j)[0] = 255;
frame.at<Vec3b>(i, j)[1] = 255;
frame.at<Vec3b>(i, j)[2] = 255;
break;
}
}
}
if (!bSuccess) //if not success, break loop
{
cout << "ERROR: Cannot read a frame from video file" << endl;
break;
}
if (startNewRecording == true) {
oVideoWriter = VideoWriter("F:/WAŻNE/Praca magisterska/Projekt pokrycie powierzchni/pokrycie powierzchni2/Data/output.avi" + intToString(inc) + ".avi", CV_FOURCC('D', 'I', 'V', '3'), 20, frameSize, true); //initialize the VideoWriter object
recording = true;
startNewRecording = false;
cout << "New video file created F:/MyVideo" + intToString(inc) + ".avi " << endl;
if (!oVideoWriter.isOpened()) //if not initialize the VideoWriter successfully, exit the program
{
cout << "ERROR: Failed to initialize video writing" << endl;
getchar();
return -1;
}
}
//if we're in recording mode, write to file
if (recording) {
oVideoWriter.write(frame);
//show "REC" in top left corner in red
//be sure to do this AFTER you write to the file so that "REC" doesn't show up
//on the recorded video.
putText(frame, "REC", Point(0, 60), 2, 2, Scalar(0, 0, 255), 2);
}
imshow("MyVideo", frame); //show the frame in "MyVideo" window
// imshow("edges", edges);
switch (waitKey(10)) {
case 114:
//'r' has been pressed.
//toggle recording mode
recording = !recording;
if (firstRun == true) {
cout << "New Recording Started" << endl;
oVideoWriter = VideoWriter("F:/WAŻNE/Praca magisterska/Projekt pokrycie powierzchni/pokrycie powierzchni2/Data/output.avi", CV_FOURCC('D', 'I', 'V', '3'), 20, frameSize, true);
if (!oVideoWriter.isOpened())
{
cout << "ERROR: Failed to initialize video writing" << endl;
getchar();
return -1;
}
firstRun = false;
}
else {
if (!recording)cout << "Recording Stopped" << endl;
else cout << "Recording Started" << endl;
}
break;
case 110:
//'n' has been pressed
//start new video file
startNewRecording = true;
cout << "New Recording Started" << endl;
//increment video file name
inc += 1;
break;
case 27:
//'esc' has been pressed
//exit program.
cout << "Exit Program" << endl;
return 0;
}
}
return 0;
}