The method of retrieving a table using a LUT (lookup table) to increase the speed at which pixels are read at color reduce.
Implements reading of the data in the Mat object and calculates the speed of color reduce.
1#include <opencv2/core/core.hpp>2#include <opencv2/highgui/highgui.hpp>3#include <iostream>4#include <sstream>5 6 using namespacestd;7 using namespaceCV;8 9 Static voidHelp () {Ten cout One<<"Author:bjtshang"<<Endl A<<"2016-12-22, CityU"<<Endl -<<"Use :"<<"./1222_lup imageName dividewith [G]"<<Endl -<<Endl; the } - -mat& scanimageandreducec (mat& I,Constuchar*table); - + intMainintargcChar**argv) { - Help (); + if(argc<3){ Acout <<"Not enough parameters!"<<Endl; at return-1; - } - - Mat I; - Char* ImageName = argv[1]; - if(argc==4&&!strcmp (argv[3],"G")){ inI =Imread (ImageName, cv_load_image_grayscale); -}Else{ toI =Imread (ImageName, cv_load_image_color); + } - the if(!i.data) { *cout <<"The image"<< ImageName <<"Has no data!"; $ return-1;Panax Notoginseng } - the intDividewith =0; + StringStream S; AS << argv[2]; theS >>Dividewith; + if(!s | |!Dividewith) { -cout <<"Invalid Dividewith, input again (positive integer)!"<<Endl; $ return-1; $ } - - //Use the This table to search for (by simple assignment) reduced intensity, the //instead of calculating for each pixel, which is computational high-cost -Uchar table[ the];Wuyi for(intI=0; I < the; ++i) { theTable[i] = Uchar ((i/dividewith) *dividewith); - } Wu -Int64 t0 =GetTickCount (); AboutMat J =I.clone (); $J =scanimageandreducec (J, table); - DoubleT = (Double) (GetTickCount ()-T0)/gettickfrequency (); -cout <<"Elapse time ="<< t* +<<"Ms"<<Endl; - ANamedwindow ("before", cv_window_autosize); +Namedwindow ("After color reduce by LUT", cv_window_autosize); theImshow ("before", I); -Imshow ("After color reduce by LUT", J); $Waitkey (0); the return 0; the } the themat& scanimageandreducec (mat& I,Constuchar*table) { -Cv_assert (i.depth () = =cv_8u); in Const intChannels =i.channels (); the the intNRows =i.rows; About intNCols = i.cols*channels; the the if(I.iscontinuous ()) { theNCols *=nRows; +NRows =1; - } the Bayiuchar* p =NULL; the for(size_t i=0; i<nrows; ++i) { thep = i.ptr<uchar>(i); - for(size_t j=0; j<ncols; ++j) { -P[J] =Table[p[j]]; the } the } the returnI; the}
Results:
Read images, Lut, and time-consuming calculations