opencv图像的操作day2(c++)

#include<opencv2\opencv.hpp>
#include<iostream>
#include<math.h>

using namespace cv;
using namespace std;
int main()
{
	Mat src=imread("aa.jpg");
	if(!src.data)
	cout<<"failed";
	imshow("src",src);

	Mat grey;
	cvtColor(src,grey,CV_BGR2GRAY);
	//imshow("grey",grey);

	int height=grey.rows;
	int width= grey.cols;

	for(int i=0;i<height;i++)
	{
		for(int j=0;j<width;j++)
		{
				int gray=grey.at<uchar>(i,j);
				grey.at<uchar>(i,j)=255-gray;
		}
	}
	imshow("grey",grey);

	Mat dst,dst1;
	int nc=src.channels();
	dst.create(src.size(),src.type());
	int row =src.rows;
	int colo=src.cols;
	for(int i=0;i<row;i++)
	{
		for(int j=0;j<colo;j++)
		{
			if(nc==1)
			{
				int gray =grey.at<uchar>(i,j);
				grey.at<uchar>(i,j)=255-gray;
			}
			else if(nc==3)
			{
				int b=src.at<Vec3b>(i,j)[0];
				int g=src.at<Vec3b>(i,j)[1];
				int r=src.at<Vec3b>(i,j)[2];
				dst.at<Vec3b>(i,j)[0]=255-b;
				dst.at<Vec3b>(i,j)[1]=255-g;
				dst.at<Vec3b>(i,j)[2]=255-r;
			}
		}
	}
	imshow("dst",dst);
	bitwise_not(src,dst1);
	imshow("dst1",dst1);
	waitKey(0);
	
	return 0;
 }

猜你喜欢

转载自blog.csdn.net/suntingsheng123/article/details/87877506