OpenCV [OpenCV] OpenCV Actual Warfare From Beginning to Proficiency 12 - Teach you how to use pointers to access pixels

This article is the twelfth in the OpenCV Practical Warfare Series from Introduction to Proficiency

[OpenCV] OpenCV Actual Warfare: An Introduction to OpenCV Macro

[OpenCV] OpenCV Practical Operations From Introduction to Proficiency II--How OpenCV Carries out Image Corrosion Operations

[OpenCV] OpenCV Practical Combat from Introduction to Proficiency 3 - canny Edge Detection

[OpenCV] OpenCV Practical Warfare: From Beginning to Proficiency IV - Common Function Explanations (mat, imread, imshow, imwrite, nameWindow)

[OpenCV] OpenCV Practical Warfare From Introduction to Proficiency Fifth - Teach you to Use Rect() Function

OpenCV [OpenCV][OpenCV] Actual combat from entry to proficiency six - teach you how to use the slider - createTrackbar() function

OpenCV [OpenCV] OpenCV Practical Warfare From Beginning to Proficiency Seven - Teach you how to use the mouse to operate SetMouseCallback

[OpenCV] OpenCV Practical Warfare From Beginning to Proficiency Eighth - Take you to a deeper understanding of Mat Containers

[OpenCV] OpenCV Practical Warfare From Beginning to Proficiency Ninth - What are the commonly used data structures and functions of OpenCV?

[OpenCV] OpenCV Practical Warfare From Beginning to Proficiency 10 - Teach you how to use OpenCV to draw basic graphics

[OpenCV] OpenCV Practical Warfare From Introduction to Proficiency 11--Explanation of LUT Functions and How Pixels are Stored in Matrix

Catalog

This article is the twelfth in the OpenCV Practical Warfare Series from Introduction to Proficiency

Three methods

Operation results:

Be careful:

Three methods

//------------------------[]-------------------------------
//           
//---------------------------------------------------------

//------------------------ - [Three ways of accessing elements] - --------------------------------------------------------------------------------------------------------------------------------
//           Header file, namespace
//---------------------------------------------------------
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<iostream>
using namespace std;
using namespace cv;

//-------------------------- - [Global Function Declarations] - --------------------------------------------------------------------------------------------------------------------------
//           
//---------------------------------------------------------
void colorReduce(Mat& inputImage,Mat& outputImage,int div);

//The main function
//           
//---------------------------------------------------------
int main()
{
	//[1] Create the original graph and display it
	Mat srcImage = imread("1.jpg");
	imshow("original image", srcImage);

	//[2] Create effect maps according to the parameters and specifications of original maps
	Mat dstImage;
	dstImage.create(srcImage.rows, srcImage.cols, srcImage.type());//Same size, same type

	//[3] Record start time
	double time0 = static_cast<double>(getTickCount());

	//[4] Call the color space reduction function
	colorReduce(srcImage, dstImage, 32);

	//[5] Calculate run time and output
	time0 = ((double)getTickCount() - time0) / getTickFrequency();
	cout << "This method runs in time of; " << time0 << "second" << endl;

	//[6] Display effect map
	imshow("Design sketch", dstImage);
	waitKey(0);
}
//colorReduce function
//                      Pointer access, operator in C language []
//---------------------------------------------------------
void colorReduce(Mat& inputImage, Mat& outputImage, int div)
{
	//Parameter preparation
	outputImage = inputImage.clone(); //First copy to temporary variables
	int rowNumber = outputImage.rows; //Row number
	int colNumber = outputImage.cols*outputImage.channels();//Column number x channel number = number of elements per row

	//Double loops, traversing all pixel values
	for (int i = 0; i < rowNumber; i++) //Circulate
	{
		uchar* data = outputImage.ptr<uchar>(i); //Get the first address of line i
		for (int j = 0; j < colNumber; j++) //Column loop
		{
			data[j] = data[j] / div*div + div / 2;
			//It can also be written as * data+= * data/div*div+div/2;
		}
	}

}


Operation results:

 

The meaning of each line has been detailed in the code.

Be careful:

uchar* data = outputImage.ptr<uchar>(i); //Get the first address of line i

 

Tags: OpenCV C

Posted on Thu, 10 Oct 2019 14:39:33 -0700 by drak