(1)Opencv Mat圖像像素的訪問與遍歷

在開始之前先下載一個可以查看Mat圖像數據的插件

1、打開vs,找到菜單欄->工具->擴展與更新->聯機->搜索Image

2、下載完成之後關閉VS,提示安裝信息後安裝即可。

3、打開ImageWatch。程序運行過程中可在調試模式下查看圖像信息。

 

#include <iostream>
#include <thread>
#include "Matx.h"
#include <opencv2\core.hpp>
#include <opencv2\highgui.hpp>
#include <opencv2\imgproc.hpp>

using namespace cv;
using namespace std;
void colorInverse(cv::Mat src,cv::Mat &dst)
{
	//dst = cv::Mat(src.rows, src.cols, src.type);
	if (src.channels() == 1)
	{
		//像素的遍歷2---通過迭代器訪問像素
		cv::Mat_<uchar>::iterator it = dst.begin<uchar>();
		cv::Mat_<uchar>::iterator it_end = dst.end<uchar>();
		while (it != it_end)
		{
			(*it) = 255 - (*it);
			it++;
		}
	}
	else if (src.channels() == 3)
	{
		cv::Mat_<Vec3b>::iterator it = dst.begin<Vec3b>();
		cv::Mat_<Vec3b>::iterator it_end = dst.end<Vec3b>();
		while (it != it_end)
		{
			(*it)[0] = 255 - (*it)[0];
			(*it)[1] = 255 - (*it)[1];
			(*it)[2] = 255 - (*it)[2];
			it++;
		}
	}



}

int main()
{
	int div = 5;
	cv::Mat src = cv::imread("C:\\Users\\Administrator\\Desktop\\board\\board-01.png");
	cvtColor(src, src, COLOR_BGR2GRAY);
	//像素的訪問1
	uchar *data = src.ptr<uchar>(5);
	uchar d = data[6];
	//像素的訪問2
	d = src.at<uchar>(6,6);
	cout << d << endl;	
	//像素的遍歷1---通過指針遍歷像素
	for (int i = 0; i < src.rows; i++)
	{
		uchar *data = src.ptr<uchar>(i);
		for (int j = 0; j < src.cols*src.channels(); j++)
		{
			data[j] = 255 - data[j];
		}
	}
	Mat dst = src.clone();

	colorInverse(src, dst);

	system("pause");

	return 0;
}

 

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