几个入门的简单程序,和对应的CMakeList, 虽然简单重新测一下写一下也是好的。
图像显示
ShowImage.cxx
#include <opencv2/opencv.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv2/imgcodecs/imgcodecs.hpp> int main(){ cv::Mat srcImage = cv::imread("1.jpg"); imshow("Original Image", srcImage); cv::waitKey(0); return 0; }
CMakeLists.txt
cmake_minimum_required (VERSION 2.8) project (ShowImage) find_package( OpenCV REQUIRED ) include_directories( ${OpenCV_INCLUDE_DIRS} ) # add the executable add_executable (ShowImage ShowImage.cxx) target_link_libraries(ShowImage opencv_core opencv_highgui opencv_imgcodecs)
图像腐蚀
ErodeImage.cxx
#include <opencv2/opencv.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv2/imgcodecs/imgcodecs.hpp> #include <opencv2/imgproc/imgproc.hpp> int main(){ // read image cv::Mat srcImage = cv::imread("1.jpg"); // show original image imshow("Original Image", srcImage); // erode image cv::Mat element = getStructuringElement(cv::MORPH_RECT, cv::Size(15,15)); cv::Mat dstImage; erode(srcImage, dstImage, element); // show results imshow("Image Erosion", dstImage); cv::waitKey(0); return 0; }
CMakeList.txt
cmake_minimum_required (VERSION 2.8) project (ErodeImage) # find OpenCV packages find_package( OpenCV REQUIRED ) include_directories( ${OpenCV_INCLUDE_DIRS} ) # add the executable add_executable (ErodeImage ErodeImage.cxx) target_link_libraries(ErodeImage opencv_core opencv_highgui opencv_imgproc opencv_imgcodecs)
图像模糊
BlurImage.cxx
#include <opencv2/opencv.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv2/imgcodecs/imgcodecs.hpp> #include <opencv2/imgproc/imgproc.hpp> int main(){ // read image cv::Mat srcImage = cv::imread("1.jpg"); // show original image imshow("Original Image", srcImage); // blur image cv::Mat dstImage; blur(srcImage, dstImage, cv::Size(7,7)); // show results imshow("Image Blurring", dstImage); cv::waitKey(0); return 0; }
CMakeList.txt
cmake_minimum_required (VERSION 2.8) project (BlurImage) # find OpenCV packages find_package( OpenCV REQUIRED ) include_directories( ${OpenCV_INCLUDE_DIRS} ) # add the executable add_executable (BlurImage BlurImage.cxx) target_link_libraries(BlurImage opencv_core opencv_highgui opencv_imgproc opencv_imgcodecs)
Canny边缘检测
CannyEdgeDetection.cxx
#include <opencv2/opencv.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv2/imgcodecs/imgcodecs.hpp> #include <opencv2/imgproc/imgproc.hpp> int main(){ // read image cv::Mat srcImage = cv::imread("1.jpg"); // show original image imshow("Original Image", srcImage); // blur image cv::Mat dstImage, edge, grayImage; // create a matrix the same size as srcImage dstImage.create(srcImage.size(), srcImage.type()); // convert color image to gray-scale image // OpenCV2 // cv::cvtColor(srcImage, grayImage, cv::CV_BGR2GRAY); // OpenCV3 cv::cvtColor(srcImage, grayImage, cv::COLOR_BGR2GRAY); // denoising blur(srcImage, dstImage, cv::Size(3,3)); // apply Canny operator Canny( dstImage, edge, 3, 9, 3); // show results imshow("Canny Edge Detection", edge); cv::waitKey(0); return 0; }
CMakeList.txt
cmake_minimum_required (VERSION 2.8) project (CannyEdgeDetection) # find OpenCV packages find_package( OpenCV REQUIRED ) include_directories( ${OpenCV_INCLUDE_DIRS} ) # add the executable add_executable (CannyEdgeDetection CannyEdgeDetection.cxx) target_link_libraries(CannyEdgeDetection opencv_core opencv_highgui opencv_imgproc opencv_imgcodecs)
原文:http://www.cnblogs.com/Xiaoyan-Li/p/5674767.html