mirror of
https://github.com/opencv/opencv.git
synced 2026-07-30 07:43:03 +04:00
Merge remote-tracking branch 'upstream/3.4' into merge-3.4
This commit is contained in:
@@ -18,7 +18,6 @@ class GStreamerPipeline
|
||||
"{h help usage ? | | print help messages }"
|
||||
"{m mode | | coding mode (supported: encode, decode) }"
|
||||
"{p pipeline |default | pipeline name (supported: 'default', 'gst-basic', 'gst-vaapi', 'gst-libav', 'ffmpeg') }"
|
||||
"{ct container |mp4 | container name (supported: 'mp4', 'mov', 'avi', 'mkv') }"
|
||||
"{cd codec |h264 | codec name (supported: 'h264', 'h265', 'mpeg2', 'mpeg4', 'mjpeg', 'vp8') }"
|
||||
"{f file path | | path to file }"
|
||||
"{vr resolution |720p | video resolution for encoding (supported: '720p', '1080p', '4k') }"
|
||||
@@ -30,24 +29,29 @@ class GStreamerPipeline
|
||||
if (cmd_parser->has("help"))
|
||||
{
|
||||
cmd_parser->printMessage();
|
||||
exit_code = -1;
|
||||
CV_Error(Error::StsBadArg, "Called help.");
|
||||
}
|
||||
|
||||
fast_measure = cmd_parser->has("fast"); // fast measure fps
|
||||
fix_fps = cmd_parser->get<int>("fps"); // fixed frame per second
|
||||
pipeline = cmd_parser->get<string>("pipeline"), // gstreamer pipeline type
|
||||
container = cmd_parser->get<string>("container"), // container type
|
||||
mode = cmd_parser->get<string>("mode"), // coding mode
|
||||
codec = cmd_parser->get<string>("codec"), // codec type
|
||||
file_name = cmd_parser->get<string>("file"), // path to videofile
|
||||
resolution = cmd_parser->get<string>("resolution"); // video resolution
|
||||
|
||||
size_t found = file_name.rfind(".");
|
||||
if (found != string::npos)
|
||||
{
|
||||
container = file_name.substr(found + 1); // container type
|
||||
}
|
||||
else { CV_Error(Error::StsBadArg, "Can not parse container extension."); }
|
||||
|
||||
if (!cmd_parser->check())
|
||||
{
|
||||
cmd_parser->printErrors();
|
||||
exit_code = -1;
|
||||
CV_Error(Error::StsBadArg, "Failed parse arguments.");
|
||||
}
|
||||
exit_code = 0;
|
||||
}
|
||||
|
||||
~GStreamerPipeline() { delete cmd_parser; }
|
||||
@@ -55,7 +59,6 @@ class GStreamerPipeline
|
||||
// Start pipeline
|
||||
int run()
|
||||
{
|
||||
if (exit_code < 0) { return exit_code; }
|
||||
if (mode == "decode") { if (createDecodePipeline() < 0) return -1; }
|
||||
else if (mode == "encode") { if (createEncodePipeline() < 0) return -1; }
|
||||
else
|
||||
@@ -423,7 +426,6 @@ class GStreamerPipeline
|
||||
resolution; // video resolution
|
||||
int fix_fps; // fixed frame per second
|
||||
Size fix_size; // fixed frame size
|
||||
int exit_code;
|
||||
VideoWriter wrt;
|
||||
VideoCapture cap;
|
||||
ostringstream stream_pipeline;
|
||||
@@ -432,6 +434,14 @@ class GStreamerPipeline
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
GStreamerPipeline pipe(argc, argv);
|
||||
return pipe.run();
|
||||
try
|
||||
{
|
||||
GStreamerPipeline pipe(argc, argv);
|
||||
return pipe.run();
|
||||
}
|
||||
catch(const Exception& e)
|
||||
{
|
||||
cerr << e.what() << endl;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ static void help()
|
||||
<< endl;
|
||||
}
|
||||
|
||||
static void colorizeDisparity( const Mat& gray, Mat& rgb, double maxDisp=-1.f, float S=1.f, float V=1.f )
|
||||
static void colorizeDisparity( const Mat& gray, Mat& rgb, double maxDisp=-1.f)
|
||||
{
|
||||
CV_Assert( !gray.empty() );
|
||||
CV_Assert( gray.type() == CV_8UC1 );
|
||||
@@ -42,41 +42,9 @@ static void colorizeDisparity( const Mat& gray, Mat& rgb, double maxDisp=-1.f, f
|
||||
if( maxDisp < 1 )
|
||||
return;
|
||||
|
||||
for( int y = 0; y < gray.rows; y++ )
|
||||
{
|
||||
for( int x = 0; x < gray.cols; x++ )
|
||||
{
|
||||
uchar d = gray.at<uchar>(y,x);
|
||||
unsigned int H = ((uchar)maxDisp - d) * 240 / (uchar)maxDisp;
|
||||
|
||||
unsigned int hi = (H/60) % 6;
|
||||
float f = H/60.f - H/60;
|
||||
float p = V * (1 - S);
|
||||
float q = V * (1 - f * S);
|
||||
float t = V * (1 - (1 - f) * S);
|
||||
|
||||
Point3f res;
|
||||
|
||||
if( hi == 0 ) //R = V, G = t, B = p
|
||||
res = Point3f( p, t, V );
|
||||
if( hi == 1 ) // R = q, G = V, B = p
|
||||
res = Point3f( p, V, q );
|
||||
if( hi == 2 ) // R = p, G = V, B = t
|
||||
res = Point3f( t, V, p );
|
||||
if( hi == 3 ) // R = p, G = q, B = V
|
||||
res = Point3f( V, q, p );
|
||||
if( hi == 4 ) // R = t, G = p, B = V
|
||||
res = Point3f( V, p, t );
|
||||
if( hi == 5 ) // R = V, G = p, B = q
|
||||
res = Point3f( q, p, V );
|
||||
|
||||
uchar b = (uchar)(std::max(0.f, std::min (res.x, 1.f)) * 255.f);
|
||||
uchar g = (uchar)(std::max(0.f, std::min (res.y, 1.f)) * 255.f);
|
||||
uchar r = (uchar)(std::max(0.f, std::min (res.z, 1.f)) * 255.f);
|
||||
|
||||
rgb.at<Point3_<uchar> >(y,x) = Point3_<uchar>(b, g, r);
|
||||
}
|
||||
}
|
||||
Mat tmp;
|
||||
convertScaleAbs(gray, tmp, 255.f / maxDisp);
|
||||
applyColorMap(tmp, rgb, COLORMAP_JET);
|
||||
}
|
||||
|
||||
static float getMaxDisparity( VideoCapture& capture )
|
||||
|
||||
@@ -6,9 +6,10 @@
|
||||
|
||||
#include "opencv2/imgcodecs.hpp"
|
||||
#include "opencv2/highgui.hpp"
|
||||
#include <stdio.h>
|
||||
#include <iostream>
|
||||
|
||||
using namespace cv;
|
||||
using std::cout;
|
||||
|
||||
/** Global Variables */
|
||||
const int alpha_slider_max = 100;
|
||||
@@ -29,11 +30,8 @@ Mat dst;
|
||||
static void on_trackbar( int, void* )
|
||||
{
|
||||
alpha = (double) alpha_slider/alpha_slider_max ;
|
||||
|
||||
beta = ( 1.0 - alpha );
|
||||
|
||||
addWeighted( src1, alpha, src2, beta, 0.0, dst);
|
||||
|
||||
imshow( "Linear Blend", dst );
|
||||
}
|
||||
//![on_trackbar]
|
||||
@@ -50,8 +48,8 @@ int main( void )
|
||||
src2 = imread("../data/WindowsLogo.jpg");
|
||||
//![load]
|
||||
|
||||
if( src1.empty() ) { printf("Error loading src1 \n"); return -1; }
|
||||
if( src2.empty() ) { printf("Error loading src2 \n"); return -1; }
|
||||
if( src1.empty() ) { cout << "Error loading src1 \n"; return -1; }
|
||||
if( src2.empty() ) { cout << "Error loading src2 \n"; return -1; }
|
||||
|
||||
/// Initialize values
|
||||
alpha_slider = 0;
|
||||
|
||||
@@ -31,7 +31,7 @@ void Dilation( int, void* );
|
||||
int main( int argc, char** argv )
|
||||
{
|
||||
/// Load an image
|
||||
CommandLineParser parser( argc, argv, "{@input | ../data/chicky_512.png | input image}" );
|
||||
CommandLineParser parser( argc, argv, "{@input | ../data/LinuxLogo.jpg | input image}" );
|
||||
src = imread( parser.get<String>( "@input" ), IMREAD_COLOR );
|
||||
if( src.empty() )
|
||||
{
|
||||
|
||||
@@ -33,7 +33,7 @@ void Morphology_Operations( int, void* );
|
||||
int main( int argc, char** argv )
|
||||
{
|
||||
//![load]
|
||||
CommandLineParser parser( argc, argv, "{@input | ../data/baboon.jpg | input image}" );
|
||||
CommandLineParser parser( argc, argv, "{@input | ../data/LinuxLogo.jpg | input image}" );
|
||||
src = imread( parser.get<String>( "@input" ), IMREAD_COLOR );
|
||||
if (src.empty())
|
||||
{
|
||||
|
||||
@@ -36,52 +36,90 @@ int main( int argc, char ** argv )
|
||||
const char* filename = argc >=2 ? argv[1] : "../data/lena.jpg";
|
||||
|
||||
src = imread( filename, IMREAD_COLOR );
|
||||
if(src.empty()){
|
||||
if(src.empty())
|
||||
{
|
||||
printf(" Error opening image\n");
|
||||
printf(" Usage: ./Smoothing [image_name -- default ../data/lena.jpg] \n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if( display_caption( "Original Image" ) != 0 ) { return 0; }
|
||||
if( display_caption( "Original Image" ) != 0 )
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
dst = src.clone();
|
||||
if( display_dst( DELAY_CAPTION ) != 0 ) { return 0; }
|
||||
|
||||
if( display_dst( DELAY_CAPTION ) != 0 )
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
/// Applying Homogeneous blur
|
||||
if( display_caption( "Homogeneous Blur" ) != 0 ) { return 0; }
|
||||
if( display_caption( "Homogeneous Blur" ) != 0 )
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
//![blur]
|
||||
for ( int i = 1; i < MAX_KERNEL_LENGTH; i = i + 2 )
|
||||
{ blur( src, dst, Size( i, i ), Point(-1,-1) );
|
||||
if( display_dst( DELAY_BLUR ) != 0 ) { return 0; } }
|
||||
{
|
||||
blur( src, dst, Size( i, i ), Point(-1,-1) );
|
||||
if( display_dst( DELAY_BLUR ) != 0 )
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
//![blur]
|
||||
|
||||
/// Applying Gaussian blur
|
||||
if( display_caption( "Gaussian Blur" ) != 0 ) { return 0; }
|
||||
if( display_caption( "Gaussian Blur" ) != 0 )
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
//![gaussianblur]
|
||||
for ( int i = 1; i < MAX_KERNEL_LENGTH; i = i + 2 )
|
||||
{ GaussianBlur( src, dst, Size( i, i ), 0, 0 );
|
||||
if( display_dst( DELAY_BLUR ) != 0 ) { return 0; } }
|
||||
{
|
||||
GaussianBlur( src, dst, Size( i, i ), 0, 0 );
|
||||
if( display_dst( DELAY_BLUR ) != 0 )
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
//![gaussianblur]
|
||||
|
||||
/// Applying Median blur
|
||||
if( display_caption( "Median Blur" ) != 0 ) { return 0; }
|
||||
if( display_caption( "Median Blur" ) != 0 )
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
//![medianblur]
|
||||
for ( int i = 1; i < MAX_KERNEL_LENGTH; i = i + 2 )
|
||||
{ medianBlur ( src, dst, i );
|
||||
if( display_dst( DELAY_BLUR ) != 0 ) { return 0; } }
|
||||
{
|
||||
medianBlur ( src, dst, i );
|
||||
if( display_dst( DELAY_BLUR ) != 0 )
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
//![medianblur]
|
||||
|
||||
/// Applying Bilateral Filter
|
||||
if( display_caption( "Bilateral Blur" ) != 0 ) { return 0; }
|
||||
if( display_caption( "Bilateral Blur" ) != 0 )
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
//![bilateralfilter]
|
||||
for ( int i = 1; i < MAX_KERNEL_LENGTH; i = i + 2 )
|
||||
{ bilateralFilter ( src, dst, i, i*2, i/2 );
|
||||
if( display_dst( DELAY_BLUR ) != 0 ) { return 0; } }
|
||||
{
|
||||
bilateralFilter ( src, dst, i, i*2, i/2 );
|
||||
if( display_dst( DELAY_BLUR ) != 0 )
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
//![bilateralfilter]
|
||||
|
||||
/// Done
|
||||
|
||||
@@ -1,35 +1,8 @@
|
||||
#ifndef __OPENCV_SAMPLES_DNN_CUSTOM_LAYERS__
|
||||
#define __OPENCV_SAMPLES_DNN_CUSTOM_LAYERS__
|
||||
|
||||
#include <opencv2/dnn.hpp>
|
||||
|
||||
//! [A custom layer interface]
|
||||
class MyLayer : public cv::dnn::Layer
|
||||
{
|
||||
public:
|
||||
//! [MyLayer::MyLayer]
|
||||
MyLayer(const cv::dnn::LayerParams ¶ms);
|
||||
//! [MyLayer::MyLayer]
|
||||
|
||||
//! [MyLayer::create]
|
||||
static cv::Ptr<cv::dnn::Layer> create(cv::dnn::LayerParams& params);
|
||||
//! [MyLayer::create]
|
||||
|
||||
//! [MyLayer::getMemoryShapes]
|
||||
virtual bool getMemoryShapes(const std::vector<std::vector<int> > &inputs,
|
||||
const int requiredOutputs,
|
||||
std::vector<std::vector<int> > &outputs,
|
||||
std::vector<std::vector<int> > &internals) const CV_OVERRIDE;
|
||||
//! [MyLayer::getMemoryShapes]
|
||||
|
||||
//! [MyLayer::forward]
|
||||
virtual void forward(std::vector<cv::Mat*> &inputs, std::vector<cv::Mat> &outputs, std::vector<cv::Mat> &internals) CV_OVERRIDE;
|
||||
//! [MyLayer::forward]
|
||||
|
||||
//! [MyLayer::finalize]
|
||||
virtual void finalize(const std::vector<cv::Mat*> &inputs, std::vector<cv::Mat> &outputs) CV_OVERRIDE;
|
||||
//! [MyLayer::finalize]
|
||||
|
||||
virtual void forward(cv::InputArrayOfArrays inputs, cv::OutputArrayOfArrays outputs, cv::OutputArrayOfArrays internals) CV_OVERRIDE;
|
||||
};
|
||||
//! [A custom layer interface]
|
||||
#include <opencv2/dnn/shape_utils.hpp> // getPlane
|
||||
|
||||
//! [InterpLayer]
|
||||
class InterpLayer : public cv::dnn::Layer
|
||||
@@ -113,15 +86,33 @@ private:
|
||||
//! [InterpLayer]
|
||||
|
||||
//! [ResizeBilinearLayer]
|
||||
class ResizeBilinearLayer : public cv::dnn::Layer
|
||||
class ResizeBilinearLayer CV_FINAL : public cv::dnn::Layer
|
||||
{
|
||||
public:
|
||||
ResizeBilinearLayer(const cv::dnn::LayerParams ¶ms) : Layer(params)
|
||||
{
|
||||
CV_Assert(!params.get<bool>("align_corners", false));
|
||||
CV_Assert(blobs.size() == 1, blobs[0].type() == CV_32SC1);
|
||||
outHeight = blobs[0].at<int>(0, 0);
|
||||
outWidth = blobs[0].at<int>(0, 1);
|
||||
CV_Assert(!blobs.empty());
|
||||
|
||||
for (size_t i = 0; i < blobs.size(); ++i)
|
||||
CV_Assert(blobs[i].type() == CV_32SC1);
|
||||
|
||||
// There are two cases of input blob: a single blob which contains output
|
||||
// shape and two blobs with scaling factors.
|
||||
if (blobs.size() == 1)
|
||||
{
|
||||
CV_Assert(blobs[0].total() == 2);
|
||||
outHeight = blobs[0].at<int>(0, 0);
|
||||
outWidth = blobs[0].at<int>(0, 1);
|
||||
factorHeight = factorWidth = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
CV_Assert(blobs.size() == 2, blobs[0].total() == 1, blobs[1].total() == 1);
|
||||
factorHeight = blobs[0].at<int>(0, 0);
|
||||
factorWidth = blobs[1].at<int>(0, 0);
|
||||
outHeight = outWidth = 0;
|
||||
}
|
||||
}
|
||||
|
||||
static cv::Ptr<cv::dnn::Layer> create(cv::dnn::LayerParams& params)
|
||||
@@ -130,25 +121,32 @@ public:
|
||||
}
|
||||
|
||||
virtual bool getMemoryShapes(const std::vector<std::vector<int> > &inputs,
|
||||
const int requiredOutputs,
|
||||
const int,
|
||||
std::vector<std::vector<int> > &outputs,
|
||||
std::vector<std::vector<int> > &internals) const CV_OVERRIDE
|
||||
std::vector<std::vector<int> > &) const CV_OVERRIDE
|
||||
{
|
||||
CV_UNUSED(requiredOutputs); CV_UNUSED(internals);
|
||||
std::vector<int> outShape(4);
|
||||
outShape[0] = inputs[0][0]; // batch size
|
||||
outShape[1] = inputs[0][1]; // number of channels
|
||||
outShape[2] = outHeight;
|
||||
outShape[3] = outWidth;
|
||||
outShape[2] = outHeight != 0 ? outHeight : (inputs[0][2] * factorHeight);
|
||||
outShape[3] = outWidth != 0 ? outWidth : (inputs[0][3] * factorWidth);
|
||||
outputs.assign(1, outShape);
|
||||
return false;
|
||||
}
|
||||
|
||||
virtual void finalize(const std::vector<cv::Mat*>&, std::vector<cv::Mat> &outputs) CV_OVERRIDE
|
||||
{
|
||||
if (!outWidth && !outHeight)
|
||||
{
|
||||
outHeight = outputs[0].size[2];
|
||||
outWidth = outputs[0].size[3];
|
||||
}
|
||||
}
|
||||
|
||||
// This implementation is based on a reference implementation from
|
||||
// https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/lite/kernels/internal/reference/reference_ops.h
|
||||
virtual void forward(std::vector<cv::Mat*> &inputs, std::vector<cv::Mat> &outputs, std::vector<cv::Mat> &internals) CV_OVERRIDE
|
||||
virtual void forward(std::vector<cv::Mat*> &inputs, std::vector<cv::Mat> &outputs, std::vector<cv::Mat> &) CV_OVERRIDE
|
||||
{
|
||||
CV_UNUSED(internals);
|
||||
cv::Mat& inp = *inputs[0];
|
||||
cv::Mat& out = outputs[0];
|
||||
const float* inpData = (float*)inp.data;
|
||||
@@ -195,19 +193,54 @@ private:
|
||||
return x + size[3] * (y + size[2] * (c + size[1] * b));
|
||||
}
|
||||
|
||||
int outWidth, outHeight;
|
||||
int outWidth, outHeight, factorWidth, factorHeight;
|
||||
};
|
||||
//! [ResizeBilinearLayer]
|
||||
|
||||
//! [Register a custom layer]
|
||||
#include <opencv2/dnn/layer.details.hpp> // CV_DNN_REGISTER_LAYER_CLASS macro
|
||||
//
|
||||
// The folowing code is used only to generate tutorials documentation.
|
||||
//
|
||||
|
||||
int main(int argc, char** argv)
|
||||
//! [A custom layer interface]
|
||||
class MyLayer : public cv::dnn::Layer
|
||||
{
|
||||
CV_DNN_REGISTER_LAYER_CLASS(MyType, MyLayer);
|
||||
public:
|
||||
//! [MyLayer::MyLayer]
|
||||
MyLayer(const cv::dnn::LayerParams ¶ms);
|
||||
//! [MyLayer::MyLayer]
|
||||
|
||||
//! [MyLayer::create]
|
||||
static cv::Ptr<cv::dnn::Layer> create(cv::dnn::LayerParams& params);
|
||||
//! [MyLayer::create]
|
||||
|
||||
//! [MyLayer::getMemoryShapes]
|
||||
virtual bool getMemoryShapes(const std::vector<std::vector<int> > &inputs,
|
||||
const int requiredOutputs,
|
||||
std::vector<std::vector<int> > &outputs,
|
||||
std::vector<std::vector<int> > &internals) const CV_OVERRIDE;
|
||||
//! [MyLayer::getMemoryShapes]
|
||||
|
||||
//! [MyLayer::forward]
|
||||
virtual void forward(std::vector<cv::Mat*> &inputs, std::vector<cv::Mat> &outputs, std::vector<cv::Mat> &internals) CV_OVERRIDE;
|
||||
//! [MyLayer::forward]
|
||||
|
||||
//! [MyLayer::finalize]
|
||||
virtual void finalize(const std::vector<cv::Mat*> &inputs, std::vector<cv::Mat> &outputs) CV_OVERRIDE;
|
||||
//! [MyLayer::finalize]
|
||||
|
||||
virtual void forward(cv::InputArrayOfArrays inputs, cv::OutputArrayOfArrays outputs, cv::OutputArrayOfArrays internals) CV_OVERRIDE;
|
||||
};
|
||||
//! [A custom layer interface]
|
||||
|
||||
//! [Register a custom layer]
|
||||
#include <opencv2/dnn/layer.details.hpp> // CV_DNN_REGISTER_LAYER_CLASS
|
||||
|
||||
static inline void loadNet()
|
||||
{
|
||||
CV_DNN_REGISTER_LAYER_CLASS(Interp, InterpLayer);
|
||||
// ...
|
||||
//! [Register a custom layer]
|
||||
CV_UNUSED(argc); CV_UNUSED(argv);
|
||||
|
||||
//! [Register InterpLayer]
|
||||
CV_DNN_REGISTER_LAYER_CLASS(Interp, InterpLayer);
|
||||
cv::dnn::Net caffeNet = cv::dnn::readNet("/path/to/config.prototxt", "/path/to/weights.caffemodel");
|
||||
@@ -217,16 +250,8 @@ int main(int argc, char** argv)
|
||||
CV_DNN_REGISTER_LAYER_CLASS(ResizeBilinear, ResizeBilinearLayer);
|
||||
cv::dnn::Net tfNet = cv::dnn::readNet("/path/to/graph.pb");
|
||||
//! [Register ResizeBilinearLayer]
|
||||
|
||||
if (false) loadNet(); // To prevent unused function warning.
|
||||
}
|
||||
|
||||
cv::Ptr<cv::dnn::Layer> MyLayer::create(cv::dnn::LayerParams& params)
|
||||
{
|
||||
return cv::Ptr<cv::dnn::Layer>(new MyLayer(params));
|
||||
}
|
||||
MyLayer::MyLayer(const cv::dnn::LayerParams&) {}
|
||||
bool MyLayer::getMemoryShapes(const std::vector<std::vector<int> >&, const int,
|
||||
std::vector<std::vector<int> >&,
|
||||
std::vector<std::vector<int> >&) const { return false; }
|
||||
void MyLayer::forward(std::vector<cv::Mat*>&, std::vector<cv::Mat>&, std::vector<cv::Mat>&) {}
|
||||
void MyLayer::finalize(const std::vector<cv::Mat*>&, std::vector<cv::Mat>&) {}
|
||||
void MyLayer::forward(cv::InputArrayOfArrays, cv::OutputArrayOfArrays, cv::OutputArrayOfArrays) {}
|
||||
#endif // __OPENCV_SAMPLES_DNN_CUSTOM_LAYERS__
|
||||
@@ -0,0 +1,159 @@
|
||||
#include <opencv2/imgproc.hpp>
|
||||
#include <opencv2/highgui.hpp>
|
||||
#include <opencv2/dnn.hpp>
|
||||
|
||||
#include "custom_layers.hpp"
|
||||
|
||||
using namespace cv;
|
||||
using namespace cv::dnn;
|
||||
|
||||
const char* keys =
|
||||
"{ help h | | Print help message. }"
|
||||
"{ input i | | Path to input image or video file. Skip this argument to capture frames from a camera.}"
|
||||
"{ model m | | Path to a binary .pb file contains trained network.}"
|
||||
"{ width | 320 | Preprocess input image by resizing to a specific width. It should be multiple by 32. }"
|
||||
"{ height | 320 | Preprocess input image by resizing to a specific height. It should be multiple by 32. }"
|
||||
"{ thr | 0.5 | Confidence threshold. }"
|
||||
"{ nms | 0.4 | Non-maximum suppression threshold. }";
|
||||
|
||||
void decode(const Mat& scores, const Mat& geometry, float scoreThresh,
|
||||
std::vector<RotatedRect>& detections, std::vector<float>& confidences);
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
// Parse command line arguments.
|
||||
CommandLineParser parser(argc, argv, keys);
|
||||
parser.about("Use this script to run TensorFlow implementation (https://github.com/argman/EAST) of "
|
||||
"EAST: An Efficient and Accurate Scene Text Detector (https://arxiv.org/abs/1704.03155v2)");
|
||||
if (argc == 1 || parser.has("help"))
|
||||
{
|
||||
parser.printMessage();
|
||||
return 0;
|
||||
}
|
||||
|
||||
float confThreshold = parser.get<float>("thr");
|
||||
float nmsThreshold = parser.get<float>("nms");
|
||||
int inpWidth = parser.get<int>("width");
|
||||
int inpHeight = parser.get<int>("height");
|
||||
CV_Assert(parser.has("model"));
|
||||
String model = parser.get<String>("model");
|
||||
|
||||
// Register a custom layer.
|
||||
CV_DNN_REGISTER_LAYER_CLASS(ResizeBilinear, ResizeBilinearLayer);
|
||||
|
||||
// Load network.
|
||||
Net net = readNet(model);
|
||||
|
||||
// Open a video file or an image file or a camera stream.
|
||||
VideoCapture cap;
|
||||
if (parser.has("input"))
|
||||
cap.open(parser.get<String>("input"));
|
||||
else
|
||||
cap.open(0);
|
||||
|
||||
static const std::string kWinName = "EAST: An Efficient and Accurate Scene Text Detector";
|
||||
namedWindow(kWinName, WINDOW_NORMAL);
|
||||
|
||||
std::vector<Mat> outs;
|
||||
std::vector<String> outNames(2);
|
||||
outNames[0] = "feature_fusion/Conv_7/Sigmoid";
|
||||
outNames[1] = "feature_fusion/concat_3";
|
||||
|
||||
Mat frame, blob;
|
||||
while (waitKey(1) < 0)
|
||||
{
|
||||
cap >> frame;
|
||||
if (frame.empty())
|
||||
{
|
||||
waitKey();
|
||||
break;
|
||||
}
|
||||
|
||||
blobFromImage(frame, blob, 1.0, Size(inpWidth, inpHeight), Scalar(123.68, 116.78, 103.94), true, false);
|
||||
net.setInput(blob);
|
||||
net.forward(outs, outNames);
|
||||
|
||||
Mat scores = outs[0];
|
||||
Mat geometry = outs[1];
|
||||
|
||||
// Decode predicted bounding boxes.
|
||||
std::vector<RotatedRect> boxes;
|
||||
std::vector<float> confidences;
|
||||
decode(scores, geometry, confThreshold, boxes, confidences);
|
||||
|
||||
// Apply non-maximum suppression procedure.
|
||||
std::vector<int> indices;
|
||||
NMSBoxes(boxes, confidences, confThreshold, nmsThreshold, indices);
|
||||
|
||||
// Render detections.
|
||||
Point2f ratio((float)frame.cols / inpWidth, (float)frame.rows / inpHeight);
|
||||
for (size_t i = 0; i < indices.size(); ++i)
|
||||
{
|
||||
RotatedRect& box = boxes[indices[i]];
|
||||
|
||||
Point2f vertices[4];
|
||||
box.points(vertices);
|
||||
for (int j = 0; j < 4; ++j)
|
||||
{
|
||||
vertices[j].x *= ratio.x;
|
||||
vertices[j].y *= ratio.y;
|
||||
}
|
||||
for (int j = 0; j < 4; ++j)
|
||||
line(frame, vertices[j], vertices[(j + 1) % 4], Scalar(0, 255, 0), 1);
|
||||
}
|
||||
|
||||
// Put efficiency information.
|
||||
std::vector<double> layersTimes;
|
||||
double freq = getTickFrequency() / 1000;
|
||||
double t = net.getPerfProfile(layersTimes) / freq;
|
||||
std::string label = format("Inference time: %.2f ms", t);
|
||||
putText(frame, label, Point(0, 15), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0, 255, 0));
|
||||
|
||||
imshow(kWinName, frame);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void decode(const Mat& scores, const Mat& geometry, float scoreThresh,
|
||||
std::vector<RotatedRect>& detections, std::vector<float>& confidences)
|
||||
{
|
||||
detections.clear();
|
||||
CV_Assert(scores.dims == 4, geometry.dims == 4, scores.size[0] == 1,
|
||||
geometry.size[0] == 1, scores.size[1] == 1, geometry.size[1] == 5,
|
||||
scores.size[2] == geometry.size[2], scores.size[3] == geometry.size[3]);
|
||||
|
||||
const int height = scores.size[2];
|
||||
const int width = scores.size[3];
|
||||
for (int y = 0; y < height; ++y)
|
||||
{
|
||||
const float* scoresData = scores.ptr<float>(0, 0, y);
|
||||
const float* x0_data = geometry.ptr<float>(0, 0, y);
|
||||
const float* x1_data = geometry.ptr<float>(0, 1, y);
|
||||
const float* x2_data = geometry.ptr<float>(0, 2, y);
|
||||
const float* x3_data = geometry.ptr<float>(0, 3, y);
|
||||
const float* anglesData = geometry.ptr<float>(0, 4, y);
|
||||
for (int x = 0; x < width; ++x)
|
||||
{
|
||||
float score = scoresData[x];
|
||||
if (score < scoreThresh)
|
||||
continue;
|
||||
|
||||
// Decode a prediction.
|
||||
// Multiple by 4 because feature maps are 4 time less than input image.
|
||||
float offsetX = x * 4.0f, offsetY = y * 4.0f;
|
||||
float angle = anglesData[x];
|
||||
float cosA = std::cos(angle);
|
||||
float sinA = std::sin(angle);
|
||||
float h = x0_data[x] + x2_data[x];
|
||||
float w = x1_data[x] + x3_data[x];
|
||||
|
||||
Point2f offset(offsetX + cosA * x1_data[x] + sinA * x2_data[x],
|
||||
offsetY - sinA * x1_data[x] + cosA * x2_data[x]);
|
||||
Point2f p1 = Point2f(-sinA * h, -cosA * h) + offset;
|
||||
Point2f p3 = Point2f(-cosA * w, sinA * w) + offset;
|
||||
RotatedRect r(0.5f * (p1 + p3), Size2f(w, h), -angle * 180.0f / (float)CV_PI);
|
||||
detections.push_back(r);
|
||||
confidences.push_back(score);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,159 @@
|
||||
import java.awt.BorderLayout;
|
||||
import java.awt.Container;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.awt.image.DataBufferByte;
|
||||
|
||||
import javax.swing.BoxLayout;
|
||||
import javax.swing.ImageIcon;
|
||||
import javax.swing.JComboBox;
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.JSlider;
|
||||
import javax.swing.event.ChangeEvent;
|
||||
import javax.swing.event.ChangeListener;
|
||||
|
||||
import org.opencv.core.Core;
|
||||
import org.opencv.core.Mat;
|
||||
import org.opencv.core.Point;
|
||||
import org.opencv.core.Size;
|
||||
import org.opencv.imgcodecs.Imgcodecs;
|
||||
import org.opencv.imgproc.Imgproc;
|
||||
|
||||
public class MorphologyDemo1 {
|
||||
private static final String[] ELEMENT_TYPE = { "Rectangle", "Cross", "Ellipse" };
|
||||
private static final String[] MORPH_OP = { "Erosion", "Dilatation" };
|
||||
private static final int MAX_KERNEL_SIZE = 21;
|
||||
private Mat matImgSrc;
|
||||
private Mat matImgDst = new Mat();
|
||||
private int elementType = Imgproc.CV_SHAPE_RECT;
|
||||
private int kernelSize = 0;
|
||||
private boolean doErosion = true;
|
||||
private JFrame frame;
|
||||
private JLabel imgLabel;
|
||||
|
||||
public MorphologyDemo1(String[] args) {
|
||||
String imagePath = args.length > 0 ? args[0] : "../data/LinuxLogo.jpg";
|
||||
matImgSrc = Imgcodecs.imread(imagePath);
|
||||
if (matImgSrc.empty()) {
|
||||
System.out.println("Empty image: " + imagePath);
|
||||
System.exit(0);
|
||||
}
|
||||
|
||||
// Create and set up the window.
|
||||
frame = new JFrame("Erosion and dilatation demo");
|
||||
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
// Set up the content pane.
|
||||
BufferedImage img = toBufferedImage(matImgSrc);
|
||||
addComponentsToPane(frame.getContentPane(), img);
|
||||
// Use the content pane's default BorderLayout. No need for
|
||||
// setLayout(new BorderLayout());
|
||||
// Display the window.
|
||||
frame.pack();
|
||||
frame.setVisible(true);
|
||||
}
|
||||
|
||||
private void addComponentsToPane(Container pane, BufferedImage img) {
|
||||
if (!(pane.getLayout() instanceof BorderLayout)) {
|
||||
pane.add(new JLabel("Container doesn't use BorderLayout!"));
|
||||
return;
|
||||
}
|
||||
|
||||
JPanel sliderPanel = new JPanel();
|
||||
sliderPanel.setLayout(new BoxLayout(sliderPanel, BoxLayout.PAGE_AXIS));
|
||||
|
||||
JComboBox<String> elementTypeBox = new JComboBox<>(ELEMENT_TYPE);
|
||||
elementTypeBox.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
@SuppressWarnings("unchecked")
|
||||
JComboBox<String> cb = (JComboBox<String>)e.getSource();
|
||||
if (cb.getSelectedIndex() == 0) {
|
||||
elementType = Imgproc.CV_SHAPE_RECT;
|
||||
} else if (cb.getSelectedIndex() == 1) {
|
||||
elementType = Imgproc.CV_SHAPE_CROSS;
|
||||
} else if (cb.getSelectedIndex() == 2) {
|
||||
elementType = Imgproc.CV_SHAPE_ELLIPSE;
|
||||
}
|
||||
update();
|
||||
}
|
||||
});
|
||||
sliderPanel.add(elementTypeBox);
|
||||
|
||||
sliderPanel.add(new JLabel("Kernel size: 2n + 1"));
|
||||
JSlider slider = new JSlider(0, MAX_KERNEL_SIZE, 0);
|
||||
slider.setMajorTickSpacing(5);
|
||||
slider.setMinorTickSpacing(5);
|
||||
slider.setPaintTicks(true);
|
||||
slider.setPaintLabels(true);
|
||||
slider.addChangeListener(new ChangeListener() {
|
||||
@Override
|
||||
public void stateChanged(ChangeEvent e) {
|
||||
JSlider source = (JSlider) e.getSource();
|
||||
kernelSize = source.getValue();
|
||||
update();
|
||||
}
|
||||
});
|
||||
sliderPanel.add(slider);
|
||||
|
||||
JComboBox<String> morphOpBox = new JComboBox<>(MORPH_OP);
|
||||
morphOpBox.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
@SuppressWarnings("unchecked")
|
||||
JComboBox<String> cb = (JComboBox<String>)e.getSource();
|
||||
doErosion = cb.getSelectedIndex() == 0;
|
||||
update();
|
||||
}
|
||||
});
|
||||
sliderPanel.add(morphOpBox);
|
||||
|
||||
pane.add(sliderPanel, BorderLayout.PAGE_START);
|
||||
imgLabel = new JLabel(new ImageIcon(img));
|
||||
pane.add(imgLabel, BorderLayout.CENTER);
|
||||
}
|
||||
|
||||
private BufferedImage toBufferedImage(Mat matrix) {
|
||||
int type = BufferedImage.TYPE_BYTE_GRAY;
|
||||
if (matrix.channels() > 1) {
|
||||
type = BufferedImage.TYPE_3BYTE_BGR;
|
||||
}
|
||||
int bufferSize = matrix.channels() * matrix.cols() * matrix.rows();
|
||||
byte[] buffer = new byte[bufferSize];
|
||||
matrix.get(0, 0, buffer); // get all the pixels
|
||||
BufferedImage image = new BufferedImage(matrix.cols(), matrix.rows(), type);
|
||||
final byte[] targetPixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
|
||||
System.arraycopy(buffer, 0, targetPixels, 0, buffer.length);
|
||||
return image;
|
||||
}
|
||||
|
||||
private void update() {
|
||||
Mat element = Imgproc.getStructuringElement(elementType, new Size(2 * kernelSize + 1, 2 * kernelSize + 1),
|
||||
new Point(kernelSize, kernelSize));
|
||||
|
||||
if (doErosion) {
|
||||
Imgproc.erode(matImgSrc, matImgDst, element);
|
||||
} else {
|
||||
Imgproc.dilate(matImgSrc, matImgDst, element);
|
||||
}
|
||||
BufferedImage img = toBufferedImage(matImgDst);
|
||||
imgLabel.setIcon(new ImageIcon(img));
|
||||
frame.repaint();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
// Load the native OpenCV library
|
||||
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
|
||||
|
||||
// Schedule a job for the event dispatch thread:
|
||||
// creating and showing this application's GUI.
|
||||
javax.swing.SwingUtilities.invokeLater(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
new MorphologyDemo1(args);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,157 @@
|
||||
import java.awt.BorderLayout;
|
||||
import java.awt.Container;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.awt.image.DataBufferByte;
|
||||
|
||||
import javax.swing.BoxLayout;
|
||||
import javax.swing.ImageIcon;
|
||||
import javax.swing.JComboBox;
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.JSlider;
|
||||
import javax.swing.event.ChangeEvent;
|
||||
import javax.swing.event.ChangeListener;
|
||||
|
||||
import org.opencv.core.Core;
|
||||
import org.opencv.core.Mat;
|
||||
import org.opencv.core.Point;
|
||||
import org.opencv.core.Size;
|
||||
import org.opencv.imgcodecs.Imgcodecs;
|
||||
import org.opencv.imgproc.Imgproc;
|
||||
|
||||
public class MorphologyDemo2 {
|
||||
private static final String[] MORPH_OP = { "Opening", "Closing", "Gradient", "Top Hat", "Black Hat" };
|
||||
private static final int[] MORPH_OP_TYPE = { Imgproc.MORPH_OPEN, Imgproc.MORPH_CLOSE,
|
||||
Imgproc.MORPH_GRADIENT, Imgproc.MORPH_TOPHAT, Imgproc.MORPH_BLACKHAT };
|
||||
private static final String[] ELEMENT_TYPE = { "Rectangle", "Cross", "Ellipse" };
|
||||
private static final int MAX_KERNEL_SIZE = 21;
|
||||
private Mat matImgSrc;
|
||||
private Mat matImgDst = new Mat();
|
||||
private int morphOpType = Imgproc.MORPH_OPEN;
|
||||
private int elementType = Imgproc.CV_SHAPE_RECT;
|
||||
private int kernelSize = 0;
|
||||
private JFrame frame;
|
||||
private JLabel imgLabel;
|
||||
|
||||
public MorphologyDemo2(String[] args) {
|
||||
String imagePath = args.length > 0 ? args[0] : "../data/LinuxLogo.jpg";
|
||||
matImgSrc = Imgcodecs.imread(imagePath);
|
||||
if (matImgSrc.empty()) {
|
||||
System.out.println("Empty image: " + imagePath);
|
||||
System.exit(0);
|
||||
}
|
||||
|
||||
// Create and set up the window.
|
||||
frame = new JFrame("Morphology Transformations demo");
|
||||
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
// Set up the content pane.
|
||||
BufferedImage img = toBufferedImage(matImgSrc);
|
||||
addComponentsToPane(frame.getContentPane(), img);
|
||||
// Use the content pane's default BorderLayout. No need for
|
||||
// setLayout(new BorderLayout());
|
||||
// Display the window.
|
||||
frame.pack();
|
||||
frame.setVisible(true);
|
||||
}
|
||||
|
||||
private void addComponentsToPane(Container pane, BufferedImage img) {
|
||||
if (!(pane.getLayout() instanceof BorderLayout)) {
|
||||
pane.add(new JLabel("Container doesn't use BorderLayout!"));
|
||||
return;
|
||||
}
|
||||
|
||||
JPanel sliderPanel = new JPanel();
|
||||
sliderPanel.setLayout(new BoxLayout(sliderPanel, BoxLayout.PAGE_AXIS));
|
||||
|
||||
JComboBox<String> morphOpBox = new JComboBox<>(MORPH_OP);
|
||||
morphOpBox.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
@SuppressWarnings("unchecked")
|
||||
JComboBox<String> cb = (JComboBox<String>)e.getSource();
|
||||
morphOpType = MORPH_OP_TYPE[cb.getSelectedIndex()];
|
||||
update();
|
||||
}
|
||||
});
|
||||
sliderPanel.add(morphOpBox);
|
||||
|
||||
JComboBox<String> elementTypeBox = new JComboBox<>(ELEMENT_TYPE);
|
||||
elementTypeBox.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
@SuppressWarnings("unchecked")
|
||||
JComboBox<String> cb = (JComboBox<String>)e.getSource();
|
||||
if (cb.getSelectedIndex() == 0) {
|
||||
elementType = Imgproc.CV_SHAPE_RECT;
|
||||
} else if (cb.getSelectedIndex() == 1) {
|
||||
elementType = Imgproc.CV_SHAPE_CROSS;
|
||||
} else if (cb.getSelectedIndex() == 2) {
|
||||
elementType = Imgproc.CV_SHAPE_ELLIPSE;
|
||||
}
|
||||
update();
|
||||
}
|
||||
});
|
||||
sliderPanel.add(elementTypeBox);
|
||||
|
||||
sliderPanel.add(new JLabel("Kernel size: 2n + 1"));
|
||||
JSlider slider = new JSlider(0, MAX_KERNEL_SIZE, 0);
|
||||
slider.setMajorTickSpacing(5);
|
||||
slider.setMinorTickSpacing(5);
|
||||
slider.setPaintTicks(true);
|
||||
slider.setPaintLabels(true);
|
||||
slider.addChangeListener(new ChangeListener() {
|
||||
@Override
|
||||
public void stateChanged(ChangeEvent e) {
|
||||
JSlider source = (JSlider) e.getSource();
|
||||
kernelSize = source.getValue();
|
||||
update();
|
||||
}
|
||||
});
|
||||
sliderPanel.add(slider);
|
||||
|
||||
pane.add(sliderPanel, BorderLayout.PAGE_START);
|
||||
imgLabel = new JLabel(new ImageIcon(img));
|
||||
pane.add(imgLabel, BorderLayout.CENTER);
|
||||
}
|
||||
|
||||
private BufferedImage toBufferedImage(Mat matrix) {
|
||||
int type = BufferedImage.TYPE_BYTE_GRAY;
|
||||
if (matrix.channels() > 1) {
|
||||
type = BufferedImage.TYPE_3BYTE_BGR;
|
||||
}
|
||||
int bufferSize = matrix.channels() * matrix.cols() * matrix.rows();
|
||||
byte[] buffer = new byte[bufferSize];
|
||||
matrix.get(0, 0, buffer); // get all the pixels
|
||||
BufferedImage image = new BufferedImage(matrix.cols(), matrix.rows(), type);
|
||||
final byte[] targetPixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
|
||||
System.arraycopy(buffer, 0, targetPixels, 0, buffer.length);
|
||||
return image;
|
||||
}
|
||||
|
||||
private void update() {
|
||||
Mat element = Imgproc.getStructuringElement(elementType, new Size(2 * kernelSize + 1, 2 * kernelSize + 1),
|
||||
new Point(kernelSize, kernelSize));
|
||||
|
||||
Imgproc.morphologyEx(matImgSrc, matImgDst, morphOpType, element);
|
||||
BufferedImage img = toBufferedImage(matImgDst);
|
||||
imgLabel.setIcon(new ImageIcon(img));
|
||||
frame.repaint();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
// Load the native OpenCV library
|
||||
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
|
||||
|
||||
// Schedule a job for the event dispatch thread:
|
||||
// creating and showing this application's GUI.
|
||||
javax.swing.SwingUtilities.invokeLater(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
new MorphologyDemo2(args);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
import java.awt.BorderLayout;
|
||||
import java.awt.Container;
|
||||
import java.awt.Image;
|
||||
|
||||
import javax.swing.BoxLayout;
|
||||
import javax.swing.ImageIcon;
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.JSlider;
|
||||
import javax.swing.event.ChangeEvent;
|
||||
import javax.swing.event.ChangeListener;
|
||||
|
||||
import org.opencv.core.Core;
|
||||
import org.opencv.core.Mat;
|
||||
import org.opencv.highgui.HighGui;
|
||||
import org.opencv.imgcodecs.Imgcodecs;
|
||||
|
||||
public class AddingImagesTrackbar {
|
||||
private static final int ALPHA_SLIDER_MAX = 100;
|
||||
private int alphaVal = 0;
|
||||
private Mat matImgSrc1;
|
||||
private Mat matImgSrc2;
|
||||
private Mat matImgDst = new Mat();
|
||||
private JFrame frame;
|
||||
private JLabel imgLabel;
|
||||
|
||||
public AddingImagesTrackbar(String[] args) {
|
||||
//! [load]
|
||||
String imagePath1 = "../data/LinuxLogo.jpg";
|
||||
String imagePath2 = "../data/WindowsLogo.jpg";
|
||||
if (args.length > 1) {
|
||||
imagePath1 = args[0];
|
||||
imagePath2 = args[1];
|
||||
}
|
||||
matImgSrc1 = Imgcodecs.imread(imagePath1);
|
||||
matImgSrc2 = Imgcodecs.imread(imagePath2);
|
||||
//! [load]
|
||||
if (matImgSrc1.empty()) {
|
||||
System.out.println("Empty image: " + imagePath1);
|
||||
System.exit(0);
|
||||
}
|
||||
if (matImgSrc2.empty()) {
|
||||
System.out.println("Empty image: " + imagePath2);
|
||||
System.exit(0);
|
||||
}
|
||||
|
||||
//! [window]
|
||||
// Create and set up the window.
|
||||
frame = new JFrame("Linear Blend");
|
||||
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
// Set up the content pane.
|
||||
Image img = HighGui.toBufferedImage(matImgSrc2);
|
||||
addComponentsToPane(frame.getContentPane(), img);
|
||||
// Use the content pane's default BorderLayout. No need for
|
||||
// setLayout(new BorderLayout());
|
||||
// Display the window.
|
||||
frame.pack();
|
||||
frame.setVisible(true);
|
||||
//! [window]
|
||||
}
|
||||
|
||||
private void addComponentsToPane(Container pane, Image img) {
|
||||
if (!(pane.getLayout() instanceof BorderLayout)) {
|
||||
pane.add(new JLabel("Container doesn't use BorderLayout!"));
|
||||
return;
|
||||
}
|
||||
|
||||
JPanel sliderPanel = new JPanel();
|
||||
sliderPanel.setLayout(new BoxLayout(sliderPanel, BoxLayout.PAGE_AXIS));
|
||||
|
||||
//! [create_trackbar]
|
||||
sliderPanel.add(new JLabel(String.format("Alpha x %d", ALPHA_SLIDER_MAX)));
|
||||
JSlider slider = new JSlider(0, ALPHA_SLIDER_MAX, 0);
|
||||
slider.setMajorTickSpacing(20);
|
||||
slider.setMinorTickSpacing(5);
|
||||
slider.setPaintTicks(true);
|
||||
slider.setPaintLabels(true);
|
||||
//! [create_trackbar]
|
||||
//! [on_trackbar]
|
||||
slider.addChangeListener(new ChangeListener() {
|
||||
@Override
|
||||
public void stateChanged(ChangeEvent e) {
|
||||
JSlider source = (JSlider) e.getSource();
|
||||
alphaVal = source.getValue();
|
||||
update();
|
||||
}
|
||||
});
|
||||
//! [on_trackbar]
|
||||
sliderPanel.add(slider);
|
||||
|
||||
pane.add(sliderPanel, BorderLayout.PAGE_START);
|
||||
imgLabel = new JLabel(new ImageIcon(img));
|
||||
pane.add(imgLabel, BorderLayout.CENTER);
|
||||
}
|
||||
|
||||
private void update() {
|
||||
double alpha = alphaVal / (double) ALPHA_SLIDER_MAX;
|
||||
double beta = 1.0 - alpha;
|
||||
Core.addWeighted(matImgSrc1, alpha, matImgSrc2, beta, 0, matImgDst);
|
||||
Image img = HighGui.toBufferedImage(matImgDst);
|
||||
imgLabel.setIcon(new ImageIcon(img));
|
||||
frame.repaint();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
// Load the native OpenCV library
|
||||
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
|
||||
|
||||
// Schedule a job for the event dispatch thread:
|
||||
// creating and showing this application's GUI.
|
||||
javax.swing.SwingUtilities.invokeLater(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
new AddingImagesTrackbar(args);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
from __future__ import print_function
|
||||
from __future__ import division
|
||||
import cv2 as cv
|
||||
import argparse
|
||||
|
||||
alpha_slider_max = 100
|
||||
title_window = 'Linear Blend'
|
||||
|
||||
## [on_trackbar]
|
||||
def on_trackbar(val):
|
||||
alpha = val / alpha_slider_max
|
||||
beta = ( 1.0 - alpha )
|
||||
dst = cv.addWeighted(src1, alpha, src2, beta, 0.0)
|
||||
cv.imshow(title_window, dst)
|
||||
## [on_trackbar]
|
||||
|
||||
parser = argparse.ArgumentParser(description='Code for Adding a Trackbar to our applications tutorial.')
|
||||
parser.add_argument('--input1', help='Path to the first input image.', default='../data/LinuxLogo.jpg')
|
||||
parser.add_argument('--input2', help='Path to the second input image.', default='../data/WindowsLogo.jpg')
|
||||
args = parser.parse_args()
|
||||
|
||||
## [load]
|
||||
# Read images ( both have to be of the same size and type )
|
||||
src1 = cv.imread(args.input1)
|
||||
src2 = cv.imread(args.input2)
|
||||
## [load]
|
||||
if src1 is None:
|
||||
print('Could not open or find the image: ', args.input1)
|
||||
exit(0)
|
||||
|
||||
if src2 is None:
|
||||
print('Could not open or find the image: ', args.input2)
|
||||
exit(0)
|
||||
|
||||
## [window]
|
||||
cv.namedWindow(title_window)
|
||||
## [window]
|
||||
|
||||
## [create_trackbar]
|
||||
trackbar_name = 'Alpha x %d' % alpha_slider_max
|
||||
cv.createTrackbar(trackbar_name, title_window , 0, alpha_slider_max, on_trackbar)
|
||||
## [create_trackbar]
|
||||
|
||||
# Show some stuff
|
||||
on_trackbar(0)
|
||||
|
||||
# Wait until user press some key
|
||||
cv.waitKey()
|
||||
@@ -0,0 +1,63 @@
|
||||
from __future__ import print_function
|
||||
import cv2 as cv
|
||||
import numpy as np
|
||||
import argparse
|
||||
|
||||
erosion_size = 0
|
||||
max_elem = 2
|
||||
max_kernel_size = 21
|
||||
title_trackbar_element_type = 'Element:\n 0: Rect \n 1: Cross \n 2: Ellipse'
|
||||
title_trackbar_kernel_size = 'Kernel size:\n 2n +1'
|
||||
title_erosion_window = 'Erosion Demo'
|
||||
title_dilatation_window = 'Dilation Demo'
|
||||
|
||||
def erosion(val):
|
||||
erosion_size = cv.getTrackbarPos(title_trackbar_kernel_size, title_erosion_window)
|
||||
erosion_type = 0
|
||||
val_type = cv.getTrackbarPos(title_trackbar_element_type, title_erosion_window)
|
||||
if val_type == 0:
|
||||
erosion_type = cv.MORPH_RECT
|
||||
elif val_type == 1:
|
||||
erosion_type = cv.MORPH_CROSS
|
||||
elif val_type == 2:
|
||||
erosion_type = cv.MORPH_ELLIPSE
|
||||
|
||||
element = cv.getStructuringElement(erosion_type, (2*erosion_size + 1, 2*erosion_size+1), (erosion_size, erosion_size))
|
||||
erosion_dst = cv.erode(src, element)
|
||||
cv.imshow(title_erosion_window, erosion_dst)
|
||||
|
||||
def dilatation(val):
|
||||
dilatation_size = cv.getTrackbarPos(title_trackbar_kernel_size, title_dilatation_window)
|
||||
dilatation_type = 0
|
||||
val_type = cv.getTrackbarPos(title_trackbar_element_type, title_dilatation_window)
|
||||
if val_type == 0:
|
||||
dilatation_type = cv.MORPH_RECT
|
||||
elif val_type == 1:
|
||||
dilatation_type = cv.MORPH_CROSS
|
||||
elif val_type == 2:
|
||||
dilatation_type = cv.MORPH_ELLIPSE
|
||||
|
||||
element = cv.getStructuringElement(dilatation_type, (2*dilatation_size + 1, 2*dilatation_size+1), (dilatation_size, dilatation_size))
|
||||
dilatation_dst = cv.dilate(src, element)
|
||||
cv.imshow(title_dilatation_window, dilatation_dst)
|
||||
|
||||
parser = argparse.ArgumentParser(description='Code for Eroding and Dilating tutorial.')
|
||||
parser.add_argument('--input', help='Path to input image.', default='../data/LinuxLogo.jpg')
|
||||
args = parser.parse_args()
|
||||
|
||||
src = cv.imread(args.input)
|
||||
if src is None:
|
||||
print('Could not open or find the image: ', args.input)
|
||||
exit(0)
|
||||
|
||||
cv.namedWindow(title_erosion_window)
|
||||
cv.createTrackbar(title_trackbar_element_type, title_erosion_window , 0, max_elem, erosion)
|
||||
cv.createTrackbar(title_trackbar_kernel_size, title_erosion_window , 0, max_kernel_size, erosion)
|
||||
|
||||
cv.namedWindow(title_dilatation_window)
|
||||
cv.createTrackbar(title_trackbar_element_type, title_dilatation_window , 0, max_elem, dilatation)
|
||||
cv.createTrackbar(title_trackbar_kernel_size, title_dilatation_window , 0, max_kernel_size, dilatation)
|
||||
|
||||
erosion(0)
|
||||
dilatation(0)
|
||||
cv.waitKey()
|
||||
@@ -0,0 +1,48 @@
|
||||
from __future__ import print_function
|
||||
import cv2 as cv
|
||||
import numpy as np
|
||||
import argparse
|
||||
|
||||
morph_size = 0
|
||||
max_operator = 4
|
||||
max_elem = 2
|
||||
max_kernel_size = 21
|
||||
title_trackbar_operator_type = 'Operator:\n 0: Opening - 1: Closing \n 2: Gradient - 3: Top Hat \n 4: Black Hat'
|
||||
title_trackbar_element_type = 'Element:\n 0: Rect - 1: Cross - 2: Ellipse'
|
||||
title_trackbar_kernel_size = 'Kernel size:\n 2n + 1'
|
||||
title_window = 'Morphology Transformations Demo'
|
||||
morph_op_dic = {0: cv.MORPH_OPEN, 1: cv.MORPH_CLOSE, 2: cv.MORPH_GRADIENT, 3: cv.MORPH_TOPHAT, 4: cv.MORPH_BLACKHAT}
|
||||
|
||||
def morphology_operations(val):
|
||||
morph_operator = cv.getTrackbarPos(title_trackbar_operator_type, title_window)
|
||||
morph_size = cv.getTrackbarPos(title_trackbar_kernel_size, title_window)
|
||||
morph_elem = 0
|
||||
val_type = cv.getTrackbarPos(title_trackbar_element_type, title_window)
|
||||
if val_type == 0:
|
||||
morph_elem = cv.MORPH_RECT
|
||||
elif val_type == 1:
|
||||
morph_elem = cv.MORPH_CROSS
|
||||
elif val_type == 2:
|
||||
morph_elem = cv.MORPH_ELLIPSE
|
||||
|
||||
element = cv.getStructuringElement(morph_elem, (2*morph_size + 1, 2*morph_size+1), (morph_size, morph_size))
|
||||
operation = morph_op_dic[morph_operator]
|
||||
dst = cv.morphologyEx(src, operation, element)
|
||||
cv.imshow(title_window, dst)
|
||||
|
||||
parser = argparse.ArgumentParser(description='Code for More Morphology Transformations tutorial.')
|
||||
parser.add_argument('--input', help='Path to input image.', default='../data/LinuxLogo.jpg')
|
||||
args = parser.parse_args()
|
||||
|
||||
src = cv.imread(args.input)
|
||||
if src is None:
|
||||
print('Could not open or find the image: ', args.input)
|
||||
exit(0)
|
||||
|
||||
cv.namedWindow(title_window)
|
||||
cv.createTrackbar(title_trackbar_operator_type, title_window , 0, max_operator, morphology_operations)
|
||||
cv.createTrackbar(title_trackbar_element_type, title_window , 0, max_elem, morphology_operations)
|
||||
cv.createTrackbar(title_trackbar_kernel_size, title_window , 0, max_kernel_size, morphology_operations)
|
||||
|
||||
morphology_operations(0)
|
||||
cv.waitKey()
|
||||
Reference in New Issue
Block a user