opencv图像差分+otsu方法

来源:互联网 发布:caffeine for mac 编辑:程序博客网 时间:2024/05/15 04:31

最近项目中用到了图像差分,到网上一搜,资料大部分为opencv1.0的,可是我使用的是opencv2.46,因此在编程过程中出现不少问题,在进行 otsu处理的时候,要求图像必须是8bit的,可能没注意到这点,折腾了我两天,总算解决啦。下面直接附上代码吧!

#include "stdafx.h"#include <iostream>#include <opencv2\opencv.hpp>using namespace std;using namespace cv;int main(int argc, char* argv[]){Mat oddImg;Mat evenImg;Mat diffImg;//读取图像oddImg = imread("2odd.bmp",CV_LOAD_IMAGE_GRAYSCALE ); //之前总是错误是因为少了一个0evenImg = imread("2even.bmp",CV_LOAD_IMAGE_GRAYSCALE);imshow("奇场图像",oddImg);imshow("偶场图像",evenImg);//差分图像absdiff(evenImg, oddImg, diffImg);imshow("差分图像",diffImg);//二值化处理:自适应阈值otus,自适应阈值threshold(diffImg, diffImg, 0, 255, CV_THRESH_BINARY | CV_THRESH_OTSU);    imshow("二值化图像",diffImg);waitKey(0);return 0;}

之前我因为在读取图像的时候,是直接使用如下的。

oddImg = imread("2odd.bmp");

evenImg = imread("2even.bmp");

imread函数的原型: Mat imread(const string& filename, int flags=1 ), 

   flags为:

enum{/* 8bit, color or not */    CV_LOAD_IMAGE_UNCHANGED  =-1,/* 8bit, gray */    CV_LOAD_IMAGE_GRAYSCALE  =0,/* ?, color */    CV_LOAD_IMAGE_COLOR      =1,/* any depth, ? */    CV_LOAD_IMAGE_ANYDEPTH   =2,/* ?, any color */    CV_LOAD_IMAGE_ANYCOLOR   =4};

显然,imread默认标记为:CV_LOAD_IMAGE_COLOR

后来,我加上了标记:CV_LOAD_IMAGE_COLOR ,即0,就可以了。


参考:

1.http://docs.opencv.org/modules/highgui/doc/reading_and_writing_images_and_video.html#Mat imread(const string& filename, int flags)

2.http://www.xuebuyuan.com/1528344.html

0 0
原创粉丝点击