#include <opencv2\opencv.hpp>void colorreduce (cv::mat &image, int div=64) { int nr= image.rows;//number of rows int nc= image.cols * image.channels ();//total number of elements per line if (image.iscontinuous ()) { //Then no padded pixels nc= nc*nr; Nr= 1; It is now a 1D array } int n= static_cast<int> (log (static_cast<double> (div))/log (2.0)); Mask used to round the pixel value uchar mask= 0xff<<n;//e.g. for div=16, mask= 0xF0 for (int j=0; j& LT;NR; J + +) { uchar* data= image.ptr<uchar> (j); for (int i=0; i<nc; i++) { *data++= *data&mask + div/2; }//End of Row } } int main (int a rgc,char* argv[]) {Cv::mat pimg;pimg=cv::imread ("lena.jpg"); Cv::imshow ("Image", pImg); Colorreduce (PIMG); cv::imshow ("PIMG", pImg); Cv::waitkey (0); return 0;}
Mat::reshape
Change the shape and number of channels or one of the 2D matrices without duplicating data.
C + +: Mat mat::reshape (int cn, int rows=0) const
Parameters:
CN-Keying the number of new channels. If cn=0, then the number of channels will remain unchanged.
rows– the new number of rows. If rows = 0, then the number of rows remains the same.
if (image.iscontinuous ()) {image.reshape (1,image.cols*image.rows);} int nr= image.rows; Number of rows int nc= image.cols * image.channels ();//total number of elements per line
The same effect as above!
Opencv.2.computer.vision.application.programming.cookbook--efficient scanning of continuous images