mirror of
https://gitcode.com/gh_mirrors/es/esp32-opencv.git
synced 2025-08-14 18:50:49 +08:00
initial commit
This commit is contained in:
57
samples/cpp/tutorial_code/core/AddingImages/AddingImages.cpp
Normal file
57
samples/cpp/tutorial_code/core/AddingImages/AddingImages.cpp
Normal file
@ -0,0 +1,57 @@
|
||||
/**
|
||||
* @file AddingImages.cpp
|
||||
* @brief Simple linear blender ( dst = alpha*src1 + beta*src2 )
|
||||
* @author OpenCV team
|
||||
*/
|
||||
#include "opencv2/imgcodecs.hpp"
|
||||
#include "opencv2/highgui.hpp"
|
||||
#include <iostream>
|
||||
|
||||
using namespace cv;
|
||||
|
||||
// we're NOT "using namespace std;" here, to avoid collisions between the beta variable and std::beta in c++17
|
||||
using std::cin;
|
||||
using std::cout;
|
||||
using std::endl;
|
||||
|
||||
/**
|
||||
* @function main
|
||||
* @brief Main function
|
||||
*/
|
||||
int main( void )
|
||||
{
|
||||
double alpha = 0.5; double beta; double input;
|
||||
|
||||
Mat src1, src2, dst;
|
||||
|
||||
/// Ask the user enter alpha
|
||||
cout << " Simple Linear Blender " << endl;
|
||||
cout << "-----------------------" << endl;
|
||||
cout << "* Enter alpha [0.0-1.0]: ";
|
||||
cin >> input;
|
||||
|
||||
// We use the alpha provided by the user if it is between 0 and 1
|
||||
if( input >= 0 && input <= 1 )
|
||||
{ alpha = input; }
|
||||
|
||||
//![load]
|
||||
/// Read images ( both have to be of the same size and type )
|
||||
src1 = imread( samples::findFile("LinuxLogo.jpg") );
|
||||
src2 = imread( samples::findFile("WindowsLogo.jpg") );
|
||||
//![load]
|
||||
|
||||
if( src1.empty() ) { cout << "Error loading src1" << endl; return EXIT_FAILURE; }
|
||||
if( src2.empty() ) { cout << "Error loading src2" << endl; return EXIT_FAILURE; }
|
||||
|
||||
//![blend_images]
|
||||
beta = ( 1.0 - alpha );
|
||||
addWeighted( src1, alpha, src2, beta, 0.0, dst);
|
||||
//![blend_images]
|
||||
|
||||
//![display]
|
||||
imshow( "Linear Blend", dst );
|
||||
waitKey(0);
|
||||
//![display]
|
||||
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,95 @@
|
||||
#include "opencv2/core.hpp"
|
||||
#include "opencv2/imgproc.hpp"
|
||||
#include "opencv2/imgcodecs.hpp"
|
||||
#include "opencv2/highgui.hpp"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
using namespace cv;
|
||||
using namespace std;
|
||||
|
||||
static void help(char ** argv)
|
||||
{
|
||||
cout << endl
|
||||
<< "This program demonstrated the use of the discrete Fourier transform (DFT). " << endl
|
||||
<< "The dft of an image is taken and it's power spectrum is displayed." << endl << endl
|
||||
<< "Usage:" << endl
|
||||
<< argv[0] << " [image_name -- default lena.jpg]" << endl << endl;
|
||||
}
|
||||
|
||||
int main(int argc, char ** argv)
|
||||
{
|
||||
help(argv);
|
||||
|
||||
const char* filename = argc >=2 ? argv[1] : "lena.jpg";
|
||||
|
||||
Mat I = imread( samples::findFile( filename ), IMREAD_GRAYSCALE);
|
||||
if( I.empty()){
|
||||
cout << "Error opening image" << endl;
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
//! [expand]
|
||||
Mat padded; //expand input image to optimal size
|
||||
int m = getOptimalDFTSize( I.rows );
|
||||
int n = getOptimalDFTSize( I.cols ); // on the border add zero values
|
||||
copyMakeBorder(I, padded, 0, m - I.rows, 0, n - I.cols, BORDER_CONSTANT, Scalar::all(0));
|
||||
//! [expand]
|
||||
|
||||
//! [complex_and_real]
|
||||
Mat planes[] = {Mat_<float>(padded), Mat::zeros(padded.size(), CV_32F)};
|
||||
Mat complexI;
|
||||
merge(planes, 2, complexI); // Add to the expanded another plane with zeros
|
||||
//! [complex_and_real]
|
||||
|
||||
//! [dft]
|
||||
dft(complexI, complexI); // this way the result may fit in the source matrix
|
||||
//! [dft]
|
||||
|
||||
// compute the magnitude and switch to logarithmic scale
|
||||
// => log(1 + sqrt(Re(DFT(I))^2 + Im(DFT(I))^2))
|
||||
//! [magnitude]
|
||||
split(complexI, planes); // planes[0] = Re(DFT(I), planes[1] = Im(DFT(I))
|
||||
magnitude(planes[0], planes[1], planes[0]);// planes[0] = magnitude
|
||||
Mat magI = planes[0];
|
||||
//! [magnitude]
|
||||
|
||||
//! [log]
|
||||
magI += Scalar::all(1); // switch to logarithmic scale
|
||||
log(magI, magI);
|
||||
//! [log]
|
||||
|
||||
//! [crop_rearrange]
|
||||
// crop the spectrum, if it has an odd number of rows or columns
|
||||
magI = magI(Rect(0, 0, magI.cols & -2, magI.rows & -2));
|
||||
|
||||
// rearrange the quadrants of Fourier image so that the origin is at the image center
|
||||
int cx = magI.cols/2;
|
||||
int cy = magI.rows/2;
|
||||
|
||||
Mat q0(magI, Rect(0, 0, cx, cy)); // Top-Left - Create a ROI per quadrant
|
||||
Mat q1(magI, Rect(cx, 0, cx, cy)); // Top-Right
|
||||
Mat q2(magI, Rect(0, cy, cx, cy)); // Bottom-Left
|
||||
Mat q3(magI, Rect(cx, cy, cx, cy)); // Bottom-Right
|
||||
|
||||
Mat tmp; // swap quadrants (Top-Left with Bottom-Right)
|
||||
q0.copyTo(tmp);
|
||||
q3.copyTo(q0);
|
||||
tmp.copyTo(q3);
|
||||
|
||||
q1.copyTo(tmp); // swap quadrant (Top-Right with Bottom-Left)
|
||||
q2.copyTo(q1);
|
||||
tmp.copyTo(q2);
|
||||
//! [crop_rearrange]
|
||||
|
||||
//! [normalize]
|
||||
normalize(magI, magI, 0, 1, NORM_MINMAX); // Transform the matrix with float values into a
|
||||
// viewable image form (float between values 0 and 1).
|
||||
//! [normalize]
|
||||
|
||||
imshow("Input Image" , I ); // Show the result
|
||||
imshow("spectrum magnitude", magI);
|
||||
waitKey();
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
@ -0,0 +1,191 @@
|
||||
#include <opencv2/core.hpp>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
using namespace cv;
|
||||
using namespace std;
|
||||
|
||||
static void help(char** av)
|
||||
{
|
||||
cout << endl
|
||||
<< av[0] << " shows the usage of the OpenCV serialization functionality." << endl
|
||||
<< "usage: " << endl
|
||||
<< av[0] << " outputfile.yml.gz" << endl
|
||||
<< "The output file may be either XML (xml) or YAML (yml/yaml). You can even compress it by "
|
||||
<< "specifying this in its extension like xml.gz yaml.gz etc... " << endl
|
||||
<< "With FileStorage you can serialize objects in OpenCV by using the << and >> operators" << endl
|
||||
<< "For example: - create a class and have it serialized" << endl
|
||||
<< " - use it to read and write matrices." << endl;
|
||||
}
|
||||
|
||||
class MyData
|
||||
{
|
||||
public:
|
||||
MyData() : A(0), X(0), id()
|
||||
{}
|
||||
explicit MyData(int) : A(97), X(CV_PI), id("mydata1234") // explicit to avoid implicit conversion
|
||||
{}
|
||||
//! [inside]
|
||||
void write(FileStorage& fs) const //Write serialization for this class
|
||||
{
|
||||
fs << "{" << "A" << A << "X" << X << "id" << id << "}";
|
||||
}
|
||||
void read(const FileNode& node) //Read serialization for this class
|
||||
{
|
||||
A = (int)node["A"];
|
||||
X = (double)node["X"];
|
||||
id = (string)node["id"];
|
||||
}
|
||||
//! [inside]
|
||||
public: // Data Members
|
||||
int A;
|
||||
double X;
|
||||
string id;
|
||||
};
|
||||
|
||||
//These write and read functions must be defined for the serialization in FileStorage to work
|
||||
//! [outside]
|
||||
static void write(FileStorage& fs, const std::string&, const MyData& x)
|
||||
{
|
||||
x.write(fs);
|
||||
}
|
||||
static void read(const FileNode& node, MyData& x, const MyData& default_value = MyData()){
|
||||
if(node.empty())
|
||||
x = default_value;
|
||||
else
|
||||
x.read(node);
|
||||
}
|
||||
//! [outside]
|
||||
|
||||
// This function will print our custom class to the console
|
||||
static ostream& operator<<(ostream& out, const MyData& m)
|
||||
{
|
||||
out << "{ id = " << m.id << ", ";
|
||||
out << "X = " << m.X << ", ";
|
||||
out << "A = " << m.A << "}";
|
||||
return out;
|
||||
}
|
||||
|
||||
int main(int ac, char** av)
|
||||
{
|
||||
if (ac != 2)
|
||||
{
|
||||
help(av);
|
||||
return 1;
|
||||
}
|
||||
|
||||
string filename = av[1];
|
||||
{ //write
|
||||
//! [iomati]
|
||||
Mat R = Mat_<uchar>::eye(3, 3),
|
||||
T = Mat_<double>::zeros(3, 1);
|
||||
//! [iomati]
|
||||
//! [customIOi]
|
||||
MyData m(1);
|
||||
//! [customIOi]
|
||||
|
||||
//! [open]
|
||||
FileStorage fs(filename, FileStorage::WRITE);
|
||||
// or:
|
||||
// FileStorage fs;
|
||||
// fs.open(filename, FileStorage::WRITE);
|
||||
//! [open]
|
||||
|
||||
//! [writeNum]
|
||||
fs << "iterationNr" << 100;
|
||||
//! [writeNum]
|
||||
//! [writeStr]
|
||||
fs << "strings" << "["; // text - string sequence
|
||||
fs << "image1.jpg" << "Awesomeness" << "../data/baboon.jpg";
|
||||
fs << "]"; // close sequence
|
||||
//! [writeStr]
|
||||
|
||||
//! [writeMap]
|
||||
fs << "Mapping"; // text - mapping
|
||||
fs << "{" << "One" << 1;
|
||||
fs << "Two" << 2 << "}";
|
||||
//! [writeMap]
|
||||
|
||||
//! [iomatw]
|
||||
fs << "R" << R; // cv::Mat
|
||||
fs << "T" << T;
|
||||
//! [iomatw]
|
||||
|
||||
//! [customIOw]
|
||||
fs << "MyData" << m; // your own data structures
|
||||
//! [customIOw]
|
||||
|
||||
//! [close]
|
||||
fs.release(); // explicit close
|
||||
//! [close]
|
||||
cout << "Write Done." << endl;
|
||||
}
|
||||
|
||||
{//read
|
||||
cout << endl << "Reading: " << endl;
|
||||
FileStorage fs;
|
||||
fs.open(filename, FileStorage::READ);
|
||||
|
||||
//! [readNum]
|
||||
int itNr;
|
||||
//fs["iterationNr"] >> itNr;
|
||||
itNr = (int) fs["iterationNr"];
|
||||
//! [readNum]
|
||||
cout << itNr;
|
||||
if (!fs.isOpened())
|
||||
{
|
||||
cerr << "Failed to open " << filename << endl;
|
||||
help(av);
|
||||
return 1;
|
||||
}
|
||||
|
||||
//! [readStr]
|
||||
FileNode n = fs["strings"]; // Read string sequence - Get node
|
||||
if (n.type() != FileNode::SEQ)
|
||||
{
|
||||
cerr << "strings is not a sequence! FAIL" << endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
FileNodeIterator it = n.begin(), it_end = n.end(); // Go through the node
|
||||
for (; it != it_end; ++it)
|
||||
cout << (string)*it << endl;
|
||||
//! [readStr]
|
||||
|
||||
|
||||
//! [readMap]
|
||||
n = fs["Mapping"]; // Read mappings from a sequence
|
||||
cout << "Two " << (int)(n["Two"]) << "; ";
|
||||
cout << "One " << (int)(n["One"]) << endl << endl;
|
||||
//! [readMap]
|
||||
|
||||
|
||||
MyData m;
|
||||
Mat R, T;
|
||||
|
||||
//! [iomat]
|
||||
fs["R"] >> R; // Read cv::Mat
|
||||
fs["T"] >> T;
|
||||
//! [iomat]
|
||||
//! [customIO]
|
||||
fs["MyData"] >> m; // Read your own structure_
|
||||
//! [customIO]
|
||||
|
||||
cout << endl
|
||||
<< "R = " << R << endl;
|
||||
cout << "T = " << T << endl << endl;
|
||||
cout << "MyData = " << endl << m << endl << endl;
|
||||
|
||||
//Show default behavior for non existing nodes
|
||||
//! [nonexist]
|
||||
cout << "Attempt to read NonExisting (should initialize the data structure with its default).";
|
||||
fs["NonExisting"] >> m;
|
||||
cout << endl << "NonExisting = " << endl << m << endl;
|
||||
//! [nonexist]
|
||||
}
|
||||
|
||||
cout << endl
|
||||
<< "Tip: Open up " << filename << " with a text editor to see the serialized data." << endl;
|
||||
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,230 @@
|
||||
#include <opencv2/core.hpp>
|
||||
#include <opencv2/core/utility.hpp>
|
||||
#include "opencv2/imgcodecs.hpp"
|
||||
#include <opencv2/highgui.hpp>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
|
||||
using namespace std;
|
||||
using namespace cv;
|
||||
|
||||
static void help()
|
||||
{
|
||||
cout
|
||||
<< "\n--------------------------------------------------------------------------" << endl
|
||||
<< "This program shows how to scan image objects in OpenCV (cv::Mat). As use case"
|
||||
<< " we take an input image and divide the native color palette (255) with the " << endl
|
||||
<< "input. Shows C operator[] method, iterators and at function for on-the-fly item address calculation."<< endl
|
||||
<< "Usage:" << endl
|
||||
<< "./how_to_scan_images <imageNameToUse> <divideWith> [G]" << endl
|
||||
<< "if you add a G parameter the image is processed in gray scale" << endl
|
||||
<< "--------------------------------------------------------------------------" << endl
|
||||
<< endl;
|
||||
}
|
||||
|
||||
Mat& ScanImageAndReduceC(Mat& I, const uchar* table);
|
||||
Mat& ScanImageAndReduceIterator(Mat& I, const uchar* table);
|
||||
Mat& ScanImageAndReduceRandomAccess(Mat& I, const uchar * table);
|
||||
|
||||
int main( int argc, char* argv[])
|
||||
{
|
||||
help();
|
||||
if (argc < 3)
|
||||
{
|
||||
cout << "Not enough parameters" << endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
Mat I, J;
|
||||
if( argc == 4 && !strcmp(argv[3],"G") )
|
||||
I = imread(argv[1], IMREAD_GRAYSCALE);
|
||||
else
|
||||
I = imread(argv[1], IMREAD_COLOR);
|
||||
|
||||
if (I.empty())
|
||||
{
|
||||
cout << "The image" << argv[1] << " could not be loaded." << endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
//! [dividewith]
|
||||
int divideWith = 0; // convert our input string to number - C++ style
|
||||
stringstream s;
|
||||
s << argv[2];
|
||||
s >> divideWith;
|
||||
if (!s || !divideWith)
|
||||
{
|
||||
cout << "Invalid number entered for dividing. " << endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
uchar table[256];
|
||||
for (int i = 0; i < 256; ++i)
|
||||
table[i] = (uchar)(divideWith * (i/divideWith));
|
||||
//! [dividewith]
|
||||
|
||||
const int times = 100;
|
||||
double t;
|
||||
|
||||
t = (double)getTickCount();
|
||||
|
||||
for (int i = 0; i < times; ++i)
|
||||
{
|
||||
cv::Mat clone_i = I.clone();
|
||||
J = ScanImageAndReduceC(clone_i, table);
|
||||
}
|
||||
|
||||
t = 1000*((double)getTickCount() - t)/getTickFrequency();
|
||||
t /= times;
|
||||
|
||||
cout << "Time of reducing with the C operator [] (averaged for "
|
||||
<< times << " runs): " << t << " milliseconds."<< endl;
|
||||
|
||||
t = (double)getTickCount();
|
||||
|
||||
for (int i = 0; i < times; ++i)
|
||||
{
|
||||
cv::Mat clone_i = I.clone();
|
||||
J = ScanImageAndReduceIterator(clone_i, table);
|
||||
}
|
||||
|
||||
t = 1000*((double)getTickCount() - t)/getTickFrequency();
|
||||
t /= times;
|
||||
|
||||
cout << "Time of reducing with the iterator (averaged for "
|
||||
<< times << " runs): " << t << " milliseconds."<< endl;
|
||||
|
||||
t = (double)getTickCount();
|
||||
|
||||
for (int i = 0; i < times; ++i)
|
||||
{
|
||||
cv::Mat clone_i = I.clone();
|
||||
ScanImageAndReduceRandomAccess(clone_i, table);
|
||||
}
|
||||
|
||||
t = 1000*((double)getTickCount() - t)/getTickFrequency();
|
||||
t /= times;
|
||||
|
||||
cout << "Time of reducing with the on-the-fly address generation - at function (averaged for "
|
||||
<< times << " runs): " << t << " milliseconds."<< endl;
|
||||
|
||||
//! [table-init]
|
||||
Mat lookUpTable(1, 256, CV_8U);
|
||||
uchar* p = lookUpTable.ptr();
|
||||
for( int i = 0; i < 256; ++i)
|
||||
p[i] = table[i];
|
||||
//! [table-init]
|
||||
|
||||
t = (double)getTickCount();
|
||||
|
||||
for (int i = 0; i < times; ++i)
|
||||
//! [table-use]
|
||||
LUT(I, lookUpTable, J);
|
||||
//! [table-use]
|
||||
|
||||
t = 1000*((double)getTickCount() - t)/getTickFrequency();
|
||||
t /= times;
|
||||
|
||||
cout << "Time of reducing with the LUT function (averaged for "
|
||||
<< times << " runs): " << t << " milliseconds."<< endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
//! [scan-c]
|
||||
Mat& ScanImageAndReduceC(Mat& I, const uchar* const table)
|
||||
{
|
||||
// accept only char type matrices
|
||||
CV_Assert(I.depth() == CV_8U);
|
||||
|
||||
int channels = I.channels();
|
||||
|
||||
int nRows = I.rows;
|
||||
int nCols = I.cols * channels;
|
||||
|
||||
if (I.isContinuous())
|
||||
{
|
||||
nCols *= nRows;
|
||||
nRows = 1;
|
||||
}
|
||||
|
||||
int i,j;
|
||||
uchar* p;
|
||||
for( i = 0; i < nRows; ++i)
|
||||
{
|
||||
p = I.ptr<uchar>(i);
|
||||
for ( j = 0; j < nCols; ++j)
|
||||
{
|
||||
p[j] = table[p[j]];
|
||||
}
|
||||
}
|
||||
return I;
|
||||
}
|
||||
//! [scan-c]
|
||||
|
||||
//! [scan-iterator]
|
||||
Mat& ScanImageAndReduceIterator(Mat& I, const uchar* const table)
|
||||
{
|
||||
// accept only char type matrices
|
||||
CV_Assert(I.depth() == CV_8U);
|
||||
|
||||
const int channels = I.channels();
|
||||
switch(channels)
|
||||
{
|
||||
case 1:
|
||||
{
|
||||
MatIterator_<uchar> it, end;
|
||||
for( it = I.begin<uchar>(), end = I.end<uchar>(); it != end; ++it)
|
||||
*it = table[*it];
|
||||
break;
|
||||
}
|
||||
case 3:
|
||||
{
|
||||
MatIterator_<Vec3b> it, end;
|
||||
for( it = I.begin<Vec3b>(), end = I.end<Vec3b>(); it != end; ++it)
|
||||
{
|
||||
(*it)[0] = table[(*it)[0]];
|
||||
(*it)[1] = table[(*it)[1]];
|
||||
(*it)[2] = table[(*it)[2]];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return I;
|
||||
}
|
||||
//! [scan-iterator]
|
||||
|
||||
//! [scan-random]
|
||||
Mat& ScanImageAndReduceRandomAccess(Mat& I, const uchar* const table)
|
||||
{
|
||||
// accept only char type matrices
|
||||
CV_Assert(I.depth() == CV_8U);
|
||||
|
||||
const int channels = I.channels();
|
||||
switch(channels)
|
||||
{
|
||||
case 1:
|
||||
{
|
||||
for( int i = 0; i < I.rows; ++i)
|
||||
for( int j = 0; j < I.cols; ++j )
|
||||
I.at<uchar>(i,j) = table[I.at<uchar>(i,j)];
|
||||
break;
|
||||
}
|
||||
case 3:
|
||||
{
|
||||
Mat_<Vec3b> _I = I;
|
||||
|
||||
for( int i = 0; i < I.rows; ++i)
|
||||
for( int j = 0; j < I.cols; ++j )
|
||||
{
|
||||
_I(i,j)[0] = table[_I(i,j)[0]];
|
||||
_I(i,j)[1] = table[_I(i,j)[1]];
|
||||
_I(i,j)[2] = table[_I(i,j)[2]];
|
||||
}
|
||||
I = _I;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return I;
|
||||
}
|
||||
//! [scan-random]
|
@ -0,0 +1,147 @@
|
||||
#include <iostream>
|
||||
#include <opencv2/core.hpp>
|
||||
#include <opencv2/imgcodecs.hpp>
|
||||
|
||||
using namespace std;
|
||||
using namespace cv;
|
||||
|
||||
namespace
|
||||
{
|
||||
//! [mandelbrot-escape-time-algorithm]
|
||||
int mandelbrot(const complex<float> &z0, const int max)
|
||||
{
|
||||
complex<float> z = z0;
|
||||
for (int t = 0; t < max; t++)
|
||||
{
|
||||
if (z.real()*z.real() + z.imag()*z.imag() > 4.0f) return t;
|
||||
z = z*z + z0;
|
||||
}
|
||||
|
||||
return max;
|
||||
}
|
||||
//! [mandelbrot-escape-time-algorithm]
|
||||
|
||||
//! [mandelbrot-grayscale-value]
|
||||
int mandelbrotFormula(const complex<float> &z0, const int maxIter=500) {
|
||||
int value = mandelbrot(z0, maxIter);
|
||||
if(maxIter - value == 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
return cvRound(sqrt(value / (float) maxIter) * 255);
|
||||
}
|
||||
//! [mandelbrot-grayscale-value]
|
||||
|
||||
//! [mandelbrot-parallel]
|
||||
class ParallelMandelbrot : public ParallelLoopBody
|
||||
{
|
||||
public:
|
||||
ParallelMandelbrot (Mat &img, const float x1, const float y1, const float scaleX, const float scaleY)
|
||||
: m_img(img), m_x1(x1), m_y1(y1), m_scaleX(scaleX), m_scaleY(scaleY)
|
||||
{
|
||||
}
|
||||
|
||||
virtual void operator ()(const Range& range) const CV_OVERRIDE
|
||||
{
|
||||
for (int r = range.start; r < range.end; r++)
|
||||
{
|
||||
int i = r / m_img.cols;
|
||||
int j = r % m_img.cols;
|
||||
|
||||
float x0 = j / m_scaleX + m_x1;
|
||||
float y0 = i / m_scaleY + m_y1;
|
||||
|
||||
complex<float> z0(x0, y0);
|
||||
uchar value = (uchar) mandelbrotFormula(z0);
|
||||
m_img.ptr<uchar>(i)[j] = value;
|
||||
}
|
||||
}
|
||||
|
||||
ParallelMandelbrot& operator=(const ParallelMandelbrot &) {
|
||||
return *this;
|
||||
};
|
||||
|
||||
private:
|
||||
Mat &m_img;
|
||||
float m_x1;
|
||||
float m_y1;
|
||||
float m_scaleX;
|
||||
float m_scaleY;
|
||||
};
|
||||
//! [mandelbrot-parallel]
|
||||
|
||||
//! [mandelbrot-sequential]
|
||||
void sequentialMandelbrot(Mat &img, const float x1, const float y1, const float scaleX, const float scaleY)
|
||||
{
|
||||
for (int i = 0; i < img.rows; i++)
|
||||
{
|
||||
for (int j = 0; j < img.cols; j++)
|
||||
{
|
||||
float x0 = j / scaleX + x1;
|
||||
float y0 = i / scaleY + y1;
|
||||
|
||||
complex<float> z0(x0, y0);
|
||||
uchar value = (uchar) mandelbrotFormula(z0);
|
||||
img.ptr<uchar>(i)[j] = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
//! [mandelbrot-sequential]
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
//! [mandelbrot-transformation]
|
||||
Mat mandelbrotImg(4800, 5400, CV_8U);
|
||||
float x1 = -2.1f, x2 = 0.6f;
|
||||
float y1 = -1.2f, y2 = 1.2f;
|
||||
float scaleX = mandelbrotImg.cols / (x2 - x1);
|
||||
float scaleY = mandelbrotImg.rows / (y2 - y1);
|
||||
//! [mandelbrot-transformation]
|
||||
|
||||
double t1 = (double) getTickCount();
|
||||
|
||||
#ifdef CV_CXX11
|
||||
|
||||
//! [mandelbrot-parallel-call-cxx11]
|
||||
parallel_for_(Range(0, mandelbrotImg.rows*mandelbrotImg.cols), [&](const Range& range){
|
||||
for (int r = range.start; r < range.end; r++)
|
||||
{
|
||||
int i = r / mandelbrotImg.cols;
|
||||
int j = r % mandelbrotImg.cols;
|
||||
|
||||
float x0 = j / scaleX + x1;
|
||||
float y0 = i / scaleY + y1;
|
||||
|
||||
complex<float> z0(x0, y0);
|
||||
uchar value = (uchar) mandelbrotFormula(z0);
|
||||
mandelbrotImg.ptr<uchar>(i)[j] = value;
|
||||
}
|
||||
});
|
||||
//! [mandelbrot-parallel-call-cxx11]
|
||||
|
||||
#else
|
||||
|
||||
//! [mandelbrot-parallel-call]
|
||||
ParallelMandelbrot parallelMandelbrot(mandelbrotImg, x1, y1, scaleX, scaleY);
|
||||
parallel_for_(Range(0, mandelbrotImg.rows*mandelbrotImg.cols), parallelMandelbrot);
|
||||
//! [mandelbrot-parallel-call]
|
||||
|
||||
#endif
|
||||
|
||||
t1 = ((double) getTickCount() - t1) / getTickFrequency();
|
||||
cout << "Parallel Mandelbrot: " << t1 << " s" << endl;
|
||||
|
||||
Mat mandelbrotImgSequential(4800, 5400, CV_8U);
|
||||
double t2 = (double) getTickCount();
|
||||
sequentialMandelbrot(mandelbrotImgSequential, x1, y1, scaleX, scaleY);
|
||||
t2 = ((double) getTickCount() - t2) / getTickFrequency();
|
||||
cout << "Sequential Mandelbrot: " << t2 << " s" << endl;
|
||||
cout << "Speed-up: " << t2/t1 << " X" << endl;
|
||||
|
||||
imwrite("Mandelbrot_parallel.png", mandelbrotImg);
|
||||
imwrite("Mandelbrot_sequential.png", mandelbrotImgSequential);
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
@ -0,0 +1,108 @@
|
||||
#include <opencv2/imgcodecs.hpp>
|
||||
#include <opencv2/highgui.hpp>
|
||||
#include <opencv2/imgproc.hpp>
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
using namespace cv;
|
||||
|
||||
static void help(char* progName)
|
||||
{
|
||||
cout << endl
|
||||
<< "This program shows how to filter images with mask: the write it yourself and the"
|
||||
<< "filter2d way. " << endl
|
||||
<< "Usage:" << endl
|
||||
<< progName << " [image_path -- default lena.jpg] [G -- grayscale] " << endl << endl;
|
||||
}
|
||||
|
||||
|
||||
void Sharpen(const Mat& myImage,Mat& Result);
|
||||
|
||||
int main( int argc, char* argv[])
|
||||
{
|
||||
help(argv[0]);
|
||||
const char* filename = argc >=2 ? argv[1] : "lena.jpg";
|
||||
|
||||
Mat src, dst0, dst1;
|
||||
|
||||
if (argc >= 3 && !strcmp("G", argv[2]))
|
||||
src = imread( samples::findFile( filename ), IMREAD_GRAYSCALE);
|
||||
else
|
||||
src = imread( samples::findFile( filename ), IMREAD_COLOR);
|
||||
|
||||
if (src.empty())
|
||||
{
|
||||
cerr << "Can't open image [" << filename << "]" << endl;
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
namedWindow("Input", WINDOW_AUTOSIZE);
|
||||
namedWindow("Output", WINDOW_AUTOSIZE);
|
||||
|
||||
imshow( "Input", src );
|
||||
double t = (double)getTickCount();
|
||||
|
||||
Sharpen( src, dst0 );
|
||||
|
||||
t = ((double)getTickCount() - t)/getTickFrequency();
|
||||
cout << "Hand written function time passed in seconds: " << t << endl;
|
||||
|
||||
imshow( "Output", dst0 );
|
||||
waitKey();
|
||||
|
||||
//![kern]
|
||||
Mat kernel = (Mat_<char>(3,3) << 0, -1, 0,
|
||||
-1, 5, -1,
|
||||
0, -1, 0);
|
||||
//![kern]
|
||||
|
||||
t = (double)getTickCount();
|
||||
|
||||
//![filter2D]
|
||||
filter2D( src, dst1, src.depth(), kernel );
|
||||
//![filter2D]
|
||||
t = ((double)getTickCount() - t)/getTickFrequency();
|
||||
cout << "Built-in filter2D time passed in seconds: " << t << endl;
|
||||
|
||||
imshow( "Output", dst1 );
|
||||
|
||||
waitKey();
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
//! [basic_method]
|
||||
void Sharpen(const Mat& myImage,Mat& Result)
|
||||
{
|
||||
//! [8_bit]
|
||||
CV_Assert(myImage.depth() == CV_8U); // accept only uchar images
|
||||
//! [8_bit]
|
||||
|
||||
//! [create_channels]
|
||||
const int nChannels = myImage.channels();
|
||||
Result.create(myImage.size(),myImage.type());
|
||||
//! [create_channels]
|
||||
|
||||
//! [basic_method_loop]
|
||||
for(int j = 1 ; j < myImage.rows-1; ++j)
|
||||
{
|
||||
const uchar* previous = myImage.ptr<uchar>(j - 1);
|
||||
const uchar* current = myImage.ptr<uchar>(j );
|
||||
const uchar* next = myImage.ptr<uchar>(j + 1);
|
||||
|
||||
uchar* output = Result.ptr<uchar>(j);
|
||||
|
||||
for(int i= nChannels;i < nChannels*(myImage.cols-1); ++i)
|
||||
{
|
||||
*output++ = saturate_cast<uchar>(5*current[i]
|
||||
-current[i-nChannels] - current[i+nChannels] - previous[i] - next[i]);
|
||||
}
|
||||
}
|
||||
//! [basic_method_loop]
|
||||
|
||||
//! [borders]
|
||||
Result.row(0).setTo(Scalar(0));
|
||||
Result.row(Result.rows-1).setTo(Scalar(0));
|
||||
Result.col(0).setTo(Scalar(0));
|
||||
Result.col(Result.cols-1).setTo(Scalar(0));
|
||||
//! [borders]
|
||||
}
|
||||
//! [basic_method]
|
170
samples/cpp/tutorial_code/core/mat_operations/mat_operations.cpp
Normal file
170
samples/cpp/tutorial_code/core/mat_operations/mat_operations.cpp
Normal file
@ -0,0 +1,170 @@
|
||||
/* Snippet code for Operations with images tutorial (not intended to be run but should built successfully) */
|
||||
|
||||
#include "opencv2/core.hpp"
|
||||
#include "opencv2/imgcodecs.hpp"
|
||||
#include "opencv2/imgproc.hpp"
|
||||
#include "opencv2/highgui.hpp"
|
||||
#include <iostream>
|
||||
|
||||
using namespace cv;
|
||||
using namespace std;
|
||||
|
||||
int main(int,char**)
|
||||
{
|
||||
std::string filename = "";
|
||||
// Input/Output
|
||||
{
|
||||
//! [Load an image from a file]
|
||||
Mat img = imread(filename);
|
||||
//! [Load an image from a file]
|
||||
CV_UNUSED(img);
|
||||
}
|
||||
{
|
||||
//! [Load an image from a file in grayscale]
|
||||
Mat img = imread(filename, IMREAD_GRAYSCALE);
|
||||
//! [Load an image from a file in grayscale]
|
||||
CV_UNUSED(img);
|
||||
}
|
||||
{
|
||||
Mat img(4,4,CV_8U);
|
||||
//! [Save image]
|
||||
imwrite(filename, img);
|
||||
//! [Save image]
|
||||
}
|
||||
// Accessing pixel intensity values
|
||||
{
|
||||
Mat img(4,4,CV_8U);
|
||||
int y = 0, x = 0;
|
||||
{
|
||||
//! [Pixel access 1]
|
||||
Scalar intensity = img.at<uchar>(y, x);
|
||||
//! [Pixel access 1]
|
||||
CV_UNUSED(intensity);
|
||||
}
|
||||
{
|
||||
//! [Pixel access 2]
|
||||
Scalar intensity = img.at<uchar>(Point(x, y));
|
||||
//! [Pixel access 2]
|
||||
CV_UNUSED(intensity);
|
||||
}
|
||||
{
|
||||
//! [Pixel access 3]
|
||||
Vec3b intensity = img.at<Vec3b>(y, x);
|
||||
uchar blue = intensity.val[0];
|
||||
uchar green = intensity.val[1];
|
||||
uchar red = intensity.val[2];
|
||||
//! [Pixel access 3]
|
||||
CV_UNUSED(blue);
|
||||
CV_UNUSED(green);
|
||||
CV_UNUSED(red);
|
||||
}
|
||||
{
|
||||
//! [Pixel access 4]
|
||||
Vec3f intensity = img.at<Vec3f>(y, x);
|
||||
float blue = intensity.val[0];
|
||||
float green = intensity.val[1];
|
||||
float red = intensity.val[2];
|
||||
//! [Pixel access 4]
|
||||
CV_UNUSED(blue);
|
||||
CV_UNUSED(green);
|
||||
CV_UNUSED(red);
|
||||
}
|
||||
{
|
||||
//! [Pixel access 5]
|
||||
img.at<uchar>(y, x) = 128;
|
||||
//! [Pixel access 5]
|
||||
}
|
||||
{
|
||||
int i = 0;
|
||||
//! [Mat from points vector]
|
||||
vector<Point2f> points;
|
||||
//... fill the array
|
||||
Mat pointsMat = Mat(points);
|
||||
//! [Mat from points vector]
|
||||
|
||||
//! [Point access]
|
||||
Point2f point = pointsMat.at<Point2f>(i, 0);
|
||||
//! [Point access]
|
||||
CV_UNUSED(point);
|
||||
}
|
||||
}
|
||||
// Memory management and reference counting
|
||||
{
|
||||
//! [Reference counting 1]
|
||||
std::vector<Point3f> points;
|
||||
// .. fill the array
|
||||
Mat pointsMat = Mat(points).reshape(1);
|
||||
//! [Reference counting 1]
|
||||
CV_UNUSED(pointsMat);
|
||||
}
|
||||
{
|
||||
//! [Reference counting 2]
|
||||
Mat img = imread("image.jpg");
|
||||
Mat img1 = img.clone();
|
||||
//! [Reference counting 2]
|
||||
CV_UNUSED(img1);
|
||||
}
|
||||
{
|
||||
//! [Reference counting 3]
|
||||
Mat img = imread("image.jpg");
|
||||
Mat sobelx;
|
||||
Sobel(img, sobelx, CV_32F, 1, 0);
|
||||
//! [Reference counting 3]
|
||||
}
|
||||
// Primitive operations
|
||||
{
|
||||
Mat img;
|
||||
{
|
||||
//! [Set image to black]
|
||||
img = Scalar(0);
|
||||
//! [Set image to black]
|
||||
}
|
||||
{
|
||||
//! [Select ROI]
|
||||
Rect r(10, 10, 100, 100);
|
||||
Mat smallImg = img(r);
|
||||
//! [Select ROI]
|
||||
CV_UNUSED(smallImg);
|
||||
}
|
||||
}
|
||||
{
|
||||
//! [BGR to Gray]
|
||||
Mat img = imread("image.jpg"); // loading a 8UC3 image
|
||||
Mat grey;
|
||||
cvtColor(img, grey, COLOR_BGR2GRAY);
|
||||
//! [BGR to Gray]
|
||||
}
|
||||
{
|
||||
Mat dst, src;
|
||||
//! [Convert to CV_32F]
|
||||
src.convertTo(dst, CV_32F);
|
||||
//! [Convert to CV_32F]
|
||||
}
|
||||
// Visualizing images
|
||||
{
|
||||
//! [imshow 1]
|
||||
Mat img = imread("image.jpg");
|
||||
namedWindow("image", WINDOW_AUTOSIZE);
|
||||
imshow("image", img);
|
||||
waitKey();
|
||||
//! [imshow 1]
|
||||
}
|
||||
{
|
||||
//! [imshow 2]
|
||||
Mat img = imread("image.jpg");
|
||||
Mat grey;
|
||||
cvtColor(img, grey, COLOR_BGR2GRAY);
|
||||
Mat sobelx;
|
||||
Sobel(grey, sobelx, CV_32F, 1, 0);
|
||||
double minVal, maxVal;
|
||||
minMaxLoc(sobelx, &minVal, &maxVal); //find minimum and maximum intensities
|
||||
Mat draw;
|
||||
sobelx.convertTo(draw, CV_8U, 255.0/(maxVal - minVal), -minVal * 255.0/(maxVal - minVal));
|
||||
namedWindow("image", WINDOW_AUTOSIZE);
|
||||
imshow("image", draw);
|
||||
waitKey();
|
||||
//! [imshow 2]
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,119 @@
|
||||
/* For description look into the help() function. */
|
||||
|
||||
#include "opencv2/core.hpp"
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
using namespace cv;
|
||||
|
||||
static void help()
|
||||
{
|
||||
cout
|
||||
<< "\n---------------------------------------------------------------------------" << endl
|
||||
<< "This program shows how to create matrices(cv::Mat) in OpenCV and its serial"
|
||||
<< " out capabilities" << endl
|
||||
<< "That is, cv::Mat M(...); M.create and cout << M. " << endl
|
||||
<< "Shows how output can be formatted to OpenCV, python, numpy, csv and C styles." << endl
|
||||
<< "Usage:" << endl
|
||||
<< "./mat_the_basic_image_container" << endl
|
||||
<< "-----------------------------------------------------------------------------" << endl
|
||||
<< endl;
|
||||
}
|
||||
|
||||
int main(int,char**)
|
||||
{
|
||||
help();
|
||||
// create by using the constructor
|
||||
//! [constructor]
|
||||
Mat M(2,2, CV_8UC3, Scalar(0,0,255));
|
||||
cout << "M = " << endl << " " << M << endl << endl;
|
||||
//! [constructor]
|
||||
|
||||
// create by using the create function()
|
||||
//! [create]
|
||||
M.create(4,4, CV_8UC(2));
|
||||
cout << "M = "<< endl << " " << M << endl << endl;
|
||||
//! [create]
|
||||
|
||||
// create multidimensional matrices
|
||||
//! [init]
|
||||
int sz[3] = {2,2,2};
|
||||
Mat L(3,sz, CV_8UC(1), Scalar::all(0));
|
||||
//! [init]
|
||||
|
||||
// Cannot print via operator <<
|
||||
|
||||
// Create using MATLAB style eye, ones or zero matrix
|
||||
//! [matlab]
|
||||
Mat E = Mat::eye(4, 4, CV_64F);
|
||||
cout << "E = " << endl << " " << E << endl << endl;
|
||||
Mat O = Mat::ones(2, 2, CV_32F);
|
||||
cout << "O = " << endl << " " << O << endl << endl;
|
||||
Mat Z = Mat::zeros(3,3, CV_8UC1);
|
||||
cout << "Z = " << endl << " " << Z << endl << endl;
|
||||
//! [matlab]
|
||||
|
||||
// create a 3x3 double-precision identity matrix
|
||||
//! [comma]
|
||||
Mat C = (Mat_<double>(3,3) << 0, -1, 0, -1, 5, -1, 0, -1, 0);
|
||||
cout << "C = " << endl << " " << C << endl << endl;
|
||||
//! [comma]
|
||||
// do the same with initializer_list
|
||||
#ifdef CV_CXX11
|
||||
//! [list]
|
||||
C = (Mat_<double>({0, -1, 0, -1, 5, -1, 0, -1, 0})).reshape(3);
|
||||
cout << "C = " << endl << " " << C << endl << endl;
|
||||
//! [list]
|
||||
#endif
|
||||
//! [clone]
|
||||
Mat RowClone = C.row(1).clone();
|
||||
cout << "RowClone = " << endl << " " << RowClone << endl << endl;
|
||||
//! [clone]
|
||||
|
||||
// Fill a matrix with random values
|
||||
//! [random]
|
||||
Mat R = Mat(3, 2, CV_8UC3);
|
||||
randu(R, Scalar::all(0), Scalar::all(255));
|
||||
//! [random]
|
||||
|
||||
// Demonstrate the output formatting options
|
||||
//! [out-default]
|
||||
cout << "R (default) = " << endl << R << endl << endl;
|
||||
//! [out-default]
|
||||
//! [out-python]
|
||||
cout << "R (python) = " << endl << format(R, Formatter::FMT_PYTHON) << endl << endl;
|
||||
//! [out-python]
|
||||
//! [out-numpy]
|
||||
cout << "R (numpy) = " << endl << format(R, Formatter::FMT_NUMPY ) << endl << endl;
|
||||
//! [out-numpy]
|
||||
//! [out-csv]
|
||||
cout << "R (csv) = " << endl << format(R, Formatter::FMT_CSV ) << endl << endl;
|
||||
//! [out-csv]
|
||||
//! [out-c]
|
||||
cout << "R (c) = " << endl << format(R, Formatter::FMT_C ) << endl << endl;
|
||||
//! [out-c]
|
||||
|
||||
//! [out-point2]
|
||||
Point2f P(5, 1);
|
||||
cout << "Point (2D) = " << P << endl << endl;
|
||||
//! [out-point2]
|
||||
|
||||
//! [out-point3]
|
||||
Point3f P3f(2, 6, 7);
|
||||
cout << "Point (3D) = " << P3f << endl << endl;
|
||||
//! [out-point3]
|
||||
|
||||
//! [out-vector]
|
||||
vector<float> v;
|
||||
v.push_back( (float)CV_PI); v.push_back(2); v.push_back(3.01f);
|
||||
cout << "Vector of floats via Mat = " << Mat(v) << endl << endl;
|
||||
//! [out-vector]
|
||||
|
||||
//! [out-vector-points]
|
||||
vector<Point2f> vPoints(20);
|
||||
for (size_t i = 0; i < vPoints.size(); ++i)
|
||||
vPoints[i] = Point2f((float)(i * 5), (float)(i % 7));
|
||||
cout << "A vector of 2D Points = " << vPoints << endl << endl;
|
||||
//! [out-vector-points]
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user