opencv(彩色图像通道操作 +单个像素的定义)

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

int main(int argc, char** argv)
{
	Mat src = imread("1.jpg");
	if (src.empty())
	{
		cout << "could not load image" << endl;
		return -1;
	}
	namedWindow("input", CV_WINDOW_AUTOSIZE);
	imshow("input", src);

	Mat gray;
	cvtColor(src, gray, CV_BGR2GRAY);

	int rows = gray.rows;
	int cols = gray.cols;
	Mat dst = Mat(src.size(), src.type());
	int nc = src.channels();
	for (int row = 0; row < rows; row++)
	{
		for (int col = 0; col < cols; col++)
		{
			if (nc == 1)
			{
				int gray_pixel = gray.at<uchar>(row, col);
				gray.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] = b;
				dst.at<Vec3b>(row, col)[1] = 0;
				dst.at<Vec3b>(row, col)[2] = 0;
				//gray.at<uchar>(row, col) = min(r, min(b, g));
			}
		}
	}

	//位操作,按位取反,实现反相:bitwise_not(src, dst);
	namedWindow("output", CV_WINDOW_AUTOSIZE);
	imshow("output", dst);
	waitKey(0);
	return 0;
}

 

/*3通道的图像的像素定义+单通道的像素的定义*/

int b = src.at<Vec3b>(row, col)[0]; 

gray.at<uchar>(row, col);

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值