Merge branch 'master' of https://github.com/Itseez/opencv
This commit is contained in:
@@ -47,8 +47,8 @@ int main( void )
|
||||
src1 = imread("../images/LinuxLogo.jpg");
|
||||
src2 = imread("../images/WindowsLogo.jpg");
|
||||
|
||||
if( !src1.data ) { printf("Error loading src1 \n"); return -1; }
|
||||
if( !src2.data ) { printf("Error loading src2 \n"); return -1; }
|
||||
if( src1.empty() ) { printf("Error loading src1 \n"); return -1; }
|
||||
if( src2.empty() ) { printf("Error loading src2 \n"); return -1; }
|
||||
|
||||
/// Initialize values
|
||||
alpha_slider = 0;
|
||||
|
242
samples/cpp/tutorial_code/HighGUI/GDAL_IO/gdal-image.cpp
Normal file
242
samples/cpp/tutorial_code/HighGUI/GDAL_IO/gdal-image.cpp
Normal file
@@ -0,0 +1,242 @@
|
||||
/**
|
||||
* gdal_image.cpp -- Load GIS data into OpenCV Containers using the Geospatial Data Abstraction Library
|
||||
*/
|
||||
|
||||
/// OpenCV Headers
|
||||
#include "opencv2/core/core.hpp"
|
||||
#include "opencv2/imgproc/imgproc.hpp"
|
||||
#include "opencv2/highgui/highgui.hpp"
|
||||
|
||||
/// C++ Standard Libraries
|
||||
#include <cmath>
|
||||
#include <iostream>
|
||||
#include <stdexcept>
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
/// define the corner points
|
||||
/// Note that GDAL can natively determine this
|
||||
cv::Point2d tl( -122.441017, 37.815664 );
|
||||
cv::Point2d tr( -122.370919, 37.815311 );
|
||||
cv::Point2d bl( -122.441533, 37.747167 );
|
||||
cv::Point2d br( -122.3715, 37.746814 );
|
||||
|
||||
/// determine dem corners
|
||||
cv::Point2d dem_bl( -122.0, 38);
|
||||
cv::Point2d dem_tr( -123.0, 37);
|
||||
|
||||
/// range of the heat map colors
|
||||
std::vector<std::pair<cv::Vec3b,double> > color_range;
|
||||
|
||||
|
||||
/// List of all function prototypes
|
||||
cv::Point2d lerp( const cv::Point2d&, const cv::Point2d&, const double& );
|
||||
|
||||
cv::Vec3b get_dem_color( const double& );
|
||||
|
||||
cv::Point2d world2dem( const cv::Point2d&, const cv::Size&);
|
||||
|
||||
cv::Point2d pixel2world( const int&, const int&, const cv::Size& );
|
||||
|
||||
void add_color( cv::Vec3b& pix, const uchar& b, const uchar& g, const uchar& r );
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Linear Interpolation
|
||||
* p1 - Point 1
|
||||
* p2 - Point 2
|
||||
* t - Ratio from Point 1 to Point 2
|
||||
*/
|
||||
cv::Point2d lerp( cv::Point2d const& p1, cv::Point2d const& p2, const double& t ){
|
||||
return cv::Point2d( ((1-t)*p1.x) + (t*p2.x),
|
||||
((1-t)*p1.y) + (t*p2.y));
|
||||
}
|
||||
|
||||
/**
|
||||
* Interpolate Colors
|
||||
*/
|
||||
template <typename DATATYPE, int N>
|
||||
cv::Vec<DATATYPE,N> lerp( cv::Vec<DATATYPE,N> const& minColor,
|
||||
cv::Vec<DATATYPE,N> const& maxColor,
|
||||
double const& t ){
|
||||
|
||||
cv::Vec<DATATYPE,N> output;
|
||||
for( int i=0; i<N; i++ ){
|
||||
output[i] = (uchar)(((1-t)*minColor[i]) + (t * maxColor[i]));
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
/**
|
||||
* Compute the dem color
|
||||
*/
|
||||
cv::Vec3b get_dem_color( const double& elevation ){
|
||||
|
||||
// if the elevation is below the minimum, return the minimum
|
||||
if( elevation < color_range[0].second ){
|
||||
return color_range[0].first;
|
||||
}
|
||||
// if the elevation is above the maximum, return the maximum
|
||||
if( elevation > color_range.back().second ){
|
||||
return color_range.back().first;
|
||||
}
|
||||
|
||||
// otherwise, find the proper starting index
|
||||
int idx=0;
|
||||
double t = 0;
|
||||
for( int x=0; x<(int)(color_range.size()-1); x++ ){
|
||||
|
||||
// if the current elevation is below the next item, then use the current
|
||||
// two colors as our range
|
||||
if( elevation < color_range[x+1].second ){
|
||||
idx=x;
|
||||
t = (color_range[x+1].second - elevation)/
|
||||
(color_range[x+1].second - color_range[x].second);
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// interpolate the color
|
||||
return lerp( color_range[idx].first, color_range[idx+1].first, t);
|
||||
}
|
||||
|
||||
/**
|
||||
* Given a pixel coordinate and the size of the input image, compute the pixel location
|
||||
* on the DEM image.
|
||||
*/
|
||||
cv::Point2d world2dem( cv::Point2d const& coordinate, const cv::Size& dem_size ){
|
||||
|
||||
|
||||
// relate this to the dem points
|
||||
// ASSUMING THAT DEM DATA IS ORTHORECTIFIED
|
||||
double demRatioX = ((dem_tr.x - coordinate.x)/(dem_tr.x - dem_bl.x));
|
||||
double demRatioY = 1-((dem_tr.y - coordinate.y)/(dem_tr.y - dem_bl.y));
|
||||
|
||||
cv::Point2d output;
|
||||
output.x = demRatioX * dem_size.width;
|
||||
output.y = demRatioY * dem_size.height;
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a pixel coordinate to world coordinates
|
||||
*/
|
||||
cv::Point2d pixel2world( const int& x, const int& y, const cv::Size& size ){
|
||||
|
||||
// compute the ratio of the pixel location to its dimension
|
||||
double rx = (double)x / size.width;
|
||||
double ry = (double)y / size.height;
|
||||
|
||||
// compute LERP of each coordinate
|
||||
cv::Point2d rightSide = lerp(tr, br, ry);
|
||||
cv::Point2d leftSide = lerp(tl, bl, ry);
|
||||
|
||||
// compute the actual Lat/Lon coordinate of the interpolated coordinate
|
||||
return lerp( leftSide, rightSide, rx );
|
||||
}
|
||||
|
||||
/**
|
||||
* Add color to a specific pixel color value
|
||||
*/
|
||||
void add_color( cv::Vec3b& pix, const uchar& b, const uchar& g, const uchar& r ){
|
||||
|
||||
if( pix[0] + b < 255 && pix[0] + b >= 0 ){ pix[0] += b; }
|
||||
if( pix[1] + g < 255 && pix[1] + g >= 0 ){ pix[1] += g; }
|
||||
if( pix[2] + r < 255 && pix[2] + r >= 0 ){ pix[2] += r; }
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Main Function
|
||||
*/
|
||||
int main( int argc, char* argv[] ){
|
||||
|
||||
/**
|
||||
* Check input arguments
|
||||
*/
|
||||
if( argc < 3 ){
|
||||
cout << "usage: " << argv[0] << " <image> <dem>" << endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
/// load the image (note that we don't have the projection information. You will
|
||||
/// need to load that yourself or use the full GDAL driver. The values are pre-defined
|
||||
/// at the top of this file
|
||||
cv::Mat image = cv::imread(argv[1], cv::IMREAD_LOAD_GDAL | cv::IMREAD_COLOR );
|
||||
|
||||
/// load the dem model
|
||||
cv::Mat dem = cv::imread(argv[2], cv::IMREAD_LOAD_GDAL | cv::IMREAD_ANYDEPTH );
|
||||
|
||||
/// create our output products
|
||||
cv::Mat output_dem( image.size(), CV_8UC3 );
|
||||
cv::Mat output_dem_flood( image.size(), CV_8UC3 );
|
||||
|
||||
/// for sanity sake, make sure GDAL Loads it as a signed short
|
||||
if( dem.type() != CV_16SC1 ){ throw std::runtime_error("DEM image type must be CV_16SC1"); }
|
||||
|
||||
/// define the color range to create our output DEM heat map
|
||||
// Pair format ( Color, elevation ); Push from low to high
|
||||
// Note: This would be perfect for a configuration file, but is here for a working demo.
|
||||
color_range.push_back( std::pair<cv::Vec3b,double>(cv::Vec3b( 188, 154, 46), -1));
|
||||
color_range.push_back( std::pair<cv::Vec3b,double>(cv::Vec3b( 110, 220, 110), 0.25));
|
||||
color_range.push_back( std::pair<cv::Vec3b,double>(cv::Vec3b( 150, 250, 230), 20));
|
||||
color_range.push_back( std::pair<cv::Vec3b,double>(cv::Vec3b( 160, 220, 200), 75));
|
||||
color_range.push_back( std::pair<cv::Vec3b,double>(cv::Vec3b( 220, 190, 170), 100));
|
||||
color_range.push_back( std::pair<cv::Vec3b,double>(cv::Vec3b( 250, 180, 140), 200));
|
||||
|
||||
// define a minimum elevation
|
||||
double minElevation = -10;
|
||||
|
||||
// iterate over each pixel in the image, computing the dem point
|
||||
for( int y=0; y<image.rows; y++ ){
|
||||
for( int x=0; x<image.cols; x++ ){
|
||||
|
||||
// convert the pixel coordinate to lat/lon coordinates
|
||||
cv::Point2d coordinate = pixel2world( x, y, image.size() );
|
||||
|
||||
// compute the dem image pixel coordinate from lat/lon
|
||||
cv::Point2d dem_coordinate = world2dem( coordinate, dem.size() );
|
||||
|
||||
// extract the elevation
|
||||
double dz;
|
||||
if( dem_coordinate.x >= 0 && dem_coordinate.y >= 0 &&
|
||||
dem_coordinate.x < dem.cols && dem_coordinate.y < dem.rows ){
|
||||
dz = dem.at<short>(dem_coordinate);
|
||||
}else{
|
||||
dz = minElevation;
|
||||
}
|
||||
|
||||
// write the pixel value to the file
|
||||
output_dem_flood.at<cv::Vec3b>(y,x) = image.at<cv::Vec3b>(y,x);
|
||||
|
||||
// compute the color for the heat map output
|
||||
cv::Vec3b actualColor = get_dem_color(dz);
|
||||
output_dem.at<cv::Vec3b>(y,x) = actualColor;
|
||||
|
||||
// show effect of a 10 meter increase in ocean levels
|
||||
if( dz < 10 ){
|
||||
add_color( output_dem_flood.at<cv::Vec3b>(y,x), 90, 0, 0 );
|
||||
}
|
||||
// show effect of a 50 meter increase in ocean levels
|
||||
else if( dz < 50 ){
|
||||
add_color( output_dem_flood.at<cv::Vec3b>(y,x), 0, 90, 0 );
|
||||
}
|
||||
// show effect of a 100 meter increase in ocean levels
|
||||
else if( dz < 100 ){
|
||||
add_color( output_dem_flood.at<cv::Vec3b>(y,x), 0, 0, 90 );
|
||||
}
|
||||
|
||||
}}
|
||||
|
||||
// print our heat map
|
||||
cv::imwrite( "heat-map.jpg" , output_dem );
|
||||
|
||||
// print the flooding effect image
|
||||
cv::imwrite( "flooded.jpg", output_dem_flood);
|
||||
|
||||
return 0;
|
||||
}
|
@@ -26,7 +26,7 @@ int main( int, char** argv )
|
||||
/// Load image
|
||||
src = imread( argv[1], 1 );
|
||||
|
||||
if( !src.data )
|
||||
if( src.empty() )
|
||||
{ cout<<"Usage: ./Histogram_Demo <path_to_image>"<<endl;
|
||||
return -1;
|
||||
}
|
||||
|
@@ -23,7 +23,7 @@ int main( int, char** argv )
|
||||
/// Load image
|
||||
src = imread( argv[1], 1 );
|
||||
|
||||
if( !src.data )
|
||||
if( src.empty() )
|
||||
{ return -1; }
|
||||
|
||||
/// Separate the image in 3 places ( B, G and R )
|
||||
|
@@ -35,8 +35,8 @@ int main( void )
|
||||
src1 = imread("../images/LinuxLogo.jpg");
|
||||
src2 = imread("../images/WindowsLogo.jpg");
|
||||
|
||||
if( !src1.data ) { std::cout<< "Error loading src1"<<std::endl; return -1; }
|
||||
if( !src2.data ) { std::cout<< "Error loading src2"<<std::endl; return -1; }
|
||||
if( src1.empty() ) { std::cout<< "Error loading src1"<<std::endl; return -1; }
|
||||
if( src2.empty() ) { std::cout<< "Error loading src2"<<std::endl; return -1; }
|
||||
|
||||
/// Create Windows
|
||||
namedWindow("Linear Blend", 1);
|
||||
|
@@ -34,7 +34,7 @@ int main( int, char** argv )
|
||||
/// Load an image
|
||||
src = imread( argv[1] );
|
||||
|
||||
if( !src.data )
|
||||
if( src.empty() )
|
||||
{ return -1; }
|
||||
|
||||
/// Create windows
|
||||
|
@@ -36,7 +36,7 @@ int main( int, char** argv )
|
||||
/// Load an image
|
||||
src = imread( argv[1] );
|
||||
|
||||
if( !src.data )
|
||||
if( src.empty() )
|
||||
{ return -1; }
|
||||
|
||||
/// Create window
|
||||
|
@@ -33,7 +33,7 @@ int main( void )
|
||||
|
||||
/// Test image - Make sure it s divisible by 2^{n}
|
||||
src = imread( "../images/chicky_512.png" );
|
||||
if( !src.data )
|
||||
if( src.empty() )
|
||||
{ printf(" No data! -- Exiting the program \n");
|
||||
return -1; }
|
||||
|
||||
|
@@ -52,7 +52,7 @@ int main( int, char** argv )
|
||||
/// Load an image
|
||||
src = imread( argv[1] );
|
||||
|
||||
if( !src.data )
|
||||
if( src.empty() )
|
||||
{ return -1; }
|
||||
|
||||
/// Create a matrix of the same type and size as src (for dst)
|
||||
|
@@ -65,7 +65,7 @@ int main(int argc, char** argv)
|
||||
// Read the image
|
||||
src = imread( argv[1], 1 );
|
||||
|
||||
if( !src.data )
|
||||
if( src.empty() )
|
||||
{
|
||||
std::cerr<<"Invalid input image\n";
|
||||
std::cout<<usage;
|
||||
|
@@ -28,7 +28,7 @@ int main( int, char** argv )
|
||||
/// Load an image
|
||||
src = imread( argv[1] );
|
||||
|
||||
if( !src.data )
|
||||
if( src.empty() )
|
||||
{ return -1; }
|
||||
|
||||
/// Remove noise by blurring with a Gaussian filter
|
||||
|
@@ -28,7 +28,7 @@ int main( int, char** argv )
|
||||
/// Load an image
|
||||
src = imread( argv[1] );
|
||||
|
||||
if( !src.data )
|
||||
if( src.empty() )
|
||||
{ return -1; }
|
||||
|
||||
GaussianBlur( src, src, Size(3,3), 0, 0, BORDER_DEFAULT );
|
||||
|
@@ -30,7 +30,7 @@ int main( int, char** argv )
|
||||
/// Load an image
|
||||
src = imread( argv[1] );
|
||||
|
||||
if( !src.data )
|
||||
if( src.empty() )
|
||||
{
|
||||
printf(" No data entered, please enter the path to an image file \n");
|
||||
return -1;
|
||||
|
@@ -32,7 +32,7 @@ int main ( int, char** argv )
|
||||
/// Load an image
|
||||
src = imread( argv[1] );
|
||||
|
||||
if( !src.data )
|
||||
if( src.empty() )
|
||||
{ return -1; }
|
||||
|
||||
/// Create window
|
||||
|
@@ -33,7 +33,7 @@ int main( int argc, char** argv )
|
||||
Mat imgDisparity16S = Mat( imgLeft.rows, imgLeft.cols, CV_16S );
|
||||
Mat imgDisparity8U = Mat( imgLeft.rows, imgLeft.cols, CV_8UC1 );
|
||||
|
||||
if( !imgLeft.data || !imgRight.data )
|
||||
if( imgLeft.empty() || imgRight.empty() )
|
||||
{ std::cout<< " --(!) Error reading images " << std::endl; return -1; }
|
||||
|
||||
//-- 2. Call the constructor for StereoBM
|
||||
|
@@ -3,8 +3,9 @@
|
||||
* @brief Simple sample code
|
||||
*/
|
||||
|
||||
#include <opencv2/core/core.hpp>
|
||||
#include <opencv2/highgui/highgui.hpp>
|
||||
#include <opencv2/core.hpp>
|
||||
#include <opencv2/imgproc.hpp>
|
||||
#include <opencv2/highgui.hpp>
|
||||
|
||||
#define w 400
|
||||
|
||||
|
@@ -3,8 +3,9 @@
|
||||
* @brief Simple sample code
|
||||
*/
|
||||
|
||||
#include <opencv2/core/core.hpp>
|
||||
#include <opencv2/highgui/highgui.hpp>
|
||||
#include <opencv2/core.hpp>
|
||||
#include <opencv2/imgproc.hpp>
|
||||
#include <opencv2/highgui.hpp>
|
||||
#include <iostream>
|
||||
#include <stdio.h>
|
||||
|
||||
|
@@ -41,7 +41,7 @@ int main( int argc, char* argv[])
|
||||
else
|
||||
I = imread(argv[1], IMREAD_COLOR);
|
||||
|
||||
if (!I.data)
|
||||
if (I.empty())
|
||||
{
|
||||
cout << "The image" << argv[1] << " could not be loaded." << endl;
|
||||
return -1;
|
||||
@@ -107,7 +107,7 @@ int main( int argc, char* argv[])
|
||||
<< times << " runs): " << t << " milliseconds."<< endl;
|
||||
|
||||
Mat lookUpTable(1, 256, CV_8U);
|
||||
uchar* p = lookUpTable.data;
|
||||
uchar* p = lookUpTable.ptr();
|
||||
for( int i = 0; i < 256; ++i)
|
||||
p[i] = table[i];
|
||||
|
||||
|
@@ -17,7 +17,7 @@ int main( int argc, char** argv )
|
||||
Mat image;
|
||||
image = imread(argv[1], IMREAD_COLOR); // Read the file
|
||||
|
||||
if(! image.data ) // Check for invalid input
|
||||
if( image.empty() ) // Check for invalid input
|
||||
{
|
||||
cout << "Could not open or find the image" << std::endl ;
|
||||
return -1;
|
||||
|
@@ -17,7 +17,7 @@ int main( int argc, char** argv )
|
||||
Mat image;
|
||||
image = imread(argv[1], IMREAD_COLOR); // Read the file
|
||||
|
||||
if(! image.data ) // Check for invalid input
|
||||
if( image.empty() ) // Check for invalid input
|
||||
{
|
||||
cout << "Could not open or find the image" << std::endl ;
|
||||
return -1;
|
||||
|
@@ -1,7 +1,8 @@
|
||||
#include <opencv2/core/core.hpp>
|
||||
#include <opencv2/core.hpp>
|
||||
#include <opencv2/imgproc.hpp>
|
||||
#include "opencv2/imgcodecs.hpp"
|
||||
#include <opencv2/highgui/highgui.hpp>
|
||||
#include <opencv2/ml/ml.hpp>
|
||||
#include <opencv2/highgui.hpp>
|
||||
#include <opencv2/ml.hpp>
|
||||
|
||||
using namespace cv;
|
||||
using namespace cv::ml;
|
||||
|
@@ -1,8 +1,9 @@
|
||||
#include <iostream>
|
||||
#include <opencv2/core/core.hpp>
|
||||
#include <opencv2/core.hpp>
|
||||
#include <opencv2/imgproc.hpp>
|
||||
#include "opencv2/imgcodecs.hpp"
|
||||
#include <opencv2/highgui/highgui.hpp>
|
||||
#include <opencv2/ml/ml.hpp>
|
||||
#include <opencv2/highgui.hpp>
|
||||
#include <opencv2/ml.hpp>
|
||||
|
||||
#define NTRAINING_SAMPLES 100 // Number of training samples per class
|
||||
#define FRAC_LINEAR_SEP 0.9f // Fraction of samples which compose the linear separable part
|
||||
|
@@ -37,7 +37,7 @@ int main(int argc, char* argv[])
|
||||
|
||||
Mat I = imread(argv[1]);
|
||||
|
||||
if(!I.data)
|
||||
if(I.empty())
|
||||
{
|
||||
cout << "Image not found" << endl;
|
||||
exit(0);
|
||||
|
@@ -318,12 +318,12 @@ int main()
|
||||
|
||||
img2 = imread(dest);
|
||||
|
||||
if(!img0.data)
|
||||
if(img0.empty())
|
||||
{
|
||||
cout << "Source Image does not exist" << endl;
|
||||
exit(0);
|
||||
}
|
||||
if(!img2.data)
|
||||
if(img2.empty())
|
||||
{
|
||||
cout << "Destination Image does not exist" << endl;
|
||||
exit(0);
|
||||
@@ -366,7 +366,7 @@ int main()
|
||||
|
||||
img0 = imread(src);
|
||||
|
||||
if(!img0.data)
|
||||
if(img0.empty())
|
||||
{
|
||||
cout << "Source Image does not exist" << endl;
|
||||
exit(0);
|
||||
@@ -396,7 +396,7 @@ int main()
|
||||
|
||||
img0 = imread(src);
|
||||
|
||||
if(!img0.data)
|
||||
if(img0.empty())
|
||||
{
|
||||
cout << "Source Image does not exist" << endl;
|
||||
exit(0);
|
||||
@@ -429,7 +429,7 @@ int main()
|
||||
|
||||
img0 = imread(src);
|
||||
|
||||
if(!img0.data)
|
||||
if(img0.empty())
|
||||
{
|
||||
cout << "Source Image does not exist" << endl;
|
||||
exit(0);
|
||||
|
@@ -6,9 +6,10 @@
|
||||
|
||||
//opencv
|
||||
#include "opencv2/imgcodecs.hpp"
|
||||
#include "opencv2/imgproc.hpp"
|
||||
#include "opencv2/videoio.hpp"
|
||||
#include <opencv2/highgui/highgui.hpp>
|
||||
#include <opencv2/video/background_segm.hpp>
|
||||
#include <opencv2/highgui.hpp>
|
||||
#include <opencv2/video.hpp>
|
||||
//C
|
||||
#include <stdio.h>
|
||||
//C++
|
||||
@@ -130,7 +131,7 @@ void processVideo(char* videoFilename) {
|
||||
void processImages(char* fistFrameFilename) {
|
||||
//read the first file of the sequence
|
||||
frame = imread(fistFrameFilename);
|
||||
if(!frame.data){
|
||||
if(frame.empty()){
|
||||
//error in opening the first image
|
||||
cerr << "Unable to open first image frame: " << fistFrameFilename << endl;
|
||||
exit(EXIT_FAILURE);
|
||||
@@ -169,7 +170,7 @@ void processImages(char* fistFrameFilename) {
|
||||
string nextFrameFilename = prefix + nextFrameNumberString + suffix;
|
||||
//read the next frame
|
||||
frame = imread(nextFrameFilename);
|
||||
if(!frame.data){
|
||||
if(frame.empty()){
|
||||
//error in opening the next image in the sequence
|
||||
cerr << "Unable to open image frame: " << nextFrameFilename << endl;
|
||||
exit(EXIT_FAILURE);
|
||||
|
Reference in New Issue
Block a user