# Include <opencv2/CORE/core. HPP> # include <opencv2/highgui. HPP> # include <iostream> # include <sstream> using namespace STD; using namespace CV; void help () {cout <"\ n tests" <Endl <"this program shows how to scan image objects in opencv (CV: mat ). as Use Case "<" we take an input image and divide the native color palette (255) With the "<Endl <" input. shows C operator [] method, iterators and at function for on-the-fly item address calculation. "<Endl <" Usage: "<Endl <". /howtoscanimages imagenametouse dividewith [g] "<Endl <" if you add a G parameter the image is processed in gray scale "<Endl <" handle "<Endl <<Endl ;} mat & scanimageandre Ducec (MAT & I, const uchar * Table); MAT & scanimageandreduceiterator (MAT & I, const uchar * Table); MAT & scanimageandreducerandomaccess (MAT & I, const uchar * table ); int main (INT argc, char * argv []) {help (); If (argc <3) {cout <"not enough Parameters" <Endl; return-1;} mat I, j; If (argc = 4 &&! Strcmp (argv [3], "G") I = imread (argv [1], cv_load_image_grayscale); elseI = imread (argv [1], cv_load_image_color); If (! I. data) {cout <"the image" <argv [1] <"cocould not be loaded. "<Endl; Return-1;} int dividewith; // convert our input string to number-C ++ stylestringstream s; S <argv [2]; // execute Data Structure Transformation S> dividewith; If (! S) {cout <"invalid number entered for dividing. "<Endl; Return-1;} uchar table [256]; for (INT I = 0; I <256; ++ I) table [I] = dividewith * (I/dividewith); const int times = 100; // constant of the number of operation cycles: 100 // scanimageandreducec operation time --- double T; t = (double) gettickcount (); For (INT I = 0; I <times; ++ I) {CV: mat clone_ I = I. clone (); j = scanimageandreducec (clone_ I, table);} t = 1000 * (double) gettickcount ()-T)/gett Ickfrequency (); T/= times; cout <"time of matching with the c operator [] (averaged for" <Times <"runs ): "<t <" milliseconds. "<Endl; // scanimageandreduceiterator operation time --- t = (double) gettickcount (); For (INT I = 0; I <times; ++ I) {CV :: mat clone_ I = I. clone (); j = scanimageandreduceiterator (clone_ I, table);} t = 1000 * (double) gettickcount ()-T)/gettickfrequency (); T/= times; cout <"time Matching with the iterator (averaged for "<Times <" runs): "<t <" milliseconds. "<Endl; // scanimageandreducerandomaccess operation time --- t = (double) gettickcount (); For (INT I = 0; I <times; ++ I) {CV :: mat clone_ I = I. clone (); scanimageandreducerandomaccess (clone_ I, table);} t = 1000 * (double) gettickcount ()-T)/gettickfrequency (); T/= times; cout <"time of clustering with the on-the-fly address generat Ion-at function (averaged for "<Times <" runs): "<t <" milliseconds. "<Endl; // The core functionltumat lookuptable (1,256, cv_8u); uchar * P = lookuptable. data; For (INT I = 0; I <256; ++ I) P [I] = table [I]; // LUT operation time t = (double) gettickcount (); For (INT I = 0; I <times; ++ I) LUT (I, lookuptable, J); t = 1000 * (double) gettickcount () -T)/gettickfrequency (); T/= times; cout <"time of switching wi Th the LUT function (averaged for "<Times <" runs): "<t <" milliseconds. "<Endl; return 0;} mat & scanimageandreducec (MAT & I, const uchar * const table) {// accept only char type matricescv_assert (I. depth ()! = Sizeof (uchar); int channels = I. channels (); int nrows = I. rows; int ncols = I. cols * channels; if (I. iscontinuous () {ncols * = nrows; nrows = 1;} int I, j; uchar * P; for (I = 0; I <nrows; ++ I) {P = I. PTR <uchar> (I); For (j = 0; j <ncols; ++ J) {P [J] = table [p [J];} return I;} mat & scanimageandreduceiterator (MAT & I, const uchar * const table) {// accept only char type matricescv_assert (I. depth ()! = Sizeof (uchar); const int channels = I. channels (); Switch (channels) {Case 1: {matiterator _ <uchar> it, end; For (IT = I. begin <uchar> (), end = I. end <uchar> (); it! = End; ++ it) * It = table [* It]; break;} Case 3: {matiterator _ <vec3b> it, end; For (IT = I. begin <vec3b> (), end = I. end <vec3b> (); it! = End; ++ it) {(* It) [0] = table [(* It) [0]; (* It) [1] = table [(* It) [1]; (* It) [2] = table [(* It) [2] ;}} return I;} mat & scanimageandreducerandomaccess (MAT & I, const uchar * const table) {// accept only char type matricescv_assert (I. depth ()! = Sizeof (uchar); const int channels = I. channels (); Switch (channels) {Case 1: {for (INT I = 0; I <I. rows; ++ I) for (Int J = 0; j <I. cols; ++ J) I. at <uchar> (I, j) = table [I. at <uchar> (I, j)]; break;} Case 3: {mat _ <vec3b> _ I = I; for (INT I = 0; I <I. rows; ++ I) for (Int J = 0; j <I. cols; ++ J) {_ I (I, j) [0] = table [_ I (I, j) [0]; _ I (I, j) [1] = table [_ I (I, j) [1]; _ I (I, j) [2] = table [_ I (I, j) [2];} I = _ I; break;} return I ;}