使用opencv对图像进行负片处理

参考文章

对图像进行“负片处理”

#include "opencv2/highgui/highgui.hpp"  
#include "opencv2/imgproc/imgproc.hpp"  
#include<iostream>
using namespace cv;
void LutFunc(Mat &I);
void AtFunc(Mat &I);
void PtrFunc(Mat &I);
void  IteratorFunc(Mat &I);

int main(int, char** argv)
{
	Mat image, gray;
	image = imread("D:/picture/lena.jpg");
	if (!image.data)
		return -1;
	cvtColor(image, gray, CV_BGR2GRAY);
	Mat img_lut, img_at, img_ptr, img_iterator;
	gray.copyTo(img_lut);
	gray.copyTo(img_at);
	gray.copyTo(img_ptr);
	gray.copyTo(img_iterator);
	double  TickCount = (double)getTickCount();
	LutFunc(img_lut);
	std::cout <<"lut function"<<
		((double)getTickCount() - TickCount) / getTickFrequency()*1000 << "ms" << std::endl;

	TickCount = (double)getTickCount();
	AtFunc(img_at);
	std::cout << "at function" <<
		((double)getTickCount() - TickCount) / getTickFrequency() * 1000 << "ms" << std::endl;

	TickCount = (double)getTickCount();
	PtrFunc(img_ptr);
	std::cout << "ptr function" <<
		((double)getTickCount() - TickCount) / getTickFrequency() * 1000 << "ms" << std::endl;

	TickCount = (double)getTickCount();
	IteratorFunc(img_iterator);
	std::cout << "iterator function" <<
		((double)getTickCount() - TickCount) / getTickFrequency() * 1000 << "ms" << std::endl;

	namedWindow("sample");
	imshow("sample", img_lut);
	waitKey(0);
	return 0;
}

Mat applyLookUp(const cv::Mat& image, const cv::Mat& lookup) {
	Mat result;
	cv::LUT(image, lookup, result);
	return result;
}

void LutFunc(Mat &I)
{
	Mat lut(1, 256, CV_8U);
	for (int i = 0; i < 256; i++) {
		lut.at<uchar>(i) = 255 - i;
	}
	I = applyLookUp(I, lut);
}

void AtFunc(Mat &I)
{
	for (int i = 0; i < I.rows; ++i)
		for (int j = 0; j < I.cols; ++j)
			I.at<uchar>(i, j) = 255 - I.at<uchar>(i, j);
}

void PtrFunc(Mat &I)
{
	for (int i = 0; i < I.rows; ++i)
	{
		uchar *p = I.ptr<uchar>(i);
		for (int j = 0; j < I.cols; ++j)
		{
			p[j] = 255 - p[j];
		}
	}
}

void  IteratorFunc(Mat &I)
{
	MatIterator_<uchar> it, end;
	for (it = I.begin<uchar>(), end = I.end<uchar>(); it != end; ++it)
			*it = 255 - *it;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值