Skip to content

Commit

Permalink
integrated Qimage to Mat conversion into Frame
Browse files Browse the repository at this point in the history
  • Loading branch information
BrennoCaldato committed Jun 27, 2020
1 parent ae48651 commit 4364e18
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 26 deletions.
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
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
25 changes: 1 addition & 24 deletions src/examples/Example_opencv.cpp
Expand Up @@ -42,16 +42,6 @@ using namespace openshot;
using namespace cv;


cv::Mat 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.clone();
};


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

openshot::Settings *s = openshot::Settings::Instance();
Expand Down Expand Up @@ -95,36 +85,23 @@ int main(int argc, char* argv[]) {
//int frame_number = (rand() % 750) + 1;
int frame_number = frame;
std::shared_ptr<openshot::Frame> f = r9.GetFrame(frame_number);

std::shared_ptr<QImage> qimage = f->GetImage();
// qimage->convertToFormat(QImage::Format_RGB888);

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


if(!trackerInit){
// Rect2d bbox(287, 23, 86, 320);
Rect2d bbox = selectROI("Display Image", cvimage);

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

}


// opencv code
if ( !cvimage.data )
{
std::cout << "No image data \n";
}

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

cv::waitKey(30);


Expand Down

0 comments on commit 4364e18

Please sign in to comment.