图像反转:f[i] = 255-f[i]

方法1:

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);

方法2:

for( i = 0; i < nRows; ++i){
        p = I.ptr<uchar>(i);
        for ( j = 0; j < nCols; ++j){
            p[j] = 255 - p[j];
        }
}

方法3:

MatIterator_<uchar> it, end;
            for( it = I.begin<uchar>(), end = I.end<uchar>(); it != end; ++it)
                *it = 255 - *it;

方法4:

opencv提供的映射关系

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
using namespace cv;
 
Mat applyLookUp(const cv::Mat& image,const cv::Mat& lookup) { 
	Mat result;
	cv::LUT(image,lookup,result);
	return result;
}
 
int main( int, char** argv )
{
  	Mat image,gray;
  	image = imread( argv[1], 1 );
  	if( !image.data )
  		return -1;
  	cvtColor(image, gray, CV_BGR2GRAY);
    
	Mat lut(1,256,CV_8U);
	for (int i=0; i<256; i++) {
		lut.at<uchar>(i)= 255-i;
	}
 
	Mat out = applyLookUp(gray,lut);
	namedWindow("sample");
	imshow("sample",out);
 
  	waitKey(0);
  	return 0;
}

耗时:

第一种:On-The-Fly RA93.7878 milliseconds
第二种:Efficient Way79.4717 milliseconds
第三种:Iterator83.7201 milliseconds
第四种:LUT function32.5759 milliseconds

发布了210 篇原创文章 · 获赞 105 · 访问量 12万+

猜你喜欢

转载自blog.csdn.net/qq_30263737/article/details/95624769
今日推荐