站点图标 AI技术聚合

opencv二值化代码实现

基于OPENCV的图像二值化代码:

#include <opencv2/core/core.hpp>  
#include<opencv2/highgui/highgui.hpp>  
#include"opencv2/imgproc/imgproc.hpp"  
#include <iostream>  
 
using namespace cv;
Mat SrcImage;
Mat GrayImage;
Mat BinaryImage;
 
void on_trackbar(int pos,void*)
{
	//转化为二值图
	threshold(GrayImage, BinaryImage, pos, 255, CV_THRESH_BINARY);
	namedWindow("二值图");
	imshow("二值图", BinaryImage);
}
 
void to_GrayImage()
{
	//创建与原图同类型和同大小的矩阵
	GrayImage.create(SrcImage.size(), SrcImage.type());
	//将原图转换为灰度图像
	cvtColor(SrcImage, GrayImage, CV_BGR2GRAY);
	namedWindow("灰度图");
	imshow("灰度图", GrayImage);
}
 
 
void creat_trackbar()
{
	int nThreshold = 0;
	createTrackbar("二值图阈值", "二值图", &nThreshold, 254, on_trackbar);
}
 
int main()
{
	SrcImage = imread("/home/caozilong/Workspace/python/beauty.jpg");
	namedWindow("原图");
	imshow("原图", SrcImage);
	to_GrayImage();
	on_trackbar(100, 0);
	creat_trackbar();
	waitKey();
}

Table of Contents

编译

 g++ erzhihua.cpp `pkg-config --cflags --libs opencv`

运行结果:

减小阈值

CV_THRESH_BINARY_INV模式:

 

结束

文章出处登录后可见!

已经登录?立即刷新
退出移动版