您的位置:首页 > 移动开发

opencv 2 computer vision application programming第五章翻译

2013-05-01 15:41 453 查看
第五章 用生态学过滤方法改变图像

这一章我们讨论:
用形态过滤器磨损和扩大图像
用形态过滤器打开和关闭图像
用形态过滤器做边缘检测和角点检测
用水印分割图像
用抓取切割算法提取前景中物体

到google上找到了书对应的代码下载了,然后条码的边缘检测有了点想法。

/*------------------------------------------------------------------------------------------*\
This file contains material supporting chapter 5 of the cookbook:
Computer Vision Programming using the OpenCV Library.
by Robert Laganiere, Packt Publishing, 2011.

This program is free software; permission is hereby granted to use, copy, modify,
and distribute this source code, or portions thereof, for any purpose, without fee,
subject to the restriction that the copyright notice may not be removed
or altered from any source or altered source distribution.
The software is released on an as-is basis and without any warranties of any kind.
In particular, the software is not guaranteed to be fault-tolerant or free from failure.
The author disclaims all warranties with regard to this software, any use,
and any consequent failure, is purely the responsibility of the user.

Copyright (C) 2010-2011 Robert Laganiere, www.laganiere.name
\*------------------------------------------------------------------------------------------*/

#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include "morphoFeatures.h"

int main()
{
// Read input image
cv::Mat image= cv::imread("C:/testdir/barcode5.jpg",0);
if (!image.data)
return 0;

// Display the image
//   cv::namedWindow("Image");
cv::imshow("Image",image);

// Create the morphological features instance
MorphoFeatures morpho;
morpho.setThreshold(40);

// Get the edges
cv::Mat edges;
edges= morpho.getEdges(image);

// Display the edge image
cv::namedWindow("Edge Image");
cv::imshow("Edge Image",edges);

// Get the corners
morpho.setThreshold(-1);
cv::Mat corners;
corners= morpho.getCorners(image);
cv::morphologyEx(corners,corners,cv::MORPH_TOPHAT,cv::Mat());
cv::threshold(corners, corners, 40, 255, cv::THRESH_BINARY_INV);

// Display the corner image
cv::namedWindow("Corner Image");
cv::imshow("Corner Image",corners);

// Display the corner on the image
morpho.drawOnImage(corners,image);
cv::namedWindow("Corners on Image");
cv::imshow("Corners on Image",image);
/*
// Read and display image of square
image= cv::imread("C:/testdir/square.jpg",0);
cv::namedWindow("Square Image");
cv::imshow("Square Image",image);

// Creating the cross-shaped structuring element
cv::Mat cross(5,5,CV_8U,cv::Scalar(0));
for (int i=0; i<5; i++) {

cross.at<uchar>(2,i)= 1;
cross.at<uchar>(i,2)= 1;
}

// Dilate with a cross
cv::Mat result;
cv::dilate(image,result,cross);

// Display the result
cv::namedWindow("Dilated square with cross");
cv::imshow("Dilated square with cross",result);

// Creating the diamond-shaped structuring element
cv::Mat diamond(5,5,CV_8U,cv::Scalar(1));
diamond.at<uchar>(0,0)= 0;
diamond.at<uchar>(0,1)= 0;
diamond.at<uchar>(1,0)= 0;
diamond.at<uchar>(4,4)= 0;
diamond.at<uchar>(3,4)= 0;
diamond.at<uchar>(4,3)= 0;
diamond.at<uchar>(4,0)= 0;
diamond.at<uchar>(4,1)= 0;
diamond.at<uchar>(3,0)= 0;
diamond.at<uchar>(0,4)= 0;
diamond.at<uchar>(0,3)= 0;
diamond.at<uchar>(1,4)= 0;

// Erode with a diamond
cv::Mat result2;
cv::erode(result,result2,diamond);

// Display the result
cv::namedWindow("Eroded square with diamond");
cv::imshow("Eroded square with diamond",result2);

// Combine the images into one
cv::Mat final(100,300,CV_8U);
cv::Mat window= final(cv::Rect(0,0,100,100));
image.copyTo(window);
window= final(cv::Rect(100,0,100,100));
result.copyTo(window);
window= final(cv::Rect(200,0,100,100));
result2.copyTo(window);

// Display the combined result
cv::namedWindow("Combined");
cv::imshow("Combined",final);

// Save combined result
cv::imwrite("squares.bmp",final);
*/
cv::waitKey();

return 0;
}


morphoFeatures.h:

/*------------------------------------------------------------------------------------------*\
This file contains material supporting chapter 5 of the cookbook:
Computer Vision Programming using the OpenCV Library.
by Robert Laganiere, Packt Publishing, 2011.

This program is free software; permission is hereby granted to use, copy, modify,
and distribute this source code, or portions thereof, for any purpose, without fee,
subject to the restriction that the copyright notice may not be removed
or altered from any source or altered source distribution.
The software is released on an as-is basis and without any warranties of any kind.
In particular, the software is not guaranteed to be fault-tolerant or free from failure.
The author disclaims all warranties with regard to this software, any use,
and any consequent failure, is purely the responsibility of the user.

Copyright (C) 2010-2011 Robert Laganiere, www.laganiere.name
\*------------------------------------------------------------------------------------------*/

#if !defined MORPHOF
#define MORPHOF

#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>

class MorphoFeatures {

private:

// threshold to produce binary image
int threshold;
// structuring elements used in corner detection
cv::Mat cross;
cv::Mat diamond;
cv::Mat square;
cv::Mat x;

void applyThreshold(cv::Mat& result) {

// Apply threshold on result
if (threshold>0)
cv::threshold(result, result, threshold, 255, cv::THRESH_BINARY_INV);
}

public:

MorphoFeatures() : threshold(-1), cross(5,5,CV_8U,cv::Scalar(0)),
diamond(5,5,CV_8U,cv::Scalar(1)),
square(5,5,CV_8U,cv::Scalar(1)),
x(5,5,CV_8U,cv::Scalar(0)){

// Creating the cross-shaped structuring element
for (int i=0; i<5; i++) {

cross.at<uchar>(2,i)= 1;
cross.at<uchar>(i,2)= 1;
}

// Creating the diamond-shaped structuring element
diamond.at<uchar>(0,0)= 0;
diamond.at<uchar>(0,1)= 0;
diamond.at<uchar>(1,0)= 0;
diamond.at<uchar>(4,4)= 0;
diamond.at<uchar>(3,4)= 0;
diamond.at<uchar>(4,3)= 0;
diamond.at<uchar>(4,0)= 0;
diamond.at<uchar>(4,1)= 0;
diamond.at<uchar>(3,0)= 0;
diamond.at<uchar>(0,4)= 0;
diamond.at<uchar>(0,3)= 0;
diamond.at<uchar>(1,4)= 0;

// Creating the x-shaped structuring element
for (int i=0; i<5; i++) {

x.at<uchar>(i,i)= 1;
x.at<uchar>(4-i,i)= 1;
}
}

void setThreshold(int t) {

threshold= t;
}

int getThreshold() const {

return threshold;
}

cv::Mat getEdges(const cv::Mat &image) {

// Get the gradient image
cv::Mat result;
cv::morphologyEx(image,result,cv::MORPH_GRADIENT,cv::Mat());

// Apply threshold to obtain a binary image
applyThreshold(result);

return result;
}

cv::Mat getCorners(const cv::Mat &image) {

cv::Mat result;

// Dilate with a cross
cv::dilate(image,result,cross);

// Erode with a diamond
cv::erode(result,result,diamond);

cv::Mat result2;
// Dilate with a X
cv::dilate(image,result2,x);

// Erode with a square
cv::erode(result2,result2,square);

// Corners are obtained by differencing
// the two closed images
cv::absdiff(result2,result,result);

// Apply threshold to obtain a binary image
applyThreshold(result);

return result;
}

void drawOnImage(const cv::Mat& binary, cv::Mat& image) {

cv::Mat_<uchar>::const_iterator it= binary.begin<uchar>();
cv::Mat_<uchar>::const_iterator itend= binary.end<uchar>();

// for each pixel
for (int i=0; it!= itend; ++it,++i) {
if (!*it)
cv::circle(image,cv::Point(i%image.step,i/image.step),5,cv::Scalar(255,0,0));
}
}
};

#endif


用500万像素的手机拍条码,边缘检测后虽然条码里面很乱但条码边框(黑色)能够确定了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: