<h2><a id="1OpenCV__0"></a>1.OpenCV Image loading,display, Modify and save</h2>
-(CV:: imread) load image
-(CV:: namedwindow) create a window named OpenCV
namedWindow("OpenCV window", 2)// A value of 2 allows you to resize the window with the mouse
-(CV:: imshow) displays the image in the OpenCV window
-(CV:: cvtcolor) converts an image from BGR to grayscale format
-(CV:: imwrite) save the image imwrite(E:... / data/test.tif", result4);
1.2. Image format: imgcodecs hpp
File path: e: \ desktop \ opencv \ build \ include \ opencv2 \ imgcodecs hpp
-(IMREAD_UNCHANGED (< 0)) loads images as is, including alpha channels, if present
-(IMREAD_GRAYSCALE (0)) loads the image as intensity
-(IMREAD_COLOR (> 0)) loads the image in RGB format
1.3. Color conversion code: imgproc hpp
File path: e: \ desktop \ opencv \ sources \ modules \ imgproc \ include \ opencv2 \ imgproc hpp
- (COLOR_RGB2GRAY)
- (COLOR_BGR2YUV)
- (COLOR_BGR2YCrCb)
1.4. Read pictures using OpenCV:
Example 1: myopencv cpp
using namespace cv; using namespace std; int main(int argc, char** argv) { String imageName("../data/HappyFish.jpg"); if (argc > 1) //Ensure that a valid image name parameter is obtained from the command line. Otherwise, the default camera is happyfish jpg { imageName = argv[1]; //Call the cv::imread function of the image name specified by the first parameter (argv[1]). } Mat image; //Create a Mat object that will store the data of the loaded image. image = imread(imageName, IMREAD_COLOR); //Read the file and load the image in RGB format if (image.empty()) // Check invalid input { cout << "Could not open or find the image!Cannot open or find image!" << std::endl; return -1; } namedWindow("Display window", WINDOW_AUTOSIZE); // Create a window called OpenCV imshow("Display window", image); // Display image in OpenCV window waitKey(0); // Wait for the key in the window, and the window is displayed until the user presses a key return 0; }
Example 2: myopencv cpp
#include <opencv2\opencv.hpp> int main() { cv::Mat img = cv::imread("E:/desktop/test.jpg");//Load image cv::imshow("test", img); //Display image in window cv::waitKey(); return 0; }
Execution results:
1.5. Convert images from BGR to grayscale format
Example 1: myopencv cpp
#include <opencv2/core.hpp> #include <opencv2/imgcodecs.hpp> #include <opencv2/highgui.hpp> #include <iostream> #include <string> #include <opencv2/opencv.hpp> using namespace cv; using namespace std;
int main(int argc, char argv)
{
String imageName(".../data/test.jpg");
If (argc > 1) / / make sure to obtain a valid image name parameter from the command line. Otherwise, the default camera is happyfish jpg
{
imageName = argv[1]; // Call the cv::imread function of the image name specified by the first parameter (argv [1]).
}
Mat image; // Create a mat object that will store the data of the loaded image.
image = imread(imageName, IMREAD_COLOR); // Read the file and load the image in RGB format
if (image.empty()) / / check for invalid input
{
Cout < < "Could not open or find the image!"<< std::endl;
return -1;
}
Mat gray_image; // Create a mat object that will store the data of the loaded image.
cvtColor(image, gray_image, COLOR_BGR2GRAY);// Convert pictures from BGR to grayscale format
imwrite("…/data/test1.jpg", gray_image);// Save the converted picture
namedWindow(imageName, WINDOW_AUTOSIZE); // Create a window called OpenCV
namedWindow(“gray_image”, WINDOW_AUTOSIZE); // Create a window called OpenCV
imshow(imageName, image); // Display image in OpenCV window
imshow(“gray_image”, gray_image); // Display image in OpenCV window
waitKey(0); // Wait for the key in the window, and the window is displayed until the user presses a key
return 0;
}
Execution results:
2. Take photos, read videos and save videos
2.1. c + + reading video using OpenCV:
#include <opencv2\opencv.hpp> using namespace cv; using namespace std; int main() { //Read video or camera VideoCapture capture(0); while (true) { Mat frame; capture >> frame; imshow("Read video", frame); waitKey(30); //Delay 30 } return 0; }
Execution results:
2.2. photograph
#include <opencv2/opencv.hpp> #include <iostream> using namespace std; using namespace cv; //In the temp folder under the project directory, and sln temp file of the same level string writePath = "../data/"; int main(int argc, char** argv) { VideoCapture capture(0); string name; namedWindow("hello", WINDOW_AUTOSIZE); int i = 0; while (1) { Mat frame; capture >> frame; if (32 == waitKey(20)) { //Space photography name = writePath + to_string(i) + ".jpg"; imwrite(name, frame); cout << name << endl; i++; } if (97 == waitKey(10)) { //'a' exit break; } imshow("hello", frame); imwrite("E:../data/test.tif", frame);//Save image } //waitKey(0); }
Take pictures by space
2.3. Batch save
char file_img[100]; String num[11] = { " ","1","2","3","4","5","6","7","8","9","0"}; for (int i = 1; i <= 10; i++) { ... //Mat roi = src(rect); sprintf_s(file_img, "../data/number/%s.jpg",num[i]);// Give file_img assignment: 1 jpg 2. Jpg et al imwrite(file_img, roi); }
3.OpenCV practice
3.1. Number of tests
#include <opencv2/opencv.hpp> #include <iostream> #include <math.h>
using namespace cv;
using namespace std;
int main(int argc, char argv)
{
Mat src = imread("./image/test14.jpg");
//medianBlur(src, src,5);// Median filter, removing salt and pepper noise
imshow("src", src);
Mat src_gray<span class="token punctuation">,</span> binary<span class="token punctuation">;</span> <span class="token function">cvtColor</span><span class="token punctuation">(</span>src<span class="token punctuation">,</span> src_gray<span class="token punctuation">,</span> COLOR_BGR2GRAY<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">threshold</span><span class="token punctuation">(</span>src_gray<span class="token punctuation">,</span> binary<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">255</span><span class="token punctuation">,</span> THRESH_BINARY <span class="token operator">|</span> THRESH_OTSU<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//Single color , Use THRESH_TRIANGLE is better than OTSU</span> <span class="token function">imshow</span><span class="token punctuation">(</span><span class="token string">"binary"</span><span class="token punctuation">,</span> binary<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//Morphological operation</span> Mat kernel <span class="token operator">=</span> <span class="token function">getStructuringElement</span><span class="token punctuation">(</span>MORPH_RECT<span class="token punctuation">,</span> <span class="token function">Size</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">dilate</span><span class="token punctuation">(</span>binary<span class="token punctuation">,</span> binary<span class="token punctuation">,</span> kernel<span class="token punctuation">,</span> <span class="token function">Point</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token number">2.9</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//Distance transformation</span> Mat dist<span class="token punctuation">;</span> <span class="token function">bitwise_not</span><span class="token punctuation">(</span>binary<span class="token punctuation">,</span> binary<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//Reverse</span> <span class="token function">distanceTransform</span><span class="token punctuation">(</span>binary<span class="token punctuation">,</span> dist<span class="token punctuation">,</span> DIST_L2<span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">normalize</span><span class="token punctuation">(</span>dist<span class="token punctuation">,</span> dist<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1.0</span><span class="token punctuation">,</span> NORM_MINMAX<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">imshow</span><span class="token punctuation">(</span><span class="token string">"dist"</span><span class="token punctuation">,</span> dist<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//Thresholding binary segmentation</span> <span class="token function">threshold</span><span class="token punctuation">(</span>dist<span class="token punctuation">,</span> dist<span class="token punctuation">,</span><span class="token number">0.4</span><span class="token punctuation">,</span><span class="token number">1.0</span><span class="token punctuation">,</span>THRESH_BINARY<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//Filter the distance , Remove edge parts</span> <span class="token function">normalize</span><span class="token punctuation">(</span>dist<span class="token punctuation">,</span> dist<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">255</span><span class="token punctuation">,</span> NORM_MINMAX<span class="token punctuation">)</span><span class="token punctuation">;</span> Mat dist_8U<span class="token punctuation">;</span> dist<span class="token punctuation">.</span><span class="token function">convertTo</span><span class="token punctuation">(</span>dist_8U<span class="token punctuation">,</span> CV_8U<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">adaptiveThreshold</span><span class="token punctuation">(</span>dist_8U<span class="token punctuation">,</span> dist_8U<span class="token punctuation">,</span> <span class="token number">255</span><span class="token punctuation">,</span> ADAPTIVE_THRESH_GAUSSIAN_C<span class="token punctuation">,</span> THRESH_BINARY<span class="token punctuation">,</span> <span class="token number">85</span><span class="token punctuation">,</span> <span class="token number">0.0</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//Adaptive threshold , Replace the above threshold operation</span> <span class="token comment">//Morphological operation , Make partial disconnection</span> kernel <span class="token operator">=</span> <span class="token function">getStructuringElement</span><span class="token punctuation">(</span>MORPH_RECT<span class="token punctuation">,</span> <span class="token function">Size</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">Point</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">dilate</span><span class="token punctuation">(</span>dist_8U<span class="token punctuation">,</span> dist_8U<span class="token punctuation">,</span> kernel<span class="token punctuation">,</span> <span class="token function">Point</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">imshow</span><span class="token punctuation">(</span><span class="token string">"dist_8U"</span><span class="token punctuation">,</span> dist_8U<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// Connected area count</span> vector<span class="token operator"><</span>vector<span class="token operator"><</span>Point<span class="token operator">>></span> contours<span class="token punctuation">;</span> <span class="token function">findContours</span><span class="token punctuation">(</span>dist_8U<span class="token punctuation">,</span> contours<span class="token punctuation">,</span> RETR_EXTERNAL<span class="token punctuation">,</span> CHAIN_APPROX_SIMPLE<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// draw result</span> Mat markers <span class="token operator">=</span> <span class="token class-name">Mat</span><span class="token operator">::</span><span class="token function">zeros</span><span class="token punctuation">(</span>src<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> CV_8UC3<span class="token punctuation">)</span><span class="token punctuation">;</span> RNG <span class="token function">rng</span><span class="token punctuation">(</span><span class="token number">12345</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span>size_t t <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> t <span class="token operator"><</span> contours<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> t<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span> <span class="token function">drawContours</span><span class="token punctuation">(</span>markers<span class="token punctuation">,</span> contours<span class="token punctuation">,</span> <span class="token keyword">static_cast</span><span class="token operator"><</span><span class="token keyword">int</span><span class="token operator">></span><span class="token punctuation">(</span>t<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">Scalar</span><span class="token punctuation">(</span>rng<span class="token punctuation">.</span><span class="token function">uniform</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">255</span><span class="token punctuation">)</span><span class="token punctuation">,</span> rng<span class="token punctuation">.</span><span class="token function">uniform</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">255</span><span class="token punctuation">)</span><span class="token punctuation">,</span> rng<span class="token punctuation">.</span><span class="token function">uniform</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">255</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token function">Mat</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"number of corns : %d"</span><span class="token punctuation">,</span> contours<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">imshow</span><span class="token punctuation">(</span><span class="token string">"Final result"</span><span class="token punctuation">,</span> markers<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">waitKey</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
}
Execution results:
4. (CV:: createtrackbar) slider
CV_EXPORTS int createTrackbar(const String& trackbarname, const String& winname, int* value, int count, TrackbarCallback onChange = 0, void* userdata = 0);
- trackbarname: track bar name.
- winname: the name of the window that will be used as the parent window of the created track bar.
- Value: a pointer to an integer indicating the position of the slider. At the time of creation, the initial position of the slider is the current value of the variable.
- count: value indicating the maximum position that the slider can reach. The value of the minimum position of the slider is always 0.
- onChange: first note that it has a default value of 0. This is a pointer to the callback function, which will call back every time the slider position changes. And the prototype of this function must be void fun(int,void *), where the first parameter is the position of the track bar and the second parameter is user data (see the sixth parameter below). If the callback is a NULL pointer, it means that there is no call to the callback function, and only the third parameter value changes.
- userdata: it also has a default value of 0. This parameter is the data passed to the callback function by the user to process the track bar event. If the value argument of the third parameter used is a global variable, you can completely ignore the userdata parameter.
#include "opencv2/highgui/highgui.hpp" #include "opencv2/imgproc/imgproc.hpp" using namespace cv; using namespace std; int value1 = 10; //Initial value of slider Mat image, image1; static void on_track1(int, void*); //Callback function
int main()
{
//Read in image
image = imread("./image/test2.jpg");
namedWindow("bilateral filtering", 2)// A value of 2 allows you to resize the window with the mouse
//Create slider
createTrackbar("linear dimension of aperture", "bilateral filtering", & value1, 50, on_track1);
on_track1(value1, 0);
<span class="token function">waitKey</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span> <span class="token number">1</span><span class="token punctuation">;</span>
}
static void on_track1(int, void)
{
bilateralFilter(image, image1, value1, value1 2, value1 / 2);
imshow("bilateral filtering", image1);
}
Execution results: