opencv代码

#include<opencv2/opencv.hpp>
#include
using namespace std;
using namespace cv;
Mat src,dst;
void CallBack_Demo(int, void*);
char out[] = “outPut”;
int element_size = 3;
int max_size = 21;
int main()
{
//显示图像

src = imread("C:\\Users\\siasun\\Desktop\\10.jpg");
if (src.empty())
{
	cout << "counld not load image......" << endl;
	return -1;
}
namedWindow("image", CV_WINDOW_AUTOSIZE);
imshow("image", src);
//显示灰度图
cvtColor(src, dst, CV_BGR2GRAY);
namedWindow("image", CV_WINDOW_AUTOSIZE);
imshow("image", dst);
printf("input image channels:%d\n", dst.channels());

//显示图像行列
int cols = dst.cols;
int rows = dst.rows;
printf("rows:%d cols:%d\n", rows,cols);
const uchar*firstRow = dst.ptr<uchar>(0);
printf("input image channels:%d\n", *firstRow);

//创建一张mat图像
Mat M(3, 3, CV_8UC3, Scalar(0, 0, 255));
//imshow("image", M);

//创建与某图大小类型一致的空白图像
Mat m2 = Mat::zeros(dst.size(), dst.type());
//imshow("image", m2);

//掩模,图像增强的两种方法
cols = src.cols * src.channels();
rows = src.rows;
int offset = src.channels();
Mat dst1 = Mat::zeros(src.size(), src.type());
/*for (int row = 1; row < rows; row++)
{
	const uchar* current = src.ptr<uchar>(row);
	const uchar* previous = src.ptr<uchar>(row - 1);
	const uchar* next = src.ptr<uchar>(row + 1);
	uchar* output = img.ptr<uchar>(row);
	for (int col = offset; col < cols; col++)

		output[col] = saturate_cast<uchar>(5 * current[col] - (current[col - offset] + current[col + offset] + previous[col] + next[col]));

}*/
Mat kernel = (Mat_<char>(3, 3) << 0, -1, 0, -1, 5, -1, 0, -1, 0);
filter2D(src, dst1, src.depth(),kernel);
//imshow("image", dst1);
//imshow("image1", src);

//读取图像像素,获取反差图
Mat gray;
cvtColor(src, gray, CV_BGR2GRAY);
int height, width;
height = gray.rows;
width = gray.cols;
for (int row = 0; row < height; row++)
{
	for (int col = 0; col < width; col++)
	{
		int gray_value = gray.at<uchar>(row, col);
		gray.at<uchar>(row, col) = 255 - gray_value;
	}
}

//多通道反差图
Mat grayMul,invertImg;
cvtColor(src, gray, CV_BGR2GRAY);
invertImg = Mat::zeros(src.size(), src.type());                                                                                                                                       
int height1, width1;
height1 = gray.rows;
width1 = gray.cols;
int channel = src.channels();
for (int row = 0; row < height1; row++)
{
	for (int col = 0; col < width1; col++)
	{
		if (channel == 1)
		{
			int gray_value = gray.at<uchar>(row, col);
			gray.at<uchar>(row, col) = 255 - gray_value;
		}
		else
		{
			int chan1 = src.at<Vec3b>(row, col)[0];
			int chan2 = src.at<Vec3b>(row, col)[1];
			int chan3 = src.at<Vec3b>(row, col)[2];
			invertImg.at<Vec3b>(row, col)[0] = 255 - chan1;
			invertImg.at<Vec3b>(row, col)[1] = 255 - chan2;
			invertImg.at<Vec3b>(row, col)[2] = 255 - chan3;
		}
	}
}
//opencv反差图算法
bitwise_not(src, invertImg);//一个算子搞定
//Vec3b对应三通道的顺序是bgr的uchar类
//Vec3f对应float类型数据
//把cv_8uc1转到cv32f1:src.convertTo(dst,CV_32F)
//取三个数最小值思想min(a,min(b,c))

//图像混合
/*int alpha = 0.5;//权重
addWeighted(src, alpha, src1, (1 - alpha),dst);*/
char input_title[] = "input title";
//模糊图像
blur(src, dst, Size(5, 5), Point(-1, -1));
GaussianBlur(src, dst, Size(5, 5), 11, 11);
medianBlur(src, dst, 3);//输入,输出,模板大小
//双边模糊,更好的保护边缘信息
bilateralFilter(src, dst, 15, 100, 5);//计算半径,差值范围(以内的用来计算),模板大小
imshow(input_title, dst);
//膨胀 结构元素覆盖下图像的最大像素值
//腐蚀 结构元素覆盖下图像的最小像素值
//char out[] = "outPut";
namedWindow("out", CV_WINDOW_AUTOSIZE);
createTrackbar("element_size:", "out", &element_size, max_size, CallBack_Demo);
CallBack_Demo(0, 0);
//形态学
//基本梯度 膨胀减去腐蚀
//内部梯度 原图减去
//方向梯度 x与y方向做梯度
//黑帽 闭操作与原图的差值图像
//顶帽 开操作与原图像的差值图像
Mat strucElement = getStructuringElement(MORPH_RECT, Size(3,3), Point(0, 0));
morphologyEx(src, dst, CV_MOP_TOPHAT, strucElement);

//图像金字塔
//上采样
pyrUp(src, dst, Size(src.cols * 2, src.rows * 2));
pyrDown(src, dst, Size(src.cols / 2, src.rows / 2));
//高斯不同
Mat g1, g2, gr, dogImg;
cvtColor(src, gr, CV_BGR2GRAY);

GaussianBlur(gr, g1, Size(5, 5), 0, 0);
GaussianBlur(g1, g2, Size(5, 5), 0, 0);
subtract(g1, g2, dogImg, Mat());
//sobel边缘检测
Mat gray_src;
GaussianBlur(src, dst, Size(3, 3), 0, 0);
cvtColor(dst, gray_src, CV_BGR2GRAY);
Mat xgrad, ygrad;
Sobel(gray_src, xgrad, CV_16S, 1, 0, 3);
Sobel(gray_src, ygrad, CV_16S, 0, 1, 3);
convertScaleAbs(xgrad, xgrad);//取绝对值
convertScaleAbs(ygrad, ygrad);
Mat xygray;
//x,y分配权重
addWeighted(xgrad, 0.5, ygrad, 0.5,0, xygray,-1);
//分配权重,替换addweighted
xygray = Mat(xgrad.size(), xgrad.type());
int heigh = ygrad.rows;
int weigh = xgrad.cols;
for (int row = 0; row < heigh; row++)
{
	for (int col = 0; col < weigh; col++)
	{
		int x = xgrad.at<char>(row, col);
		int y = ygrad.at<char>(row, col);
		int n = x + y;
		xygray.at<char>(row, col) = saturate_cast<char>(n);
	}
}
//another method 求边缘
Scharr(gray_src, xgrad, CV_16S, 1, 0);
Scharr(gray_src, ygrad, CV_16S, 0, 1);
//laplance边缘
Mat lap_img;
GaussianBlur(src, dst, Size(3, 3), 0, 0);
cvtColor(dst, gray_src, CV_BGR2GRAY);
Laplacian(gray_src, lap_img, CV_16S, 3);
convertScaleAbs(lap_img, lap_img);
threshold(lap_img, lap_img, 0, 255, THRESH_OTSU | THRESH_BINARY);
//canny 边缘
//高斯模糊、灰度、梯度(sobel,scharr)、非最大值抑制、阈值
Canny(gray_src, dst, 20, 40, 3, false);
//直方图均衡化,提高图像对比度
equalizeHist(src, dst);//src需为八位单通道图像
waitKey(0);

return 0;

}

void CallBack_Demo(int, void*)
{
int s = element_size * 2 + 1;
Mat strucElement = getStructuringElement(MORPH_RECT, Size(s,s), Point(0,0));
dilate(src, dst, strucElement, Point(-1, -1),1);//后面的1指膨胀迭代次数
imshow(“out”, dst);
return;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值