數字圖像處理2:通過滑桿控制圖像

#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

using namespace std;
using namespace cv;
const int slider_max = 64;
int slider;
Mat image;
Mat result;

void colorReduce(Mat &inputImage, Mat &outputImage, int div)
{
	outputImage = inputImage.clone();
	int rows = inputImage.rows;
	int cols = inputImage.cols * inputImage.channels();
	if (inputImage.isContinuous())
	{
		cols *= rows;
		rows = 1;
	}
	for (int i = 0; i < rows; i++)
	{
		uchar *dataout = outputImage.ptr<uchar>(i);
		for (int j = 0; j < cols; j++)
		{
			dataout[j] = dataout[j] / div * div + div / 2;//指針也可以像數組一樣用索引訪問
		}
	}
}
void ontrackBar(int pos, void *)
{
	if (pos <= 0)
		result = image;
	else
		colorReduce(image, result, pos);
	imshow("顯示結果", result);
}
int main()
{
	image = imread("f:\\圖片\\小包總.jpg");
	namedWindow("原圖片");
	namedWindow("顯示結果");
	slider = 0;
	createTrackbar("ColorReduce", "顯示結果", &slider, slider_max, ontrackBar);
	imshow("原圖片", image);
	imshow("顯示結果", image);
	waitKey(0);
	return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章