OpenCV学习遍历图像像素


#include <opencv2/core.hpp>
#include <opencv2/core/utility.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <iostream>

using namespace std;
using namespace cv;

/*static void help(char* progName)
{
	cout << endl
		<< "This program shows how to filter images with mask: the write it yourself and the"
		<< "filter2d way. " << endl
		<< "Usage:" << endl
		<< progName << " [image_name -- default ../data/lena.jpg] [G -- grayscale] " << endl << endl;
}*/


void Sharpen(const Mat& myImage, Mat& Result);

int main(int argc, char* argv[])
{
	//help(argv[0]);
	const char* filename = argc >= 2 ? argv[1] : "C:/Users/dell/Desktop/1.jpg";

	Mat I, J, K;

	if (argc >= 3 && !strcmp("G", argv[2]))
		I = imread(filename, IMREAD_GRAYSCALE);
	else
		I = imread(filename, IMREAD_COLOR);

	namedWindow("Input", WINDOW_AUTOSIZE);
	namedWindow("Output", WINDOW_AUTOSIZE);

	imshow("Input", I);
	double t = (double)getTickCount();//它返回从操作系统启动到当前所经的计时周期数

	Sharpen(I, J);

	t = ((double)getTickCount() - t) / getTickFrequency();//函数执行计时
	cout << "Hand written function times passed in seconds: " << t << endl;

	imshow("OutputJ", J);
	waitKey(0);

	Mat kern = (Mat_<char>(3, 3) << 0, -1, 0,
		-1, 5, -1,
		0, -1, 0);
	t = (double)getTickCount();
	filter2D(I, K, I.depth(), kern);
	t = ((double)getTickCount() - t) / getTickFrequency();
	cout << "Built-in filter2D time passed in seconds:      " << t << endl;

	imshow("OutputK", K);

	waitKey(0);
	return 0;
}
void Sharpen(const Mat& myImage, Mat& Result)
{
	CV_Assert(myImage.depth() == CV_8U);  // accept only uchar images若括号中的表达式值为false,则返回一个错误信息。

	const int nChannels = myImage.channels();
	Result.create(myImage.size(), myImage.type());//create an output image with the same size and the same type as our input.

	for (int j = 1; j < myImage.rows - 1; ++j)
	{
		/*获取图像相邻三行指针*/
		const uchar* previous = myImage.ptr<uchar>(j - 1);
		const uchar* current = myImage.ptr<uchar>(j);
		const uchar* next = myImage.ptr<uchar>(j + 1);
		/*目标图像行*/
		uchar* output = Result.ptr<uchar>(j);
		/*模板如下*/
		/*[p0 p1 p2
		c0 c1 c2
		n0 n1 n2]*/
		//使用如下公式计算目标图像第j行第i列像素值,5*c1-c0-c2-p1-n1;
		for (int i = nChannels; i < nChannels*(myImage.cols - 1); ++i)
		{
			*output++ = saturate_cast<uchar>(5 * current[i]//
				- current[i - nChannels] - current[i + nChannels] - previous[i] - next[i]);//saturate_cast将小于0的置为0,大于255的值置为255,防止溢出。
		}
	}
	/*因为是图像第一行,最后一行,第一列,最后一列没处理,将其置为0*/
	Result.row(0).setTo(Scalar(0));
	Result.row(Result.rows - 1).setTo(Scalar(0));
	Result.col(0).setTo(Scalar(0));
	Result.col(Result.cols - 1).setTo(Scalar(0));
}



#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
double alpha; /*< Simple contrast control */
int beta;  /*< Simple brightness control */
int main(int argc, char** argv)
{
	Mat image = imread("C:/Users/dell/Desktop/1.jpg");
	Mat new_image1 = Mat::zeros(image.size(), image.type());
	Mat new_image2 = Mat::zeros(image.size(), image.type());
	std::cout << " Basic Linear Transforms " << std::endl;
	std::cout << "-------------------------" << std::endl;
	std::cout << "* Enter the alpha value [1.0-3.0]: "; std::cin >> alpha;
	std::cout << "* Enter the beta value [0-100]: "; std::cin >> beta;
	/*
	Since we are operating with BGR images, we will have three values per pixel (B, G and R), 
	so we will also access them separately.
	*/
	for (int y = 0; y < image.rows; y++) {
		for (int x = 0; x < image.cols; x++) {
			for (int c = 0; c < 3; c++) {
				new_image2.at<Vec3b>(y, x)[c] =
					//we use cv::saturate_cast to make sure the values are valid.
					saturate_cast<uchar>(alpha*(image.at<Vec3b>(y, x)[c]) + beta);
			}
		}
	}
	/*where cv::Mat::convertTo would effectively perform *new_image = a*image + beta*.
	However, we wanted to show you how to access each pixel. 
	In any case, both methods give the same result but convertTo is more optimized and works a lot faster.
	*/
	image.convertTo(new_image1, -1, alpha, beta);
	namedWindow("Original Image", 1);
	namedWindow("New Image", 1);
	imshow("Original Image", image);
	imshow("New Image", new_image1);
	imshow("New Image", new_image2);
	waitKey();
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值