访问像素级改变图片效果

/*
像素扫描改变图像
*/
#include<opencv2\opencv.hpp>
#include<iostream>
using namespace cv;
using namespace std;

int main() {
	double t = (double)getTickCount();

	Mat img = imread("D://图片//5.jpg");
	int div = 64;
	if (img.empty()) {
		cout << "图片加载失败" << endl;
		return -1;
	}

	imshow("show_org", img);
	Mat img_tem = img.clone();    //减色
	Mat img_t2 = img.clone();        //通道不同变换
	Mat img_t3 = img.clone();     //锐化图像处理
	Mat img_t4 = img.clone();       //通过核心矩阵锐化图像

	Mat_<Vec3b>::iterator it = img_tem.begin<Vec3b>();
	Mat_<Vec3b>::iterator itend = img_tem.end<Vec3b>();          //基本减色算法,常规迭代遍历
	for (; it != itend; ++it) {
		(*it)[0] = (*it)[0] / div*div + div / 2;
		(*it)[1] = (*it)[1] / div*div + div / 2;
		(*it)[2] = (*it)[2] / div*div + div / 2;
	}

	imwrite("633.jpg", img_tem);     //存入磁盘

	int n1 = img_t2.rows;
	int n2 = img_t2.channels();
	int n3 = img_t2.cols;
	for (int i = 0; i < img_t2.rows; i++) {
		uchar *data = img_t2.ptr<uchar>(i);   //指向对应的一行
		for (int j = 0; j < n3; j++) {
			data[j*n2 + 0] = data[j*n2 + 0] - data[j*n2 + 0] % div - div / 2;
			data[j*n2 + 1] = data[j*n2 + 1] - data[j*n2 + 1] % div;
			data[j*n2 + 2] = data[j*n2 + 2] - data[j*n2 + 2] % div + div / 2;
		}
	}

	int m1 = img_t2.rows;
	int m2 = img_t2.channels();
	int m3 = img_t2.cols;
	Mat result(img.size(), img.type());

	for (int i = 1; i < m1-1; i++) {               //访问相邻像素锐化图像边缘,这里不访问没有完整相邻像素的边缘像素

		uchar *p_now = img_t3.ptr<uchar>(i);
		uchar *p_pre = img_t3.ptr<uchar>(i-1);
		uchar *p_nex = img_t3.ptr<uchar>(i+1);
		uchar *p_out = result.ptr<uchar>(i);

		for (int j = 0; j < m3; j++) {
			if (j == 0 || j == m3-1) {
				p_out[j*m2 + 0] = 0;
				p_out[j*m2 + 1] = 0;
		    	p_out[j*m2 + 2] = 0;
			}
			else {
				p_out[j*m2 + 0] = p_now[j*m2 + 0] * 5 - p_nex[j*m2 + 0] - p_pre[j*m2 + 0] - p_now[(j - 1) * m2 + 0] - p_now[(j + 1) * m2 + 0];
				p_out[j*m2 + 1] = p_now[j*m2 + 1] * 5 - p_nex[j*m2 + 1] - p_pre[j*m2 + 1] - p_now[(j - 1) * m2 + 1] - p_now[(j + 1) * m2 + 1];
				p_out[j*m2 + 2] = p_now[j*m2 + 2] * 5 - p_nex[j*m2 + 2] - p_pre[j*m2 + 2] - p_now[(j - 1) * m2 + 2] - p_now[(j + 1) * m2 + 2];
			}
		}

	}
	
	uchar *r = result.ptr<uchar>(0);
	uchar *rr = result.ptr<uchar>(result.rows-1);
	for (int i = 0; i < m3; i++) {     //对第一行和最后一行没有处理的像素设置黑色
		r[i*m2 + 0] = 0;
		r[i*m2 + 1] = 0;
		r[i*m2 + 2] = 0;

		rr[i*m2 + 0] = 0;
		rr[i*m2 + 1] = 0;
		rr[i*m2 + 2] = 0;
	}

	Mat kernel(3, 3, CV_32F, Scalar(0));   //核定义
	kernel.at<float>(1, 1) = 5;
	kernel.at<float>(0, 1) = -1;
	kernel.at<float>(2, 1) = -1;
	kernel.at<float>(1, 0) = -1;
	kernel.at<float>(1, 2) = -1;
	filter2D(img_t4, img_t4, img_t4.depth(), kernel); //核变换

	
	imshow("show_change", img_tem);
	imshow("show_change_2", img_t2);
	imshow("show_change_3", result);
	imshow("show_KernelChange", img_t4);

	int count = 200;
	for (int i = 0; i < count; i++) {           //椒盐噪声,随机选择一些点变成白色或者黑色
		int j = rand() % img.rows;
		int k = rand() % img.cols;

		if (img.type() == CV_8UC1) {        //不同的图片通道类型对应不同的变换
			img.at<uchar>(j, k) = 255;
		}

		else if(img.type() == CV_8UC3){
			img.at<Vec3b>(j, k)[0] = 255;
			img.at<Vec3b>(j, k)[1] = 255;
			img.at<Vec3b>(j, k)[2] = 255;
		}
	}

	imshow("show_change_img", img);

	double Time = ((double)getTickCount() - t) / getTickFrequency();
	cout << "Time = " << Time << endl;

	waitKey(0);
	destroyAllWindows;
	return 0;
}

运行结果:

访问像素级改变图片效果_第1张图片

锐化一也不知道是什么原因显示奇怪,之后再修改吧

你可能感兴趣的:(访问像素级改变图片效果)