使用opencv对图像进行负片处理
程序员文章站
2022-07-05 16:14:22
...
对图像进行“负片处理”
#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;
}
上一篇: Mac怎么连接多个蓝牙音箱?