二值图像的创建很容易,本文绘制有阶跃梯度的边缘图像,为后面的实验做准备,黑色变白色以及白色变黑色便会产生明显边缘。
本文还绘制了倾斜的边缘。
首先,创建左右黑白二值图像:
#include<iostream>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace std;
using namespace cv;
int main()
{
Mat src(Size(200,200),CV_8UC1,Scalar::all(0));
//src(Range::all(),Range(0,100))=255;
src(Range::all(),Range(101,200))=255;
imshow("src",src);
waitKey(0);
system("pause");
return 0;
}
fig.1
fig.2
然后,创建上下黑白二值图像:
#include<iostream>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace std;
using namespace cv;
int main()
{
Mat src(Size(200,200),CV_8UC1,Scalar::all(0));
//src(Range(101,200),Range::all())=255;
src(Range(0,100),Range::all())=255;
imshow("src",src);
waitKey(0);
system("pause");
return 0;
}
fig.3
fig.4
再然后绘制斜对角线图像:
#include<iostream>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace std;
using namespace cv;
int main()
{
Mat src(Size(200,200),CV_8UC1,Scalar::all(0));
int center_x=src.cols/2;
int center_y=src.rows/2;
const double k=1.0;
for (int j=0;j<src.rows;j++)
{
int y=k*(j-center_x)+center_y;
//src(Range(0,j),Range(y,200))=255;
src(Range(j,200),Range(0,y))=255;
}
imshow("src",src);
waitKey(0);
system("pause");
return 0;
}
fig.5
fig.6
改变起始点斜对角线:
#include<iostream>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace std;
using namespace cv;
int main()
{
Mat src(Size(200,200),CV_8UC1,Scalar::all(0));
int center_x=src.cols/2;
int center_y=src.rows/2;
const double k=0.5;
for (int j=0;j<src.rows;j++)
{
int y=k*(j-center_x)+center_y;
//src(Range(0,j+1),Range(y,200))=255;
src(Range(j,200),Range(0,y))=255;
}
imshow("src",src);
imwrite("13.bmp",src);
waitKey(0);
system("pause");
return 0;
}
fig.7
fig.8
相反方向:
#include<iostream>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace std;
using namespace cv;
int main()
{
Mat src(Size(200,200),CV_8UC1,Scalar::all(0));
int center_x=src.cols/2;
int center_y=src.rows/2;
const double k=-0.5;
for (int j=0;j<src.rows;j++)
{
int y=k*(j-center_x)+center_y;
src(Range(0,j+1),Range(0,y))=255;
//src(Range(j,200),Range(y,200))=255;
}
imshow("src",src);
imwrite("10.bmp",src);
waitKey(0);
system("pause");
return 0;
}
fig.9
fig.10
到此,基本的二值图像绘制完毕
模糊边缘待补充~~~