欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Opencv学习笔记(三) -------图像操作

程序员文章站 2022-03-25 14:01:48
...

(一)

读取一个GRAY像素值
gray.at(row, col);
读取一个RGB像素值
blue = src.at(row, col)[0];
green = src.at(row, col)[1];
red = src.at(row, col)[2];

(二) Vec3b与Vec3F
Vec3b对应三通道的顺序是blue green red的uchar类型数据
Vec3F对应三通道的float类型数据

(三)位取反函数
bitwise_not(src, dst);

(代码部分)

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

using namespace cv;
using namespace std;

int main(int argc, char** argv)
{
	Mat src;
	src = imread("D:/picture/Curry.jpeg");
	if (src.empty())     //如果没有找到图片
	{
		printf("could not find picture.....\n");
		return -1;
	}
	namedWindow("input image", CV_WINDOW_AUTOSIZE);
	imshow("input image", src);

	Mat gray;
	cvtColor(src, gray, CV_BGR2GRAY);
	int rows = gray.rows;
	int cols = gray.cols;
	for (int row = 0; row < rows; row++)
	{
		for (int col = 0; col < cols; col++)
		{
			int graypros = gray.at<uchar>(row, col);
			gray.at<uchar>(row, col) = 255 - graypros;
		}
	}

	namedWindow("output gray image", CV_WINDOW_AUTOSIZE);
	imshow("output gray image", gray);

	Mat dst;
	dst.create(src.size(), src.type());
	int height = dst.rows;
	int weight = dst.cols;
	for (int row = 0; row < height; row++)
	{
		for (int col = 0; col < weight; col++)
		{
			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("output  image", CV_WINDOW_AUTOSIZE);
	imshow("output  image", dst);
	waitKey(0);   //等待
	return 0;
}

实验效果
Opencv学习笔记(三) -------图像操作