#include stdio.h #include "opencv2/core/core.hpp" #include "opencv2/features2d/features2d.hpp" #include "opencv2/highgui/highgui.hpp" #include "opencv2/nonfree/nonfree.hpp" #ifndef _DEBUG #pragma comment(lib,"IlmImf.lib") #pragma comment(li
#include <stdio.h>
#include "opencv2/core/core.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/nonfree/nonfree.hpp"
#ifndef _DEBUG
#pragma comment(lib,"IlmImf.lib")
#pragma comment(lib,"libjasper.lib")
#pragma comment(lib,"libjpeg.lib")
#pragma comment(lib,"libpng.lib")
#pragma comment(lib,"libtiff.lib")
#pragma comment(lib,"zlib.lib")
#pragma comment(lib,"opencv_calib3d2411.lib")
#pragma comment(lib,"opencv_contrib2411.lib")
#pragma comment(lib,"opencv_core2411.lib")
#pragma comment(lib,"opencv_features2d2411.lib")
#pragma comment(lib,"opencv_flann2411.lib")
#pragma comment(lib,"opencv_gpu2411.lib")
#pragma comment(lib,"opencv_highgui2411.lib")
#pragma comment(lib,"opencv_imgproc2411.lib")
#pragma comment(lib,"opencv_legacy2411.lib")
#pragma comment(lib,"opencv_ml2411.lib")
#pragma comment(lib,"opencv_nonfree2411.lib")
#pragma comment(lib,"opencv_objdetect2411.lib")
#pragma comment(lib,"opencv_ocl2411.lib")
#pragma comment(lib,"opencv_photo2411.lib")
#pragma comment(lib,"opencv_stitching2411.lib")
#pragma comment(lib,"opencv_superres2411.lib")
#pragma comment(lib,"opencv_ts2411.lib")
#pragma comment(lib,"opencv_video2411.lib")
#pragma comment(lib,"opencv_videostab2411.lib")
#else
#pragma comment(lib,"zlibd.lib")
#pragma comment(lib,"IlmImfd.lib")
#pragma comment(lib,"libjasperd.lib")
#pragma comment(lib,"libjpegd.lib")
#pragma comment(lib,"libpngd.lib")
#pragma comment(lib,"libtiffd.lib")
#pragma comment(lib,"opencv_calib3d2411d.lib")
#pragma comment(lib,"opencv_contrib2411d.lib")
#pragma comment(lib,"opencv_core2411d.lib")
#pragma comment(lib,"opencv_features2d2411d.lib")
#pragma comment(lib,"opencv_flann2411d.lib")
#pragma comment(lib,"opencv_gpu2411d.lib")
#pragma comment(lib,"opencv_highgui2411d.lib")
#pragma comment(lib,"opencv_imgproc2411d.lib")
#pragma comment(lib,"opencv_legacy2411d.lib")
#pragma comment(lib,"opencv_ml2411d.lib")
#pragma comment(lib,"opencv_nonfree2411d.lib")
#pragma comment(lib,"opencv_objdetect2411d.lib")
#pragma comment(lib,"opencv_ocl2411d.lib")
#pragma comment(lib,"opencv_photo2411d.lib")
#pragma comment(lib,"opencv_stitching2411d.lib")
#pragma comment(lib,"opencv_superres2411d.lib")
#pragma comment(lib,"opencv_ts2411d.lib")
#pragma comment(lib,"opencv_video2411d.lib")
#pragma comment(lib,"opencv_videostab2411d.lib")
#endif/*------------------------------------------------------------------------------------------*\
This file contains material supporting chapter 1 of the cookbook:
Computer Vision Programming using the OpenCV Library
Second Edition
by Robert Laganiere, Packt Publishing, 2013. This program is free software; permission is hereby granted to use, copy, modify,
and distribute this source code, or portions thereof, for any purpose, without fee,
subject to the restriction that the copyright notice may not be removed
or altered from any source or altered source distribution.
The software is released on an as-is basis and without any warranties of any kind.
In particular, the software is not guaranteed to be fault-tolerant or free from failure.
The author disclaims all warranties with regard to this software, any use,
and any consequent failure, is purely the responsibility of the user.
Copyright (C) 2013 Robert Laganiere, www.laganiere.name \*------------------------------------------------------------------------------------------*/#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>// test function that creates an image
cv::Mat function() { // create image
cv::Mat ima(500,500,CV_8U,50);
// return it
return ima;
}int main1() {
// define image windows
cv::namedWindow("Image 1");
cv::namedWindow("Image 2");
cv::namedWindow("Image 3");
cv::namedWindow("Image 4");
cv::namedWindow("Image 5");
cv::namedWindow("Image"); // create a new image made of 240 rows and 320 columns
cv::Mat image1(240,320,CV_8U,100);
// or:
// cv::Mat image1(240,320,CV_8U,cv::Scalar(100)); cv::imshow("Image", image1); // show the image
cv::waitKey(0); // wait for a key pressed // re-allocate a new image
// (only if size or type are different)
image1.create(200,200,CV_8U);
image1= 200; cv::imshow("Image", image1); // show the image
cv::waitKey(0); // wait for a key pressed // create a red color image
// channel order is BGR
cv::Mat image2(240,320,CV_8UC3,cv::Scalar(0,0,255)); // or:
// cv::Mat image2(cv::Size(320,240),CV_8UC3);
// image2= cv::Scalar(0,0,255); cv::imshow("Image", image2); // show the image
cv::waitKey(0); // wait for a key pressed // read an image
cv::Mat image3= cv::imread("lena.jpg"); // all these images point to the same data block
cv::Mat image4(image3);
image1= image3; // these images are new copies of the source image
image3.copyTo(image2);
cv::Mat image5= image3.clone(); // transform the image for testing
cv::flip(image3,image3,1); // check which images have been affected by the processing
cv::imshow("Image 3", image3);
cv::imshow("Image 1", image1);
cv::imshow("Image 2", image2);
cv::imshow("Image 4", image4);
cv::imshow("Image 5", image5);
cv::waitKey(0); // wait for a key pressed // get a gray-level image from a function
cv::Mat gray= function(); cv::imshow("Image", gray); // show the image
cv::waitKey(0); // wait for a key pressed // read the image in gray scale
image1= cv::imread("lena.jpg", CV_LOAD_IMAGE_GRAYSCALE); // convert the image into a floating point image [0,1]
image1.convertTo(image2,CV_32F,1/255.0,0.0); cv::imshow("Image", image2); // show the image
cv::waitKey(0); // wait for a key pressed return 0;
} int main() {
// create a new image made of 240 rows and 320 columns
//cv::Mat image1(240,320,CV_8U,100);
// or:
//cv::Mat image1(240,320,CV_8U,cv::Scalar(100));
cv::Mat image1(cv::Size(320,240),CV_8U,cv::Scalar(100)); cv::imshow("Image", image1); // show the image
cv::waitKey(0); // wait for a key pressed // re-allocate a new image
// (only if size or type are different)
image1.create(200,200,CV_8U);
image1= 255; cv::imshow("Image", image1); // show the image
cv::waitKey(0); // wait for a key pressed // create a red color image
// channel order is BGR
cv::Mat image2(240,320,CV_8UC3,cv::Scalar(0,0,255)); // or:
// cv::Mat image2(cv::Size(320,240),CV_8UC3);
// image2= cv::Scalar(0,0,255); cv::imshow("ImageRed", image2); // show the image
cv::waitKey(0); // wait for a key pressed // read an image
cv::Mat image3= cv::imread("lena.jpg"); // all these images point to the same data block
cv::Mat image4(image3);
image1= image3;
cv::imshow("Image 4", image4);
cv::imshow("Image 1", image1);
cv::waitKey(0); // wait for a key pressed
cv::flip(image3,image3,1);
cv::imshow("Image 43", image4);
cv::imshow("Image 13", image1);
cv::waitKey(0); // wait for a key pressed
// these images are new copies of the source image
image3.copyTo(image2);
cv::Mat image5= image3.clone(); // transform the image for testing
cv::flip(image3,image3,1);
//
// check which images have been affected by the processing
cv::imshow("Image 3", image3);
cv::imshow("Image 2", image2);
cv::imshow("Image 5", image5);
cv::waitKey(0); // wait for a key pressed // get a gray-level image from a function
cv::Mat gray= function(); cv::imshow("Image", gray); // show the image
cv::waitKey(0); // wait for a key pressed // read the image in gray scale
image1= cv::imread("lena.jpg", CV_LOAD_IMAGE_GRAYSCALE);
cv::imshow("image1-CV_LOAD_IMAGE_GRAYSCALE", image1); // show the image
// convert the image into a floating point image [0,1]
image1.convertTo(image2,CV_32F,1/255.0,0.0); cv::imshow("Image2 floating point", image2); // show the image
cv::waitKey(0); // wait for a key pressed cv::Mat ima(500,500,CV_8U,50);
cv::imshow("ima", ima);
cv::waitKey(0); // wait for a key pressed cv::Scalar red(0,0,255);
std::cout<<red<<std::endl;
cv::Mat imaC3(500,500,CV_8UC3,red);
cv::imshow("imaC3", imaC3);
cv::waitKey(0); // wait for a key pressed cv::Scalar blue(255,0,0);
std::cout<<blue<<std::endl;
cv::Mat imablue(500,500,CV_8UC3,blue);
cv::imshow("blue", imablue);
cv::waitKey(0); // wait for a key pressed
return 0;
}