Skip to content

Commit

Permalink
Merge branch 'OS-4-include-opencv-in-cmake' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
BrennoCaldato committed Jun 27, 2020
2 parents 517f289 + 4364e18 commit 622c6a8
Show file tree
Hide file tree
Showing 7 changed files with 280 additions and 4 deletions.
20 changes: 20 additions & 0 deletions include/CVTracker.h
@@ -0,0 +1,20 @@

#include <opencv2/opencv.hpp>
#include <opencv2/tracking.hpp>
#include <opencv2/core.hpp>

using namespace cv;


class CVTracker {
public:
std::string trackerType;
Ptr<Tracker> tracker;
Rect2d bbox;

CVTracker();
Ptr<Tracker> select_tracker(std::string trackerType);
bool initTracker(Rect2d bbox, Mat &frame);
bool trackFrame(Mat &frame);

};
18 changes: 16 additions & 2 deletions include/Frame.h
Expand Up @@ -26,11 +26,18 @@
*
* You should have received a copy of the GNU Lesser General Public License
* along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
*/
*/

#ifndef OPENSHOT_FRAME_H
#define OPENSHOT_FRAME_H


#define int64 opencv_broken_int
#define uint64 opencv_broken_uint
#include <opencv2/imgproc/imgproc.hpp>
#undef uint64
#undef int64

#include <iomanip>
#include <sstream>
#include <queue>
Expand Down Expand Up @@ -107,6 +114,7 @@ namespace openshot
{
private:
std::shared_ptr<QImage> image;
cv::Mat imagecv;
std::shared_ptr<QImage> wave_image;
std::shared_ptr<juce::AudioSampleBuffer> audio;
std::shared_ptr<QApplication> previewApp;
Expand Down Expand Up @@ -157,7 +165,7 @@ namespace openshot

/// Add (or replace) pixel data to the frame
void AddImage(int new_width, int new_height, int bytes_per_pixel, QImage::Format type, const unsigned char *pixels_);

/// Add (or replace) pixel data to the frame
void AddImage(std::shared_ptr<QImage> new_image);

Expand Down Expand Up @@ -226,6 +234,9 @@ namespace openshot
/// Get pointer to Qt QImage image object
std::shared_ptr<QImage> GetImage();

/// Get pointer to OpenCV Mat image object
cv::Mat GetImageCV();

#ifdef USE_IMAGEMAGICK
/// Get pointer to ImageMagick image object
std::shared_ptr<Magick::Image> GetMagickImage();
Expand Down Expand Up @@ -286,6 +297,9 @@ namespace openshot

/// Play audio samples for this frame
void Play();

/// Convert Qimage to Mat
cv::Mat Qimage2mat( std::shared_ptr<QImage>& qimage);
};

}
Expand Down
20 changes: 18 additions & 2 deletions src/CMakeLists.txt
Expand Up @@ -77,6 +77,11 @@ if (ImageMagick_FOUND)

endif()

################ OPENCV ##################

find_package( OpenCV 4 REQUIRED )


################# LIBOPENSHOT-AUDIO ###################
# Find JUCE-based openshot Audio libraries
find_package(OpenShotAudio 0.2.0 REQUIRED)
Expand Down Expand Up @@ -163,7 +168,8 @@ set(OPENSHOT_SOURCES
QtPlayer.cpp
QtTextReader.cpp
Settings.cpp
Timeline.cpp)
Timeline.cpp
CVTracker.cpp)

# Video effects
set(EFFECTS_SOURCES
Expand Down Expand Up @@ -365,7 +371,8 @@ endif()
# Link remaining dependency libraries
target_link_libraries(openshot PUBLIC
${LIBOPENSHOT_AUDIO_LIBRARIES}
${PROFILER})
${PROFILER}
${OpenCV_LIBS})

if(ImageMagick_FOUND)
target_link_libraries(openshot PUBLIC ${ImageMagick_LIBRARIES})
Expand All @@ -384,6 +391,10 @@ endif()
############### CLI EXECUTABLES ################
# Create test executable
add_executable(openshot-example examples/Example.cpp)
# Create test Opencv executable
add_executable( openshotCV-example examples/Example_opencv.cpp )



# Define path to test input files
SET(TEST_MEDIA_PATH "${PROJECT_SOURCE_DIR}/src/examples/")
Expand All @@ -393,9 +404,14 @@ endif()
target_compile_definitions(openshot-example PRIVATE
-DTEST_MEDIA_PATH="${TEST_MEDIA_PATH}" )

target_compile_definitions(openshotCV-example PRIVATE
-DTEST_MEDIA_PATH="${TEST_MEDIA_PATH}" )

# Link test executable to the new library
target_link_libraries(openshot-example openshot)

target_link_libraries( openshotCV-example ${OpenCV_LIBS} openshot)

add_executable(openshot-html-test examples/ExampleHtml.cpp)
target_link_libraries(openshot-html-test openshot Qt5::Gui)

Expand Down
81 changes: 81 additions & 0 deletions src/CVTracker.cpp
@@ -0,0 +1,81 @@
#include "../include/CVTracker.h"

using namespace cv;


CVTracker::CVTracker(){
// List of tracker types in OpenCV 3.4.1
std::string trackerTypes[8] = {"BOOSTING", "MIL", "KCF", "TLD","MEDIANFLOW", "GOTURN", "MOSSE", "CSRT"};
// vector <string> trackerTypes(types, std::end(types));

// Create a tracker
trackerType = trackerTypes[2];

tracker = select_tracker(trackerType);

}

Ptr<Tracker> CVTracker::select_tracker(std::string trackerType){
Ptr<Tracker> t;
#if (CV_MINOR_VERSION < 3)
{
t = Tracker::create(trackerType);
}
#else
{
if (trackerType == "BOOSTING")
t = TrackerBoosting::create();
if (trackerType == "MIL")
t = TrackerMIL::create();
if (trackerType == "KCF")
t = TrackerKCF::create();
if (trackerType == "TLD")
t = TrackerTLD::create();
if (trackerType == "MEDIANFLOW")
t = TrackerMedianFlow::create();
if (trackerType == "GOTURN")
t = TrackerGOTURN::create();
if (trackerType == "MOSSE")
t = TrackerMOSSE::create();
if (trackerType == "CSRT")
t = TrackerCSRT::create();
}
#endif

return t;
}


bool CVTracker::initTracker(Rect2d initial_bbox, Mat &frame){
// Rect2d bbox(287, 23, 86, 320);
bbox = initial_bbox;

// Uncomment the line below to select a different bounding box
// bbox = selectROI(frame, false);
// Display bounding box.
rectangle(frame, bbox, Scalar( 255, 0, 0 ), 2, 1 );

tracker = select_tracker(trackerType);

tracker->init(frame, bbox);

return true;
}

bool CVTracker::trackFrame(Mat &frame){
// Update the tracking result
bool ok = tracker->update(frame, bbox);

if (ok)
{
// Tracking success : Draw the tracked object
rectangle(frame, bbox, Scalar( 255, 0, 0 ), 2, 1 );
}
else
{
// Tracking failure detected.
putText(frame, "Tracking failure detected", Point(100,80), FONT_HERSHEY_SIMPLEX, 0.75, Scalar(0,0,255),2);
}

return ok;
}
25 changes: 25 additions & 0 deletions src/Frame.cpp
Expand Up @@ -925,6 +925,31 @@ std::shared_ptr<QImage> Frame::GetImage()
return image;
}

// Convert Qimage to Mat
cv::Mat Frame::Qimage2mat( std::shared_ptr<QImage>& qimage) {

cv::Mat mat = cv::Mat(qimage->height(), qimage->width(), CV_8UC4, (uchar*)qimage->bits(), qimage->bytesPerLine());
cv::Mat mat2 = cv::Mat(mat.rows, mat.cols, CV_8UC3 );
int from_to[] = { 0,0, 1,1, 2,2 };
cv::mixChannels( &mat, 1, &mat2, 1, from_to, 3 );
return mat2;
};

// Get pointer to OpenCV image object
cv::Mat Frame::GetImageCV()
{
// Check for blank image
if (!image)
// Fill with black
AddColor(width, height, color);

if (imagecv.empty())
// Convert Qimage to Mat
imagecv = Qimage2mat(image);

return imagecv;
}

#ifdef USE_IMAGEMAGICK
// Get pointer to ImageMagick image object
std::shared_ptr<Magick::Image> Frame::GetMagickImage()
Expand Down
Binary file added src/examples/Boneyard Memories.mp4
Binary file not shown.
120 changes: 120 additions & 0 deletions src/examples/Example_opencv.cpp
@@ -0,0 +1,120 @@
/**
* @file
* @brief Source file for Example Executable (example app for libopenshot)
* @author Jonathan Thomas <jonathan@openshot.org>
*
* @ref License
*/

/* LICENSE
*
* Copyright (c) 2008-2019 OpenShot Studios, LLC
* <http://www.openshotstudios.com/>. This file is part of
* OpenShot Library (libopenshot), an open-source project dedicated to
* delivering high quality video editing and animation solutions to the
* world. For more information visit <http://www.openshot.org/>.
*
* OpenShot Library (libopenshot) is free software: you can redistribute it
* and/or modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* OpenShot Library (libopenshot) is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
*/

#include <fstream>
#include <iostream>
#include <memory>
#include <opencv2/opencv.hpp>
#include "../../include/CVTracker.h"


#include "../../include/OpenShot.h"
#include "../../include/CrashHandler.h"

using namespace openshot;
using namespace cv;


int main(int argc, char* argv[]) {

openshot::Settings *s = openshot::Settings::Instance();
s->HARDWARE_DECODER = 2; // 1 VA-API, 2 NVDEC, 6 VDPAU
s->HW_DE_DEVICE_SET = 0;

std::string input_filepath = TEST_MEDIA_PATH;
input_filepath += "Boneyard Memories.mp4";

openshot::FFmpegReader r9(input_filepath);
r9.Open();
r9.DisplayInfo();

/* WRITER ---------------- */
openshot::FFmpegWriter w9("metadata.mp4");

// Set options
w9.SetAudioOptions(true, "libmp3lame", r9.info.sample_rate, r9.info.channels, r9.info.channel_layout, 128000);
w9.SetVideoOptions(true, "libx264", r9.info.fps, 1024, 576, openshot::Fraction(1,1), false, false, 3000000);

w9.info.metadata["title"] = "testtest";
w9.info.metadata["artist"] = "aaa";
w9.info.metadata["album"] = "bbb";
w9.info.metadata["year"] = "2015";
w9.info.metadata["description"] = "ddd";
w9.info.metadata["comment"] = "eee";
w9.info.metadata["comment"] = "comment";
w9.info.metadata["copyright"] = "copyright OpenShot!";

// Open writer
w9.Open();
// opencv display window
cv::namedWindow("Display Image", cv::WINDOW_NORMAL );

CVTracker kcfTracker;
bool trackerInit = false;


for (long int frame = 1100; frame <= 1500; frame++)
{
//int frame_number = (rand() % 750) + 1;
int frame_number = frame;
std::shared_ptr<openshot::Frame> f = r9.GetFrame(frame_number);

// convert to opencv image
cv::Mat cvimage = f->GetImageCV();
cvtColor(cvimage, cvimage, CV_RGB2BGR);


if(!trackerInit){
Rect2d bbox = selectROI("Display Image", cvimage);

kcfTracker.initTracker(bbox, cvimage);
trackerInit = true;
}
else{
trackerInit = kcfTracker.trackFrame(cvimage);
}

cv::imshow("Display Image", cvimage);
cv::waitKey(30);


w9.WriteFrame(f);
}

// Close writer & reader
w9.Close();

// Close timeline
r9.Close();

std::cout << "Completed successfully!" << std::endl;

return 0;
}

0 comments on commit 622c6a8

Please sign in to comment.