opencv4-图像操作

这里Vex3f 也可以是Vec3b

#include<opencv2\opencv.hpp>
#include<iostream>
#include<math.h>
using namespace cv;
using namespace std;

//读写图像,
//读写像素,
//修改像素值
int main()
{
	Mat src = imread("E:\\vs2015\\opencvstudy\\2.jpg", 1);
	if (!src.data)
	{
		cout << "could not load image!" << endl;
		return -1;
	}
	//imshow("inputImage", src);


	//单通道取反
	Mat gray_src;
	cvtColor(src, gray_src, CV_BGR2GRAY);
	//imshow("gray_src", gray_src);

	int rows = gray_src.rows;
	int cols = gray_src.cols;
	
	Mat gray_src_invert = Mat::zeros(gray_src.size(), gray_src.type());
	/*for (int row = 0; row < rows; row++)
	{
		for (int col = 0; col < cols; col++)
		{
			int gray_pixel = gray_src.at<uchar>(row, col);
			gray_src_invert.at<uchar>(row, col) = 255 - gray_pixel;
		}
	}
	imshow("gray_src_invert", gray_src_invert);*/

	//3通道取反
	Mat dst,dst2;
	dst.create(src.size(), src.type());
	dst2.create(src.size(), src.type());

	int height = src.rows;
	int width = src.cols;
	int nc = src.channels();
	for (int row = 0; row < height; row++)
	{
		for (int col = 0; col < width; col++)
		{
			if (nc == 1)
			{
				int gray_pixel = gray_src.at<uchar>(row, col);
				gray_src_invert.at<uchar>(row, col) = 255 - gray_pixel;
			}
			else if (nc == 3)
			{
				int b = src.at<Vec3b>(row, col)[0];
				int g = src.at<Vec3b>(row, col)[1];
				int r = src.at<Vec3b>(row, col)[2];
				dst.at<Vec3b>(row, col)[0] = 255 - b;
				dst.at<Vec3b>(row, col)[1] = 255 - g;
				dst.at<Vec3b>(row, col)[2] = 255 - r;
				dst2.at<Vec3b>(row, col) = (r, max(g, b));
			}
		}
	}
	//imshow("3通道取反像素操作", dst);
	Mat bitwise_not_dst;
	bitwise_not(src, bitwise_not_dst);  //位操作取反。等价于上面的像素操作
	//imshow("3通道取反bitwise_not", bitwise_not_dst);
	imshow("dst2", dst2);

	//空白图像赋值
	Mat M3;
	M3.create(gray_src.size(), gray_src.type());
	M3 = Scalar(0);
	imshow("空白图像", M3);

	//ROI选择
	Rect r(300, 300, 1000, 400);  //左上角的横坐标,纵坐标,矩形高度,宽度
	cout << "gray_image height:" << gray_src.rows << endl;
	cout << "gray_image width:" << gray_src.cols << endl;
	Mat smallImg = gray_src(r);
	imshow("ROI截取图像", smallImg);
	waitKey(0);
	return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

chde2Wang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值