几个入门的简单程序,和对应的CMakeList, 虽然简单重新测一下写一下也是好的。

CMake教程传送门

图像显示

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();
return ;
}

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(,));
cv::Mat dstImage;
erode(srcImage, dstImage, element); // show results
imshow("Image Erosion", dstImage);
cv::waitKey();
return ;
}

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(,)); // show results
imshow("Image Blurring", dstImage);
cv::waitKey();
return ;
}

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(,)); // apply Canny operator
Canny( dstImage, edge, , , ); // show results
imshow("Canny Edge Detection", edge);
cv::waitKey();
return ;
}

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)
05-08 15:24