首页 > 其他 > 详细

图像处理简单实例[OpenCV 笔记1]

时间:2016-07-16 00:03:20      阅读:389      评论:0      收藏:0      [点我收藏+]

几个入门的简单程序,和对应的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(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)

 

图像处理简单实例[OpenCV 笔记1]

原文:http://www.cnblogs.com/Xiaoyan-Li/p/5674767.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!