Commit 26cabf9b authored by Nicola Murino's avatar Nicola Murino Committed by Nicolas Dufresne

opencv: port to c++

parent f9742466
/*
* GStreamer
* Copyright (C) 2011 Robert Jobbagy <jobbagy.robert@gmail.com>
* Copyright (C) 2011 Nicola Murino <nicola.murino@gmail.com>
* Copyright (C) 2011 - 2018 Nicola Murino <nicola.murino@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
......@@ -49,9 +49,6 @@
#include <errno.h>
#include "MotionCells.h"
#include <opencv2/imgproc.hpp>
#if (CV_MAJOR_VERSION >= 4)
#include <opencv2/imgproc/imgproc_c.h>
#endif
MotionCells::MotionCells ()
{
......@@ -60,11 +57,6 @@ MotionCells::MotionCells ()
m_motioncellsidxcstr = NULL;
m_saveInDatafile = false;
mc_savefile = NULL;
m_pcurFrame = NULL;
m_pprevFrame = NULL;
transparencyimg = NULL;
m_pdifferenceImage = NULL;
m_pbwImage = NULL;
m_initdatafilefailed = new char[BUSMSGLEN];
m_savedatafilefailed = new char[BUSMSGLEN];
m_initerrorcode = 0;
......@@ -101,20 +93,16 @@ MotionCells::~MotionCells ()
delete[]m_savedatafilefailed;
if (m_motioncellsidxcstr)
delete[]m_motioncellsidxcstr;
if (m_pcurFrame)
cvReleaseImage (&m_pcurFrame);
if (m_pprevFrame)
cvReleaseImage (&m_pprevFrame);
if (transparencyimg)
cvReleaseImage (&transparencyimg);
if (m_pdifferenceImage)
cvReleaseImage (&m_pdifferenceImage);
if (m_pbwImage)
cvReleaseImage (&m_pbwImage);
m_pcurFrame.release ();
m_pprevFrame.release ();
transparencyimg.release ();
m_pdifferenceImage.release ();
m_pbwImage.release ();
}
int
MotionCells::performDetectionMotionCells (IplImage * p_frame,
MotionCells::performDetectionMotionCells (cv::Mat p_frame,
double p_sensitivity, double p_framerate, int p_gridx, int p_gridy,
gint64 timestamp_millisec, bool p_isVisible, bool p_useAlpha,
int motionmaskcoord_count, motionmaskcoordrect * motionmaskcoords,
......@@ -126,7 +114,7 @@ MotionCells::performDetectionMotionCells (IplImage * p_frame,
int sumframecnt = 0;
int ret = 0;
CvSize frameSize;
cv::Size frameSize;
p_framerate >= 1 ? p_framerate <= 5 ? sumframecnt = 1
: p_framerate <= 10 ? sumframecnt = 2
......@@ -147,38 +135,36 @@ MotionCells::performDetectionMotionCells (IplImage * p_frame,
return ret;
}
frameSize = cvGetSize (p_frame);
frameSize = p_frame.size ();
frameSize.width /= 2;
frameSize.height /= 2;
setMotionCells (frameSize.width, frameSize.height);
m_sensitivity = 1 - p_sensitivity;
m_isVisible = p_isVisible;
m_pcurFrame = cvCloneImage (p_frame);
IplImage *m_pcurgreyImage = cvCreateImage (frameSize, IPL_DEPTH_8U, 1);
IplImage *m_pprevgreyImage = cvCreateImage (frameSize, IPL_DEPTH_8U, 1);
IplImage *m_pgreyImage = cvCreateImage (frameSize, IPL_DEPTH_8U, 1);
IplImage *m_pcurDown =
cvCreateImage (frameSize, m_pcurFrame->depth, m_pcurFrame->nChannels);
IplImage *m_pprevDown = cvCreateImage (frameSize, m_pprevFrame->depth,
m_pprevFrame->nChannels);
m_pbwImage = cvCreateImage (frameSize, IPL_DEPTH_8U, 1);
cvPyrDown (m_pprevFrame, m_pprevDown);
cvCvtColor (m_pprevDown, m_pprevgreyImage, CV_RGB2GRAY);
cvPyrDown (m_pcurFrame, m_pcurDown);
cvCvtColor (m_pcurDown, m_pcurgreyImage, CV_RGB2GRAY);
m_pdifferenceImage = cvCloneImage (m_pcurgreyImage);
m_pcurFrame = p_frame.clone ();
cv::Mat m_pcurgreyImage = cv::Mat (frameSize, CV_8UC1);
cv::Mat m_pprevgreyImage = cv::Mat (frameSize, CV_8UC1);
cv::Mat m_pgreyImage = cv::Mat (frameSize, CV_8UC1);
cv::Mat m_pcurDown = cv::Mat (frameSize, m_pcurFrame.type ());
cv::Mat m_pprevDown = cv::Mat (frameSize, m_pprevFrame.type ());
m_pbwImage.create (frameSize, CV_8UC1);
pyrDown (m_pprevFrame, m_pprevDown);
cvtColor (m_pprevDown, m_pprevgreyImage, cv::COLOR_RGB2GRAY);
pyrDown (m_pcurFrame, m_pcurDown);
cvtColor (m_pcurDown, m_pcurgreyImage, cv::COLOR_RGB2GRAY);
m_pdifferenceImage = m_pcurgreyImage.clone ();
//cvSmooth(m_pcurgreyImage, m_pcurgreyImage, CV_GAUSSIAN, 3, 0);//TODO camera noise reduce,something smoothing, and rethink runningavg weights
//Minus the current gray frame from the 8U moving average.
cvAbsDiff (m_pprevgreyImage, m_pcurgreyImage, m_pdifferenceImage);
cv::absdiff (m_pprevgreyImage, m_pcurgreyImage, m_pdifferenceImage);
//Convert the image to black and white.
cvAdaptiveThreshold (m_pdifferenceImage, m_pbwImage, 255,
CV_ADAPTIVE_THRESH_GAUSSIAN_C, CV_THRESH_BINARY_INV, 7);
cv::adaptiveThreshold (m_pdifferenceImage, m_pbwImage, 255,
cv::ADAPTIVE_THRESH_GAUSSIAN_C, cv::THRESH_BINARY_INV, 7, 5);
// Dilate and erode to get object blobs
cvDilate (m_pbwImage, m_pbwImage, NULL, 2);
cvErode (m_pbwImage, m_pbwImage, NULL, 2);
cv::dilate (m_pbwImage, m_pbwImage, cv::Mat (), cv::Point (-1, -1), 2);
cv::erode (m_pbwImage, m_pbwImage, cv::Mat (), cv::Point (-1, -1), 2);
//mask-out the overlay on difference image
if (motionmaskcoord_count > 0)
......@@ -188,15 +174,12 @@ MotionCells::performDetectionMotionCells (IplImage * p_frame,
if (getIsNonZero (m_pbwImage)) { //detect Motion
if (m_MotionCells.size () > 0) //it contains previous motioncells what we used when frames dropped
m_MotionCells.clear ();
if (transparencyimg)
cvReleaseImage (&transparencyimg);
(motioncells_count > 0) ?
calculateMotionPercentInMotionCells (motioncellsidx,
motioncells_count)
: calculateMotionPercentInMotionCells (motionmaskcellsidx, 0);
transparencyimg = cvCreateImage (cvGetSize (p_frame), p_frame->depth, 3);
cvSetZero (transparencyimg);
transparencyimg = cv::Mat::zeros (p_frame.size (), p_frame.type ());
if (m_motioncellsidxcstr)
delete[]m_motioncellsidxcstr;
m_motioncells_idx_count = m_MotionCells.size () * MSGLEN; //one motion cell idx: (lin idx : col idx,) it's up to 6 character except last motion cell idx
......@@ -204,22 +187,20 @@ MotionCells::performDetectionMotionCells (IplImage * p_frame,
char *tmpstr = new char[MSGLEN + 1];
tmpstr[0] = 0;
for (unsigned int i = 0; i < m_MotionCells.size (); i++) {
CvPoint pt1, pt2;
cv::Point pt1, pt2;
pt1.x = m_MotionCells.at (i).cell_pt1.x * 2;
pt1.y = m_MotionCells.at (i).cell_pt1.y * 2;
pt2.x = m_MotionCells.at (i).cell_pt2.x * 2;
pt2.y = m_MotionCells.at (i).cell_pt2.y * 2;
if (m_useAlpha && m_isVisible) {
cvRectangle (transparencyimg,
pt1,
pt2,
cv::rectangle (transparencyimg,
pt1, pt2,
CV_RGB (motioncellscolor.B_channel_value,
motioncellscolor.G_channel_value,
motioncellscolor.R_channel_value), CV_FILLED);
motioncellscolor.R_channel_value), cv::FILLED);
} else if (m_isVisible) {
cvRectangle (p_frame,
pt1,
pt2,
cv::rectangle (p_frame,
pt1, pt2,
CV_RGB (motioncellscolor.B_channel_value,
motioncellscolor.G_channel_value,
motioncellscolor.R_channel_value), p_thickness);
......@@ -256,30 +237,10 @@ MotionCells::performDetectionMotionCells (IplImage * p_frame,
m_motioncells_idx_count = 0;
if (m_MotionCells.size () > 0)
m_MotionCells.clear ();
if (transparencyimg)
cvReleaseImage (&transparencyimg);
}
if (m_pprevFrame)
cvReleaseImage (&m_pprevFrame);
m_pprevFrame = cvCloneImage (m_pcurFrame);
m_pprevFrame = m_pcurFrame.clone ();
m_framecnt = 0;
if (m_pcurFrame)
cvReleaseImage (&m_pcurFrame);
if (m_pdifferenceImage)
cvReleaseImage (&m_pdifferenceImage);
if (m_pcurgreyImage)
cvReleaseImage (&m_pcurgreyImage);
if (m_pprevgreyImage)
cvReleaseImage (&m_pprevgreyImage);
if (m_pgreyImage)
cvReleaseImage (&m_pgreyImage);
if (m_pbwImage)
cvReleaseImage (&m_pbwImage);
if (m_pprevDown)
cvReleaseImage (&m_pprevDown);
if (m_pcurDown)
cvReleaseImage (&m_pcurDown);
if (m_pCells) {
for (int i = 0; i < m_gridy; ++i) {
delete[]m_pCells[i];
......@@ -290,27 +251,25 @@ MotionCells::performDetectionMotionCells (IplImage * p_frame,
if (p_framerate <= 5) {
if (m_MotionCells.size () > 0)
m_MotionCells.clear ();
if (transparencyimg)
cvReleaseImage (&transparencyimg);
}
} else { //we do frame drop
m_motioncells_idx_count = 0;
ret = -2;
for (unsigned int i = 0; i < m_MotionCells.size (); i++) {
CvPoint pt1, pt2;
cv::Point pt1, pt2;
pt1.x = m_MotionCells.at (i).cell_pt1.x * 2;
pt1.y = m_MotionCells.at (i).cell_pt1.y * 2;
pt2.x = m_MotionCells.at (i).cell_pt2.x * 2;
pt2.y = m_MotionCells.at (i).cell_pt2.y * 2;
if (m_useAlpha && m_isVisible) {
cvRectangle (transparencyimg,
cv::rectangle (transparencyimg,
pt1,
pt2,
CV_RGB (motioncellscolor.B_channel_value,
motioncellscolor.G_channel_value,
motioncellscolor.R_channel_value), CV_FILLED);
motioncellscolor.R_channel_value), cv::FILLED);
} else if (m_isVisible) {
cvRectangle (p_frame,
cv::rectangle (p_frame,
pt1,
pt2,
CV_RGB (motioncellscolor.B_channel_value,
......@@ -447,8 +406,7 @@ MotionCells::calculateMotionPercentInCell (int p_row, int p_col,
for (int i = ybegin; i < yend; i++) {
for (int j = xbegin; j < xend; j++) {
cntpixelsnum++;
if ((((uchar *) (m_pbwImage->imageData + m_pbwImage->widthStep * i))[j]) >
0) {
if ((((uchar *) (m_pbwImage.data + m_pbwImage.step[0] * i))[j]) > 0) {
cntmotionpixelnum++;
if (cntmotionpixelnum >= thresholdmotionpixelnum) { //we dont needs calculate anymore
*p_motionarea = cntmotionpixelnum;
......@@ -487,7 +445,7 @@ MotionCells::calculateMotionPercentInMotionCells (motioncellidx *
mci.cell_pt2.y = floor ((double) (i + 1) * m_cellheight);
int w = mci.cell_pt2.x - mci.cell_pt1.x;
int h = mci.cell_pt2.y - mci.cell_pt1.y;
mci.motioncell = cvRect (mci.cell_pt1.x, mci.cell_pt1.y, w, h);
mci.motioncell = cv::Rect (mci.cell_pt1.x, mci.cell_pt1.y, w, h);
m_MotionCells.push_back (mci);
}
}
......@@ -512,7 +470,7 @@ MotionCells::calculateMotionPercentInMotionCells (motioncellidx *
mci.cell_pt2.y = floor ((double) (i + 1) * m_cellheight);
int w = mci.cell_pt2.x - mci.cell_pt1.x;
int h = mci.cell_pt2.y - mci.cell_pt1.y;
mci.motioncell = cvRect (mci.cell_pt1.x, mci.cell_pt1.y, w, h);
mci.motioncell = cv::Rect (mci.cell_pt1.x, mci.cell_pt1.y, w, h);
m_MotionCells.push_back (mci);
}
}
......@@ -523,10 +481,10 @@ void
MotionCells::performMotionMaskCoords (motionmaskcoordrect * p_motionmaskcoords,
int p_motionmaskcoords_count)
{
CvPoint upperleft;
cv::Point upperleft;
upperleft.x = 0;
upperleft.y = 0;
CvPoint lowerright;
cv::Point lowerright;
lowerright.x = 0;
lowerright.y = 0;
for (int i = 0; i < p_motionmaskcoords_count; i++) {
......@@ -534,8 +492,8 @@ MotionCells::performMotionMaskCoords (motionmaskcoordrect * p_motionmaskcoords,
upperleft.y = p_motionmaskcoords[i].upper_left_y;
lowerright.x = p_motionmaskcoords[i].lower_right_x;
lowerright.y = p_motionmaskcoords[i].lower_right_y;
cvRectangle (m_pbwImage, upperleft, lowerright, CV_RGB (0, 0, 0),
CV_FILLED);
cv::rectangle (m_pbwImage, upperleft, lowerright, CV_RGB (0, 0, 0),
cv::FILLED);
}
}
......@@ -552,7 +510,7 @@ MotionCells::performMotionMask (motioncellidx * p_motionmaskcellsidx,
(double) p_motionmaskcellsidx[k].lineidx * m_cellheight + m_cellheight;
for (int i = beginy; i < endy; i++)
for (int j = beginx; j < endx; j++) {
((uchar *) (m_pbwImage->imageData + m_pbwImage->widthStep * i))[j] = 0;
((uchar *) (m_pbwImage.data + m_pbwImage.step[0] * i))[j] = 0;
}
}
}
......@@ -560,17 +518,17 @@ MotionCells::performMotionMask (motioncellidx * p_motionmaskcellsidx,
///BGR if we use only OpenCV
//RGB if we use gst+OpenCV
void
MotionCells::blendImages (IplImage * p_actFrame, IplImage * p_cellsFrame,
MotionCells::blendImages (cv::Mat p_actFrame, cv::Mat p_cellsFrame,
float p_alpha, float p_beta)
{
int height = p_actFrame->height;
int width = p_actFrame->width;
int step = p_actFrame->widthStep / sizeof (uchar);
int channels = p_actFrame->nChannels;
int cellstep = p_cellsFrame->widthStep / sizeof (uchar);
uchar *curImageData = (uchar *) p_actFrame->imageData;
uchar *cellImageData = (uchar *) p_cellsFrame->imageData;
int height = p_actFrame.size ().height;
int width = p_actFrame.size ().width;
int step = p_actFrame.step[0] / sizeof (uchar);
int channels = p_actFrame.channels ();
int cellstep = p_cellsFrame.step[0] / sizeof (uchar);
uchar *curImageData = (uchar *) p_actFrame.data;
uchar *cellImageData = (uchar *) p_cellsFrame.data;
for (int i = 0; i < height; i++)
for (int j = 0; j < width; j++)
......
/*
* GStreamer
* Copyright (C) 2011 Robert Jobbagy <jobbagy.robert@gmail.com>
* Copyright (C) 2011 Nicola Murino <nicola.murino@gmail.com>
* Copyright (C) 2011 - 2018 Nicola Murino <nicola.murino@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
......@@ -46,11 +46,6 @@
#define MOTIONCELLS_H_
#include <opencv2/core.hpp>
#if (CV_MAJOR_VERSION >= 4)
#include <opencv2/core/types_c.h>
#include <opencv2/core/core_c.h>
#endif
#include <fstream>
#include <vector>
#include <glib.h>
......@@ -109,18 +104,18 @@ struct Cell
struct MotionCellsIdx
{
CvRect motioncell;
cv::Rect motioncell;
//Points for the edges of the rectangle.
CvPoint cell_pt1;
CvPoint cell_pt2;
cv::Point cell_pt1;
cv::Point cell_pt2;
int lineidx;
int colidx;
};
struct OverlayRegions
{
CvPoint upperleft;
CvPoint lowerright;
cv::Point upperleft;
cv::Point lowerright;
};
class MotionCells
......@@ -130,7 +125,7 @@ public:
MotionCells ();
virtual ~ MotionCells ();
int performDetectionMotionCells (IplImage * p_frame, double p_sensitivity,
int performDetectionMotionCells (cv::Mat p_frame, double p_sensitivity,
double p_framerate, int p_gridx, int p_gridy, gint64 timestamp_millisec,
bool p_isVisble, bool p_useAlpha, int motionmaskcoord_count,
motionmaskcoordrect * motionmaskcoords, int motionmaskcells_count,
......@@ -138,9 +133,9 @@ public:
int motioncells_count, motioncellidx * motioncellsidx, gint64 starttime,
char *datafile, bool p_changed_datafile, int p_thickness);
void setPrevFrame (IplImage * p_prevframe)
void setPrevFrame (cv::Mat p_prevframe)
{
m_pprevFrame = cvCloneImage (p_prevframe);
m_pprevFrame = p_prevframe.clone();
}
char *getMotionCellsIdx ()
{
......@@ -198,26 +193,26 @@ private:
p_motionmaskcellsidx, int p_motionmaskcells_count = 0);
int saveMotionCells (gint64 timestamp_millisec);
int initDataFile (char *p_datafile, gint64 starttime);
void blendImages (IplImage * p_actFrame, IplImage * p_cellsFrame,
void blendImages (cv::Mat p_actFrame, cv::Mat p_cellsFrame,
float p_alpha, float p_beta);
void setData (IplImage * img, int lin, int col, uchar valor)
void setData (cv::Mat img, int lin, int col, uchar valor)
{
((uchar *) (img->imageData + img->widthStep * lin))[col] = valor;
((uchar *) (img.data + img.step[0] * lin))[col] = valor;
}
uchar getData (IplImage * img, int lin, int col)
uchar getData (cv::Mat img, int lin, int col)
{
return ((uchar *) (img->imageData + img->widthStep * lin))[col];
return ((uchar *) (img.data + img.step[0] * lin))[col];
}
bool getIsNonZero (IplImage * img)
bool getIsNonZero (cv::Mat img)
{
int lin, col;
for (lin = 0; lin < img->height; lin++)
for (col = 0; col < img->width; col++) {
if ((((uchar *) (img->imageData + img->widthStep * lin))[col]) > 0)
for (lin = 0; lin < img.size().height; lin++)
for (col = 0; col < img.size().width; col++) {
if ((((uchar *) (img.data + img.step[0] * lin))[col]) > 0)
return true;
}
return false;
......@@ -243,8 +238,8 @@ private:
}
}
IplImage *m_pcurFrame, *m_pprevFrame, *m_pdifferenceImage,
*m_pbwImage,*transparencyimg;
cv::Mat m_pprevFrame, m_pdifferenceImage, m_pbwImage, m_pcurFrame,
transparencyimg;
bool m_isVisible, m_changed_datafile, m_useAlpha, m_saveInDatafile;
Cell **m_pCells;
vector < MotionCellsIdx > m_MotionCells;
......
......@@ -39,7 +39,7 @@ gst_camera_event_new_calibrated (gchar * settings)
GstStructure *s;
s = gst_structure_new (GST_CAMERA_EVENT_CALIBRATED_NAME,
"undistort-settings", G_TYPE_STRING, g_strdup(settings), NULL);
"undistort-settings", G_TYPE_STRING, g_strdup (settings), NULL);
calibrated_event = gst_event_new_custom (GST_EVENT_CUSTOM_BOTH, s);
......@@ -66,16 +66,16 @@ gst_camera_event_parse_calibrated (GstEvent * event, gchar ** settings)
g_return_val_if_fail (event != NULL, FALSE);
if (GST_EVENT_TYPE (event) != GST_EVENT_CUSTOM_BOTH)
return FALSE; /* Not a calibrated event */
return FALSE; /* Not a calibrated event */
s = gst_event_get_structure (event);
if (s == NULL
|| !gst_structure_has_name (s, GST_CAMERA_EVENT_CALIBRATED_NAME))
return FALSE; /* Not a calibrated event */
const gchar *str = gst_structure_get_string(s, "undistort-settings");
const gchar *str = gst_structure_get_string (s, "undistort-settings");
if (!str)
return FALSE; /* Not calibrated frame event */
return FALSE; /* Not calibrated frame event */
*settings = g_strdup (str);
......
......@@ -85,9 +85,6 @@
#include "gstcameracalibrate.h"
#include <opencv2/imgproc.hpp>
#if (CV_MAJOR_VERSION >= 4)
#include <opencv2/imgproc/imgproc_c.h>
#endif
#include <opencv2/calib3d.hpp>
#include <gst/opencv/gstopencvutils.h>
......@@ -172,7 +169,7 @@ static void gst_camera_calibrate_get_property (GObject * object, guint prop_id,
static GstFlowReturn
gst_camera_calibrate_transform_frame_ip (GstOpencvVideoFilter * cvfilter,
GstBuffer * frame, IplImage * img);
GstBuffer * frame, cv::Mat img);
/* clean up */
static void
......@@ -455,14 +452,14 @@ gst_camera_calibrate_get_property (GObject * object, guint prop_id,
}
}
void camera_calibrate_run (GstCameraCalibrate * calib, IplImage * img);
void camera_calibrate_run (GstCameraCalibrate * calib, cv::Mat img);
/*
* Performs the camera calibration
*/
static GstFlowReturn
gst_camera_calibrate_transform_frame_ip (GstOpencvVideoFilter * cvfilter,
G_GNUC_UNUSED GstBuffer * frame, IplImage * img)
G_GNUC_UNUSED GstBuffer * frame, cv::Mat img)
{
GstCameraCalibrate *calib = GST_CAMERA_CALIBRATE (cvfilter);
......@@ -476,14 +473,13 @@ bool camera_calibrate_calibrate (GstCameraCalibrate * calib,
std::vector < std::vector < cv::Point2f > >imagePoints);
void
camera_calibrate_run (GstCameraCalibrate * calib, IplImage * img)
camera_calibrate_run (GstCameraCalibrate * calib, cv::Mat img)
{
cv::Mat view = cv::cvarrToMat (img);
// For camera only take new samples after delay time
if (calib->mode == CAPTURING) {
// get_input
cv::Size imageSize = view.size ();
cv::Size imageSize = img.size ();
/* find_pattern
* FIXME find ways to reduce CPU usage
......@@ -506,15 +502,15 @@ camera_calibrate_run (GstCameraCalibrate * calib, IplImage * img)
switch (calib->calibrationPattern) {
case GST_CAMERA_CALIBRATION_PATTERN_CHESSBOARD:
found =
cv::findChessboardCorners (view, calib->boardSize, pointBuf,
cv::findChessboardCorners (img, calib->boardSize, pointBuf,
chessBoardFlags);
break;
case GST_CAMERA_CALIBRATION_PATTERN_CIRCLES_GRID:
found = cv::findCirclesGrid (view, calib->boardSize, pointBuf);
found = cv::findCirclesGrid (img, calib->boardSize, pointBuf);
break;
case GST_CAMERA_CALIBRATION_PATTERN_ASYMMETRIC_CIRCLES_GRID:
found =
cv::findCirclesGrid (view, calib->boardSize, pointBuf,
cv::findCirclesGrid (img, calib->boardSize, pointBuf,
cv::CALIB_CB_ASYMMETRIC_GRID);
break;
default:
......@@ -531,7 +527,7 @@ camera_calibrate_run (GstCameraCalibrate * calib, IplImage * img)
* the color convert should be done once (if needed) and shared
* FIXME keep viewGray around to avoid reallocating it each time... */
cv::Mat viewGray;
cv::cvtColor (view, viewGray, cv::COLOR_BGR2GRAY);
cv::cvtColor (img, viewGray, cv::COLOR_BGR2GRAY);
cv::cornerSubPix (viewGray, pointBuf, cv::Size (11, 11), cv::Size (-1,
-1),
cv::TermCriteria (cv::TermCriteria::EPS + cv::TermCriteria::COUNT,
......@@ -549,7 +545,7 @@ camera_calibrate_run (GstCameraCalibrate * calib, IplImage * img)
/* draw the corners */
if (calib->showCorners) {
cv::drawChessboardCorners (view, calib->boardSize, cv::Mat (pointBuf),
cv::drawChessboardCorners (img, calib->boardSize, cv::Mat (pointBuf),
found);
}
}
......@@ -598,7 +594,7 @@ camera_calibrate_run (GstCameraCalibrate * calib, IplImage * img)
}
if (calib->mode == CAPTURING && blinkOutput) {
bitwise_not (view, view);
bitwise_not (img, img);
}
}
......@@ -613,8 +609,8 @@ camera_calibrate_run (GstCameraCalibrate * calib, IplImage * img)
(calib->mode == CALIBRATED) ? "Calibrated" : "Waiting...";
int baseLine = 0;
cv::Size textSize = cv::getTextSize (msg, 1, 1, 1, &baseLine);
cv::Point textOrigin (view.cols - 2 * textSize.width - 10,
view.rows - 2 * baseLine - 10);
cv::Point textOrigin (img.cols - 2 * textSize.width - 10,
img.rows - 2 * baseLine - 10);
if (calib->mode == CAPTURING) {
msg =
......@@ -624,7 +620,7 @@ camera_calibrate_run (GstCameraCalibrate * calib, IplImage * img)
const cv::Scalar RED (0, 0, 255);
const cv::Scalar GREEN (0, 255, 0);
cv::putText (view, msg, textOrigin, 1, 1,
cv::putText (img, msg, textOrigin, 1, 1,
calib->mode == CALIBRATED ? GREEN : RED);
}
......
......@@ -61,7 +61,7 @@
* gst-launch-1.0 -v v4l2src ! videoconvert ! cameraundistort settings="???" ! autovideosink
* ]| will correct camera distortion based on provided settings.
* |[
* gst-launch-1.0 -v v4l2src ! videoconvert ! cameraundistort ! cameracalibrate | autovideosink
* gst-launch-1.0 -v v4l2src ! videoconvert ! cameraundistort ! cameracalibrate ! autovideosink
* ]| will correct camera distortion once camera calibration is done.
* </refsect2>
*/
......@@ -78,9 +78,6 @@
#include "gstcameraundistort.h"
#include <opencv2/imgproc.hpp>
#if (CV_MAJOR_VERSION >= 4)
#include <opencv2/imgproc/imgproc_c.h>
#endif
#include <opencv2/calib3d.hpp>
#include <gst/opencv/gstopencvutils.h>
......@@ -111,19 +108,19 @@ static void gst_camera_undistort_get_property (GObject * object, guint prop_id,
GValue * value, GParamSpec * pspec);