Today, the second question of the second assignment of the tree chart is to be recorded, image Patch Extraction. This concept is really not difficult to understand, but if I actually write it, I really do not know how to traverse the image matrix to extract blocks. I would like to thank the Tang God for his enthusiastic help, told me a traversal of the idea _ (: З"∠) _
At first I was thinking about the loop from the original image matrix, that is, the boundary of the two-layer loop is the width and height of the original image respectively. Thinking like this, I have absolutely no idea how to move this patch.
After thinking about the patch list to consider, but also on both sides of the loop boundary is w-w+1 and H-h+1 (W is the original image of the width,h is the original image of the height,w and H respectively is the width and height of the patch), so think I still stuck, While it's convenient to get a patch list, it's been a long time since I've been using these index variables to locate the pixels on the original.
After consulting the Deng, get an accurate and workable loop, which is roughly as follows:
1 for (i:w-w+1) 2 for (j:h-h+1) 3 for (ii:w) 4 for (JJ:H)
The second thought is the same way of thinking, and because this is the right cycle to determine, so when thinking about it will not be afraid of mistakes. Plus after looking at his Java code, know to locate the original image, you can directly use Img.ptr<uchar> (I+II) [J+JJ] to locate, that is, two-dimensional coordinate positioning, point to the pixel coordinates (I+II, J+JJ). Feel that their language is not clear, or try to draw a diagram to illustrate.
Will always be the image of the storage mode of the qualitative linear storage habits to be well changed _ (: З"∠) _
While the extracted blocks exist in a three-dimensional fixed-length array, patches[w-w+1][h-h+1][w*h], the left-to-right subscript indicates which element in the first row , in the matrix of the first column , So, to be exact, each patch is stored in a one-dimensional array of length w*h, so the element is accessed in a linear way, ii*w+jj. The first two parameters represent the coordinates of the decomposed matrix, and the basic element that makes up the matrix is an array of w*h.
At first I was using a three-layer vector of push_back to save patches, the code is as follows
1Vector<vector<vector<uchar>>>patches;2 for(inti =0; i < rows; i++) {3Vector<vector<uchar>>Pcols;4 for(intj =0; J < cols; J + +) {5Vector<uchar>Patch;6 for(intII =0; II < H; ii++) {7 for(intJJ =0; JJ < W; jj++) {8Patch.push_back (img.ptr<uchar> (i+ii) [j+JJ]);9 }Ten } One pcols.push_back (patch); A } - Patches.push_back (pcols); -}
The results found that the operation of the calculation patches unexpectedly to use more than three minutes, I heard that generally a minute do not need time, I ran out of such a long time is also scared fast _ (: З"∠) _ Think about it, it should be because the vector is a dynamic allocation of the array, and every time into a new cycle has created a new vector, This part of the time has been so long, so I reviewed how to allocate a fixed-length three-dimensional array with new.
Because new is seldom used to allocate multidimensional arrays, the code for allocating space is posted here. The basic idea is to create an array of perimeters before creating an array of inner circumference.
1uchar***patches;2Patches =Newuchar**[rows];3 for(inti =0; i < rows; i++) {4Patches[i] =Newuchar*[cols];5 for(intj =0; J < cols; J + +) {6PATCHES[I][J] =NewUchar [w *h];7 }8}
The subsequent access is directly labeled with the array. After running, the discovery time saves a lot, takes only 30-40 seconds. So it should be that each cycle creates a new vector that wastes time, and if you create three vectors before the loop, it's time to save up the reuse of the chunks that are processed every single line. But I think the fixed length of the array time will be less, so temporarily no longer experiment vector improvement.
It is also important to note that after the array is used, the delete operation is required, as is the need to use a two-layer loop to delete from the inside out.
Finish this topic, feel or logical thinking is not clear, although thinking when there are painting examples than the strokes, but obviously already close to the results, but no way to reach the end of their own, or need to change the angle of thinking, from a variety of angles to see the problem. In addition to know how to extend the basic knowledge into practical applications, such as this time the multidimensional array space allocation, even at the beginning of wondering why the second row of the type is uchar** ...
You can't throw away what you've learned.
The above contents refer to these several blogs, thanks for their sharing!
Http://www.lewensky.cn/read.php/186.htm
http://pppboy.blog.163.com/blog/static/302037962010218103838998/
Digital Image processing Jobs using OPENCV-block extraction