The Core Functionality4(Operations with images)

Input/Output 

使用imdecode和imencode来读取内存中的图像而不是文件!

Basic operations with images:

Accessing pixel intensity values

这个写了一个程序进行测试:

  1 #include <opencv2/opencv.hpp>
  2 #include <opencv2/imgcodecs.hpp>
  3 #include <opencv2/highgui.hpp>
  4 #include <opencv2/imgproc.hpp>
  5 #include <iostream>
  6 
  7 using namespace std;
  8 using namespace cv;
  9 
 10 int main( int argc, char* argv[])
 11 {
 12   int x = 2;
 13   int y = 2;
 14   Mat img = imread("../build/lena.jpg",IMREAD_GRAYSCALE);
 15   Mat img1 = imread("../build/lena.jpg");
 16   Scalar intensity1 = img.at<uchar>(x, y);
 17   Scalar intensity2 = img.at<uchar>(Point(x, y));
 18 
 19   Vec3b intensity = img.at<Vec3b>(x, y);
 20 
 21   int a = img.at<Vec3b>(x, y)[0];
 22 
 23   uchar blue = intensity.val[0];
 24   uchar green = intensity.val[1];
 25   uchar red = intensity.val[2];
 26 
 27   int xx = img.at<uchar>(2, 2);
 28   cout << xx << endl;
 29   cout << intensity1 << endl;
 30   cout << intensity2 << endl;
 31 
 32   cout << (int)blue << " " << (int)green << " " << (int)red << endl;
 33   cout << a << endl;
 34 
 35   namedWindow("Input", WINDOW_AUTOSIZE);
 36   imshow( "Input", img );
 37   waitKey();
 38 

 39 }

测试结果如下:可以看到


Memory management and reference counting

利用std::vector生成一系列的二维点和三维点,填充数组,然后转化为Mat类型:

vector<Point2f> points;
//... fill the array
Mat pointsMat = Mat(points);

以及:访问某一个点:

Point2f point = pointsMat.at<Point2f>(i, 0);

Memory management and reference counting

计数系统是为了减小内存开销而推出的,由于图像通常占用较大的内存,会造成计算紧张,因而除非深度复制才会复制数据,其他情况下,用C语言的话讲就是新添加一个指针指向数据所在的内存。

//三维点

std::vector<Point3f> points;
// .. fill the array
Mat pointsMat = Mat(points).reshape(1); //reshape貌似是更改了通道数

//深层复制

Mat img = imread("image.jpg");
Mat img1 = img.clone();


在之前使用C API开发这需要提供一个输出的image,与其不同的是,现在为每个函数提高提供了一个空的Mat。每个实现为destination matrix调用Mat::create。若为空,则分配数据;非空且维度,数据精度都正确,则啥都不干;如果维度和数据精度不正确,则数据会丢失且会被分配新数据。(话说程序很简单,但这段解释略绕那)

Mat img = imread( "image.jpg");
Mat sobelx;
Sobel(img, sobelx, CV_32F, 1, 0); //sobel算子,以后会学习

Primitive operations

img = Scalar(0); //img其实就是0


ROI选择:

Rect r(10, 10, 100, 100); //矩形
Mat smallImg = img(r);

Mat和传统的C API的转换:

Mat img = imread( "image.jpg");
IPIimage img1 = img;
CvMat m = img;

注意:Note that there is no data copying here.这个是必须的

颜色空间的转换:

Mat img = imread( "image.jpg"); // loading a 8UC3 image
Mat grey;
cvtColor(img, grey, COLOR_BGR2GRAY);

src.convertTo(dst, CV_32F); //原数据src,dstination image

Visualizing images

//没啥意思,很简单!之前已经练习过很多次了。

Mat img = imread( "image.jpg");
namedWindow( "image", WINDOW_AUTOSIZE);
imshow( "image", img);
waitKey();

最后打算把这个程序放上去测试下:

#include <opencv2/opencv.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <iostream>

using namespace std;
using namespace cv;

int main( int argc, char* argv[])
{
  Mat img = imread("lena.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
  cout << minVal << "pixel min intensity" << endl;
  cout << maxVal << "pixel max intensity" << endl;
  
  Mat draw;
  sobelx.convertTo(draw, CV_8U, 255.0/(maxVal - minVal), -minVal * 255.0/(maxVal - minVal));
  namedWindow("image", WINDOW_AUTOSIZE);
  imshow("image", draw);
  waitKey();
  
}

CMakeLists.txt文件,添加了调试内容:

cmake_minimum_required(VERSION 2.8)
set(CMAKE_CXX_FLAGS "-std=c++11") #支持C++11
set(VMAKE_BUILD_TYPE "Debug") #调试
project( DisplayImage )
find_package( OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )
add_executable( DisplayImage main.cpp )
target_link_libraries( DisplayImage ${OpenCV_LIBS} )

install(TARGETS DisplayImage RUNTIME DESTINATION bin)

测试结果如下:


猜你喜欢

转载自blog.csdn.net/qq_27806947/article/details/80156252