Input/Output
Use imdecode and imencode to read images in memory instead of files!
Basic operations with images:
Accessing pixel intensity values
I wrote a program to test this:
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 }
The test results are as follows: you can see
Memory management and reference counting
Use std::vector to generate a series of two-dimensional and three-dimensional points, fill the array, and then convert to Mat type:
and: visit a point:
Point2f point = pointsMat.at<Point2f>(i, 0);
Memory management and reference counting
The counting system is introduced to reduce memory overhead. Since images usually occupy a large amount of memory, it will cause computational tension. Therefore, data will be copied unless deep copying is performed. In other cases, in C language, a new pointer is added to point to The memory where the data resides.
//3D point
//deep copy
Unlike the previous C API development which required providing an output image, an empty Mat is now provided for each function boost. Each implementation calls Mat::create for the destination matrix. If it is empty, the data will be allocated; if it is not empty and the dimensions and data precision are correct, then nothing will be done; if the dimensions and data precision are not correct, the data will be lost and new data will be allocated. (Speaking of the program is very simple, but this explanation is slightly detoured)
Primitive operations
img = Scalar(0); //img is actually 0
ROI selection:
Conversion of Mat and traditional C API:
Note: Note that there is no data copying here. This one is required
Conversion of color space:
src.convertTo(dst, CV_32F); //Original data src, dstination image
Visualizing images
//It doesn't mean anything, it's very simple! I have practiced many times before.
Finally, I plan to put this program up and test it:
#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 file with debug content added:
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)
The test results are as follows: