OpenCV学习之图像操作

读写图像

imread();
namedWindow();
imshow();
moveWinodw();

读写像素

  • 读一个GRAY像素点的像素值(CV_8UC1)Scalar intensity = img.at(x,y);或者者Scalar intensity = img.at(Point(x,y));
  • 读一个RGB像素点的像素值

Vec3f intensity = img.at(y,x);
float blue = intensity.val[0];
float green = intensity.val[1];
float red = intensity.val[2];

修改像素值

  • 灰度图像

img.at(y,x) = 128;

  • RGB三通道图像

img.at(y,x)[0] = 128;//blue
img.at(y,x)[1] = 128;/green
img.at(y,x)[2] = 128;//red

  • 空白图像

img = Scalar(0);

  • ROI选择

Rect r(10,10,100,100);
Mat smalling = img( r );

#include<oencv2/opencv.hpp>
#include<iostream>
#include<math.h>

using namespace std;
using namespace cv;

int main(int argv,char* argc)
{
	src = imread("C:/Users/26444/Desktop/testopencvinstall/OIP.jpg");
	if(src.empty())
	{
		printf("could not image.....\n");
		return -1;
	}
	namedWindow("inpt",WINDOW_AUTOSIZE);
	imshow("input",src);	
	/***************************************/
	//改变灰度值
	cvtColor(src,gray_src,COLOR_BGR2GRAY);
	namedWindow("output1",WINDOW_AUTOSIZE);
	imshow("output1",gray_src);
	
	int height = gray_src.rows;//获取总行数
	int width = gray_src.cols;//获取总列
	/***************************************/
	//单通道
	for(int row = 0;row < height; row++)
	{
		for(int col = 0;col < width;col++)
		{
			int gray = gray_src.at<uchar>(row,col);
			gray_src.at<uchar>(row,col) = 255 - gray;
		}
	}
	namedWindow("output2",WINDOW_AUTOSIZE);
	inshow("output2",gray_src);
	/***************************************/
	//三通道
	Mat dst;
	dst.create(src.size(),src.type());//初始化图像
	height = src.rows;
	width = src.cols;
	int nc = src.channels();//通道数
	for(int row = 0;row < height ;row++)
	{
		for(int col = 0;col < width; coll++)
		{
			if(nc == 1)
			{
				int gray = src.at<uchar>(row,col);
				src.at<uchar>(row,col) = 255 - gray;
			}
			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;
			}
		}
	}
		//bitwise_not(src,dst);//三通道函数
	namedWindow("output3",WINDOW_AUTOSIZE);
	imshow("output3",dst);
	/**************************************************/
	//取灰度的方式 要有math.h头文件
	for (int row = 0; row < height; row++)
	{
		for (int col = 0; col < width; col++)
		{
			if (nc == 1)
			{
				int gray = src.at<uchar>(row, col);
				src.at<uchar>(row, col) = 255 - gray;
			}
			else if (nc == 3)
			{
				int b = dst.at<Vec3b>(row, col)[0];
				int g = dst.at<Vec3b>(row, col)[1];
				int r = dst.at<Vec3b>(row, col)[2];
				dst.at<Vec3b>(row, col)[0] = b;
				dst.at<Vec3b>(row, col)[1] = g;
				dst.at<Vec3b>(row, col)[2] = r;
				gray_src.at<uchar>(row, col) = max(r,max(b,g));

			}
		}
	}
	namedWindow("output4",WINDOW_AUTOSIZE);
	imshow("output4",gray_src);
	waitKey(0);
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值