初始化一张单通道图,注意:给单通道图赋值不能用成员函数at() 必须使用ptr()
访问了每个像素修改像素尺寸追溯python图像处理(一)
代码如下:


    #include<opencv2/opencv.hpp>
#include<opencv2/core/core.hpp>        // 核心组件
#include<opencv2/highgui/highgui.hpp>  // GUI
#include<opencv2/imgproc/imgproc.hpp>  // 图像处理

using namespace cv;
using namespace std;

int main()
{
	Mat image;
	image = imread("C:\\Users\\Administrator\\Desktop\\test.jpg");
	imshow("test", image);
	int height = image.rows;
	int width = image.cols;
	int h = height / 2;
	int w = width / 2;
	//Mat img(height,width,CV_8UC3,cv::Scalar::all(0));//CV_8UC1是单通道 2是双
	Mat img = Mat::zeros(h+1,w+1, CV_8UC1);
	for (int i = 0; i <height; i+=2)
	{
		int z = i / 2;
		uchar* outdata = img.ptr<uchar>(z);
		for (int j = 0; j < width; j+=2)
		{
			int x = j / 2;
			int a = image.at<Vec3b>(i, j)[0];
			int b = image.at<Vec3b>(i, j)[1];
			int c = image.at<Vec3b>(i, j)[2];
			int gray = sqrt(pow(a, 2) + pow(b, 2) + pow(c, 2));
			outdata[x] = gray;
			//cout << outdata[j];
		}
	}
	imshow("test1", img);
	waitKey(0);
	return 0;
}

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐